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 
46 
50 class Bounce
51 {
52  public:
56  Bounce();
57 
61  void attach(int pin, int mode);
62 
66  void attach(int pin);
67 
68 
72  void interval(uint16_t interval_millis);
73 
77  bool update();
78 
82  bool read();
83 
87  bool fell();
88 
92  bool rose();
93 
94  // Partial compatibility for programs written with Bounce version 1
95  bool risingEdge() { return rose(); }
96  bool fallingEdge() { return fell(); }
97  Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
98  attach(pin);
99  interval(interval_millis);
100  }
101 
105  unsigned long duration();
106 
107 
108  // WIP HELD : unsigned long held(); // Returns the duration the previous state was held
109 
110  protected:
111  unsigned long previous_millis;
112  uint16_t interval_millis;
113  uint8_t state;
114  uint8_t pin;
115  unsigned long stateChangeLastTime;
116  // WIP HELD : unsigned long durationOfPreviousState;
117  virtual bool readCurrentState() { return digitalRead(pin); }
118  virtual void setPinMode(int pin, int mode) {
119 #if defined(ARDUINO_STM_NUCLEO_F103RB) || defined(ARDUINO_GENERIC_STM32F103C)
120  pinMode(pin, (WiringPinMode)mode);
121 #else
122  pinMode(pin, mode);
123 #endif
124  }
125 
126  private:
127  inline void changeState();
128  inline void setStateFlag(const uint8_t flag) {state |= flag;}
129  inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
130  inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
131  inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);}
132 };
133 
134 #endif
Definition: Bounce2.h:50
bool read()
Definition: Bounce2.cpp:127
void interval(uint16_t interval_millis)
Definition: Bounce2.cpp:36
void attach(int pin, int mode)
Definition: Bounce2.cpp:31
bool fell()
Definition: Bounce2.cpp:137
bool update()
Definition: Bounce2.cpp:41
unsigned long duration()
Definition: Bounce2.cpp:116
Bounce()
Definition: Bounce2.cpp:11
bool rose()
Definition: Bounce2.cpp:132