Skip to main content

button_watch

Macro button_watch 

Source
macro_rules! button_watch {
    ($($tt:tt)*) => { ... };
}
Available on target_os=none only.
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.

§Constructors

§Use Cases

Use button_watch! instead of Button when you need continuous monitoring that works even in fast loops or select() operations. Button 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 device
  • pin: The GPIO pin connected to the button

Optional:

  • vis: Visibility modifier (default: private)

§Example

use device_envoy::button_watch;
use device_envoy::button::PressDuration;
use device_envoy::button::PressedTo;
use embassy_executor::Spawner;

button_watch! {
    ButtonWatch13 {
        pin: PIN_13,
    }
}

async fn example(p: embassy_rp::Peripherals, spawner: Spawner) {
    // Create the button monitor (spawns background task automatically)
    let button_watch13 = ButtonWatch13::new(p.PIN_13, PressedTo::Ground, spawner)
        .expect("Failed to create button monitor");

    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
            }
        }
    }
}

Syntax:

button_watch! {
    [<attributes>]
    [<visibility>] <Name> {
        pin: <pin_ident>,
    }
}