macro_rules! button_watch {
($($tt:tt)*) => { ... };
}Expand description
Creates a button monitoring device abstraction with a background task.
This macro creates a button monitor that runs in a dedicated background task, providing continuous monitoring without interruption.
See ButtonWatchGenerated
for a sample of what the macro generates.
Syntax:
button_watch! {
[<attributes>]
[<visibility>] <Name> {
pin: <pin_ident>,
}
}§Constructors
new()— Create from a pin
§Use Cases
Use button_watch! instead of ButtonEsp when you need continuous monitoring
that works even in fast loops or select() operations. ButtonEsp starts
fresh monitoring on each call to wait_for_press(), which can miss events in busy loops.
§Parameters
name: The struct name for the button watch devicepin: The GPIO pin connected to the button
Optional:
vis: Visibility modifier (default: private)
§Example
use device_envoy_esp::{
Result,
button::{Button as _, PressDuration, PressedTo},
button_watch,
};
use embassy_executor::Spawner;
button_watch! {
ButtonWatch13 {
pin: GPIO13,
}
}
async fn example(
p: esp_hal::peripherals::Peripherals,
spawner: Spawner,
) -> Result<()> {
// Create the button monitor (spawns background task automatically)
let mut button_watch13 = ButtonWatch13::new(p.GPIO13, PressedTo::Ground, spawner)
.await?;
loop {
// Wait for button press - never misses events even if this loop is slow
match button_watch13.wait_for_press_duration().await {
PressDuration::Short => {
// Handle short press
}
PressDuration::Long => {
// Handle long press
}
}
}
Ok(())
}