async_button/
config.rs

1use super::Duration;
2
3/// [`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.
8    pub debounce: Duration,
9    /// Time between consecutive presses to count as a press in the same sequence instead of a new
10    /// sequence.
11    pub double_click: Duration,
12    /// Time the button is held before a long press is detected.
13    pub long_press: Duration,
14    /// Button direction.
15    pub mode: Mode,
16}
17
18impl ButtonConfig {
19    /// Returns a new [ButtonConfig].
20    pub fn new(
21        debounce: Duration,
22        double_click: Duration,
23        long_press: Duration,
24        mode: Mode,
25    ) -> Self {
26        Self {
27            debounce,
28            double_click,
29            long_press,
30            mode,
31        }
32    }
33}
34
35impl Default for ButtonConfig {
36    fn default() -> Self {
37        Self {
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}
45
46/// 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]
52    PullUp,
53    /// Button is connected to a pin with a pull-down resistor. Button pressed it logic 1.
54    PullDown,
55}
56
57impl Mode {
58    /// Is button connected to a pin with a pull-up resistor?
59    pub const fn is_pullup(&self) -> bool {
60        matches!(self, Mode::PullUp)
61    }
62
63    /// Is button connected to a pin with a pull-down resistor?
64    pub const fn is_pulldown(&self) -> bool {
65        !self.is_pullup()
66    }
67}