zero latency debounce

This commit is contained in:
2025-07-18 20:31:23 +01:00
parent d69c3eb56d
commit ef97c334b5

View File

@@ -78,26 +78,24 @@ impl<KN: AdcChannel<Atmega, ADC>, KO: AdcChannel<Atmega, ADC>> Keyboard<KN, KO>
pub struct Debounce {
record: [Option<KeyReadout>; DEBOUNCE_DEPTH],
pos: usize,
last: Option<KeyReadout>,
}
impl Debounce {
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.pos += 1;
if self.pos >= self.record.len() {
self.pos = 0;
}
if self.record.iter().all(|hist| hist == &key) && self.last != key {
self.last = key;
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
}
new_key.and_then(|key| KeyPress::map(key.display_no, key.kn, key.ko))
}
}