Compare commits
3 Commits
42c18a6d5a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
cac25d6006
|
|||
|
97079c7b10
|
|||
|
d66914f74f
|
@@ -97,6 +97,10 @@ impl Brightness {
|
||||
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
|
||||
@@ -112,8 +116,8 @@ impl Segment {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
67
src/main.rs
67
src/main.rs
@@ -50,6 +50,12 @@ 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_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;
|
||||
type Calc = StackCalc<f32, STACK_DEPTH, 5, u8>;
|
||||
@@ -85,8 +91,7 @@ unsafe fn TIMER0_COMPA() {
|
||||
access_global(&IO_LOOP, cs, |io_loop| {
|
||||
access_global(&SEGMENT_TIMER, cs, |st| {
|
||||
let brightness = io_loop.display_on();
|
||||
let elapsed = st.segment_on_time(brightness.scale_brightness());
|
||||
io_loop.frame_data = Some((elapsed, brightness.unwrap()));
|
||||
st.segment_on_time(brightness.scale_brightness());
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -116,7 +121,8 @@ pub struct IOLoop {
|
||||
keyboard: Keyboard,
|
||||
readount: Option<KeyReadout>,
|
||||
debounce: Debounce,
|
||||
frame_data: Option<(u8, u8)>,
|
||||
sleep_timer: u16,
|
||||
dimming: u8,
|
||||
}
|
||||
|
||||
impl IOLoop {
|
||||
@@ -130,7 +136,8 @@ impl IOLoop {
|
||||
keyboard,
|
||||
readount: None,
|
||||
debounce: Default::default(),
|
||||
frame_data: None,
|
||||
sleep_timer: 0,
|
||||
dimming: u8::MAX,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,19 +161,42 @@ 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);
|
||||
}
|
||||
|
||||
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,10 +215,6 @@ impl IOLoop {
|
||||
pub fn frame(&self) -> (u16, usize) {
|
||||
(self.frame, self.index)
|
||||
}
|
||||
|
||||
pub fn frame_data(&self) -> Option<(u8, u8)> {
|
||||
self.frame_data
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(uDebug)]
|
||||
@@ -534,7 +560,6 @@ fn main() -> ! {
|
||||
let mut display = DispalyState::default();
|
||||
number_input.show(&mut display);
|
||||
|
||||
let mut frame_data_index: usize = 0;
|
||||
let mut calc = Calc::default();
|
||||
let mut state = State {
|
||||
transient: TransientState::Done,
|
||||
@@ -566,12 +591,6 @@ fn main() -> ! {
|
||||
state.calculator
|
||||
)
|
||||
.ok();
|
||||
match key {
|
||||
KeyPress::Num(n) => {
|
||||
frame_data_index = n as usize;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
match state.calculator.on_key(key, &mut number_input, &mut calc) {
|
||||
Ok(new_state) => state.calculator = new_state,
|
||||
Err(err) => {
|
||||
@@ -618,24 +637,16 @@ fn main() -> ! {
|
||||
.take(calc.len())
|
||||
{
|
||||
seg.dp();
|
||||
}
|
||||
for (no, seg) in display.slice(0, DISPLAY_SEGMENTS).iter_mut().enumerate() {
|
||||
seg.brightness(
|
||||
(no * (Brightness::full().unwrap() as usize) / DISPLAY_SEGMENTS) as u8,
|
||||
);
|
||||
seg.brightness(Brightness::full());
|
||||
}
|
||||
}
|
||||
TransientState::Err { .. } => display.error(),
|
||||
}
|
||||
|
||||
let ((frame, index), frame_data) = avr_device::interrupt::free(|cs| {
|
||||
avr_device::interrupt::free(|cs| {
|
||||
access_global(&IO_LOOP, cs, |io_loop| {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,13 +43,12 @@ 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) -> u8 {
|
||||
pub fn segment_on_time(&mut self, segment_on_us: u32) {
|
||||
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.timer.ocr0b.write(|w| w.bits(elapsed + delay));
|
||||
return elapsed;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user