pub trait Debounce: Default {
fn debounce(&mut self, pressed_state: bool) -> bool;
}
pub struct TrivialDebouncer ();
impl Default for TrivialDebouncer {
fn default() -> TrivialDebouncer {
TrivialDebouncer()
}
}
impl Debounce for TrivialDebouncer {
fn debounce(&mut self, pressed_state: bool) -> bool {
pressed_state
}
}
pub struct CountingDebouncer {
pressed_state: bool,
count: u8,
}
const SIGMA_MIN : u8 = 0;
const SIGMA_MAX : u8 = 12;
const SIGMA_LOW_THRESHOLD : u8 = 2;
const SIGMA_HIGH_THRESHOLD : u8 = 8;
impl Default for CountingDebouncer {
fn default() -> CountingDebouncer {
CountingDebouncer{pressed_state: false, count: SIGMA_MIN}
}
}
impl Debounce for CountingDebouncer {
fn debounce(&mut self, pressed_state: bool) -> bool {
if pressed_state {
if self.count != SIGMA_MAX {
self.count += 1;
}
if self.count > SIGMA_HIGH_THRESHOLD {
self.pressed_state = true;
}
} else {
if self.count != SIGMA_MIN {
self.count -= 1;
}
if self.count < SIGMA_LOW_THRESHOLD {
self.pressed_state = false;
}
};
self.pressed_state
}
}