diff --git a/src/display.rs b/src/display.rs index c03555c..4c0d912 100644 --- a/src/display.rs +++ b/src/display.rs @@ -82,14 +82,28 @@ impl SegmentPins { } #[derive(Clone, Copy, PartialEq, Eq)] -pub struct Brightness(pub u8); +pub struct Brightness(u8); + +impl Brightness { + pub const fn new(b: u8) -> Brightness { + Brightness(b) + } + + pub const fn full() -> Brightness { + Brightness(u8::MAX) + } + + pub const fn unwrap(self) -> u8 { + self.0 + } +} #[derive(Clone, Copy, PartialEq, Eq)] pub struct Segment(u8, Brightness); impl Segment { pub fn new() -> Segment { - Segment(0, Brightness(0xFF)) + Segment(0, Brightness::full()) } pub fn brightness(&mut self, b: u8) -> &mut Self { diff --git a/src/main.rs b/src/main.rs index e769479..f4a18a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use avr_device::{ asm::sleep, interrupt::{CriticalSection, Mutex}, }; -use core::cell::RefCell; +use core::cell::{Cell, RefCell}; use core_decimal_calc::{ calc::{StackCalc, StackCalcError}, Decimal, @@ -22,8 +22,6 @@ use keyboard::{Debounce, KeyPress, KeyReadout, Keyboard}; use arduino_hal::{ adc::channel::{ADC6, ADC7}, - hal::port::PB5, - port::{mode::Output, Pin}, Adc, }; use ufmt::derive::uDebug; @@ -52,8 +50,13 @@ 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) -const fn scale_brightness(b: u8) -> u32 { - IO_SEGMENT_ON_MIN_US + (IO_SEGMENT_ON_MAX_US - IO_SEGMENT_ON_MIN_US) * b as u32 / 0xFF +//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 @@ -67,51 +70,44 @@ type Calc = StackCalc; // * Set another timer to run for 1ms. // * On another timer interrupt expire disable display (LEDs off) and handle keyboard input. -static LED: Mutex>>> = Mutex::new(RefCell::new(None)); -static IO_LOOP: Mutex>> = Mutex::new(RefCell::new(None)); -static KEY_PRESS: Mutex>> = Mutex::new(RefCell::new(None)); -static ADC: Mutex>> = Mutex::new(RefCell::new(None)); -static SEGMENT_TIMER: Mutex>> = Mutex::new(RefCell::new(None)); +// Values shared between main and interrupt handlers +type Global = Mutex>; +type RefGlobal = Mutex>>; -fn try_access<'cs, 'v: 'cs, T, O>( +static IO_LOOP: RefGlobal = Mutex::new(RefCell::new(None)); +static KEY_PRESS: Global> = Mutex::new(Cell::new(None)); +static ADC: RefGlobal = Mutex::new(RefCell::new(None)); +static SEGMENT_TIMER: RefGlobal = Mutex::new(RefCell::new(None)); + +fn access_global<'cs, 'v: 'cs, T, O>( v: &'v Mutex>>, cs: CriticalSection<'cs>, f: impl for<'t> FnOnce(&'t mut T) -> O, -) -> Option { - if let Some(mut v) = v.borrow(cs).try_borrow_mut().ok() { - if let Some(v) = v.as_mut() { - return Some(f(v)); - } - } - None +) -> O { + let mut v = v.borrow(cs).borrow_mut(); + f(v.as_mut().unwrap()) } #[avr_device::interrupt(atmega328p)] unsafe fn TIMER0_COMPA() { avr_device::interrupt::free(|cs| { - // try_access(&LED, cs, |led| led.set_high()).expect("LED not available (COMPA)"); - let brightness = try_access(&IO_LOOP, cs, |io_loop| io_loop.display_on()); - if let Some(brightness) = brightness { - try_access(&SEGMENT_TIMER, cs, |st| { - st.segment_on_time(scale_brightness(brightness.0)) - }); - } + let brightness = access_global(&IO_LOOP, cs, |io_loop| io_loop.display_on()); + access_global(&SEGMENT_TIMER, cs, |st| { + st.segment_on_time(scale_brightness(brightness)) + }); }); } #[avr_device::interrupt(atmega328p)] unsafe fn TIMER0_COMPB() { avr_device::interrupt::free(|cs| { - // try_access(&LED, cs, |led| led.set_low()).expect("LED not available (COMPB)"); - try_access(&IO_LOOP, cs, |io_loop| { + access_global(&IO_LOOP, cs, |io_loop| { io_loop.display_off(); - try_access(&ADC, cs, |adc| { + access_global(&ADC, cs, |adc| { io_loop.read_key(adc); }); if let Some(key) = io_loop.advance() { - if let Some(mut key_press) = KEY_PRESS.borrow(cs).try_borrow_mut().ok() { - key_press.replace(key); - } + KEY_PRESS.borrow(cs).replace(Some(key)); } }); }); @@ -522,7 +518,6 @@ fn main() -> ! { ); let keyboard = Keyboard::new(ADC7, ADC6); let io_loop = IOLoop::new(io_pins, segment_pins, keyboard); - let led = pins.d13.into_output(); let adc = Adc::new(dp.ADC, Default::default()); let segment_timer = SegmentTimer::init(dp.TC0, IO_SEGMENT_RATE_US); @@ -538,7 +533,6 @@ fn main() -> ! { }; avr_device::interrupt::free(|cs| { - LED.borrow(cs).replace(Some(led)); IO_LOOP.borrow(cs).replace(Some(io_loop)); ADC.borrow(cs).replace(Some(adc)); SEGMENT_TIMER.borrow(cs).replace(Some(segment_timer)); @@ -550,7 +544,7 @@ fn main() -> ! { loop { let mut key = None; avr_device::interrupt::free(|cs| { - key = KEY_PRESS.borrow(cs).borrow_mut().take(); + key = KEY_PRESS.borrow(cs).take(); }); if let TransientState::Done = state.transient { @@ -611,14 +605,16 @@ fn main() -> ! { seg.dp(); } for (no, seg) in display.slice(0, DISPLAY_SEGMENTS).iter_mut().enumerate() { - seg.brightness((no * 0xFF / DISPLAY_SEGMENTS) as u8); + seg.brightness( + (no * (Brightness::full().unwrap() as usize) / DISPLAY_SEGMENTS) as u8, + ); } } TransientState::Err { .. } => display.error(), } avr_device::interrupt::free(|cs| { - try_access(&IO_LOOP, cs, |io_loop| { + access_global(&IO_LOOP, cs, |io_loop| { io_loop.update_display(&display); }); }); diff --git a/src/timer.rs b/src/timer.rs index ef5be87..8885aa8 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -5,10 +5,6 @@ const TIMER_PRESCALE: u32 = 64; // Timer clock tick rate per second const TIMER_FREQ: u32 = DefaultClock::FREQ / TIMER_PRESCALE; -// How much time in μs to reserve for post LED off operation (keyboard read) -const BUFFER_US: u32 = 250; -const BUFFER_TICKS: u32 = us_to_ticks(BUFFER_US); - const fn us_to_ticks(us: u32) -> u32 { TIMER_FREQ * us / 1_000_000 } @@ -46,12 +42,7 @@ impl SegmentTimer { // Set for how long the segment LEDs should be on in μs // Controls TIMER0_COMPB interrupt time after TIMER0_COMPA pub fn segment_on_time(&mut self, segment_on_us: u32) { - let ocra = self.0.ocr0a.read().bits(); let ocrb = us_to_ticks(segment_on_us); - assert!( - ocra as u32 > ocrb + BUFFER_TICKS, - "segment_on_us cannot be longer than segment_switch_us - buffer" - ); // Set the compare value for B match self.0.ocr0b.write(|w| { w.bits(