Skip to content

Commit 98c14c4

Browse files
committed
tests: driversik; gpio-async: Implement basic debounce
To avoid spurious press/releases, use a simple loop to debounce the keys each time they change. This fairly closely models how a keyboard will really work. Ideally, this should be able to block on multiple gpios, however, with the current use of the gpio-token, it is only possible to have a single call outstanding. Signed-off-by: David Brown <[email protected]>
1 parent e774695 commit 98c14c4

File tree

1 file changed

+25
-0
lines changed
  • tests/drivers/gpio-async/src

1 file changed

+25
-0
lines changed

tests/drivers/gpio-async/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
extern crate alloc;
77

8+
use embassy_time::{Duration, Ticker};
89
use zephyr::{
10+
device::gpio::{GpioPin, GpioToken},
911
embassy::Executor,
1012
raw::{GPIO_INPUT, GPIO_OUTPUT_ACTIVE, GPIO_PULL_DOWN},
1113
};
@@ -45,8 +47,31 @@ async fn main(spawner: Spawner) {
4547

4648
loop {
4749
unsafe { row0.wait_for_high(&mut gpio_token).await };
50+
// Simple debounce, Wait for 20 consecutive high samples.
51+
debounce(&mut row0, &mut gpio_token, true).await;
4852
info!("Pressed");
4953
unsafe { row0.wait_for_low(&mut gpio_token).await };
54+
debounce(&mut row0, &mut gpio_token, false).await;
5055
info!("Released");
5156
}
5257
}
58+
59+
/// Simple debounce. Scan the gpio periodically, and return when we have 20 consecutive samples of
60+
/// the intended value.
61+
async fn debounce(pin: &mut GpioPin, gpio_token: &mut GpioToken, level: bool) {
62+
let mut count = 0;
63+
let mut ticker = Ticker::every(Duration::from_millis(1));
64+
loop {
65+
ticker.next().await;
66+
67+
if unsafe { pin.get(gpio_token) } == level {
68+
count += 1;
69+
70+
if count >= 20 {
71+
return;
72+
}
73+
} else {
74+
count = 0;
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)