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>
impl<Pin: InputPin, Cfg: Debounce> Debouncer<Pin, Cfg>
Sourcepub unsafe fn init(&self, pin: Pin) -> Result<Debounced<'_, Cfg>, InitError>
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) }?;
Sourcepub unsafe fn poll(&self) -> Result<(), PollError<Pin::Error>>
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()?;
}
Sourcepub const fn uninit(zero: Cfg::Storage) -> Self
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!
.
Sourcepub unsafe fn deinit<'a>(
&self,
pin: Debounced<'a, Cfg>,
) -> Result<Pin, DeinitError<'a, Cfg>>
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.