Skip to main content

Button

Struct Button 

Source
pub struct Button<PIN> { /* private fields */ }
Expand description

Debounced wrapper around an embedded-hal input pin.

Call Button::update regularly and pass a monotonic millisecond timestamp. The timestamp can come from a hardware timer, a system tick, or any other counter that never goes backwards.

Implementations§

Source§

impl<PIN> Button<PIN>

Source

pub const fn new(pin: PIN) -> Self

Creates an active-low button with default timings.

Source

pub const fn with_config(pin: PIN, config: ButtonConfig) -> Self

Creates a button with custom timings and polarity.

Source

pub const fn config(&self) -> ButtonConfig

Returns the current configuration.

Source

pub fn set_config(&mut self, config: ButtonConfig)

Replaces the whole configuration.

Source

pub const fn debounce_ms(&self) -> Millis

Returns the current debounce time.

Source

pub fn set_debounce_ms(&mut self, debounce_ms: Millis)

Sets the debounce time.

Source

pub const fn double_click_ms(&self) -> Millis

Returns the current double-click interval.

Source

pub fn set_double_click_ms(&mut self, double_click_ms: Millis)

Sets the double-click interval.

Source

pub const fn hold_ms(&self) -> Millis

Returns the current hold threshold.

Source

pub fn set_hold_ms(&mut self, hold_ms: Millis)

Sets the hold threshold.

Source

pub const fn state(&self) -> ButtonState

Returns the stable debounced state.

Source

pub const fn is_pressed(&self) -> bool

Returns true if the stable debounced state is ButtonState::Pressed.

Source

pub const fn is_released(&self) -> bool

Returns true if the stable debounced state is ButtonState::Released.

Source

pub fn held_ms(&self, now_ms: Millis) -> Option<Millis>

Returns the current debounced hold time.

Source

pub fn pin_mut(&mut self) -> &mut PIN

Returns a mutable reference to the inner pin.

Source

pub fn into_inner(self) -> PIN

Returns the inner pin.

Source§

impl<PIN> Button<PIN>
where PIN: InputPin,

Source

pub fn update(&mut self, now_ms: Millis) -> Result<ButtonUpdate, PIN::Error>

Reads the pin once, updates the debouncer, and returns the current state with events.

This method does not block and does not wait for debounce to finish. It should be called regularly with a fresh monotonic timestamp in now_ms.

One call:

  1. Reads the physical pin through InputPin::is_high.
  2. Converts the electrical level to ButtonState using ActiveLevel.
  3. Records a raw state change and its timestamp.
  4. Accepts a raw state as stable when it has been unchanged for at least debounce_ms.
  5. Emits events.pressed when the stable state becomes ButtonState::Pressed.
  6. Emits events.released and released_after_ms when the stable state becomes ButtonState::Released.
  7. Stores a pending single click after a short release.
  8. Emits events.double_click if a second short click arrives inside double_click_ms, without emitting an earlier events.click.
  9. Emits events.click once if the pending click is not followed by a second click before double_click_ms expires.
  10. Updates held_ms while the button is pressed and emits events.hold once the hold threshold is reached.

Event flags are valid only for the returned update. Read the stable state from ButtonUpdate::state.

Auto Trait Implementations§

§

impl<PIN> Freeze for Button<PIN>
where PIN: Freeze,

§

impl<PIN> RefUnwindSafe for Button<PIN>
where PIN: RefUnwindSafe,

§

impl<PIN> Send for Button<PIN>
where PIN: Send,

§

impl<PIN> Sync for Button<PIN>
where PIN: Sync,

§

impl<PIN> Unpin for Button<PIN>
where PIN: Unpin,

§

impl<PIN> UnsafeUnpin for Button<PIN>
where PIN: UnsafeUnpin,

§

impl<PIN> UnwindSafe for Button<PIN>
where PIN: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.