Trait unflappable::Debounce
source · pub trait Debounce {
type Storage: From<u8> + BitAnd<Output = Self::Storage> + BitAndAssign + BitOr<Output = Self::Storage> + BitOrAssign + Not<Output = Self::Storage> + Shl<u8, Output = Self::Storage> + Shr<u8, Output = Self::Storage> + AddAssign + SubAssign + Eq + Copy;
const MAX_COUNT: Self::Storage;
const INIT_HIGH: bool;
}
Expand description
Static configuration of the debouncing algorithm.
Required Associated Types§
sourcetype Storage: From<u8> + BitAnd<Output = Self::Storage> + BitAndAssign + BitOr<Output = Self::Storage> + BitOrAssign + Not<Output = Self::Storage> + Shl<u8, Output = Self::Storage> + Shr<u8, Output = Self::Storage> + AddAssign + SubAssign + Eq + Copy
type Storage: From<u8> + BitAnd<Output = Self::Storage> + BitAndAssign + BitOr<Output = Self::Storage> + BitOrAssign + Not<Output = Self::Storage> + Shl<u8, Output = Self::Storage> + Shr<u8, Output = Self::Storage> + AddAssign + SubAssign + Eq + Copy
The storage type of the state. For most usages, u8
is plenty
big enough. You almost certainly don’t need more than a u8
.
Required Associated Constants§
sourceconst MAX_COUNT: Self::Storage
const MAX_COUNT: Self::Storage
The number of samples required to mark a state change.
Unlike many debouncing algorithms, the integration approach
doesn’t require a fixed number of consistent samples in a row.
Rather, if in n + m
samples we see n
of the new state and
m
of the old state, a transition will be marked if the
difference n - m
reaches MAX_COUNT
.
This should be configured based on the following formula: if
d
is the minimum debounce delay (in seconds), and f
is the
number of times the debouncer is polled per second, the
MAX_COUNT
should be set to the product d * f
. For instance,
if polling 100 times a second (100 Hz), with a minimum delay of
50 milliseconds, set this to 5.
Note: this must be non zero, and must be represented in two
bits fewer than the storage you provide (e.g. if using u8
,
MAX_COUNT
cannot exceed 0x3f
. For the algorithm to perform
any meaningful debouncing, it must be greater than 1.