| #![no_std] |
| |
| use core::cell::Cell; |
| use libtock::buttons::ButtonState; |
| use libtock::println; |
| use libtock::result::TockResult; |
| use libtock::timer::Duration; |
| |
| libtock_core::stack_size! {0x800} |
| |
| #[libtock::main] |
| async fn main() -> TockResult<()> { |
| let mut drivers = libtock::retrieve_drivers()?; |
| |
| let buttons_driver = drivers.buttons.init_driver()?; |
| let mut timer_driver = drivers.timer.create_timer_driver(); |
| let timer_driver = timer_driver.activate()?; |
| drivers.console.create_console(); |
| |
| let pressed_count = Cell::new(0usize); |
| let released_count = Cell::new(0usize); |
| |
| let mut callback = |_button_num, state| match state { |
| ButtonState::Pressed => pressed_count.set(pressed_count.get() + 1), |
| ButtonState::Released => released_count.set(released_count.get() + 1), |
| }; |
| |
| let _subscription = buttons_driver.subscribe(&mut callback)?; |
| |
| for button in buttons_driver.buttons() { |
| button.enable_interrupt()?; |
| } |
| |
| loop { |
| println!( |
| "pressed: {}, released: {}", |
| pressed_count.get(), |
| released_count.get() |
| ); |
| timer_driver.sleep(Duration::from_ms(500)).await?; |
| } |
| } |