1use super::Duration;
23/// [`Button`](super::Button) configuration.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6pub struct ButtonConfig {
7/// Time the button should be down in order to count it as a press.
8pub debounce: Duration,
9/// Time between consecutive presses to count as a press in the same sequence instead of a new
10 /// sequence.
11pub double_click: Duration,
12/// Time the button is held before a long press is detected.
13pub long_press: Duration,
14/// Button direction.
15pub mode: Mode,
16}
1718impl ButtonConfig {
19/// Returns a new [ButtonConfig].
20pub fn new(
21 debounce: Duration,
22 double_click: Duration,
23 long_press: Duration,
24 mode: Mode,
25 ) -> Self {
26Self {
27 debounce,
28 double_click,
29 long_press,
30 mode,
31 }
32 }
33}
3435impl Default for ButtonConfig {
36fn default() -> Self {
37Self {
38 debounce: Duration::from_millis(10),
39 double_click: Duration::from_millis(350),
40 long_press: Duration::from_millis(1000),
41 mode: Mode::default(),
42 }
43 }
44}
4546/// Button direction.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
48#[cfg_attr(feature = "defmt", derive(defmt::Format))]
49pub enum Mode {
50/// Button is connected to a pin with a pull-up resistor. Button pressed it logic 0.
51#[default]
52PullUp,
53/// Button is connected to a pin with a pull-down resistor. Button pressed it logic 1.
54PullDown,
55}
5657impl Mode {
58/// Is button connected to a pin with a pull-up resistor?
59pub const fn is_pullup(&self) -> bool {
60matches!(self, Mode::PullUp)
61 }
6263/// Is button connected to a pin with a pull-down resistor?
64pub const fn is_pulldown(&self) -> bool {
65 !self.is_pullup()
66 }
67}