linear dimming

This commit is contained in:
2025-10-07 19:10:43 +01:00
parent 97079c7b10
commit cac25d6006
2 changed files with 17 additions and 8 deletions

View File

@@ -97,8 +97,8 @@ impl Brightness {
self.0 self.0
} }
pub fn dimm(self, scale: u8) -> Brightness { pub fn dimm(self, dimming: u8) -> Brightness {
Brightness(self.0 >> scale) Brightness(self.0.saturating_sub(dimming))
} }
// Scales brightness (0-255) to range between IO_SEGMENT_ON_MIN_US and IO_SEGMENT_ON_MAX_US // Scales brightness (0-255) to range between IO_SEGMENT_ON_MIN_US and IO_SEGMENT_ON_MAX_US

View File

@@ -53,7 +53,8 @@ pub const IO_SEGMENT_ON_MAX_US: u32 = 700; // How long in μs to hold segment LE
// Dimming // Dimming
pub const DISPLAY_FPS: u16 = (1_000_000 / (IO_SEGMENT_RATE_US * DISPLAY_SEGMENTS as u32)) as u16; 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_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 // Calculator setup
pub const STACK_DEPTH: usize = 7; pub const STACK_DEPTH: usize = 7;
@@ -121,6 +122,7 @@ pub struct IOLoop {
readount: Option<KeyReadout>, readount: Option<KeyReadout>,
debounce: Debounce, debounce: Debounce,
sleep_timer: u16, sleep_timer: u16,
dimming: u8,
} }
impl IOLoop { impl IOLoop {
@@ -135,6 +137,7 @@ impl IOLoop {
readount: None, readount: None,
debounce: Default::default(), debounce: Default::default(),
sleep_timer: 0, sleep_timer: 0,
dimming: u8::MAX,
} }
} }
@@ -168,6 +171,12 @@ impl IOLoop {
self.sleep_timer = self.sleep_timer.saturating_add(1); 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 key
} else { } else {
None None
@@ -178,16 +187,16 @@ impl IOLoop {
self.sleep_timer >= DISPLAY_DIMM_FRAMES self.sleep_timer >= DISPLAY_DIMM_FRAMES
} }
pub fn dimming(&self) -> u8 {
self.dimming
}
pub fn display_on(&mut self) -> Brightness { pub fn display_on(&mut self) -> Brightness {
self.select_off(); self.select_off();
let segment = self.dispaly[self.index]; let segment = self.dispaly[self.index];
let brighness = segment.apply(&mut self.segment_pins); let brighness = segment.apply(&mut self.segment_pins);
self.select_on(); self.select_on();
if self.is_sleep() { brighness.dimm(self.dimming)
brighness.dimm(DISPLAY_DIMM_SCALE)
} else {
brighness
}
} }
pub fn display_off(&mut self) { pub fn display_off(&mut self) {