A step sequencer for Adafruit's RP2040-based macropad
1use embassy_rp::gpio::Input;
2use embassy_time::{Duration, Timer};
3
4pub struct DebouncedButton<'a> {
5 pub threshold: Duration,
6 input: Input<'a>,
7}
8
9impl<'a> DebouncedButton<'a> {
10 pub fn new(input: Input<'a>, threshold: Duration) -> Self {
11 DebouncedButton { threshold, input }
12 }
13
14 pub async fn on_change(&mut self) -> bool {
15 loop {
16 let l1 = self.pressed();
17 self.input.wait_for_any_edge().await;
18 Timer::after(self.threshold).await;
19 let l2 = self.pressed();
20 if l1 != l2 {
21 break l2;
22 }
23 }
24 }
25
26 fn pressed(&mut self) -> bool {
27 self.input.is_low()
28 }
29}