From a3e908e9ba0f88684f764fab05cf5de13e730ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Mon, 7 Aug 2023 11:01:19 +0200 Subject: [PATCH] feat(src): turn off autobrightness adjustment after manual adjustment --- source/src/brightness_manager.rs | 32 ++++++++++++++++++++++++++------ source/src/calendar.rs | 19 +++++++++++++++++-- source/src/clock_app.rs | 14 ++++++++++---- source/src/main.rs | 1 + 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/source/src/brightness_manager.rs b/source/src/brightness_manager.rs index 8f67f13..ded8311 100644 --- a/source/src/brightness_manager.rs +++ b/source/src/brightness_manager.rs @@ -12,6 +12,7 @@ pub struct BrightnessManager { brightness_interpolation: LinearInterpolation, current_brightness: u8, display_brightness: [u16; 8], + off_till: Option, } impl BrightnessManager { @@ -49,6 +50,7 @@ impl BrightnessManager { ]), current_brightness: 100, display_brightness: [0xFFFF; 8], + off_till: None, } } @@ -84,16 +86,34 @@ impl BrightnessManager { .set_brightness(self.display_brightness); } + /// Turns off the interpolation based on time + pub fn turn_off_for(&mut self, state: &ClockState, seconds: u32) { + self.off_till = Some(state.calendar().estimated_ticks() + seconds); + } + pub fn update(&mut self, state: &ClockState) { let calendar = state.calendar(); let minutes_in_day = calendar.hours() as u16 * 60u16 + calendar.minutes() as u16; - let interpolated = self - .brightness_interpolation - .interpolate(minutes_in_day) - .unwrap(); - if self.brightness() != interpolated as u8 { - self.set_brightness(interpolated as i8); + let set_brightness = if let Some(off_till) = self.off_till { + if state.calendar().estimated_ticks() < off_till { + false + } else { + self.off_till = None; + true + } + } else { + true + }; + + if set_brightness { + let interpolated = self + .brightness_interpolation + .interpolate(minutes_in_day) + .unwrap(); + if self.brightness() != interpolated as u8 { + self.set_brightness(interpolated as i8); + } } } } diff --git a/source/src/calendar.rs b/source/src/calendar.rs index 8fe4e23..6189afb 100644 --- a/source/src/calendar.rs +++ b/source/src/calendar.rs @@ -3,6 +3,7 @@ use core::cmp::{max, min}; #[derive(Clone, PartialEq, Eq)] pub struct Calendar { base_year: u16, + ticks: u32, frozen: bool, hours: u8, minutes: u8, @@ -27,7 +28,7 @@ impl Calendar { month: u8, year: u16, ) -> Self { - Self { + let mut result = Self { base_year, hours, minutes, @@ -36,7 +37,11 @@ impl Calendar { month, year, frozen: false, - } + ticks: 0, + }; + + result.ticks = result.to_ticks(); + result } /// Calculate current date based off of the seconds elapsed since @@ -113,6 +118,7 @@ impl Calendar { month: month as u8, year: year as u16, frozen: false, + ticks: seconds, } } @@ -141,6 +147,13 @@ impl Calendar { ticks } + /// Like Calendar::to_ticks, but the ticks may get + /// desynchronized at times. Use Calendar::to_ticks + /// for correct ticks. + pub fn estimated_ticks(&self) -> u32 { + self.ticks + } + /// Adds a second, correctly updating /// minutes, hours, days, month, year... pub fn second_elapsed(&mut self) { @@ -164,6 +177,8 @@ impl Calendar { self.month = (self.month - 1 + if month_elapsed { 1 } else { 0 }) % 12 + 1; let year_elapsed = month_elapsed && self.month == 1; self.year += if year_elapsed { 1 } else { 0 }; + + self.ticks += 1; } pub fn hours(&self) -> u8 { diff --git a/source/src/clock_app.rs b/source/src/clock_app.rs index 7b50dd2..fe7da77 100644 --- a/source/src/clock_app.rs +++ b/source/src/clock_app.rs @@ -115,12 +115,17 @@ impl ClockButton for ButtonSwitchView { } } +// How long to turn off the brightness adjustment on user brightness change, in seconds +const BRIGHT_OFF_FOR: u32 = 30 * 60; +const BRIGHT_CHANGE: i8 = 10; // How much to change the brightness on press + impl ClockButton for ButtonBrightness { fn handle(&self, state: ButtonState, app: AppState) { match state { - ButtonState::JustPressed => { + ButtonState::JustPressed | ButtonState::LongPress => { + app.brightness.turn_off_for(&app.state, BRIGHT_OFF_FOR); app.brightness - .set_brightness(app.brightness.brightness() as i8 - 10); + .set_brightness(app.brightness.brightness() as i8 - BRIGHT_CHANGE); } _ => (), } @@ -130,9 +135,10 @@ impl ClockButton for ButtonBrightness { impl ClockButton for ButtonBrightness { fn handle(&self, state: ButtonState, app: AppState) { match state { - ButtonState::JustPressed => { + ButtonState::JustPressed | ButtonState::LongPress => { + app.brightness.turn_off_for(&app.state, BRIGHT_OFF_FOR); app.brightness - .set_brightness(app.brightness.brightness() as i8 + 10); + .set_brightness(app.brightness.brightness() as i8 + BRIGHT_CHANGE); } _ => (), } diff --git a/source/src/main.rs b/source/src/main.rs index 30fc558..cb6ead4 100644 --- a/source/src/main.rs +++ b/source/src/main.rs @@ -15,6 +15,7 @@ pub mod clock_state; pub mod count_down; pub mod display; pub mod linear_interpolation; +pub mod mono_timer; pub mod number_digits; pub mod seven_segments; -- 2.48.1