Struct Button

Source
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>
where P: PinWrapper, I: InstantProvider<D> + PartialEq, D: Clone + Ord,

Source

pub const fn new(pin: P, config: ButtonConfig<D>) -> Self

Creates a new Button.

Source

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.

Source

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.

Source

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();
}
Source

pub fn is_clicked(&self) -> bool

Returns true if the button was pressed once before release.

Source

pub fn is_double_clicked(&self) -> bool

Returns true if the button was pressed twice before release.

Source

pub fn is_triple_clicked(&self) -> bool

Returns true if the button was pressed three times before release.

Source

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.

Source

pub fn current_holding_time(&self) -> Option<D>

Returns current holding duration. Returns None if the button is not being held.

Source

pub const fn raw_state(&self) -> &State<I>

Returns current button state.

Source

pub const fn raw_clicks(&self) -> usize

Returns current amount of clicks, ignoring release timeout.

Source

pub const fn raw_holds(&self) -> usize

Returns current amount of holds (how many times the button was held), ignoring release timeout.

Source

pub fn tick(&mut self)

Updates button state. Call as frequently as you can, ideally in a loop in separate thread or interrupt.

Trait Implementations§

Source§

impl<P: Clone, I: Clone, D: Clone> Clone for Button<P, I, D>

Source§

fn clone(&self) -> Button<P, I, D>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: Debug, I: Debug, D: Debug> Debug for Button<P, I, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P, I, D> Freeze for Button<P, I, D>
where P: Freeze, I: Freeze, D: Freeze,

§

impl<P, I, D> RefUnwindSafe for Button<P, I, D>

§

impl<P, I, D> Send for Button<P, I, D>
where P: Send, I: Send, D: Send,

§

impl<P, I, D> Sync for Button<P, I, D>
where P: Sync, I: Sync, D: Sync,

§

impl<P, I, D> Unpin for Button<P, I, D>
where P: Unpin, I: Unpin, D: Unpin,

§

impl<P, I, D> UnwindSafe for Button<P, I, D>
where P: UnwindSafe, I: UnwindSafe, D: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.