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 
55 class Bounce
56 {
57  public:
58 
69  Bounce();
70 
71 
81  void attach(int pin, int mode);
82 
86  void attach(int pin);
87 
88 
96  void interval(uint16_t interval_millis);
97 
98 
107  bool update();
108 
114  bool read();
115 
119  bool fell();
120 
124  bool rose();
125 
126 
130  bool risingEdge() { return rose(); }
134  bool fallingEdge() { return fell(); }
138  Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
139  attach(pin);
140  interval(interval_millis);
141  }
142 
151  unsigned long duration();
152 
153 
154  // WIP HELD : unsigned long held(); // Returns the duration the previous state was held
155 
156  protected:
157  unsigned long previous_millis;
158  uint16_t interval_millis;
159  uint8_t state;
160  uint8_t pin;
161  unsigned long stateChangeLastTime;
162  // WIP HELD : unsigned long durationOfPreviousState;
163  virtual bool readCurrentState() { return digitalRead(pin); }
164  virtual void setPinMode(int pin, int mode) {
165 #if defined(ARDUINO_STM_NUCLEO_F103RB) || defined(ARDUINO_GENERIC_STM32F103C)
166  pinMode(pin, (WiringPinMode)mode);
167 #else
168  pinMode(pin, mode);
169 #endif
170  }
171 
172  private:
173  inline void changeState();
174  inline void setStateFlag(const uint8_t flag) {state |= flag;}
175  inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
176  inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
177  inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);}
178 };
179 
180 #endif
Definition: Bounce2.h:55
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:134
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:130
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:138
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