From 1af864b0bd6b167a8b63380408ae85540fb117b3 Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Tue, 19 May 2020 14:06:37 -0400 Subject: [PATCH 1/5] Added a Button class --- .../bounceMore.ino} | 0 .../bounceTwo.ino} | 0 examples/buttonClass/buttonClass.ino | 61 +++++++++++++++++++ keywords.txt | 6 +- src/Bounce2.h | 48 +++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) rename examples/{bounce_multiple/bounce_multiple.ino => bounceMore/bounceMore.ino} (100%) rename examples/{bounce2buttons/bounce2buttons.ino => bounceTwo/bounceTwo.ino} (100%) create mode 100644 examples/buttonClass/buttonClass.ino diff --git a/examples/bounce_multiple/bounce_multiple.ino b/examples/bounceMore/bounceMore.ino similarity index 100% rename from examples/bounce_multiple/bounce_multiple.ino rename to examples/bounceMore/bounceMore.ino diff --git a/examples/bounce2buttons/bounce2buttons.ino b/examples/bounceTwo/bounceTwo.ino similarity index 100% rename from examples/bounce2buttons/bounce2buttons.ino rename to examples/bounceTwo/bounceTwo.ino diff --git a/examples/buttonClass/buttonClass.ino b/examples/buttonClass/buttonClass.ino new file mode 100644 index 0000000..56d5a57 --- /dev/null +++ b/examples/buttonClass/buttonClass.ino @@ -0,0 +1,61 @@ + + + +/* + DESCRIPTION + ==================== + Simple example of the Button class from the Bounce library. + The Button class matches an electrical state to a physical action. + that switches the debug LED when + either of 2 buttons are pressed. + */ + +// Include the Bounce2 library found here : +// https://github.com/thomasfredericks/Bounce2 +#include + +#define BUTTON_PIN_A 2 +#define BUTTON_PIN_B 3 + + +#define LED_PIN 13 + +// Instantiate a Button object +Button buttonA = Button(); + +// Instantiate another Button object +Button buttonB = Button(); + +void setup() { + + // SETUP BUTTON A + buttonA.attach( BUTTON_PIN_A , INPUT_PULLUP ); + buttonA.interval(5); // interval in ms + buttonA.setPressedState(LOW); // INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON + + // SETUP BUTTON B + buttonB.attach( BUTTON_PIN_B , INPUT_PULLUP ); + buttonB.interval(5); // interval in ms + buttonB.setPressedState(LOW); // INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON + + + // SETUP LED + pinMode(LED_PIN,OUTPUT); + +} + +void loop() { + // UPDATE THE BUTTONS + buttonA.update(); + buttonB.update(); + + + // TURN ON LED IF EITHER BUTTON IS PRESSED + if ( buttonA.pressed() || buttonB.pressed() ) { + digitalWrite(LED_PIN, HIGH ); + } + else { + digitalWrite(LED_PIN, LOW ); + } + +} diff --git a/keywords.txt b/keywords.txt index 3269e8f..8f2c4de 100644 --- a/keywords.txt +++ b/keywords.txt @@ -7,7 +7,8 @@ ####################################### Bounce KEYWORD1 -Bounce2 KEYWORD1 +Bounce2 KEYWORD1 +Button KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) @@ -20,6 +21,9 @@ attach KEYWORD2 rose KEYWORD2 fell KEYWORD2 duration KEYWORD2 +pressed KEYWORD2 +released KEYWORD2 +setPressedState KEYWORD2 ####################################### # Instances (KEYWORD2) diff --git a/src/Bounce2.h b/src/Bounce2.h index d634890..c264b21 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -208,6 +208,54 @@ class Bounce public: bool changed( ) { return getStateFlag(CHANGED_STATE); } +}; + +/** + The Button class. + */ +class Button : public Bounce{ +protected: + bool stateForPressed = 0; +public: + /*! + @brief Create an instance of the Button class. + + @code + + // Create an instance of the Button class. + Button() button; + + @endcode +*/ + Button(){ } + + /*! + @brief Set the electrical state (HIGH/LOW) that corresponds to a physical press. + + @param state + The electrical state (HIGH/LOW) that corresponds to a physical press. + +*/ + void setPressedState(bool state){ + stateForPressed = state; + } + + /*! + @brief Returns true if the button was physically pressed +*/ + bool pressed() { + return changed() && (read() == stateForPressed); + }; + + /*! + @brief Returns true if the button was physically released +*/ + bool released() { + return changed() && (read() != stateForPressed); + }; + + + }; #endif -- 2.54.0 From 6147a340581546a4abc62aaaa3e5050dbd8a0ca9 Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Thu, 21 May 2020 15:45:52 -0400 Subject: [PATCH 2/5] Changed the default stateForPressed and fixed example --- examples/buttonClass/buttonClass.ino | 10 ++++------ src/Bounce2.h | 6 +++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/buttonClass/buttonClass.ino b/examples/buttonClass/buttonClass.ino index 56d5a57..c08bb59 100644 --- a/examples/buttonClass/buttonClass.ino +++ b/examples/buttonClass/buttonClass.ino @@ -18,7 +18,7 @@ #define BUTTON_PIN_B 3 -#define LED_PIN 13 +#define LED_PIN LED_BUILTIN // Instantiate a Button object Button buttonA = Button(); @@ -50,12 +50,10 @@ void loop() { buttonB.update(); - // TURN ON LED IF EITHER BUTTON IS PRESSED + // TOGGLE THE LED IF EITHER BUTTON IS PRESSED if ( buttonA.pressed() || buttonB.pressed() ) { - digitalWrite(LED_PIN, HIGH ); + digitalWrite(LED_PIN, !digitalRead(LED_PIN) ); } - else { - digitalWrite(LED_PIN, LOW ); - } + } diff --git a/src/Bounce2.h b/src/Bounce2.h index c264b21..f266cb2 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -215,10 +215,10 @@ class Bounce */ class Button : public Bounce{ protected: - bool stateForPressed = 0; + bool stateForPressed = 1; // public: /*! - @brief Create an instance of the Button class. + @brief Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical level. @code @@ -230,7 +230,7 @@ public: Button(){ } /*! - @brief Set the electrical state (HIGH/LOW) that corresponds to a physical press. + @brief Set the electrical state (HIGH/LOW) that corresponds to a physical press. By default, the pressed state is matched to a HIGH electrical level. @param state The electrical state (HIGH/LOW) that corresponds to a physical press. -- 2.54.0 From 2b6be81dc1c5a86ddf1177f5e9c250dd89adc630 Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Thu, 21 May 2020 16:03:08 -0400 Subject: [PATCH 3/5] Updated docs and version number --- docs/files/_bounce2_8h_source.html | 30 ++- docs/files/annotated.html | 1 + docs/files/bounce2buttons_8ino-example.html | 2 +- docs/files/bounce_8ino-example.html | 1 + docs/files/bounce_multiple_8ino-example.html | 2 +- docs/files/class_bounce-members.html | 21 +- docs/files/class_bounce.html | 40 ++- docs/files/class_bounce.png | Bin 0 -> 338 bytes docs/files/class_button-members.html | 103 ++++++++ docs/files/class_button.html | 256 +++++++++++++++++++ docs/files/class_button.png | Bin 0 -> 336 bytes docs/files/classes.html | 6 +- docs/files/functions.html | 15 ++ docs/files/functions_func.html | 15 ++ docs/files/hierarchy.html | 79 ++++++ docs/files/index.html | 2 +- docs/files/menudata.js | 1 + docs/files/search/all_1.js | 1 + docs/files/search/all_5.js | 5 +- docs/files/search/all_6.js | 5 +- docs/files/search/all_7.html | 26 ++ docs/files/search/all_7.js | 4 + docs/files/search/all_8.html | 26 ++ docs/files/search/all_8.js | 4 + docs/files/search/classes_0.js | 3 +- docs/files/search/functions_1.js | 3 +- docs/files/search/functions_5.js | 5 +- docs/files/search/functions_6.js | 5 +- docs/files/search/functions_7.html | 26 ++ docs/files/search/functions_7.js | 4 + docs/files/search/functions_8.html | 26 ++ docs/files/search/functions_8.js | 4 + docs/files/search/searchdata.js | 4 +- docs/files/tabs.css | 2 +- library.json | 2 +- library.properties | 2 +- 36 files changed, 688 insertions(+), 43 deletions(-) create mode 100644 docs/files/class_bounce.png create mode 100644 docs/files/class_button-members.html create mode 100644 docs/files/class_button.html create mode 100644 docs/files/class_button.png create mode 100644 docs/files/hierarchy.html create mode 100644 docs/files/search/all_7.html create mode 100644 docs/files/search/all_7.js create mode 100644 docs/files/search/all_8.html create mode 100644 docs/files/search/all_8.js create mode 100644 docs/files/search/functions_7.html create mode 100644 docs/files/search/functions_7.js create mode 100644 docs/files/search/functions_8.html create mode 100644 docs/files/search/functions_8.js diff --git a/docs/files/_bounce2_8h_source.html b/docs/files/_bounce2_8h_source.html index c8928ec..247b63f 100644 --- a/docs/files/_bounce2_8h_source.html +++ b/docs/files/_bounce2_8h_source.html @@ -66,18 +66,24 @@ $(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 
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
+
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 
72 static const uint8_t DEBOUNCED_STATE = 0b00000001;
73 static const uint8_t UNSTABLE_STATE = 0b00000010;
74 static const uint8_t CHANGED_STATE = 0b00000100;
75 
79 class Bounce
80 {
81  public:
82 
93  Bounce();
94 
95 
104  void attach(int pin, int mode);
105 
109  void attach(int pin);
110 
111 
119  void interval(uint16_t interval_millis);
120 
121 
130  bool update();
131 
137  bool read();
138 
142  bool fell();
143 
147  bool rose();
148 
149 
153  bool risingEdge() { return rose(); }
157  bool fallingEdge() { return fell(); }
161  Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
162  attach(pin);
163  interval(interval_millis);
164  }
165 
174  unsigned long duration();
175 
183  unsigned long previousDuration();
184 
185  protected:
186  unsigned long previous_millis;
187  uint16_t interval_millis;
188  uint8_t state;
189  uint8_t pin;
190  unsigned long stateChangeLastTime;
191  unsigned long durationOfPreviousState;
192  virtual bool readCurrentState() { return digitalRead(pin); }
193  virtual void setPinMode(int pin, int mode) {
194 #if defined(ARDUINO_ARCH_STM32F1)
195  pinMode(pin, (WiringPinMode)mode);
196 #else
197  pinMode(pin, mode);
198 #endif
199  }
200 
201  private:
202  inline void changeState();
203  inline void setStateFlag(const uint8_t flag) {state |= flag;}
204  inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
205  inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
206  inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);}
207 
208  public:
209  bool changed( ) { return getStateFlag(CHANGED_STATE); }
210 
211 };
212 
216 class Button : public Bounce{
217 protected:
218  bool stateForPressed = 1; //
219 public:
230  Button(){ }
231 
239  void setPressedState(bool state){
240  stateForPressed = state;
241  }
242 
246  bool pressed() {
247  return changed() && (read() == stateForPressed);
248  };
249 
253  bool released() {
254  return changed() && (read() != stateForPressed);
255  };
256 
257 
258 
259 };
260 
261 #endif
bool pressed()
Returns true if the button was physically pressed.
Definition: Bounce2.h:246
+
Definition: Bounce2.h:79
+
bool read()
Returns the pin&#39;s state (HIGH or LOW).
Definition: Bounce2.cpp:125
+
void interval(uint16_t interval_millis)
Sets the debounce interval in milliseconds.
Definition: Bounce2.cpp:34
+
bool released()
Returns true if the button was physically released.
Definition: Bounce2.h:253
+
unsigned long previousDuration()
Returns the duration in milliseconds of the previous state.
Definition: Bounce2.cpp:110
+
bool fallingEdge()
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:157
+
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:29
+
bool fell()
Returns true if pin signal transitions from high to low.
Definition: Bounce2.cpp:135
+
bool update()
Updates the pin&#39;s state.
Definition: Bounce2.cpp:39
+
bool risingEdge()
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:153
+
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:239
+
unsigned long duration()
Returns the duration in milliseconds of the current state.
Definition: Bounce2.cpp:114
+
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:161
+
Definition: Bounce2.h:216
+
Bounce()
Create an instance of the Bounce class.
Definition: Bounce2.cpp:9
+
bool rose()
Returns true if pin signal transitions from low to high.
Definition: Bounce2.cpp:130
+
Button()
Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical...
Definition: Bounce2.h:230
diff --git a/docs/files/bounce2buttons_8ino-example.html b/docs/files/bounce2buttons_8ino-example.html index 1f7591d..7199c94 100644 --- a/docs/files/bounce2buttons_8ino-example.html +++ b/docs/files/bounce2buttons_8ino-example.html @@ -63,7 +63,7 @@ $(function() {

Example of two instances of the Bounce class that switches the debug LED when either one of the two buttons is pressed.

-
/*
DESCRIPTION
====================
Simple example of the Bounce library that switches the debug LED when
either of 2 buttons are pressed.
*/
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#define BUTTON_PIN_1 2
#define BUTTON_PIN_2 3
#define LED_PIN 13
// Instantiate a Bounce object
Bounce debouncer1 = Bounce();
// Instantiate another Bounce object
Bounce debouncer2 = Bounce();
void setup() {
// Setup the first button with an internal pull-up :
pinMode(BUTTON_PIN_1,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer1.attach(BUTTON_PIN_1);
debouncer1.interval(5); // interval in ms
// Setup the second button with an internal pull-up :
pinMode(BUTTON_PIN_2,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer2.attach(BUTTON_PIN_2);
debouncer2.interval(5); // interval in ms
//Setup the LED :
pinMode(LED_PIN,OUTPUT);
}
void loop() {
// Update the Bounce instances :
debouncer1.update();
debouncer2.update();
// Get the updated value :
int value1 = debouncer1.read();
int value2 = debouncer2.read();
// Turn on the LED if either button is pressed :
if ( value1 == LOW || value2 == LOW ) {
digitalWrite(LED_PIN, HIGH );
}
else {
digitalWrite(LED_PIN, LOW );
}
}
+