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>
impl<PIN> Button<PIN>
Sourcepub const fn with_config(pin: PIN, config: ButtonConfig) -> Self
pub const fn with_config(pin: PIN, config: ButtonConfig) -> Self
Creates a button with custom timings and polarity.
Sourcepub const fn config(&self) -> ButtonConfig
pub const fn config(&self) -> ButtonConfig
Returns the current configuration.
Sourcepub fn set_config(&mut self, config: ButtonConfig)
pub fn set_config(&mut self, config: ButtonConfig)
Replaces the whole configuration.
Sourcepub const fn debounce_ms(&self) -> Millis
pub const fn debounce_ms(&self) -> Millis
Returns the current debounce time.
Sourcepub fn set_debounce_ms(&mut self, debounce_ms: Millis)
pub fn set_debounce_ms(&mut self, debounce_ms: Millis)
Sets the debounce time.
Sourcepub const fn double_click_ms(&self) -> Millis
pub const fn double_click_ms(&self) -> Millis
Returns the current double-click interval.
Sourcepub fn set_double_click_ms(&mut self, double_click_ms: Millis)
pub fn set_double_click_ms(&mut self, double_click_ms: Millis)
Sets the double-click interval.
Sourcepub fn set_hold_ms(&mut self, hold_ms: Millis)
pub fn set_hold_ms(&mut self, hold_ms: Millis)
Sets the hold threshold.
Sourcepub const fn state(&self) -> ButtonState
pub const fn state(&self) -> ButtonState
Returns the stable debounced state.
Sourcepub const fn is_pressed(&self) -> bool
pub const fn is_pressed(&self) -> bool
Returns true if the stable debounced state is
ButtonState::Pressed.
Sourcepub const fn is_released(&self) -> bool
pub const fn is_released(&self) -> bool
Returns true if the stable debounced state is
ButtonState::Released.
Sourcepub fn held_ms(&self, now_ms: Millis) -> Option<Millis>
pub fn held_ms(&self, now_ms: Millis) -> Option<Millis>
Returns the current debounced hold time.
Sourcepub fn into_inner(self) -> PIN
pub fn into_inner(self) -> PIN
Returns the inner pin.
Source§impl<PIN> Button<PIN>where
PIN: InputPin,
impl<PIN> Button<PIN>where
PIN: InputPin,
Sourcepub fn update(&mut self, now_ms: Millis) -> Result<ButtonUpdate, PIN::Error>
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:
- Reads the physical pin through
InputPin::is_high. - Converts the electrical level to
ButtonStateusingActiveLevel. - Records a raw state change and its timestamp.
- Accepts a raw state as stable when it has been unchanged for at least
debounce_ms. - Emits
events.pressedwhen the stable state becomesButtonState::Pressed. - Emits
events.releasedandreleased_after_mswhen the stable state becomesButtonState::Released. - Stores a pending single click after a short release.
- Emits
events.double_clickif a second short click arrives insidedouble_click_ms, without emitting an earlierevents.click. - Emits
events.clickonce if the pending click is not followed by a second click beforedouble_click_msexpires. - Updates
held_mswhile the button is pressed and emitsevents.holdonce the hold threshold is reached.
Event flags are valid only for the returned update. Read the stable
state from ButtonUpdate::state.