Skip to main content

Button

Trait Button 

Source
pub trait Button: __ButtonMonitor {
    // Provided methods
    fn is_pressed(&self) -> bool { ... }
    async fn wait_for_press(&mut self) { ... }
    async fn wait_for_press_duration(&mut self) -> PressDuration { ... }
}
Expand description

Platform-agnostic button contract.

Platform crates inherit the default debouncing and press-duration behavior from shared core logic by implementing [__ButtonMonitor].

§Hardware Requirements

The button can be wired in two ways:

§Usage

Use Button::wait_for_press when you only need a debounced press event. It returns on the down edge and does not wait for release.

Use Button::wait_for_press_duration when you need to distinguish short vs. long presses. It returns as soon as it can decide, so long presses are reported before the button is released.

§Example

use device_envoy_core::button::{Button, PressDuration};

async fn log_button_presses(button: &mut impl Button) -> ! {
    // Wait for a press without measuring duration.
    button.wait_for_press().await;

    // Measure press durations in a loop.
    loop {
        match button.wait_for_press_duration().await {
            PressDuration::Short => {
                // Handle short press.
            }
            PressDuration::Long => {
                // Handle long press (fires before button is released).
            }
        }
    }
}

Provided Methods§

Source

fn is_pressed(&self) -> bool

Returns whether the button is currently pressed.

Source

async fn wait_for_press(&mut self)

Waits for the next press (button goes down, debounced). Does not wait for release.

See the Button trait documentation for usage examples.

Source

async fn wait_for_press_duration(&mut self) -> PressDuration

Waits for the next press and returns whether it was short or long (debounced).

Returns as soon as it can decide, so long presses are reported before release.

See the Button trait documentation for usage examples.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§