Compare commits
3 Commits
42c18a6d5a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
cac25d6006
|
|||
|
97079c7b10
|
|||
|
d66914f74f
|
@@ -97,6 +97,10 @@ impl Brightness {
|
|||||||
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
|
// Scales brightness (0-255) to range between IO_SEGMENT_ON_MIN_US and IO_SEGMENT_ON_MAX_US
|
||||||
pub fn scale_brightness(self) -> u32 {
|
pub fn scale_brightness(self) -> u32 {
|
||||||
// Using >> to avoid 32bit division which take ~576 cycles
|
// Using >> to avoid 32bit division which take ~576 cycles
|
||||||
@@ -112,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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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_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)
|
||||||
|
|
||||||
|
// 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
|
// 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>;
|
||||||
@@ -85,8 +91,7 @@ unsafe fn TIMER0_COMPA() {
|
|||||||
access_global(&IO_LOOP, cs, |io_loop| {
|
access_global(&IO_LOOP, cs, |io_loop| {
|
||||||
access_global(&SEGMENT_TIMER, cs, |st| {
|
access_global(&SEGMENT_TIMER, cs, |st| {
|
||||||
let brightness = io_loop.display_on();
|
let brightness = io_loop.display_on();
|
||||||
let elapsed = st.segment_on_time(brightness.scale_brightness());
|
st.segment_on_time(brightness.scale_brightness());
|
||||||
io_loop.frame_data = Some((elapsed, brightness.unwrap()));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -116,7 +121,8 @@ pub struct IOLoop {
|
|||||||
keyboard: Keyboard,
|
keyboard: Keyboard,
|
||||||
readount: Option<KeyReadout>,
|
readount: Option<KeyReadout>,
|
||||||
debounce: Debounce,
|
debounce: Debounce,
|
||||||
frame_data: Option<(u8, u8)>,
|
sleep_timer: u16,
|
||||||
|
dimming: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IOLoop {
|
impl IOLoop {
|
||||||
@@ -130,7 +136,8 @@ impl IOLoop {
|
|||||||
keyboard,
|
keyboard,
|
||||||
readount: None,
|
readount: None,
|
||||||
debounce: Default::default(),
|
debounce: Default::default(),
|
||||||
frame_data: None,
|
sleep_timer: 0,
|
||||||
|
dimming: u8::MAX,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,19 +161,42 @@ impl IOLoop {
|
|||||||
// 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) {
|
||||||
@@ -185,10 +215,6 @@ impl IOLoop {
|
|||||||
pub fn frame(&self) -> (u16, usize) {
|
pub fn frame(&self) -> (u16, usize) {
|
||||||
(self.frame, self.index)
|
(self.frame, self.index)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn frame_data(&self) -> Option<(u8, u8)> {
|
|
||||||
self.frame_data
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(uDebug)]
|
#[derive(uDebug)]
|
||||||
@@ -534,7 +560,6 @@ 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,
|
||||||
@@ -566,12 +591,6 @@ 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) => {
|
||||||
@@ -618,24 +637,16 @@ 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(),
|
||||||
}
|
}
|
||||||
|
|
||||||
let ((frame, index), frame_data) = 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);
|
||||||
(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
|
// 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) -> u8 {
|
pub fn segment_on_time(&mut self, segment_on_us: u32) {
|
||||||
let delay: u8 = us_to_ticks(segment_on_us)
|
let delay: u8 = us_to_ticks(segment_on_us)
|
||||||
.try_into()
|
.try_into()
|
||||||
.expect("timer init segment_on_us out of rage");
|
.expect("timer init segment_on_us out of rage");
|
||||||
let elapsed = self.timer.tcnt0.read().bits();
|
let elapsed = self.timer.tcnt0.read().bits();
|
||||||
// Set the compare value for B match
|
// Set the compare value for B match
|
||||||
self.timer.ocr0b.write(|w| w.bits(elapsed + delay));
|
self.timer.ocr0b.write(|w| w.bits(elapsed + delay));
|
||||||
return elapsed;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user