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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![cfg_attr(all(feature = "embassy", not(feature = "std")), no_std)]

cfg_if::cfg_if! {
    if #[cfg(feature = "std")] {
        use std::time::{Duration, Instant};
    } else if #[cfg(feature = "embassy")] {
        use embassy_time::{Duration, Instant};
    } else {
        use core::time::Duration;
        compile_error!("No `Instant` provider selected");
    }
}

use core::fmt::{self, Debug};

pub use config::{ButtonConfig, Mode};
pub use pin_wrapper::PinWrapper;

/// Button configuration.
mod config;
/// Wrappers for different APIs.
mod pin_wrapper;

#[cfg(all(test, feature = "std"))]
mod tests;

/// Generic button abstraction.
///
/// The crate is designed to provide a finished ([`released`](ButtonConfig#structfield.release)) state by the accessor methods.
/// However, it is also possible to get `raw` state using the corresponding methods.
pub struct Button<P> {
    /// An inner pin.
    pub pin: P,
    state: State,
    clicks: usize,
    held: Option<Duration>,
    config: ButtonConfig,
}

impl<P: Clone> Clone for Button<P> {
    fn clone(&self) -> Self {
        Self {
            pin: self.pin.clone(),
            config: self.config,
            state: self.state,
            clicks: self.clicks,
            held: self.held,
        }
    }
}
impl<P: Debug> Debug for Button<P> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Button")
            .field("pin", &self.pin)
            .field("state", &self.state)
            .field("clicks", &self.clicks)
            .field("held", &self.held)
            .field("config", &self.config)
            .finish()
    }
}

/// Represents current button state.
///
///
/// State machine diagram:
///```ignore
/// Down => Pressed | Released
/// Pressed => Held => Up
/// Up => Released | Down
/// Held => Released
/// Released => Down
/// Unknown => Down | Released
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum State {
    /// The button has been just pressed, so it is in *down* position.
    Down(Instant),
    /// Debounced press.
    Pressed(Instant),
    /// The button has been just released, so it is in *up* position.
    Up(Instant),
    /// The button is being held.
    Held(Instant),
    /// Fully released state, idle.
    Released,
    /// Initial state.
    Unknown,
}

impl State {
    /// Returns [true] if the state is [Down](State::Down).
    pub fn is_down(&self) -> bool {
        matches!(self, Self::Down(_))
    }

    /// Returns [true] if the state is [Pressed](State::Pressed).
    pub fn is_pressed(&self) -> bool {
        matches!(self, Self::Pressed(_))
    }

    /// Returns [true] if the state is [Up](State::Up).
    pub fn is_up(&self) -> bool {
        matches!(self, Self::Up(_))
    }

    /// Returns [true] if the state is [Held](State::Held).
    pub fn is_held(&self) -> bool {
        matches!(self, Self::Held(_))
    }

    /// Returns [true] if the state is [Released](State::Released).
    pub fn is_released(&self) -> bool {
        *self == Self::Released
    }

    /// Returns [true] if the state is [Unknown](State::Unknown).
    pub fn is_unknown(&self) -> bool {
        *self == Self::Unknown
    }
}

impl<P: PinWrapper> Button<P> {
    /// Creates a new [Button].
    pub const fn new(pin: P, config: ButtonConfig) -> Self {
        Self {
            pin,
            config,
            state: State::Unknown,
            clicks: 0,
            held: None,
        }
    }

    /// Returns number of clicks that happened before last release.
    /// Returns 0 if clicks are still being counted or a new streak has started.
    pub fn clicks(&self) -> usize {
        if self.state == State::Released {
            self.clicks
        } else {
            0
        }
    }

    /// Resets clicks amount and held time after release.
    ///
    /// Example:
    ///
    /// In this example, reset method makes "Clicked!" print once per click.
    /// ```ignore
    /// let mut button = Button::new(pin, ButtonConfig::default());
    ///
    /// loop {
    ///     button.tick();
    ///     
    ///     if button.is_clicked() {
    ///         println!("Clicked!");
    ///     }
    ///
    ///     button.reset();
    /// }
    /// ```
    pub fn reset(&mut self) {
        if self.state == State::Released {
            self.clicks = 0;
            self.held = None;
        }
    }

    /// Returns [true] if the button was pressed once before release.
    pub fn is_clicked(&self) -> bool {
        self.clicks() == 1
    }

    /// Returns [true] if the button was pressed twice before release.
    pub fn is_double_clicked(&self) -> bool {
        self.clicks() == 2
    }

    /// Returns [true] if the button was pressed three times before release.
    pub fn is_triple_clicked(&self) -> bool {
        self.clicks() == 3
    }

    /// Returns holing duration before last release.
    /// Returns [None] if the button is still being held or was not held at all.
    pub fn held_time(&self) -> Option<Duration> {
        self.held
    }

    /// Returns current holding duration.
    /// Returns [None] if the button is not being held.
    pub fn current_holding_time(&self) -> Option<Duration> {
        if let State::Held(dur) = self.state {
            Some(dur.elapsed())
        } else {
            None
        }
    }

    /// Returns current button state.
    pub fn raw_state(&self) -> State {
        self.state
    }

    /// Returns current amount of clicks, ignoring release timeout.
    pub fn raw_clicks(&self) -> usize {
        self.clicks
    }

    /// Updates button state.
    /// Call as frequently as you can, ideally in a loop in separate thread or interrupt.
    pub fn tick(&mut self) {
        match self.state {
            State::Unknown if self.is_pin_pressed() => {
                self.clicks = 1;
                self.state = State::Down(Instant::now());
            }
            State::Unknown if self.is_pin_released() => self.state = State::Released,

            State::Down(elapsed) => {
                if self.is_pin_pressed() {
                    if elapsed.elapsed() >= self.config.debounce {
                        self.state = State::Pressed(elapsed);
                    } else {
                        // debounce
                    }
                } else {
                    self.state = State::Released;
                }
            }
            State::Pressed(elapsed) => {
                if self.is_pin_pressed() {
                    if elapsed.elapsed() >= self.config.hold {
                        self.clicks = 0;
                        self.state = State::Held(elapsed);
                    } else {
                        // holding
                    }
                } else {
                    self.state = State::Up(Instant::now())
                }
            }
            State::Up(elapsed) => {
                if elapsed.elapsed() < self.config.release {
                    if self.is_pin_pressed() {
                        self.clicks += 1;
                        self.state = State::Down(Instant::now());
                    } else {
                        // waiting for the release timeout
                    }
                } else {
                    self.state = State::Released;
                }
            }

            State::Released if self.is_pin_pressed() => {
                self.clicks = 1;
                self.held = None;
                self.state = State::Down(Instant::now());
            }
            State::Held(elapsed) if self.is_pin_released() => {
                self.held = Some(elapsed.elapsed());
                self.state = State::Released;
            }
            _ => {}
        }
    }

    /// Reads current pin status, returns [true] if the button pin is released without debouncing.
    fn is_pin_released(&self) -> bool {
        self.pin.is_high() == self.config.mode.is_pullup()
    }

    /// Reads current pin status, returns [true] if the button pin is pressed without debouncing.
    fn is_pin_pressed(&self) -> bool {
        !self.is_pin_released()
    }
}