From 9141629a58305d43d5cf16da299c69f5437ac553 Mon Sep 17 00:00:00 2001 From: Ross Andrews Date: Mon, 7 Sep 2026 18:46:42 -0500 Subject: [PATCH] refactor --- src/backlight.rs | 38 +++++++++++++++++++++++++++----------- src/led.rs | 13 ------------- src/main.rs | 14 ++------------ 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/src/backlight.rs b/src/backlight.rs index fc7affc..f34b18b 100644 --- a/src/backlight.rs +++ b/src/backlight.rs @@ -1,23 +1,39 @@ use esp_hal::gpio::interconnect::PeripheralOutput; use esp_hal::gpio::DriveMode; use esp_hal::ledc::channel::{self, Channel, ChannelIFace}; -use esp_hal::ledc::timer::TimerIFace; -use esp_hal::ledc::LowSpeed; +use esp_hal::ledc::timer::{self, Timer, TimerIFace}; +use esp_hal::ledc::{LSGlobalClkSource, Ledc, LowSpeed}; +use esp_hal::peripherals::LEDC; +use esp_hal::time::Rate; + +/// PWM frequency for the backlight (Hz). +const BACKLIGHT_FREQ: Rate = Rate::from_khz(5); /// Maximum duty cycle percentage. The board documentation warns against /// running the display above 50% brightness, so `level = 1.0` maps to this. const MAX_DUTY_PCT: u8 = 50; -#[derive(Debug)] -pub enum Error { - Channel(channel::Error), +/// Create and configure the LEDC timer used by the backlight. +/// +/// Returns the `Ledc` and `Timer` so the caller can build a [`LcdBacklight`] +/// that borrows the timer. +pub fn setup_timer( + ledc: LEDC<'static>, +) -> Result<(Ledc<'static>, Timer<'static, LowSpeed>), timer::Error> { + let mut ledc = Ledc::new(ledc); + ledc.set_global_slow_clock(LSGlobalClkSource::APBClk); + + let mut timer = ledc.timer::(timer::Number::Timer0); + timer.configure(timer::config::Config { + duty: timer::config::Duty::Duty8Bit, + clock_source: timer::LSClockSource::APBClk, + frequency: BACKLIGHT_FREQ, + })?; + + Ok((ledc, timer)) } -impl From for Error { - fn from(e: channel::Error) -> Self { - Error::Channel(e) - } -} +pub type Error = channel::Error; /// Backlight PWM driver. The channel holds a reference to the caller-owned /// timer, so this type's lifetime is bounded by the timer's lifetime. @@ -45,6 +61,6 @@ impl<'a> LcdBacklight<'a> { /// documentation recommends. pub fn set_level(&mut self, level: f32) -> Result<(), Error> { let duty_pct = ((level.clamp(0.0, 1.0) * MAX_DUTY_PCT as f32) + 0.5) as u8; - self.channel.set_duty(duty_pct).map_err(Error::Channel) + self.channel.set_duty(duty_pct) } } \ No newline at end of file diff --git a/src/led.rs b/src/led.rs index 5496649..be529b5 100644 --- a/src/led.rs +++ b/src/led.rs @@ -23,20 +23,7 @@ impl RgbLed { }) } - pub fn set_color( - &mut self, - r: u8, - g: u8, - b: u8, - ) -> Result<(), esp_hal_smartled::AdapterError> { - self.led.write([RGB8 { r, g, b }]) - } - pub fn set_rgb(&mut self, rgb: RGB8) -> Result<(), esp_hal_smartled::AdapterError> { self.led.write([rgb]) } - - pub fn off(&mut self) -> Result<(), esp_hal_smartled::AdapterError> { - self.set_color(0, 0, 0) - } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a5efd62..b78d321 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,6 @@ mod led; use esp_backtrace as _; use esp_bootloader_esp_idf::esp_app_desc; -use esp_hal::ledc::timer::{self, TimerIFace}; -use esp_hal::ledc::{LSGlobalClkSource, Ledc, LowSpeed}; use esp_hal::{init, rmt::Rmt, time::Rate, Config}; use esp_println::println; use smart_leds::hsv::{hsv2rgb, Hsv}; @@ -24,16 +22,8 @@ fn main() -> ! { led::RgbLed::new(rmt.channel0, peripherals.GPIO8).expect("Failed to init RGB LED") }; - let mut ledc = Ledc::new(peripherals.LEDC); - ledc.set_global_slow_clock(LSGlobalClkSource::APBClk); - let mut timer = ledc.timer::(timer::Number::Timer0); - timer - .configure(timer::config::Config { - duty: timer::config::Duty::Duty8Bit, - clock_source: timer::LSClockSource::APBClk, - frequency: Rate::from_khz(5), - }) - .expect("Failed to configure backlight timer"); + let (_ledc, timer) = + backlight::setup_timer(peripherals.LEDC).expect("Failed to init backlight timer"); let mut backlight = backlight::LcdBacklight::new(&timer, peripherals.GPIO22).expect("Failed to init backlight");