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
282
283
284
285
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

use core::time::Duration;

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

/// Button configuration.
pub mod config;
/// Different current global time sources.
pub mod instant;
/// 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 the `raw` state using the corresponding methods.
#[derive(Clone, Debug)]
pub struct Button<P, I, D = Duration> {
    /// An inner pin.
    pub pin: P,
    state: State<I>,
    clicks: usize,
    held: Option<D>,
    holds: usize,
    config: ButtonConfig<D>,
}

/// 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<I> {
    /// The button has been just pressed, so it is in *down* position.
    Down(I),
    /// Debounced press.
    Pressed(I),
    /// The button has been just released, so it is in *up* position.
    Up(I),
    /// The button is being held.
    Held(I),
    /// Fully released state, idle.
    Released,
    /// Initial state.
    Unknown,
}

impl<I: PartialEq> State<I> {
    /// 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, I, D> Button<P, I, D>
where
    P: PinWrapper,
    I: InstantProvider<D> + PartialEq,
    D: Clone + Ord,
{
    /// Creates a new [Button].
    pub const fn new(pin: P, config: ButtonConfig<D>) -> Self {
        Self {
            pin,
            config,
            state: State::Unknown,
            clicks: 0,
            holds: 0,
            held: None,
        }
    }

    /// Returns the number of clicks that happened before the 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
        }
    }

    /// Returns the number of holds (how many times the button was held) that happened before the last release.
    /// Returns 0 if clicks or holds are still being counted or a new streak has started.
    pub fn holds(&self) -> usize {
        if self.state == State::Released {
            self.holds
        } 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.holds = 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 holding duration before the last release.
    /// Returns [None] if the button is still being held, not released or was not held at all.
    pub fn held_time(&self) -> Option<D> {
        if self.state == State::Released {
            self.held.clone()
        } else {
            None
        }
    }

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

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

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

    /// Returns current amount of holds (how many times the button was held), ignoring release timeout.
    pub const fn raw_holds(&self) -> usize {
        self.holds
    }

    /// 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.clone() {
            State::Unknown if self.is_pin_pressed() => {
                self.clicks += 1;
                self.state = State::Down(I::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.clone());
                    } else {
                        // debounce
                    }
                } else {
                    self.state = State::Released;
                }
            }
            State::Pressed(elapsed) => {
                if self.is_pin_pressed() {
                    if elapsed.elapsed() >= self.config.hold {
                        // Do not count a click that leads to a hold
                        self.clicks -= 1;
                        self.holds += 1;
                        self.state = State::Held(elapsed.clone());
                    } else {
                        // holding
                    }
                } else {
                    self.state = State::Up(I::now())
                }
            }
            State::Up(elapsed) => {
                if elapsed.elapsed() < self.config.release {
                    if self.is_pin_pressed() {
                        self.clicks += 1;
                        self.state = State::Down(I::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(I::now());
            }
            State::Held(elapsed) => {
                if self.is_pin_released() {
                    // TODO: save prior held time?
                    self.held = Some(elapsed.elapsed());
                    self.state = State::Up(I::now());
                } else {
                    // holding
                }
            }
            _ => {}
        }
    }

    /// Reads current pin status, returns [true] if the button pin is released without debouncing.
    fn is_pin_released(&mut 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(&mut self) -> bool {
        !self.is_pin_released()
    }
}