From 00c6993ee449a26c0475c6b8f7fd1999ae9f4c75 Mon Sep 17 00:00:00 2001 From: lathoub Date: Sun, 25 Aug 2019 10:15:30 +0200 Subject: [PATCH 01/53] update tyo accept external currentState --- src/Bounce2.cpp | 45 +++++++++++++++++++++++++-------------------- src/Bounce2.h | 19 +++++++++++++------ 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/Bounce2.cpp b/src/Bounce2.cpp index 049452c..2fda2cc 100644 --- a/src/Bounce2.cpp +++ b/src/Bounce2.cpp @@ -17,20 +17,25 @@ Bounce::Bounce() void Bounce::attach(int pin) { this->pin = pin; - state = 0; - if (readCurrentState()) { - setStateFlag(DEBOUNCED_STATE | UNSTABLE_STATE); - } -#ifdef BOUNCE_LOCK_OUT - previous_millis = 0; -#else - previous_millis = millis(); -#endif + bool currentState = readCurrentState(); + attach(currentState); } -void Bounce::attach(int pin, int mode){ - setPinMode(pin, mode); - this->attach(pin); +void Bounce::attach(int pin, int mode) { + setPinMode(pin, mode); + this->attach(pin); +} + +void Bounce::attach(bool currentState) { + state = 0; + if (currentState) { + setStateFlag(DEBOUNCED_STATE | UNSTABLE_STATE); + } +#ifdef BOUNCE_LOCK_OUT + previous_millis = 0; +#else + previous_millis = millis(); +#endif } void Bounce::interval(uint16_t interval_millis) @@ -39,6 +44,14 @@ void Bounce::interval(uint16_t interval_millis) } bool Bounce::update() +{ + // Read the state of the switch in a temporary variable. + bool currentState = readCurrentState(); + + update(currentState); +} + +bool Bounce::update(bool currentState) { unsetStateFlag(CHANGED_STATE); @@ -46,7 +59,6 @@ bool Bounce::update() // Ignore everything if we are locked out if (millis() - previous_millis >= interval_millis) { - bool currentState = readCurrentState(); if ( currentState != getStateFlag(DEBOUNCED_STATE) ) { previous_millis = millis(); changeState(); @@ -55,9 +67,6 @@ bool Bounce::update() #elif defined BOUNCE_WITH_PROMPT_DETECTION - // Read the state of the switch port into a temporary variable. - bool readState = readCurrentState(); - if ( readState != getStateFlag(DEBOUNCED_STATE) ) { // We have seen a change from the current button state. @@ -81,10 +90,6 @@ bool Bounce::update() #else - // Read the state of the switch in a temporary variable. - bool currentState = readCurrentState(); - - // If the reading is different from last reading, reset the debounce counter if ( currentState != getStateFlag(UNSTABLE_STATE) ) { previous_millis = millis(); diff --git a/src/Bounce2.h b/src/Bounce2.h index da3d429..738bcf6 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -93,11 +93,16 @@ class Bounce */ void attach(int pin, int mode); - /** - Attach to a pin for advanced users. Only attach the pin this way once you have previously set it up. Otherwise use attach(int pin, int mode). - */ - void attach(int pin); - + /** + Attach to a pin for advanced users. Only attach the pin this way once you have previously set it up. Otherwise use attach(int pin, int mode). + */ + void attach(int pin); + + /** + + */ + void attach(bool currentValue); + /** @brief Sets the debounce interval in milliseconds. @@ -117,7 +122,9 @@ class Bounce @return True if the pin changed state. */ - bool update(); + bool update(); + + bool update(bool); /** @brief Returns the pin's state (HIGH or LOW). -- 2.54.0 From 79317fe13489dfcda92c9344c634b2f023c719c6 Mon Sep 17 00:00:00 2001 From: lathoub Date: Sun, 25 Aug 2019 12:00:03 +0200 Subject: [PATCH 02/53] cleanup + comments --- src/Bounce2.h | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index 738bcf6..ce3674e 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -99,7 +99,10 @@ class Bounce void attach(int pin); /** - + @brief current values are provides externally from this library (eg from shifting registred). readDigital can be slow when reading a lot of buttons + + @param currentValue + */ void attach(bool currentValue); @@ -124,10 +127,17 @@ class Bounce bool update(); - bool update(bool); + /*! + @brief "update" the object state value externally. This has to be done as often as possible (that means to include it in your loop()). Only call update() once per loop(). + + @return True if the pin changed state. + */ + bool update(bool currentValue); /** - @brief Returns the pin's state (HIGH or LOW). + @brief Returns the pin's state (HIGH or LOW). + + Do not use when providing values externally @return HIGH or LOW. */ -- 2.54.0 From 01e6fc9398d3a59544c20d6d8000117d6dcd0246 Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Sat, 5 Oct 2019 16:14:33 -0400 Subject: [PATCH 03/53] Added a protected changed() function --- src/Bounce2.cpp | 2 +- src/Bounce2.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Bounce2.cpp b/src/Bounce2.cpp index 049452c..1400123 100644 --- a/src/Bounce2.cpp +++ b/src/Bounce2.cpp @@ -104,7 +104,7 @@ bool Bounce::update() #endif - return getStateFlag(CHANGED_STATE); + return changed(); } diff --git a/src/Bounce2.h b/src/Bounce2.h index da3d429..a48f7e9 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -194,6 +194,10 @@ class Bounce inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;} inline void toggleStateFlag(const uint8_t flag) {state ^= flag;} inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);} + + protected: + bool Bounce::changed( ) { return getStateFlag(CHANGED_STATE); } + }; #endif -- 2.54.0 From 220a287467764cc11d3f0f58a038af8d459f4403 Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Sun, 6 Oct 2019 13:01:12 -0400 Subject: [PATCH 04/53] Made changed() public instead of protected --- src/Bounce2.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index a48f7e9..a8ee6d6 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -195,7 +195,7 @@ class Bounce inline void toggleStateFlag(const uint8_t flag) {state ^= flag;} inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);} - protected: + public: bool Bounce::changed( ) { return getStateFlag(CHANGED_STATE); } }; -- 2.54.0 From 51032991ac52342d98384baf48cd585dc95f1218 Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Thu, 7 Nov 2019 09:33:43 -0500 Subject: [PATCH 05/53] Update Bounce2.h --- src/Bounce2.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Bounce2.h b/src/Bounce2.h index a8ee6d6..d2ce620 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -26,6 +26,12 @@ Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +/** + * @todo Make Bounce2 more abstract. Split it from the hardware layer. + * @body Remove deboucing code from Bounce2 and make a new Debounce class from that code. Bounce2 should extend Debounce. + */ + + #ifndef Bounce2_h #define Bounce2_h -- 2.54.0 From 6228e0945186079d4535b33354168c38f1baf7cb Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Thu, 21 Nov 2019 22:09:13 -0500 Subject: [PATCH 06/53] Moved literals to header as suggested by MartinNel --- src/Bounce2.cpp | 4 +--- src/Bounce2.h | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Bounce2.cpp b/src/Bounce2.cpp index 1400123..6509e94 100644 --- a/src/Bounce2.cpp +++ b/src/Bounce2.cpp @@ -3,9 +3,7 @@ #include "Bounce2.h" -static const uint8_t DEBOUNCED_STATE = 0b00000001; -static const uint8_t UNSTABLE_STATE = 0b00000010; -static const uint8_t CHANGED_STATE = 0b00000100; + Bounce::Bounce() diff --git a/src/Bounce2.h b/src/Bounce2.h index d2ce620..e9d05c7 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -69,6 +69,10 @@ Example of two instances of the Bounce class that switches the debug LED when either one of the two buttons is pressed. */ +static const uint8_t DEBOUNCED_STATE = 0b00000001; +static const uint8_t UNSTABLE_STATE = 0b00000010; +static const uint8_t CHANGED_STATE = 0b00000100; + /** The Bounce class. */ -- 2.54.0 From 26e216c98d084e6a7f5d2c2bf703cf4463ec07af Mon Sep 17 00:00:00 2001 From: Damian Wrobel Date: Fri, 22 Nov 2019 17:40:10 +0100 Subject: [PATCH 07/53] Fix compilation error libraries/Bounce2/src/Bounce2.h:209:10: error: extra qualification 'Bounce::' on member 'changed' [-fpermissive] 209 | bool Bounce::changed( ) { return getStateFlag(CHANGED_STATE); } | ^~~~~~ Regression introduced in 01e6fc9398d3a59544c20d6d8000117d6dcd0246 Signed-off-by: Damian Wrobel --- src/Bounce2.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index e9d05c7..6f165b3 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -206,7 +206,7 @@ class Bounce inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);} public: - bool Bounce::changed( ) { return getStateFlag(CHANGED_STATE); } + bool changed( ) { return getStateFlag(CHANGED_STATE); } }; -- 2.54.0 From 14f9c5c4a6bb04187d38d92559f07da6b02d9d7f Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Fri, 14 Feb 2020 08:05:43 +0100 Subject: [PATCH 08/53] Generic Arduino_STM32 support When BluePill support have been introduced, it was not enough generic https://github.com/thomasfredericks/Bounce2/pull/35 Board specific definition have been used instead of the generic core one: `ARDUINO_ARCH_STM32F1` --- src/Bounce2.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index 6f165b3..d634890 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -191,7 +191,7 @@ class Bounce unsigned long durationOfPreviousState; virtual bool readCurrentState() { return digitalRead(pin); } virtual void setPinMode(int pin, int mode) { -#if defined(ARDUINO_STM_NUCLEO_F103RB) || defined(ARDUINO_GENERIC_STM32F103C) +#if defined(ARDUINO_ARCH_STM32F1) pinMode(pin, (WiringPinMode)mode); #else pinMode(pin, mode); -- 2.54.0 From a57ba4c66a08c34297722b45aceee188c9c4dd8d Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Wed, 26 Feb 2020 14:41:36 -0500 Subject: [PATCH 09/53] Update library.properties --- library.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library.properties b/library.properties index 98bea98..b3b9e39 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=Bounce2 -version=2.52 +version=2.53 author=Thomas O Fredericks with contributions from Eric Lowry, Jim Schimpf, Tom Harkaway, Joachim Krüger and MrGradgrind. maintainer=Thomas O Fredericks sentence=Debouncing library for Arduino and Wiring. -paragraph=Deboucing switches and toggles is important. +paragraph=Debouncing switches and toggles is important. category=Signal Input/Output url=https://github.com/thomasfredericks/Bounce2 architectures=* -- 2.54.0 From cf32eda85e39ba02be2dc1d8394c58d0e6d8ddc7 Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Wed, 26 Feb 2020 14:42:35 -0500 Subject: [PATCH 10/53] v2.53 --- library.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.json b/library.json index 845cd0a..0fc225e 100644 --- a/library.json +++ b/library.json @@ -6,7 +6,7 @@ "type": "git", "url": "https://github.com/thomasfredericks/Bounce2.git" }, - "version": "2.52", + "version": "2.53", "exclude": [ "*.png", "*.zip" -- 2.54.0 From 1af864b0bd6b167a8b63380408ae85540fb117b3 Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Tue, 19 May 2020 14:06:37 -0400 Subject: [PATCH 11/53] 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 12/53] 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 13/53] 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 );
}
}
+