zero latency debounce
This commit is contained in:
@@ -78,26 +78,24 @@ impl<KN: AdcChannel<Atmega, ADC>, KO: AdcChannel<Atmega, ADC>> Keyboard<KN, KO>
|
|||||||
pub struct Debounce {
|
pub struct Debounce {
|
||||||
record: [Option<KeyReadout>; DEBOUNCE_DEPTH],
|
record: [Option<KeyReadout>; DEBOUNCE_DEPTH],
|
||||||
pos: usize,
|
pos: usize,
|
||||||
last: Option<KeyReadout>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debounce {
|
impl Debounce {
|
||||||
pub fn input(&mut self, key: Option<KeyReadout>) -> Option<KeyPress> {
|
pub fn input(&mut self, key: Option<KeyReadout>) -> Option<KeyPress> {
|
||||||
|
// React to fresh key press immediately if it was not pressed for depth of record
|
||||||
|
let new_key = key.and_then(|key| {
|
||||||
|
self.record
|
||||||
|
.iter()
|
||||||
|
.all(|hist| hist != &Some(key))
|
||||||
|
.then_some(key)
|
||||||
|
});
|
||||||
|
|
||||||
self.record[self.pos] = key;
|
self.record[self.pos] = key;
|
||||||
self.pos += 1;
|
self.pos += 1;
|
||||||
if self.pos >= self.record.len() {
|
if self.pos >= self.record.len() {
|
||||||
self.pos = 0;
|
self.pos = 0;
|
||||||
}
|
}
|
||||||
if self.record.iter().all(|hist| hist == &key) && self.last != key {
|
|
||||||
self.last = key;
|
new_key.and_then(|key| KeyPress::map(key.display_no, key.kn, key.ko))
|
||||||
key.and_then(|key_readout| {
|
|
||||||
KeyPress::map(key_readout.display_no, key_readout.kn, key_readout.ko)
|
|
||||||
})
|
|
||||||
} else if self.record.iter().all(|hist| hist == &None) {
|
|
||||||
self.last = None;
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user