diff --git a/Cargo.lock b/Cargo.lock index c8501b8..7348c76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,6 +36,7 @@ name = "avr-calc" version = "0.1.0" dependencies = [ "arduino-hal", + "avr-device", "calc-math", "embedded-hal 1.0.0", "nb 1.1.0", diff --git a/Cargo.toml b/Cargo.toml index a8c1929..1489607 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ ufmt = "0.2.0" nb = "1.1.0" embedded-hal = "1.0" calc-math = { path = "../calc-math", features = ["ufmt"] } +avr-device = "0.7.0" [dependencies.arduino-hal] git = "https://github.com/rahix/avr-hal" diff --git a/src/main.rs b/src/main.rs index b04850c..bfc8cbf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#![feature(abi_avr_interrupt)] #![no_std] #![no_main] @@ -40,6 +41,20 @@ pub const STACK_DEPTH: usize = 7; type Calc = StackCalc; +// Interrupt driven I/O +// +// * Run timer at 100Hz (10ms) to step through segments. +// * On each segment interrupt configure segments and enable output (LEDs on). +// * Set another timer to run for 1ms. +// * On another timer interrupt expire disable display (LEDs off) and handle keyboard input. + +#[avr_device::interrupt(atmega328p)] +fn TIMER1_OVF() { + //TODO: Handle timer interrupt to display next segment + // file:///home/hxd/projects/avr-calc/target/avr-none/doc/avr_device/interrupt/index.html + // ... +} + struct IOSelect<'p> { display_no: usize, pin: &'p mut Pin, @@ -442,8 +457,15 @@ fn main() -> ! { calculator: CalcluclatorState::EnterSignificant, }; + // TODO: configure timer, enable it to interrupt: dp.TC1.tifr1 + + unsafe { + avr_device::interrupt::enable(); + } + loop { let mut last_key_readout: Option = None; + // Timing: 1ms display, 0.230ms read key; every 12ms/83Hz for (mut io_select, ss) in io.iter_mut().zip(display.iter_segments()) { ss.apply(&mut seg); io_select.set_on();