Struct unflappable::Debouncer

source ·
pub struct Debouncer<Pin, Cfg: Debounce> { /* private fields */ }
Expand description

A pin debouncer.

Since this needs to be shared between the main application code and the interupt service routine, it is generally put into a static.

The preferred way to create one is with the macro debouncer_uninit!, which can be evaluated in a const context.

use unflappable::{debouncer_uninit, Debouncer, default::ActiveLow};
static DEBOUNCER: Debouncer<PinType, ActiveLow> = debouncer_uninit!();

Later, in your main application code, you can initialize it with the relevant input pin. This returns the Debounced pin for your use.

let debounced_pin = unsafe { DEBOUNCER.init(input_pin) }?;

Finally, make sure to arrange for regular polling of the Debouncer.

unsafe {
    DEBOUNCER.poll()?;
}

Implementations§

source§

impl<Pin: InputPin, Cfg: Debounce> Debouncer<Pin, Cfg>

source

pub unsafe fn init(&self, pin: Pin) -> Result<Debounced<'_, Cfg>, InitError>

Initialize the pin debouncer for a given input pin.

Returns an error if the Debouncer has already be initialized.

Safety

For this call to be safe, you must ensure that it is not run concurrently with a call to any unsafe method of this type, including init() itself. The usual way to do this is by calling init() once before enabling interrupts.

Examples
let debounced_pin = unsafe { DEBOUNCER.init(input_pin) }?;
source

pub unsafe fn poll(&self) -> Result<(), PollError<Pin::Error>>

Poll the pin debouncer.

This should be done on a regular basis at roughly the frequency used in the calculation of MAX_COUNT.

Safety

For this method to be safe, you must ensure that it is not run concurrently with a call to any unsafe method of this type, including poll() itself. The usual way to do this is to call poll() from a single interrupt service routine, and not enable interrupts until after the call to init() returns.

Examples
unsafe {
    DEBOUNCER.poll()?;
}
source

pub const fn uninit(zero: Cfg::Storage) -> Self

Create a new, uninitialized pin debouncer.

For technical reasons, you must pass in the zero value of the storage type Debounce::Storage, so prefer the macro debouncer_uninit!.

source

pub unsafe fn deinit<'a>( &self, pin: Debounced<'a, Cfg> ) -> Result<Pin, DeinitError<'a, Cfg>>

Destroy the debounced pin, returning the original input pin.

You must pass in the debounced pin produced from the call to init(). Returns an error if called with a Debounced pin not associated with this Debouncer.

Restores this Debouncer to the uninitialized state.

Safety

For this method to be safe, you must ensure that it is not run concurrently with a call to any unsafe method of this type, including deinit() itself.

If you only ever poll() in an interrupt service routine, you call this method in main application code, your architecture guarantees that main application code never preempts an interrupt service routine, and you disable interrupts before calling it, this will be safe.

Trait Implementations§

source§

impl<Pin, Cfg: Debounce> Sync for Debouncer<Pin, Cfg>

Auto Trait Implementations§

§

impl<Pin, Cfg> !RefUnwindSafe for Debouncer<Pin, Cfg>

§

impl<Pin, Cfg> Send for Debouncer<Pin, Cfg>where Cfg: Send, Pin: Send, <Cfg as Debounce>::Storage: Send,

§

impl<Pin, Cfg> Unpin for Debouncer<Pin, Cfg>where Cfg: Unpin, Pin: Unpin, <Cfg as Debounce>::Storage: Unpin,

§

impl<Pin, Cfg> UnwindSafe for Debouncer<Pin, Cfg>where Cfg: UnwindSafe, Pin: UnwindSafe, <Cfg as Debounce>::Storage: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.