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
82
pub trait Debounce: Default {
fn debounce(&mut self, pressed_state: bool) -> bool;
}
pub struct TrivialDebouncer ();
impl Default for TrivialDebouncer {
fn default() -> TrivialDebouncer {
TrivialDebouncer()
}
}
impl Debounce for TrivialDebouncer {
fn debounce(&mut self, pressed_state: bool) -> bool {
pressed_state
}
}
pub struct CountingDebouncer {
pressed_state: bool,
count: u8,
}
const SIGMA_MIN : u8 = 0;
const SIGMA_MAX : u8 = 12;
const SIGMA_LOW_THRESHOLD : u8 = 2;
const SIGMA_HIGH_THRESHOLD : u8 = 8;
impl Default for CountingDebouncer {
fn default() -> CountingDebouncer {
CountingDebouncer{pressed_state: false, count: SIGMA_MIN}
}
}
impl Debounce for CountingDebouncer {
fn debounce(&mut self, pressed_state: bool) -> bool {
if pressed_state {
if self.count != SIGMA_MAX {
self.count += 1;
}
if self.count > SIGMA_HIGH_THRESHOLD {
self.pressed_state = true;
}
} else {
if self.count != SIGMA_MIN {
self.count -= 1;
}
if self.count < SIGMA_LOW_THRESHOLD {
self.pressed_state = false;
}
};
self.pressed_state
}
}