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 #ifndef Bounce2_h
30 #define Bounce2_h
31 
32 #if defined(ARDUINO) && ARDUINO >= 100
33 #include "Arduino.h"
34 #else
35 #include "WProgram.h"
36 #endif
37 
38 // Uncomment the following line for "LOCK-OUT" debounce method
39 //#define BOUNCE_LOCK_OUT
40 
41 // Uncomment the following line for "BOUNCE_WITH_PROMPT_DETECTION" debounce method
42 //#define BOUNCE_WITH_PROMPT_DETECTION
43 
44 #include <inttypes.h>
45 
69 class Bounce
70 {
71  public:
72 
83  Bounce();
84 
85 
95  void attach(int pin, int mode);
96 
100  void attach(int pin);
101 
102 
110  void interval(uint16_t interval_millis);
111 
112 
121  bool update();
122 
128  bool read();
129 
133  bool fell();
134 
138  bool rose();
139 
140 
144  bool risingEdge() { return rose(); }
148  bool fallingEdge() { return fell(); }
152  Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
153  attach(pin);
154  interval(interval_millis);
155  }
156 
165  unsigned long duration();
166 
167 
168  // WIP HELD : unsigned long held(); // Returns the duration the previous state was held
169 
170  protected:
171  unsigned long previous_millis;
172  uint16_t interval_millis;
173  uint8_t state;
174  uint8_t pin;
175  unsigned long stateChangeLastTime;
176  // WIP HELD : unsigned long durationOfPreviousState;
177  virtual bool readCurrentState() { return digitalRead(pin); }
178  virtual void setPinMode(int pin, int mode) {
179 #if defined(ARDUINO_STM_NUCLEO_F103RB) || defined(ARDUINO_GENERIC_STM32F103C)
180  pinMode(pin, (WiringPinMode)mode);
181 #else
182  pinMode(pin, mode);
183 #endif
184  }
185 
186  private:
187  inline void changeState();
188  inline void setStateFlag(const uint8_t flag) {state |= flag;}
189  inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
190  inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
191  inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);}
192 };
193 
194 #endif
Definition: Bounce2.h:69
bool read()
Returns the pin&#39;s state (HIGH or LOW).
Definition: Bounce2.cpp:127
void interval(uint16_t interval_millis)
Sets the debounce interval in milliseconds.
Definition: Bounce2.cpp:36
bool fallingEdge()
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:148
void attach(int pin, int mode)
Attach to a pin and sets that pin&#39;s mode (INPUT, INPUT_PULLUP or OUTPUT).
Definition: Bounce2.cpp:31
bool fell()
Returns true if pin signal transitions from high to low.
Definition: Bounce2.cpp:137
bool update()
Updates the pin&#39;s state.
Definition: Bounce2.cpp:41
bool risingEdge()
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:144
unsigned long duration()
Returns the duration in milliseconds of the current state.
Definition: Bounce2.cpp:116
Bounce(uint8_t pin, unsigned long interval_millis)
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:152
Bounce()
Create an instance of the Bounce class.
Definition: Bounce2.cpp:11
bool rose()
Returns true if pin signal transitions from low to high.
Definition: Bounce2.cpp:132