Compare commits

...

5 Commits

Author SHA1 Message Date
cac25d6006 linear dimming 2025-10-07 19:10:43 +01:00
97079c7b10 dimming 2025-10-07 18:38:53 +01:00
d66914f74f remove debugging 2025-10-04 15:45:54 +01:00
42c18a6d5a brightness scaling optimization 2025-10-04 15:01:50 +01:00
e98a24d32d timer debugging 2025-10-04 14:14:10 +01:00
3 changed files with 72 additions and 32 deletions

View File

@@ -5,7 +5,7 @@ use arduino_hal::{
port::{mode::Output, Pin}, port::{mode::Output, Pin},
}; };
use crate::DISPLAY_SEGMENTS; use crate::{DISPLAY_SEGMENTS, IO_SEGMENT_ON_MAX_US, IO_SEGMENT_ON_MIN_US};
pub struct SegmentPins { pub struct SegmentPins {
kd_seg_a: Pin<Output, PD5>, kd_seg_a: Pin<Output, PD5>,
@@ -96,6 +96,16 @@ impl Brightness {
pub const fn unwrap(self) -> u8 { pub const fn unwrap(self) -> u8 {
self.0 self.0
} }
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
pub fn scale_brightness(self) -> u32 {
// Using >> to avoid 32bit division which take ~576 cycles
IO_SEGMENT_ON_MIN_US + ((IO_SEGMENT_ON_MAX_US - IO_SEGMENT_ON_MIN_US) * self.0 as u32 >> 8)
}
} }
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
@@ -106,8 +116,8 @@ impl Segment {
Segment(0, Brightness::full()) Segment(0, Brightness::full())
} }
pub fn brightness(&mut self, b: u8) -> &mut Self { pub fn brightness(&mut self, b: Brightness) -> &mut Self {
self.1 = Brightness(b); self.1 = b;
self self
} }

View File

