From 3ddefb4ba4eef14c306caf406f78879d2f4bb76b Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Sat, 8 Aug 2020 10:21:57 -0400 Subject: [PATCH] Updated documentation --- Doxyfile | 2 +- docs/files/_bounce2_8h_source.html | 24 +++--- docs/files/bounce_basic_8ino-example.html | 75 +++++++++++++++++++ docs/files/button_basic_8ino-example.html | 74 ++++++++++++++++++ docs/files/examples.html | 8 +- examples/bounce/bounce.ino | 48 ------------ examples/bounce_basic/bounce_basic.ino | 70 +++++++++++++++++ examples/bounce_button/bounce_button.ino | 72 ++++++++++++++++++ examples/buttonClass/buttonClass.ino | 59 --------------- examples/{ => more}/bounceMore/bounceMore.ino | 0 examples/{ => more}/bounceTwo/bounceTwo.ino | 0 examples/{ => more}/change/change.ino | 0 examples/{ => more}/duration/duration.ino | 0 examples/{ => more}/retrigger/retrigger.ino | 0 src/Bounce2.h | 18 +---- 15 files changed, 310 insertions(+), 140 deletions(-) create mode 100644 docs/files/bounce_basic_8ino-example.html create mode 100644 docs/files/button_basic_8ino-example.html delete mode 100644 examples/bounce/bounce.ino create mode 100644 examples/bounce_basic/bounce_basic.ino create mode 100644 examples/bounce_button/bounce_button.ino delete mode 100644 examples/buttonClass/buttonClass.ino rename examples/{ => more}/bounceMore/bounceMore.ino (100%) rename examples/{ => more}/bounceTwo/bounceTwo.ino (100%) rename examples/{ => more}/change/change.ino (100%) rename examples/{ => more}/duration/duration.ino (100%) rename examples/{ => more}/retrigger/retrigger.ino (100%) diff --git a/Doxyfile b/Doxyfile index 14ed479..934333f 100644 --- a/Doxyfile +++ b/Doxyfile @@ -906,7 +906,7 @@ EXCLUDE_SYMBOLS = # that contain example code fragments that are included (see the \include # command). -EXAMPLE_PATH = examples/bounce/bounce.ino examples/change/change.ino examples/bounce_multiple/bounce_multiple.ino examples/bounce2buttons/bounce2buttons.ino +EXAMPLE_PATH = examples/bounce_basic/bounce_basic.ino examples/change/button_basic.ino # If the value of the EXAMPLE_PATH tag contains directories, you can use the # EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp and diff --git a/docs/files/_bounce2_8h_source.html b/docs/files/_bounce2_8h_source.html index 9eab5da..ef1291b 100644 --- a/docs/files/_bounce2_8h_source.html +++ b/docs/files/_bounce2_8h_source.html @@ -66,25 +66,25 @@ $(function() {
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
+
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 
67 class Debouncer
68 {
69  // Note : this is private as it migh change in the futur
70 private:
71  static const uint8_t DEBOUNCED_STATE = 0b00000001;
72  static const uint8_t UNSTABLE_STATE = 0b00000010;
73  static const uint8_t CHANGED_STATE = 0b00000100;
74 
75 private:
76  inline void changeState();
77  inline void setStateFlag(const uint8_t flag) {state |= flag;}
78  inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
79  inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
80  inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);}
81 
82 public:
88  Debouncer();
89 
97  void interval(uint16_t interval_millis);
98 
107  bool update();
108 
114  bool read();
115 
119  bool fell();
120 
124  bool rose();
125 
126 
127 
128 public:
134  bool changed( ) { return getStateFlag(CHANGED_STATE); }
135 
144  unsigned long duration();
145 
153  unsigned long previousDuration();
154 
155 protected:
156  void begin();
157  virtual bool readCurrentState() =0;
158  unsigned long previous_millis;
159  uint16_t interval_millis;
160  uint8_t state;
161  unsigned long stateChangeLastTime;
162  unsigned long durationOfPreviousState;
163 
164 };
165 
166 
171 class Bounce : public Debouncer
172 {
173 
174 
175 public:
176 
187  Bounce();
188 
189 
198  void attach(int pin, int mode);
199 
203  void attach(int pin);
204 
205  Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
206  attach(pin);
207  interval(interval_millis);
208  }
209 
211  // Deprecated //
213 
217  bool risingEdge() { return rose(); }
221  bool fallingEdge() { return fell(); }
227 protected:
228 
229 
230  uint8_t pin;
231 
232  virtual bool readCurrentState() { return digitalRead(pin); }
233  virtual void setPinMode(int pin, int mode) {
234 #if defined(ARDUINO_ARCH_STM32F1)
235  pinMode(pin, (WiringPinMode)mode);
236 #else
237  pinMode(pin, mode);
238 #endif
239  }
240 
241 
242 
243 };
244 
248 class Button : public Bounce{
249 protected:
250  bool stateForPressed = 1; //
251  public:
262  Button(){ }
263 
271  void setPressedState(bool state){
272  stateForPressed = state;
273  }
274 
278  bool pressed() {
279  return changed() && (read() == stateForPressed);
280  };
281 
285  bool released() {
286  return changed() && (read() != stateForPressed);
287  };
288 
289 
290 
291 };
292 
293 #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:278
+
The Debouncer:Bounce class. Links the Deboucing class to a hardware pin.
Definition: Bounce2.h:171
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
+
bool released()
Returns true if the button was physically released.
Definition: Bounce2.h:285
+
The Debouce class. Just the deboucing code separated from all harware.
Definition: Bounce2.h:67
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
+
bool fallingEdge()
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:221
+
uint8_t pin
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:230
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 changed()
Returns true if the state changed on last update.
Definition: Bounce2.h:134
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 risingEdge()
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:217
+
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:271
+
The Debouncer:Bounce:Button class. The Button class matches an electrical state to a physical action...
Definition: Bounce2.h:248
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
+
Button()
Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical...
Definition: Bounce2.h:262
diff --git a/examples/bounce/bounce.ino b/examples/bounce/bounce.ino deleted file mode 100644 index 2c75bda..0000000 --- a/examples/bounce/bounce.ino +++ /dev/null @@ -1,48 +0,0 @@ - -/* - DESCRIPTION - ==================== - Simple example of the Bounce library that switches the debug LED when a button is pressed. - */ -// Include the Bounce2 library found here : -// https://github.com/thomasfredericks/Bounce2 -#include - -#define BUTTON_PIN 2 -#define LED_PIN 13 - -// Instantiate a Bounce object -Bounce debouncer = Bounce(); - -void setup() { - - // Setup the button with an internal pull-up : - pinMode(BUTTON_PIN,INPUT_PULLUP); - - // After setting up the button, setup the Bounce instance : - debouncer.attach(BUTTON_PIN); - debouncer.interval(5); // interval in ms - - //Setup the LED : - pinMode(LED_PIN,OUTPUT); - -} - -void loop() { - // Update the Bounce instance : - debouncer.update(); - - // Get the updated value : - int value = debouncer.read(); - - // Turn on or off the LED as determined by the state : - if ( value == LOW ) { - digitalWrite(LED_PIN, HIGH ); - } - else { - digitalWrite(LED_PIN, LOW ); - } - -} - - diff --git a/examples/bounce_basic/bounce_basic.ino b/examples/bounce_basic/bounce_basic.ino new file mode 100644 index 0000000..8bf1b3f --- /dev/null +++ b/examples/bounce_basic/bounce_basic.ino @@ -0,0 +1,70 @@ + +/* + DESCRIPTION + ==================== + Simple example of the Bounce library that switches a LED when + a state change (from HIGH to LOW) is triggered (for example when a button is pressed). + + Set BOUNCE_PIN to the pin attached to the input (a button for example). + Set LED_PIN to the pin attached to a LED. + +*/ + +// WE WILL attach() THE Bounce INSTANCE TO THE FOLLOWING PIN IN setup() +#define BOUNCE_PIN 2 + +// DEFINE THE PIN FOR THE LED : +// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN) +#define LED_PIN LED_BUILTIN +// 2) OTHERWISE SET YOUR OWN PIN +// #define LED_PIN 13 + +// Include the Bounce2 library found here : +// https://github.com/thomasfredericks/Bounce2 +#include + + +// INSTANTIATE A Bounce OBJECT +Bounce bounce = Bounce(); + +// SET A VARIABLE TO STORE THE LED STATE +bool ledState = LOW; + +void setup() { + + // BOUNCE SETUP + + // SELECT ONE OF THE FOLLOWING : + // 1) IF YOUR INPUT HAS AN INTERNAL PULL-UP + // bounce.attach( BOUNCE_PIN , INPUT_PULLUP ); // USE INTERNAL PULL-UP + // 2) IF YOUR INPUT USES AN EXTERNAL PULL-UP + bounce.attach( BOUNCE_PIN, INPUT ); // USE EXTERNAL PULL-UP + + // DEBOUNCE INTERVAL IN MILLISECONDS + bounce.interval(5); // interval in ms + + // LED SETUP + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, ledState); + +} + +void loop() { + // Update the Bounce instance (YOU MUST DO THIS EVERY LOOP) + bounce.update(); + + // .changed() RETURNS true IF THE STATE CHANGED (FROM HIGH TO LOW OR LOW TO HIGH) + if ( bounce.changed() ) { + // THE STATE OF THE INPUT CHANGED + // GET THE STATE + int deboucedInput = bounce.read(); + // IF THE CHANGED VALUE IS LOW + if ( deboucedInput == LOW ) { + ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState + digitalWrite(LED_PIN,ledState); // WRITE THE NEW ledState + } + } + + + +} diff --git a/examples/bounce_button/bounce_button.ino b/examples/bounce_button/bounce_button.ino new file mode 100644 index 0000000..a17a68c --- /dev/null +++ b/examples/bounce_button/bounce_button.ino @@ -0,0 +1,72 @@ + +/* + DESCRIPTION + ==================== + Pressing a physical button switches a LED on or off. + Simple example of the Button class from the Bounce library. + The Button class matches an electrical state to a physical action + with