Bounce2
Bounce2.h
1/*
2 The MIT License (MIT)
3
4 Copyright (c) 2013 thomasfredericks
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in
8 the Software without restriction, including without limitation the rights to
9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 the Software, and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*/
23
24/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
25 Main code by Thomas O Fredericks (tof@t-o-f.info)
26 Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway
27 * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29
30#ifndef Bounce2_h
31#define Bounce2_h
32
33#if defined(ARDUINO) && ARDUINO >= 100
34#include "Arduino.h"
35#else
36#include "WProgram.h"
37#endif
38
39// Uncomment the following line for "LOCK-OUT" debounce method
40//#define BOUNCE_LOCK_OUT
41
42// Uncomment the following line for "BOUNCE_WITH_PROMPT_DETECTION" debounce method
43//#define BOUNCE_WITH_PROMPT_DETECTION
44
45#include <inttypes.h>
46
63{
64 // Note : this is private as it migh change in the futur
65private:
66 static const uint8_t DEBOUNCED_STATE = 0b00000001; // Final returned calculated debounced state
67 static const uint8_t UNSTABLE_STATE = 0b00000010; // Actual last state value behind the scene
68 static const uint8_t CHANGED_STATE = 0b00000100; // The DEBOUNCED_STATE has changed since last update()
69
70// Note : this is private as it migh change in the futur
71private:
72 inline void changeState();
73 inline void setStateFlag(const uint8_t flag) {state |= flag;}
74 inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
75 inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
76 inline bool getStateFlag(const uint8_t flag) const {return((state & flag) != 0);}
77
78public:
84 Debouncer();
85
93 void interval(uint16_t interval_millis);
94
103 bool update();
104
110 bool read() const;
111
115 bool fell() const;
116
120 bool rose() const;
121
122
123
124public:
130 bool changed( ) const { return getStateFlag(CHANGED_STATE); }
131
140 unsigned long currentDuration() const;
141
152 unsigned long duration() const;
153
154
162 unsigned long previousDuration() const;
163
164protected:
165 void begin();
166 virtual bool readCurrentState() =0;
167 unsigned long previous_millis;
168 uint16_t interval_millis;
169 uint8_t state;
170 unsigned long stateChangeLastTime;
171 unsigned long durationOfPreviousState;
172
173};
174
175
180class Bounce : public Debouncer
181{
182
183
184public:
185
196 Bounce();
197
198
207 void attach(int pin, int mode);
208
212 void attach(int pin);
213
214 Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
215 attach(pin);
216 interval(interval_millis);
217 }
218
219
225 inline int getPin() const {
226 return this->pin;
227 };
228
230 // Deprecated //
232
236 bool risingEdge() const { return rose(); }
240 bool fallingEdge() const { return fell(); }
246protected:
247
248
249 uint8_t pin;
250
251 virtual bool readCurrentState() { return digitalRead(pin); }
252 virtual void setPinMode(int pin, int mode) {
253#if defined(ARDUINO_ARCH_STM32F1)
254 pinMode(pin, (WiringPinMode)mode);
255#else
256 pinMode(pin, mode);
257#endif
258 }
259
260
261
262};
263
267namespace Bounce2 {
268 // code declarations
269
270class Button : public Bounce{
271protected:
272 bool stateForPressed = 1; //
273 public:
285
293 void setPressedState(bool state){
294 stateForPressed = state;
295 }
296
300 inline bool getPressedState() const {
301 return stateForPressed;
302 };
303
307 inline bool isPressed() const {
308 return read() == getPressedState();
309 };
310
314 inline bool pressed() const {
315 return changed() && isPressed();
316 };
317
321 inline bool released() const {
322 return changed() && !isPressed();
323 };
324
325};
326};
327
328#endif
Definition: Bounce2.h:270
Button()
Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical...
Definition: Bounce2.h:284
bool getPressedState() const
Get the electrical state (HIGH/LOW) that corresponds to a physical press.
Definition: Bounce2.h:300
void setPressedState(bool state)
Set the electrical state (HIGH/LOW) that corresponds to a physical press. By default,...
Definition: Bounce2.h:293
bool pressed() const
Returns true if the button was physically pressed
Definition: Bounce2.h:314
bool isPressed() const
Returns true if the button is currently physically pressed.
Definition: Bounce2.h:307
bool released() const
Returns true if the button was physically released
Definition: Bounce2.h:321
The Debouncer:Bounce class. Links the Deboucing class to a hardware pin. This class is odly named,...
Definition: Bounce2.h:181
int getPin() const
Return pin that this Bounce is attached to.
Definition: Bounce2.h:225
uint8_t pin
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:249
bool fallingEdge() const
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:240
Bounce()
Create an instance of the Bounce class.
Definition: Bounce2.cpp:142
void attach(int pin, int mode)
Attach to a pin and sets that pin's mode (INPUT, INPUT_PULLUP or OUTPUT).
Definition: Bounce2.cpp:153
bool risingEdge() const
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:236
The Debouce class. Just the deboucing code separated from all harware.
Definition: Bounce2.h:63
bool changed() const
Returns true if the state changed on last update.
Definition: Bounce2.h:130
Debouncer()
Create an instance of the Debounce class.
Definition: Bounce2.cpp:10
bool update()
Updates the pin's state.
Definition: Bounce2.cpp:32
bool fell() const
Returns true if pin signal transitions from high to low.
Definition: Bounce2.cpp:132
unsigned long currentDuration() const
Returns the duration in milliseconds of the current state.
Definition: Bounce2.cpp:107
void interval(uint16_t interval_millis)
Sets the debounce interval in milliseconds.
Definition: Bounce2.cpp:14
bool read() const
Returns the pin's state (HIGH or LOW).
Definition: Bounce2.cpp:122
bool rose() const
Returns true if pin signal transitions from low to high.
Definition: Bounce2.cpp:127
unsigned long previousDuration() const
Returns the duration in milliseconds of the previous state.
Definition: Bounce2.cpp:103
unsigned long duration() const
Returns the duration in milliseconds of the current state.
Definition: Bounce2.cpp:111
The Debouncer:Bounce:Button class. The Button class matches an electrical state to a physical action.
Definition: Bounce2.h:267