Bounce2: src/Bounce2.h Source File

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
62 class Debouncer
63 {
64 // Note : this is private as it migh change in the futur
65 private:
66 static const uint8_t DEBOUNCED_STATE = 0b00000001;
67 static const uint8_t UNSTABLE_STATE = 0b00000010;
68 static const uint8_t CHANGED_STATE = 0b00000100;
69
70 private:
71 inline void changeState();
72 inline void setStateFlag(const uint8_t flag) {state |= flag;}
73 inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
74 inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
75 inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);}
76
77 public:
83 Debouncer();
84
92 void interval(uint16_t interval_millis);
93
102 bool update();
103
109 bool read();
110
114 bool fell();
115
119 bool rose();
120
121
122
123 public:
129 bool changed( ) { return getStateFlag(CHANGED_STATE); }
130
139 unsigned long duration();
140
148 unsigned long previousDuration();
149
150 protected:
151 void begin();
152 virtual bool readCurrentState() =0;
153 unsigned long previous_millis;
154 uint16_t interval_millis;
155 uint8_t state;
156 unsigned long stateChangeLastTime;
157 unsigned long durationOfPreviousState;
158
159 };
160
161
166 class Bounce : public Debouncer
167 {
168
169
170 public:
171
182 Bounce();
183
184
193 void attach(int pin, int mode);
194
198 void attach(int pin);
199
200 Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
201 attach(pin);
202 interval(interval_millis);
203 }
204
206 // Deprecated //
208
212 bool risingEdge() { return rose(); }
216 bool fallingEdge() { return fell(); }
222 protected:
223
224
225 uint8_t pin;
226
227 virtual bool readCurrentState() { return digitalRead(pin); }
228 virtual void setPinMode(int pin, int mode) {
229 #if defined(ARDUINO_ARCH_STM32F1)
230 pinMode(pin, (WiringPinMode)mode);
231 #else
232 pinMode(pin, mode);
233 #endif
234 }
235
236
237
238 };
239
243 class Button : public Bounce{
244 protected:
245 bool stateForPressed = 1; //
246 public:
257 Button(){ }
258
266 void setPressedState(bool state){
267 stateForPressed = state;
268 }
269
273 bool pressed() {
274 return changed() && (read() == stateForPressed);
275 };
276
280 bool released() {
281 return changed() && (read() != stateForPressed);
282 };
283
284
285
286 };
287
288 #endif
Debouncer::previousDuration
unsigned long previousDuration()
Returns the duration in milliseconds of the previous state.
Definition: Bounce2.cpp:103
Button::pressed
bool pressed()
Returns true if the button was physically pressed.
Definition: Bounce2.h:273
Bounce
The Debouncer:Bounce class. Links the Deboucing class to a hardware pin.
Definition: Bounce2.h:166
Debouncer::interval
void interval(uint16_t interval_millis)
Sets the debounce interval in milliseconds.
Definition: Bounce2.cpp:14
Debouncer::fell
bool fell()
Returns true if pin signal transitions from high to low.
Definition: Bounce2.cpp:128
Debouncer::read
bool read()
Returns the pin&#39;s state (HIGH or LOW).
Definition: Bounce2.cpp:118
Button::released
bool released()
Returns true if the button was physically released.
Definition: Bounce2.h:280
Debouncer
The Debouce class. Just the deboucing code separated from all harware.
Definition: Bounce2.h:62
Debouncer::Debouncer
Debouncer()
Create an instance of the Debounce class.
Definition: Bounce2.cpp:10
Bounce::fallingEdge
bool fallingEdge()
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:216
Bounce::pin
uint8_t pin
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:225
Debouncer::duration
unsigned long duration()
Returns the duration in milliseconds of the current state.
Definition: Bounce2.cpp:107
Debouncer::changed
bool changed()
Returns true if the state changed on last update.
Definition: Bounce2.h:129
Debouncer::rose
bool rose()
Returns true if pin signal transitions from low to high.
Definition: Bounce2.cpp:123
Bounce::risingEdge
bool risingEdge()
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:212
Button::setPressedState
void setPressedState(bool state)
Set the electrical state (HIGH/LOW) that corresponds to a physical press. By default, the pressed state is matched to a HIGH electrical level.
Definition: Bounce2.h:266
Button
The Debouncer:Bounce:Button class. The Button class matches an electrical state to a physical action...
Definition: Bounce2.h:243
Debouncer::update
bool update()
Updates the pin&#39;s state.
Definition: Bounce2.cpp:32
Button::Button
Button()
Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical...
Definition: Bounce2.h:257