refactor
This commit is contained in:
+27
-11
@@ -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::<LowSpeed>(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<channel::Error> 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)
|
||||
}
|
||||
}
|
||||
-13
@@ -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)
|
||||
}
|
||||
}
|
||||
+2
-12
@@ -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::<LowSpeed>(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");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user