1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

use tokio::{task,time};
use tokio::sync::watch;
use tokio::time::Duration;

use super::*;

pub type Debounce = DebounceAny<GpioChange>;

pub struct DebounceAny<S : Debounceable> {
  output : watch::Receiver<S>,
}

impl<S : Debounceable> Deref for DebounceAny<S> {
  type Target = watch::Receiver<S>;
  fn deref(&self) -> &Self::Target { &self.output }
}
impl<S : Debounceable> DerefMut for DebounceAny<S> {
  fn deref_mut(&mut self) -> &mut Self::Target { &mut self.output }
}

pub trait Debounceable : Copy + Send + Sync {
  type Valid : Copy + Send + Sync + 'static;
  fn valid(&self) -> Option<Self::Valid>;
  fn equivalent(a : Self::Valid, b : Self::Valid) -> bool;
}

impl Debounceable for GpioChange {
  type Valid = Level;
  fn valid(&self) -> Option<Self::Valid> { self.level }
  fn equivalent(a : Self::Valid, b : Self::Valid) -> bool { a == b }
}

impl<S : Debounceable> DebounceAny<S> {
  pub async fn new_filter(mut input : watch::Receiver<S>,
                          delays : Box<dyn Send + Fn(S::Valid) -> Duration>)
                          -> Self
  where S : 'static + Debounceable
  {
    let initial = *input.borrow();
    let (forward, output) = watch::channel(initial);
    let mut current = initial.valid();
    task::spawn(async move {
      'await_recv: loop {
        let mut recvd = input.recv().await;

        'just_recvd: loop {
          if recvd.is_none() { break 'await_recv; } // tearing down
          let proposed : S = recvd.unwrap();

          let valid = proposed.valid();
          match (current, valid) {
            (_, None) => continue 'await_recv, // startup
            (Some(cv), Some(nv)) =>
              if <S as Debounceable>::equivalent(cv,nv) {
                continue 'await_recv;
              },
            _ => (),
          };

          let delay = delays(valid.unwrap());
          let timeout = Some(time::delay_for(delay));

          tokio::select! {
            update = input.recv() => {
              recvd = update;
              continue 'just_recvd;
            }
            _ = timeout.unwrap() => {
              current = valid;
              let r = forward.broadcast(proposed);
              if r.is_err() { break 'await_recv; } // receivers gone
              continue 'await_recv;
            }
          }
        } // 'just_recvd loop
      } // 'await_recv loop
    });
    DebounceAny { output }
  }
}