Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 1 | #![no_std] |
| 2 | |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 3 | use core::cell::Cell; |
torfmaster | c84685a | 2019-02-08 19:23:22 +0100 | [diff] [blame] | 4 | use libtock::buttons::ButtonState; |
Alistair Francis | e5f74c3 | 2020-09-11 15:21:14 -0700 | [diff] [blame] | 5 | use libtock::println; |
torfmaster | 3d60524 | 2019-12-16 22:36:39 +0100 | [diff] [blame] | 6 | use libtock::result::TockResult; |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 7 | use libtock::timer::Duration; |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 8 | |
Johnathan Van Why | 074fa7d | 2020-10-14 17:06:50 -0700 | [diff] [blame] | 9 | libtock_core::stack_size! {0x800} |
| 10 | |
torfmaster | b06f637 | 2019-12-17 09:20:48 +0100 | [diff] [blame] | 11 | #[libtock::main] |
torfmaster | 3d60524 | 2019-12-16 22:36:39 +0100 | [diff] [blame] | 12 | async fn main() -> TockResult<()> { |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 13 | let mut drivers = libtock::retrieve_drivers()?; |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 14 | |
Woyten | b122620 | 2020-01-13 22:47:48 +0100 | [diff] [blame] | 15 | let buttons_driver = drivers.buttons.init_driver()?; |
| 16 | let mut timer_driver = drivers.timer.create_timer_driver(); |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 17 | let timer_driver = timer_driver.activate()?; |
Alistair Francis | e5f74c3 | 2020-09-11 15:21:14 -0700 | [diff] [blame] | 18 | drivers.console.create_console(); |
Woyten | 0833a04 | 2018-04-03 19:29:34 +0200 | [diff] [blame] | 19 | |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 20 | let pressed_count = Cell::new(0usize); |
| 21 | let released_count = Cell::new(0usize); |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 22 | |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 23 | 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()?; |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 34 | loop { |
Alistair Francis | e5f74c3 | 2020-09-11 15:21:14 -0700 | [diff] [blame] | 35 | println!( |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 36 | "pressed: {}, released: {}", |
| 37 | pressed_count.get(), |
| 38 | released_count.get() |
Alistair Francis | e5f74c3 | 2020-09-11 15:21:14 -0700 | [diff] [blame] | 39 | ); |
Woyten | 70c09f9 | 2019-12-01 15:17:32 +0100 | [diff] [blame] | 40 | timer_driver.sleep(Duration::from_ms(500)).await?; |
| 41 | } |
Woyten | a59a7e2 | 2018-01-11 22:48:31 +0100 | [diff] [blame] | 42 | } |