@@ -50,14 +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_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) pub const IO_SEGMENT_ON_MAX_US: u32 = 700; // How long in μs to hold segment LEDs on (bright)
//TODO: When inline it is much slower due to use of Brightness instead of const 0xFF?! // Dimming
#[inline(never)] pub const DISPLAY_FPS: u16 = (1_000_000 / (IO_SEGMENT_RATE_US * DISPLAY_SEGMENTS as u32)) as u16;
fn scale_brightness(b: Brightness) -> u32 { pub const DISPLAY_DIMM_FRAMES: u16 = DISPLAY_FPS * 10; // How many frames of inactivity before dimming
assert_eq!((Brightness::full().unwrap() as u32), 0xFF); pub const DISPLAY_DIMM_SPEED: u8 = 4; // Dimm by amount every frame when sleeping
IO_SEGMENT_ON_MIN_US pub const DISPLAY_UNDIMM_SPEED: u8 = 16; // Brighten by amount every frame when not sleeping
+ (IO_SEGMENT_ON_MAX_US - IO_SEGMENT_ON_MIN_US) * b.unwrap() as u32
/ (Brightness::full().unwrap() as u32)
}
// Calculator setup // Calculator setup
pub const STACK_DEPTH: usize = 7; pub const STACK_DEPTH: usize = 7;
@@ -91,9 +88,11 @@ fn access_global<'cs, 'v: 'cs, T, O>(
#[avr_device::interrupt(atmega328p)] #[avr_device::interrupt(atmega328p)]
unsafe fn TIMER0_COMPA() { unsafe fn TIMER0_COMPA() {
avr_device::interrupt::free(|cs| { avr_device::interrupt::free(|cs| {
let brightness = access_global(&IO_LOOP, cs, |io_loop| io_loop.display_on()); access_global(&IO_LOOP, cs, |io_loop| {
access_global(&SEGMENT_TIMER, cs, |st| { access_global(&SEGMENT_TIMER, cs, |st| {
st.segment_on_time(scale_brightness(brightness)) let brightness = io_loop.display_on();
st.segment_on_time(brightness.scale_brightness());
});
}); });
}); });
} }
@@ -115,24 +114,30 @@ unsafe fn TIMER0_COMPB() {
pub struct IOLoop { pub struct IOLoop {
index: usize, index: usize,
frame: u16,
io_pins: IOPins, io_pins: IOPins,
segment_pins: SegmentPins, segment_pins: SegmentPins,
dispaly: DispalyState, dispaly: DispalyState,
keyboard: Keyboard, keyboard: Keyboard,
readount: Option<KeyReadout>, readount: Option<KeyReadout>,
debounce: Debounce, debounce: Debounce,
sleep_timer: u16,
dimming: u8,
} }
impl IOLoop { impl IOLoop {
pub fn new(io_pins: IOPins, segment_pins: SegmentPins, keyboard: Keyboard) -> IOLoop { pub fn new(io_pins: IOPins, segment_pins: SegmentPins, keyboard: Keyboard) -> IOLoop {
IOLoop { IOLoop {
index: 0, index: 0,
frame: 0,
io_pins, io_pins,
segment_pins, segment_pins,
dispaly: Default::default(), dispaly: Default::default(),
keyboard, keyboard,
readount: None, readount: None,
debounce: Default::default(), debounce: Default::default(),
sleep_timer: 0,
dimming: u8::MAX,
} }
} }
@@ -151,22 +156,47 @@ impl IOLoop {
pub fn advance(&mut self) -> Option<KeyPress> { pub fn advance(&mut self) -> Option<KeyPress> {
self.index += 1; self.index += 1;
if self.index == self.io_pins.len() || self.index == self.dispaly.len() { if self.index == self.io_pins.len() || self.index == self.dispaly.len() {
// Frame done
self.frame = self.frame.wrapping_add(1);
// Start from first segment // Start from first segment
self.index = 0; self.index = 0;
// Full keyboard scan complete, debounce and return result // Full keyboard scan complete, debounce
self.debounce.input(self.readount.take()) 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);
}
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 { } else {
None None
} }
} }
pub fn is_sleep(&self) -> bool {
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();
brighness brighness.dimm(self.dimming)
} }
pub fn display_off(&mut self) { pub fn display_off(&mut self) {
@@ -181,6 +211,10 @@ impl IOLoop {
} }
self.select_off(); self.select_off();
} }
pub fn frame(&self) -> (u16, usize) {
(self.frame, self.index)
}
} }
#[derive(uDebug)] #[derive(uDebug)]
@@ -603,11 +637,7 @@ fn main() -> ! {
.take(calc.len()) .take(calc.len())
{ {
seg.dp(); seg.dp();
} seg.brightness(Brightness::full());
for (no, seg) in display.slice(0, DISPLAY_SEGMENTS).iter_mut().enumerate() {
seg.brightness(
(no * (Brightness::full().unwrap() as usize) / DISPLAY_SEGMENTS) as u8,
);
} }
} }
TransientState::Err { .. } => display.error(), TransientState::Err { .. } => display.error(),
@@ -616,7 +646,7 @@ fn main() -> ! {
avr_device::interrupt::free(|cs| { avr_device::interrupt::free(|cs| {
access_global(&IO_LOOP, cs, |io_loop| { access_global(&IO_LOOP, cs, |io_loop| {
io_loop.update_display(&display); io_loop.update_display(&display);
}); })
}); });
} }
} }

View File

@@ -10,7 +10,9 @@ const fn us_to_ticks(us: u32) -> u32 {
} }
// Timer 0 (8bit) // Timer 0 (8bit)
pub struct SegmentTimer(TC0); pub struct SegmentTimer {
timer: TC0,
}
impl SegmentTimer { impl SegmentTimer {
// Sets up timer to rise interrupts: // Sets up timer to rise interrupts:
@@ -36,19 +38,17 @@ impl SegmentTimer {
tc0.timsk0 tc0.timsk0
.write(|w| w.ocie0a().set_bit().ocie0b().set_bit()); .write(|w| w.ocie0a().set_bit().ocie0b().set_bit());
SegmentTimer(tc0) SegmentTimer { timer: tc0 }
} }
// Set for how long the segment LEDs should be on in μs // Set for how long the segment LEDs should be on in μs
// Controls TIMER0_COMPB interrupt time after TIMER0_COMPA // Controls TIMER0_COMPB interrupt time after TIMER0_COMPA
pub fn segment_on_time(&mut self, segment_on_us: u32) { pub fn segment_on_time(&mut self, segment_on_us: u32) {
let ocrb = us_to_ticks(segment_on_us); let delay: u8 = us_to_ticks(segment_on_us)
.try_into()
.expect("timer init segment_on_us out of rage");
let elapsed = self.timer.tcnt0.read().bits();
// Set the compare value for B match // Set the compare value for B match
self.0.ocr0b.write(|w| { self.timer.ocr0b.write(|w| w.bits(elapsed + delay));
w.bits(
ocrb.try_into()
.expect("timer init segment_on_us out of rage"),
)
});
} }
} }