pub struct Button<P, I, D = Duration> {
pub pin: P,
/* private fields */
}
Expand description
Generic button abstraction.
The crate is designed to provide a finished (released
) state by the accessor methods.
However, it is also possible to get the raw
state using the corresponding methods.
Fields§
§pin: P
An inner pin.
Implementations§
Source§impl<P, I, D> Button<P, I, D>
impl<P, I, D> Button<P, I, D>
Sourcepub const fn new(pin: P, config: ButtonConfig<D>) -> Self
pub const fn new(pin: P, config: ButtonConfig<D>) -> Self
Creates a new Button.
Sourcepub fn clicks(&self) -> usize
pub fn clicks(&self) -> usize
Returns the number of clicks that happened before the last release. Returns 0 if clicks are still being counted or a new streak has started.
Sourcepub fn holds(&self) -> usize
pub fn holds(&self) -> usize
Returns the number of holds (how many times the button was held) that happened before the last release. Returns 0 if clicks or holds are still being counted or a new streak has started.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets clicks amount and held time after release.
Example:
In this example, reset method makes “Clicked!” print once per click.
let mut button = Button::new(pin, ButtonConfig::default());
loop {
button.tick();
if button.is_clicked() {
println!("Clicked!");
}
button.reset();
}
Sourcepub fn is_clicked(&self) -> bool
pub fn is_clicked(&self) -> bool
Returns true if the button was pressed once before release.
Sourcepub fn is_double_clicked(&self) -> bool
pub fn is_double_clicked(&self) -> bool
Returns true if the button was pressed twice before release.
Sourcepub fn is_triple_clicked(&self) -> bool
pub fn is_triple_clicked(&self) -> bool
Returns true if the button was pressed three times before release.
Sourcepub fn held_time(&self) -> Option<D>
pub fn held_time(&self) -> Option<D>
Returns holding duration before the last release. Returns None if the button is still being held, not released or was not held at all.
Sourcepub fn current_holding_time(&self) -> Option<D>
pub fn current_holding_time(&self) -> Option<D>
Returns current holding duration. Returns None if the button is not being held.
Sourcepub const fn raw_clicks(&self) -> usize
pub const fn raw_clicks(&self) -> usize
Returns current amount of clicks, ignoring release timeout.