Skip to main content

Debounce

Struct Debounce 

Source
pub struct Debounce { /* private fields */ }
Expand description

Cleans a noisy boolean signal by requiring it to hold steady before reporting a change.

A mechanical switch, a relay contact, or a reading crossing a threshold does not flip cleanly: for a few milliseconds it chatters between states. A Debounce reports a change only after the new value has been seen for a set number of consecutive samples, so a button press, a float-switch trip, or a threshold crossing reads as one clean event. This is the standard counter debounce: N stable samples accept a change, and any contrary sample resets the count. At a fixed sample rate, N samples is the debounce time

  • sampling every 5 ms with samples of 4 debounces over 20 ms.

§Examples

use pamoja_kit::Debounce;

// A button needs three stable samples to register.
let mut button = Debounce::new(3, false);
assert!(!button.update(true)); // first press sample
assert!(!button.update(false)); // the contact bounced back
assert!(!button.update(true)); // counting restarts
assert!(!button.update(true));
assert!(button.update(true)); // three in a row: pressed

Implementations§

Source§

impl Debounce

Source

pub fn new(samples: u16, initial: bool) -> Self

Creates a debouncer.

§Arguments
  • samples - consecutive stable samples required to accept a change. 0 and 1 both accept a change on the first contrary sample.
  • initial - the starting debounced state.
§Returns

A debouncer reporting initial until a change is confirmed.

Source

pub fn update(&mut self, raw: bool) -> bool

Feeds a raw sample and returns the debounced state.

§Arguments
  • raw - the latest raw signal value.
§Returns

The debounced state after this sample. It changes only once a contrary value has held for the required number of samples.

Source

pub fn state(&self) -> bool

Returns the current debounced state.

Trait Implementations§

Source§

impl Clone for Debounce

Source§

fn clone(&self) -> Debounce

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Debounce

Source§

impl Debug for Debounce

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for Debounce

Source§

impl PartialEq for Debounce

Source§

fn eq(&self, other: &Debounce) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Debounce

Auto Trait Implementations§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.