This commit is contained in:
2025-10-07 18:38:53 +01:00
parent d66914f74f
commit 97079c7b10
2 changed files with 31 additions and 3 deletions

View File

@@ -97,6 +97,10 @@ impl Brightness {
self.0
}
pub fn dimm(self, scale: u8) -> Brightness {
Brightness(self.0 >> scale)
}
// Scales brightness (0-255) to range between IO_SEGMENT_ON_MIN_US and IO_SEGMENT_ON_MAX_US
pub fn scale_brightness(self) -> u32 {
// Using >> to avoid 32bit division which take ~576 cycles

View File

@@ -50,6 +50,11 @@ pub const IO_SEGMENT_RATE_US: u32 = 1000; // Time in μs between segment updates
pub const IO_SEGMENT_ON_MIN_US: u32 = 80; // How long in μs to hold segment LEDs on (dark)
pub const IO_SEGMENT_ON_MAX_US: u32 = 700; // How long in μs to hold segment LEDs on (bright)
// 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
// Calculator setup
pub const STACK_DEPTH: usize = 7;
type Calc = StackCalc<f32, STACK_DEPTH, 5, u8>;
@@ -115,6 +120,7 @@ pub struct IOLoop {
keyboard: Keyboard,
readount: Option<KeyReadout>,
debounce: Debounce,
sleep_timer: u16,
}
impl IOLoop {
@@ -128,6 +134,7 @@ impl IOLoop {
keyboard,
readount: None,
debounce: Default::default(),
sleep_timer: 0,
}
}
@@ -151,19 +158,36 @@ impl IOLoop {
// Start from first segment
self.index = 0;
// Full keyboard scan complete, debounce and return result
self.debounce.input(self.readount.take())
// Full keyboard scan complete, debounce
let key = self.debounce.input(self.readount.take());
// Reset or advance sleep timer
if key.is_some() {
self.sleep_timer = 0;
} else {
self.sleep_timer = self.sleep_timer.saturating_add(1);
}
key
} else {
None
}
}
pub fn is_sleep(&self) -> bool {
self.sleep_timer >= DISPLAY_DIMM_FRAMES
}
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();
brighness
if self.is_sleep() {
brighness.dimm(DISPLAY_DIMM_SCALE)
} else {
brighness
}
}
pub fn display_off(&mut self) {