~ruther/simple-clock

a3e908e9ba0f88684f764fab05cf5de13e730ec4 — František Boháček 1 year, 8 months ago 994a92f
feat(src): turn off autobrightness adjustment after manual adjustment
4 files changed, 54 insertions(+), 12 deletions(-)

M source/src/brightness_manager.rs
M source/src/calendar.rs
M source/src/clock_app.rs
M source/src/main.rs
M source/src/brightness_manager.rs => source/src/brightness_manager.rs +26 -6
@@ 12,6 12,7 @@ pub struct BrightnessManager {
    brightness_interpolation: LinearInterpolation<u16, u16>,
    current_brightness: u8,
    display_brightness: [u16; 8],
    off_till: Option<u32>,
}

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);
            }
        }
    }
}

M source/src/calendar.rs => source/src/calendar.rs +17 -2
@@ 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 {

M source/src/clock_app.rs => source/src/clock_app.rs +10 -4
@@ 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<Down> {
    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<Down> {
impl ClockButton for ButtonBrightness<Up> {
    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);
            }
            _ => (),
        }

M source/src/main.rs => source/src/main.rs +1 -0
@@ 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;


Do not follow this link