Compare commits
6 Commits
f816a09554
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
cac25d6006
|
|||
|
97079c7b10
|
|||
|
d66914f74f
|
|||
|
42c18a6d5a
|
|||
|
e98a24d32d
|
|||
|
556ac72d10
|
@@ -5,7 +5,7 @@ use arduino_hal::{
|
||||
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 {
|
||||
kd_seg_a: Pin<Output, PD5>,
|
||||
@@ -82,18 +82,42 @@ 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
|
||||
}
|
||||
|
||||
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)]
|
||||
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 {
|
||||
self.1 = Brightness(b);
|
||||
pub fn brightness(&mut self, b: Brightness) -> &mut Self {
|
||||
self.1 = b;
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
110
src/main.rs
110
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,9 +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)
|
||||
|
||||
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
|
||||
}
|
||||
// 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_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
|
||||
pub const STACK_DEPTH: usize = 7;
|
||||
@@ -67,51 +67,46 @@ type Calc = StackCalc<f32, STACK_DEPTH, 5, u8>;
|
||||
// * Set another timer to run for 1ms.
|
||||
// * On another timer interrupt expire disable display (LEDs off) and handle keyboard input.
|
||||
|
||||
static LED: Mutex<RefCell<Option<Pin<Output, PB5>>>> = Mutex::new(RefCell::new(None));
|
||||
static IO_LOOP: Mutex<RefCell<Option<IOLoop>>> = Mutex::new(RefCell::new(None));
|
||||
static KEY_PRESS: Mutex<RefCell<Option<KeyPress>>> = Mutex::new(RefCell::new(None));
|
||||
static ADC: Mutex<RefCell<Option<Adc>>> = Mutex::new(RefCell::new(None));
|
||||
static SEGMENT_TIMER: Mutex<RefCell<Option<SegmentTimer>>> = Mutex::new(RefCell::new(None));
|
||||
// Values shared between main and interrupt handlers
|
||||
type Global<T> = Mutex<Cell<T>>;
|
||||
type RefGlobal<T> = Mutex<RefCell<Option<T>>>;
|
||||
|
||||
fn try_access<'cs, 'v: 'cs, T, O>(
|
||||
static IO_LOOP: RefGlobal<IOLoop> = Mutex::new(RefCell::new(None));
|
||||
static KEY_PRESS: Global<Option<KeyPress>> = Mutex::new(Cell::new(None));
|
||||
static ADC: RefGlobal<Adc> = Mutex::new(RefCell::new(None));
|
||||
static SEGMENT_TIMER: RefGlobal<SegmentTimer> = Mutex::new(RefCell::new(None));
|
||||
|
||||
fn access_global<'cs, 'v: 'cs, T, O>(
|
||||
v: &'v Mutex<RefCell<Option<T>>>,
|
||||
cs: CriticalSection<'cs>,
|
||||
f: impl for<'t> FnOnce(&'t mut T) -> O,
|
||||
) -> Option<O> {
|
||||
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))
|
||||
access_global(&IO_LOOP, cs, |io_loop| {
|
||||
access_global(&SEGMENT_TIMER, cs, |st| {
|
||||
let brightness = io_loop.display_on();
|
||||
st.segment_on_time(brightness.scale_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));
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -119,24 +114,30 @@ unsafe fn TIMER0_COMPB() {
|
||||
|
||||
pub struct IOLoop {
|
||||
index: usize,
|
||||
frame: u16,
|
||||
io_pins: IOPins,
|
||||
segment_pins: SegmentPins,
|
||||
dispaly: DispalyState,
|
||||
keyboard: Keyboard,
|
||||
readount: Option<KeyReadout>,
|
||||
debounce: Debounce,
|
||||
sleep_timer: u16,
|
||||
dimming: u8,
|
||||
}
|
||||
|
||||
impl IOLoop {
|
||||
pub fn new(io_pins: IOPins, segment_pins: SegmentPins, keyboard: Keyboard) -> IOLoop {
|
||||
IOLoop {
|
||||
index: 0,
|
||||
frame: 0,
|
||||
io_pins,
|
||||
segment_pins,
|
||||
dispaly: Default::default(),
|
||||
keyboard,
|
||||
readount: None,
|
||||
debounce: Default::default(),
|
||||
sleep_timer: 0,
|
||||
dimming: u8::MAX,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,22 +156,47 @@ impl IOLoop {
|
||||
pub fn advance(&mut self) -> Option<KeyPress> {
|
||||
self.index += 1;
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
self.select_off();
|
||||
let segment = self.dispaly[self.index];
|
||||
let brighness = segment.apply(&mut self.segment_pins);
|
||||
self.select_on();
|
||||
brighness
|
||||
brighness.dimm(self.dimming)
|
||||
}
|
||||
|
||||
pub fn display_off(&mut self) {
|
||||
@@ -185,6 +211,10 @@ impl IOLoop {
|
||||
}
|
||||
self.select_off();
|
||||
}
|
||||
|
||||
pub fn frame(&self) -> (u16, usize) {
|
||||
(self.frame, self.index)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(uDebug)]
|
||||
@@ -522,7 +552,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 +567,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 +578,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 {
|
||||
@@ -609,18 +637,16 @@ fn main() -> ! {
|
||||
.take(calc.len())
|
||||
{
|
||||
seg.dp();
|
||||
}
|
||||
for (no, seg) in display.slice(0, DISPLAY_SEGMENTS).iter_mut().enumerate() {
|
||||
seg.brightness((no * 0xFF / DISPLAY_SEGMENTS) as u8);
|
||||
seg.brightness(Brightness::full());
|
||||
}
|
||||
}
|
||||
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);
|
||||
});
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
27
src/timer.rs
27
src/timer.rs
@@ -5,16 +5,14 @@ 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
|
||||
}
|
||||
|
||||
// Timer 0 (8bit)
|
||||
pub struct SegmentTimer(TC0);
|
||||
pub struct SegmentTimer {
|
||||
timer: TC0,
|
||||
}
|
||||
|
||||
impl SegmentTimer {
|
||||
// Sets up timer to rise interrupts:
|
||||
@@ -40,24 +38,17 @@ impl SegmentTimer {
|
||||
tc0.timsk0
|
||||
.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
|
||||
// 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"
|
||||
);
|
||||
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
|
||||
self.0.ocr0b.write(|w| {
|
||||
w.bits(
|
||||
ocrb.try_into()
|
||||
.expect("timer init segment_on_us out of rage"),
|
||||
)
|
||||
});
|
||||
self.timer.ocr0b.write(|w| w.bits(elapsed + delay));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user