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 
35 #ifndef Bounce2_h
36 #define Bounce2_h
37 
38 #if defined(ARDUINO) && ARDUINO >= 100
39 #include "Arduino.h"
40 #else
41 #include "WProgram.h"
42 #endif
43 
44 // Uncomment the following line for "LOCK-OUT" debounce method
45 //#define BOUNCE_LOCK_OUT
46 
47 // Uncomment the following line for "BOUNCE_WITH_PROMPT_DETECTION" debounce method
48 //#define BOUNCE_WITH_PROMPT_DETECTION
49 
50 #include <inttypes.h>
51 
77 class Debouncer
78 {
79  // Note : this is private as it migh change in the futur
80 private:
81  static const uint8_t DEBOUNCED_STATE = 0b00000001;
82  static const uint8_t UNSTABLE_STATE = 0b00000010;
83  static const uint8_t CHANGED_STATE = 0b00000100;
84 
85 private:
86  inline void changeState();
87  inline void setStateFlag(const uint8_t flag) {state |= flag;}
88  inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
89  inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
90  inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);}
91 
92 public:
98  Debouncer();
99 
107  void interval(uint16_t interval_millis);
108 
117  bool update();
118 
124  bool read();
125 
129  bool fell();
130 
134  bool rose();
135 
136 
137 
138 public:
144  bool changed( ) { return getStateFlag(CHANGED_STATE); }
145 
154  unsigned long duration();
155 
163  unsigned long previousDuration();
164 
165 protected:
166  void begin();
167  virtual bool readCurrentState() =0;
168  unsigned long previous_millis;
169  uint16_t interval_millis;
170  uint8_t state;
171  unsigned long stateChangeLastTime;
172  unsigned long durationOfPreviousState;
173 
174 };
175 
176 
181 class Bounce : public Debouncer
182 {
183 
184 
185 public:
186 
197  Bounce();
198 
199 
208  void attach(int pin, int mode);
209 
213  void attach(int pin);
214 
215  Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
216  attach(pin);
217  interval(interval_millis);
218  }
219 
221  // Deprecated //
223 
227  bool risingEdge() { return rose(); }
231  bool fallingEdge() { return fell(); }
237 protected:
238 
239 
240  uint8_t pin;
241 
242  virtual bool readCurrentState() { return digitalRead(pin); }
243  virtual void setPinMode(int pin, int mode) {
244 #if defined(ARDUINO_ARCH_STM32F1)
245  pinMode(pin, (WiringPinMode)mode);
246 #else
247  pinMode(pin, mode);
248 #endif
249  }
250 
251 
252 
253 };
254 
258 class Button : public Bounce{
259 protected:
260  bool stateForPressed = 1; //
261  public:
272  Button(){ }
273 
281  void setPressedState(bool state){
282  stateForPressed = state;
283  }
284 
288  bool pressed() {
289  return changed() && (read() == stateForPressed);
290  };
291 
295  bool released() {
296  return changed() && (read() != stateForPressed);
297  };
298 
299 
300 
301 };
302 
303 #endif
unsigned long previousDuration()
Returns the duration in milliseconds of the previous state.
Definition: Bounce2.cpp:103
bool pressed()
Returns true if the button was physically pressed.
Definition: Bounce2.h:288
The Debouncer:Bounce class. Links the Deboucing class to a hardware pin.
Definition: Bounce2.h:181
void interval(uint16_t interval_millis)
Sets the debounce interval in milliseconds.
Definition: Bounce2.cpp:14
bool fell()
Returns true if pin signal transitions from high to low.
Definition: Bounce2.cpp:128
bool read()
Returns the pin&#39;s state (HIGH or LOW).
Definition: Bounce2.cpp:118
bool released()
Returns true if the button was physically released.
Definition: Bounce2.h:295
The Debouce class. Just the deboucing code separated from all harware.
Definition: Bounce2.h:77
Debouncer()
Create an instance of the Debounce class.
Definition: Bounce2.cpp:10
bool fallingEdge()
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:231
uint8_t pin
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:240
unsigned long duration()
Returns the duration in milliseconds of the current state.
Definition: Bounce2.cpp:107
bool changed()
Returns true if the state changed on last update.
Definition: Bounce2.h:144
bool rose()
Returns true if pin signal transitions from low to high.
Definition: Bounce2.cpp:123
bool risingEdge()
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:227
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:281
The Debouncer:Bounce:Button class. The Button class matches an electrical state to a physical action...
Definition: Bounce2.h:258
bool update()
Updates the pin&#39;s state.
Definition: Bounce2.cpp:32
Button()
Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical...
Definition: Bounce2.h:272