blob: 34b7d8d886d81ab49d37f4c6f831e2206dcf2859 [file] [log] [blame]
Woytena59a7e22018-01-11 22:48:31 +01001#![no_std]
2
Woyten70c09f92019-12-01 15:17:32 +01003use core::cell::Cell;
torfmasterc84685a2019-02-08 19:23:22 +01004use libtock::buttons::ButtonState;
Alistair Francise5f74c32020-09-11 15:21:14 -07005use libtock::println;
torfmaster3d605242019-12-16 22:36:39 +01006use libtock::result::TockResult;
Woyten70c09f92019-12-01 15:17:32 +01007use libtock::timer::Duration;
Woytena59a7e22018-01-11 22:48:31 +01008
Johnathan Van Why074fa7d2020-10-14 17:06:50 -07009libtock_core::stack_size! {0x800}
10
torfmasterb06f6372019-12-17 09:20:48 +010011#[libtock::main]
torfmaster3d605242019-12-16 22:36:39 +010012async fn main() -> TockResult<()> {
Woytenb1226202020-01-13 22:47:48 +010013 let mut drivers = libtock::retrieve_drivers()?;
Woyten70c09f92019-12-01 15:17:32 +010014
Woytenb1226202020-01-13 22:47:48 +010015 let buttons_driver = drivers.buttons.init_driver()?;
16 let mut timer_driver = drivers.timer.create_timer_driver();
Woyten70c09f92019-12-01 15:17:32 +010017 let timer_driver = timer_driver.activate()?;
Alistair Francise5f74c32020-09-11 15:21:14 -070018 drivers.console.create_console();
Woyten0833a042018-04-03 19:29:34 +020019
Woyten70c09f92019-12-01 15:17:32 +010020 let pressed_count = Cell::new(0usize);
21 let released_count = Cell::new(0usize);
Woytena59a7e22018-01-11 22:48:31 +010022
Woyten70c09f92019-12-01 15:17:32 +010023 let mut callback = |_button_num, state| match state {
24 ButtonState::Pressed => pressed_count.set(pressed_count.get() + 1),
25 ButtonState::Released => released_count.set(released_count.get() + 1),
26 };
27
28 let _subscription = buttons_driver.subscribe(&mut callback)?;
29
30 for button in buttons_driver.buttons() {
31 button.enable_interrupt()?;
Woytena59a7e22018-01-11 22:48:31 +010032 }
33
Woyten70c09f92019-12-01 15:17:32 +010034 loop {
Alistair Francise5f74c32020-09-11 15:21:14 -070035 println!(
Woyten70c09f92019-12-01 15:17:32 +010036 "pressed: {}, released: {}",
37 pressed_count.get(),
38 released_count.get()
Alistair Francise5f74c32020-09-11 15:21:14 -070039 );
Woyten70c09f92019-12-01 15:17:32 +010040 timer_driver.sleep(Duration::from_ms(500)).await?;
41 }
Woytena59a7e22018-01-11 22:48:31 +010042}