From 700cb228511142dcf8ac1a3107118e364774b67e Mon Sep 17 00:00:00 2001 From: Stephen D <webmaster@scd31.com> Date: Tue, 2 Jul 2024 21:24:59 -0300 Subject: [PATCH] lcd backlight fading --- companion-software/src/main.rs | 65 +++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/companion-software/src/main.rs b/companion-software/src/main.rs index 8bdde51..971870d 100644 --- a/companion-software/src/main.rs +++ b/companion-software/src/main.rs @@ -40,7 +40,7 @@ mod app { SioInput, SioOutput, }, pac::{I2C1, SPI1}, - pwm::{self, Channel, FreeRunning, Pwm4, Slice}, + pwm::{self, Channel, FreeRunning, Pwm4, Pwm5, Slice}, spi, timer::{monotonic::Monotonic, Instant, Timer}, Adc, Clock, Spi, I2C, @@ -52,6 +52,8 @@ mod app { pub const HEIGHT: usize = 240; const BACKLIGHT_DELAY_TIME_SECS: u64 = 5; const BACKLIGHT_FADE_TIME_SECS: u64 = 2; + const LCD_DELAY_TIME_SECS: u64 = 15; + const LCD_FADE_TIME_SECS: u64 = 2; pub type MySpi = Spi< spi::Enabled, @@ -66,6 +68,8 @@ mod app { type KeyboardBl = Channel<Slice<Pwm4, FreeRunning>, pwm::B>; + type DisplayBl = Channel<Slice<Pwm5, FreeRunning>, pwm::A>; + type MyTouchpad = Touchpad< I2C< I2C1, @@ -99,6 +103,7 @@ mod app { keyboard: MyKeyboard, keyboard_bl: KeyboardBl, display: BufferedDisplay, + display_bl: DisplayBl, last_key_press: Instant, spi: Option<MySpi>, radio: RadioController, @@ -258,6 +263,7 @@ mod app { touchpad, keyboard, display, + display_bl, last_key_press: mono.now(), spi: Some(spi), radio, @@ -266,32 +272,18 @@ mod app { ) } - #[task(priority = 2, local = [keyboard_bl, touchpad, keyboard, last_key_press], shared = [controller])] + #[task(priority = 2, local = [keyboard_bl, display_bl,touchpad, keyboard, last_key_press], shared = [controller])] fn keyboard_update(mut ctx: keyboard_update::Context) { //while usb_dev.poll(&mut [&mut serial]) {} let keyboard_update::LocalResources { keyboard_bl, + display_bl, touchpad, keyboard, last_key_press, } = ctx.local; - let state = keyboard.read_state(); - if state > 0 { - *last_key_press = monotonics::now(); - - if let Some(k) = keyboard.pushed_key() { - ctx.shared.controller.lock(|g| g.key_push(k)); - } - } - - if let Ok((x, y)) = touchpad.get_delta_motion() { - if x != 0 || y != 0 { - ctx.shared.controller.lock(|g| g.touchpad_scroll(x, y)); - } - } - // keyboard backlight logic let delta = monotonics::now() - *last_key_press; if let Some(fade_delta) = delta.checked_sub(BACKLIGHT_DELAY_TIME_SECS.secs::<1, 1>()) { @@ -305,6 +297,45 @@ mod app { keyboard_bl.set_duty(keyboard_bl.get_max_duty()); } + // lcd backlight logic + let sleeping = + if let Some(fade_delta) = delta.checked_sub(LCD_DELAY_TIME_SECS.secs::<1, 1>()) { + let fade_delta = fade_delta.to_millis(); + let fade_time_ms = LCD_FADE_TIME_SECS * 1000; + let frac = u64::from(keyboard_bl.get_max_duty()) + * (fade_time_ms.saturating_sub(fade_delta)) + / fade_time_ms; + display_bl.set_duty(frac.try_into().unwrap()); + + true + } else { + display_bl.set_duty(keyboard_bl.get_max_duty()); + + false + }; + + let state = keyboard.read_state(); + if state > 0 { + *last_key_press = monotonics::now(); + + // if the screen is off, ignore the key press that turns us back on + if !sleeping { + if let Some(k) = keyboard.pushed_key() { + ctx.shared.controller.lock(|g| g.key_push(k)); + } + } + } + + if let Ok((x, y)) = touchpad.get_delta_motion() { + if x != 0 || y != 0 { + *last_key_press = monotonics::now(); + + if !sleeping { + ctx.shared.controller.lock(|g| g.touchpad_scroll(x, y)); + } + } + } + // re-enqueue ourselves keyboard_update::spawn_after(5.millis()).unwrap(); } -- GitLab