feat: dim led to 1% brightness

This commit is contained in:
Luca 2024-04-20 01:25:11 +02:00
parent c6887897a5
commit fefe476034
1 changed files with 53 additions and 12 deletions

View File

@ -4,6 +4,7 @@
use esp_hal::clock::ClockControl; use esp_hal::clock::ClockControl;
use esp_hal::delay::Delay; use esp_hal::delay::Delay;
use esp_hal::gpio::IO; use esp_hal::gpio::IO;
use esp_hal::ledc::{channel, timer, LSGlobalClkSource, LEDC};
use esp_hal::peripherals::Peripherals; use esp_hal::peripherals::Peripherals;
use esp_hal::prelude::*; use esp_hal::prelude::*;
@ -15,30 +16,70 @@ fn main() -> ! {
let sys = p.SYSTEM.split(); let sys = p.SYSTEM.split();
let clocks = ClockControl::boot_defaults(sys.clock_control).freeze(); 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 io = IO::new(p.GPIO, p.IO_MUX);
let mut r = io.pins.gpio5.into_push_pull_output(); let mut r = ledc.get_channel(
let mut g = io.pins.gpio6.into_push_pull_output(); channel::Number::Channel0,
let mut b = io.pins.gpio7.into_push_pull_output(); 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 _btn = io.pins.gpio9.into_pull_up_input();
let delay = Delay::new(&clocks); let delay = Delay::new(&clocks);
for [r_state, g_state, b_state] in [ for [r_state, g_state, b_state] in [
[true, false, false], [1, 0, 0],
[true, true, false], [1, 1, 0],
[false, true, false], [0, 1, 0],
[false, true, true], [0, 1, 1],
[false, false, true], [0, 0, 1],
[true, false, true], [1, 0, 1],
] ]
.iter() .iter()
.cycle() .cycle()
{ {
r.set_state(*r_state); let _ = r.set_duty(*r_state);
g.set_state(*g_state); let _ = g.set_duty(*g_state);
b.set_state(*b_state); let _ = b.set_duty(*b_state);
delay.delay_millis(200u32); delay.delay_millis(200u32);
} }