pub struct Button<'a> { /* private fields */ }target_os=none only.Expand description
A device abstraction for a button with debouncing and press duration detection.
§Hardware Requirements
The button can be wired in two ways:
PressedTo::Voltage: Button connects pin to 3.3V when pressed (uses pull-down)PressedTo::Ground: Button connects pin to GND when pressed (uses pull-up)
Important: Pico 2 (RP2350) has a known silicon bug (erratum E9) with pull-down
resistors that can leave the pin reading HIGH after release. Wire buttons to GND and
use PressedTo::Ground on Pico 2.
§Usage
Use wait_for_press() when you only need a debounced
press event. It returns on the down edge and does not wait for release.
Use 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::button::{Button, PressDuration, PressedTo};
async fn example(p: embassy_rp::Peripherals) {
let mut button = Button::new(p.PIN_13, PressedTo::Ground);
// 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)
}
}
}
}Implementations§
Source§impl<'a> Button<'a>
impl<'a> Button<'a>
Sourcepub fn new<P: Pin>(pin: Peri<'a, P>, pressed_to: PressedTo) -> Self
pub fn new<P: Pin>(pin: Peri<'a, P>, pressed_to: PressedTo) -> Self
Creates a new Button instance from a pin.
The pin is configured based on the connection type:
PressedTo::Voltage: Uses internal pull-down (button to 3.3V)PressedTo::Ground: Uses internal pull-up (button to GND)
Sourcepub fn is_pressed(&self) -> bool
pub fn is_pressed(&self) -> bool
Returns whether the button is currently pressed.
Sourcepub async fn wait_for_press(&mut self)
pub async fn wait_for_press(&mut self)
Waits for the next press (button goes down, debounced). Does not wait for release.
See Button for usage example
Sourcepub async fn wait_for_press_duration(&mut self) -> PressDuration
pub 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 Button for usage example
Sourcepub async fn wait_for_release(&mut self)
pub async fn wait_for_release(&mut self)
Waits until the button is released (debounced).
Auto Trait Implementations§
impl<'a> Freeze for Button<'a>
impl<'a> RefUnwindSafe for Button<'a>
impl<'a> Send for Button<'a>
impl<'a> Sync for Button<'a>
impl<'a> Unpin for Button<'a>
impl<'a> !UnwindSafe for Button<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more