Skip to content

Commit

Permalink
Merge pull request tock#483 from tock/fix-buttons
Browse files Browse the repository at this point in the history
examples: buttons: fix
  • Loading branch information
jrvanwhy authored Aug 4, 2023
2 parents 9022fc3 + 482be6f commit 83bb348
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions examples/buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,38 @@
#![no_main]
#![no_std]

use core::fmt::Write;
use libtock::buttons::{ButtonListener, Buttons};
use libtock::console::Console;
use libtock::leds::Leds;
use libtock::runtime::{set_main, stack_size};
use libtock_platform::{share, Syscalls};
use libtock_runtime::TockSyscalls;

set_main! {main}
stack_size! {0x100}
stack_size! {0x1000}

fn main() {
let listener = ButtonListener(|_button, _state| {
// TODO when a console driver will be available
// println!("button {}: {}", button, state);
let listener = ButtonListener(|button, state| {
let _ = Leds::toggle(button);
writeln!(Console::writer(), "button {:?}: {:?}", button, state).unwrap();
});
if let Ok(_buttons_count) = Buttons::count() {
if let Ok(()) = share::scope(|subscribe| Buttons::register_listener(&listener, subscribe)) {
if let Ok(buttons_count) = Buttons::count() {
writeln!(Console::writer(), "button count: {}", buttons_count).unwrap();

share::scope(|subscribe| {
// Subscribe to the button callback.
Buttons::register_listener(&listener, subscribe).unwrap();

// Enable interrupts for each button press.
for i in 0..buttons_count {
Buttons::enable_interrupts(i).unwrap();
}

// Wait for buttons to be pressed.
loop {
TockSyscalls::yield_wait();
}
}
});
}
}

0 comments on commit 83bb348

Please sign in to comment.