-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.rs
82 lines (74 loc) · 2.3 KB
/
config.rs
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
use core::time::Duration;
/// Default debounce time for a button.
pub const DEFAULT_DEBOUNCE: Duration = Duration::from_micros(900);
/// Default release time for a button.
pub const DEFAULT_RELEASE: Duration = Duration::from_millis(150);
/// Default hold time for a button.
pub const DEFAULT_HOLD: Duration = Duration::from_millis(500);
/// Various [Button] parameters.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ButtonConfig<D = Duration> {
/// How much time the button should be pressed to in order to count it as a press.
pub debounce: D,
/// How much time the button should not be holed to be released.
pub release: D,
/// How much time the button should be pressed to be held.
pub hold: D,
/// Button direction.
pub mode: Mode,
}
impl<D> ButtonConfig<D> {
/// Returns new [ButtonConfig].
///
/// As a general rule, `debounce` time is less then `release` time and `hold` time is larger them both.
pub fn new(debounce: D, release: D, hold: D, mode: Mode) -> Self {
Self {
debounce,
release,
hold,
mode,
}
}
}
impl Default for ButtonConfig<Duration> {
fn default() -> Self {
Self {
debounce: DEFAULT_DEBOUNCE,
release: DEFAULT_RELEASE,
hold: DEFAULT_HOLD,
mode: Mode::default(),
}
}
}
#[cfg(feature = "embassy")]
impl Default for ButtonConfig<embassy_time::Duration> {
fn default() -> Self {
use embassy_time::Duration;
// `as` is safe here because these contacts won't exceed `u64` limit
Self {
debounce: Duration::from_micros(DEFAULT_DEBOUNCE.as_micros() as u64),
release: Duration::from_millis(DEFAULT_RELEASE.as_millis() as u64),
hold: Duration::from_millis(DEFAULT_HOLD.as_millis() as u64),
mode: Mode::default(),
}
}
}
/// Button direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Mode {
/// Active 0.
#[default]
PullUp,
/// Active 1.
PullDown,
}
impl Mode {
/// Is button activated by logic zero?
pub const fn is_pullup(&self) -> bool {
matches!(self, Mode::PullUp)
}
/// Is button activated by logic one?
pub const fn is_pulldown(&self) -> bool {
!self.is_pullup()
}
}