diff --git a/src/display.rs b/src/display.rs index c293ca5..99a9325 100644 --- a/src/display.rs +++ b/src/display.rs @@ -97,8 +97,8 @@ impl Brightness { self.0 } - pub fn dimm(self, scale: u8) -> Brightness { - Brightness(self.0 >> scale) + pub fn dimm(self, dimming: u8) -> Brightness { + Brightness(self.0.saturating_sub(dimming)) } // Scales brightness (0-255) to range between IO_SEGMENT_ON_MIN_US and IO_SEGMENT_ON_MAX_US diff --git a/src/main.rs b/src/main.rs index 5a2c9d9..121439c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,8 @@ pub const IO_SEGMENT_ON_MAX_US: u32 = 700; // How long in μs to hold segment LE // Dimming pub const DISPLAY_FPS: u16 = (1_000_000 / (IO_SEGMENT_RATE_US * DISPLAY_SEGMENTS as u32)) as u16; pub const DISPLAY_DIMM_FRAMES: u16 = DISPLAY_FPS * 10; // How many frames of inactivity before dimming -pub const DISPLAY_DIMM_SCALE: u8 = 4; // Number of bit shifts of brighness to dim +pub const DISPLAY_DIMM_SPEED: u8 = 4; // Dimm by amount every frame when sleeping +pub const DISPLAY_UNDIMM_SPEED: u8 = 16; // Brighten by amount every frame when not sleeping // Calculator setup pub const STACK_DEPTH: usize = 7; @@ -121,6 +122,7 @@ pub struct IOLoop { readount: Option, debounce: Debounce, sleep_timer: u16, + dimming: u8, } impl IOLoop { @@ -135,6 +137,7 @@ impl IOLoop { readount: None, debounce: Default::default(), sleep_timer: 0, + dimming: u8::MAX, } } @@ -168,6 +171,12 @@ impl IOLoop { self.sleep_timer = self.sleep_timer.saturating_add(1); } + if self.is_sleep() { + self.dimming = self.dimming.saturating_add(DISPLAY_DIMM_SPEED); + } else { + self.dimming = self.dimming.saturating_sub(DISPLAY_UNDIMM_SPEED); + } + key } else { None @@ -178,16 +187,16 @@ impl IOLoop { self.sleep_timer >= DISPLAY_DIMM_FRAMES } + pub fn dimming(&self) -> u8 { + self.dimming + } + pub fn display_on(&mut self) -> Brightness { self.select_off(); let segment = self.dispaly[self.index]; let brighness = segment.apply(&mut self.segment_pins); self.select_on(); - if self.is_sleep() { - brighness.dimm(DISPLAY_DIMM_SCALE) - } else { - brighness - } + brighness.dimm(self.dimming) } pub fn display_off(&mut self) {