Compare commits

...

2 Commits

Author SHA1 Message Date
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 51 additions and 25 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,12 @@ impl Brightness {
pub const fn unwrap(self) -> u8 { pub const fn unwrap(self) -> u8 {
self.0 self.0
} }
// 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)]

View File

@@ -50,15 +50,6 @@ 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?!
#[inline(never)]
fn scale_brightness(b: Brightness) -> u32 {
assert_eq!((Brightness::full().unwrap() as u32), 0xFF);
IO_SEGMENT_ON_MIN_US
+ (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;
type Calc = StackCalc<f32, STACK_DEPTH, 5, u8>; type Calc = StackCalc<f32, STACK_DEPTH, 5, u8>;
@@ -91,9 +82,12 @@ 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();
let elapsed = st.segment_on_time(brightness.scale_brightness());
io_loop.frame_data = Some((elapsed, brightness.unwrap()));
});
}); });
}); });
} }
@@ -115,24 +109,28 @@ 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,
frame_data: Option<(u8, 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(),
frame_data: None,
} }
} }
@@ -151,6 +149,8 @@ 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;
@@ -181,6 +181,14 @@ impl IOLoop {
} }
self.select_off(); self.select_off();
} }
pub fn frame(&self) -> (u16, usize) {
(self.frame, self.index)
}
pub fn frame_data(&self) -> Option<(u8, u8)> {
self.frame_data
}
} }
#[derive(uDebug)] #[derive(uDebug)]
@@ -526,6 +534,7 @@ fn main() -> ! {
let mut display = DispalyState::default(); let mut display = DispalyState::default();
number_input.show(&mut display); number_input.show(&mut display);
let mut frame_data_index: usize = 0;
let mut calc = Calc::default(); let mut calc = Calc::default();
let mut state = State { let mut state = State {
transient: TransientState::Done, transient: TransientState::Done,
@@ -557,6 +566,12 @@ fn main() -> ! {
state.calculator state.calculator
) )
.ok(); .ok();
match key {
KeyPress::Num(n) => {
frame_data_index = n as usize;
}
_ => (),
}
match state.calculator.on_key(key, &mut number_input, &mut calc) { match state.calculator.on_key(key, &mut number_input, &mut calc) {
Ok(new_state) => state.calculator = new_state, Ok(new_state) => state.calculator = new_state,
Err(err) => { Err(err) => {
@@ -613,10 +628,14 @@ fn main() -> ! {
TransientState::Err { .. } => display.error(), TransientState::Err { .. } => display.error(),
} }
avr_device::interrupt::free(|cs| { let ((frame, index), frame_data) = 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);
}); (io_loop.frame(), io_loop.frame_data())
})
}); });
if frame % 64 == 0 && index == frame_data_index {
ufmt::uwriteln!(&mut serial, "[{}, {}] {:?}", frame, index, frame_data).ok();
}
} }
} }

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,18 @@ 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) -> u8 {
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( return elapsed;
ocrb.try_into()
.expect("timer init segment_on_us out of rage"),
)
});
} }
} }