From fefe476034eed01a0402fa6a4dac4cf7c1d7a57f Mon Sep 17 00:00:00 2001 From: Luca Date: Sat, 20 Apr 2024 01:25:11 +0200 Subject: [PATCH] feat: dim led to 1% brightness --- src/main.rs | 65 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0c8c265..149ed91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use esp_hal::clock::ClockControl; use esp_hal::delay::Delay; use esp_hal::gpio::IO; +use esp_hal::ledc::{channel, timer, LSGlobalClkSource, LEDC}; use esp_hal::peripherals::Peripherals; use esp_hal::prelude::*; @@ -15,30 +16,70 @@ fn main() -> ! { let sys = p.SYSTEM.split(); let clocks = ClockControl::boot_defaults(sys.clock_control).freeze(); + let mut ledc = LEDC::new(p.LEDC, &clocks); + ledc.set_global_slow_clock(LSGlobalClkSource::APBClk); + + let mut timer = ledc.get_timer(timer::Number::Timer0); + timer + .configure(timer::config::Config { + clock_source: timer::LSClockSource::APBClk, + duty: timer::config::Duty::Duty8Bit, + frequency: 24.kHz(), + }) + .unwrap(); + let io = IO::new(p.GPIO, p.IO_MUX); - let mut r = io.pins.gpio5.into_push_pull_output(); - let mut g = io.pins.gpio6.into_push_pull_output(); - let mut b = io.pins.gpio7.into_push_pull_output(); + let mut r = ledc.get_channel( + channel::Number::Channel0, + io.pins.gpio5.into_push_pull_output(), + ); + let mut g = ledc.get_channel( + channel::Number::Channel1, + io.pins.gpio6.into_push_pull_output(), + ); + let mut b = ledc.get_channel( + channel::Number::Channel2, + io.pins.gpio7.into_push_pull_output(), + ); + + r.configure(channel::config::Config { + duty_pct: 0, + pin_config: channel::config::PinConfig::PushPull, + timer: &timer, + }) + .unwrap(); + g.configure(channel::config::Config { + duty_pct: 0, + pin_config: channel::config::PinConfig::PushPull, + timer: &timer, + }) + .unwrap(); + b.configure(channel::config::Config { + duty_pct: 0, + pin_config: channel::config::PinConfig::PushPull, + timer: &timer, + }) + .unwrap(); let _btn = io.pins.gpio9.into_pull_up_input(); let delay = Delay::new(&clocks); for [r_state, g_state, b_state] in [ - [true, false, false], - [true, true, false], - [false, true, false], - [false, true, true], - [false, false, true], - [true, false, true], + [1, 0, 0], + [1, 1, 0], + [0, 1, 0], + [0, 1, 1], + [0, 0, 1], + [1, 0, 1], ] .iter() .cycle() { - r.set_state(*r_state); - g.set_state(*g_state); - b.set_state(*b_state); + let _ = r.set_duty(*r_state); + let _ = g.set_duty(*g_state); + let _ = b.set_duty(*b_state); delay.delay_millis(200u32); }