From 91a6e946b6aaa21df14b084f47f6886587526077 Mon Sep 17 00:00:00 2001 From: Christian Iversen Date: Sat, 11 Oct 2014 18:17:43 +0200 Subject: [PATCH 1/4] * Renamed BOUNCE_LOCK-OUT to BOUNCE_LOCK_OUT. Only the latter is a valid preprocessor define --- Bounce2/Bounce2.cpp | 4 ++-- Bounce2/Bounce2.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Bounce2/Bounce2.cpp b/Bounce2/Bounce2.cpp index 3f15364..7f40970 100644 --- a/Bounce2/Bounce2.cpp +++ b/Bounce2/Bounce2.cpp @@ -27,7 +27,7 @@ void Bounce::attach(int pin) { if (digitalRead(pin)) { state = _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE); } - #ifdef BOUNCE_LOCK-OUT + #ifdef BOUNCE_LOCK_OUT previous_millis = 0; #else previous_millis = millis(); @@ -41,7 +41,7 @@ void Bounce::interval(uint16_t interval_millis) bool Bounce::update() { -#ifdef BOUNCE_LOCK-OUT +#ifdef BOUNCE_LOCK_OUT state &= ~_BV(STATE_CHANGED); // Ignore everything if we are locked out if (millis() - previous_millis >= interval_millis) { diff --git a/Bounce2/Bounce2.h b/Bounce2/Bounce2.h index 59b5f13..2032650 100644 --- a/Bounce2/Bounce2.h +++ b/Bounce2/Bounce2.h @@ -30,7 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ // Uncomment the following line for "LOCK-OUT" debounce method -//#define BOUNCE_LOCK-OUT +//#define BOUNCE_LOCK_OUT #ifndef Bounce2_h -- 2.54.0 From ee2f46fb87dd31df16d7f9360b2aefbee20ec57a Mon Sep 17 00:00:00 2001 From: Christian Iversen Date: Sat, 11 Oct 2014 18:21:59 +0200 Subject: [PATCH 2/4] + Added safeguard against old invalid define --- Bounce2/Bounce2.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Bounce2/Bounce2.h b/Bounce2/Bounce2.h index 2032650..df9402c 100644 --- a/Bounce2/Bounce2.h +++ b/Bounce2/Bounce2.h @@ -29,6 +29,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +#ifdef BOUNCE_LOCK +#error You are using the invalid BOUNCE_LOCK-OUT define. Please update your sources to use BOUNCE_LOCK_OUT +#endif + // Uncomment the following line for "LOCK-OUT" debounce method //#define BOUNCE_LOCK_OUT -- 2.54.0 From 0a664243eef9e46ffbf2fd7473a157bc588a7adf Mon Sep 17 00:00:00 2001 From: Christian Iversen Date: Sat, 11 Oct 2014 18:27:20 +0200 Subject: [PATCH 3/4] * Converted tabs to spaces * Sanitized indentation --- Bounce2/Bounce2.cpp | 94 +++++++++++++++++++++---------------------- Bounce2/Bounce2.h | 97 ++++++++++++++++++++++----------------------- 2 files changed, 95 insertions(+), 96 deletions(-) diff --git a/Bounce2/Bounce2.cpp b/Bounce2/Bounce2.cpp index 7f40970..c20abd8 100644 --- a/Bounce2/Bounce2.cpp +++ b/Bounce2/Bounce2.cpp @@ -1,4 +1,3 @@ - // Please read Bounce2.h for information about the liscence and authors #if defined(ARDUINO) && ARDUINO >= 100 @@ -14,80 +13,81 @@ Bounce::Bounce() -: previous_millis(0) -, interval_millis(10) -, state(0) -, pin(0) + : previous_millis(0) + , interval_millis(10) + , state(0) + , pin(0) {} void Bounce::attach(int pin) { - this->pin = pin; - bool read = digitalRead(pin); - state = 0; - if (digitalRead(pin)) { - state = _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE); - } - #ifdef BOUNCE_LOCK_OUT - previous_millis = 0; - #else - previous_millis = millis(); - #endif + this->pin = pin; + bool read = digitalRead(pin); + state = 0; + if (digitalRead(pin)) { + state = _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE); + } +#ifdef BOUNCE_LOCK_OUT + previous_millis = 0; +#else + previous_millis = millis(); +#endif } void Bounce::interval(uint16_t interval_millis) { - this->interval_millis = interval_millis; + this->interval_millis = interval_millis; } bool Bounce::update() { #ifdef BOUNCE_LOCK_OUT state &= ~_BV(STATE_CHANGED); - // Ignore everything if we are locked out - if (millis() - previous_millis >= interval_millis) { - bool currentState = digitalRead(pin); - if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { - previous_millis = millis(); - state ^= _BV(DEBOUNCED_STATE); - state |= _BV(STATE_CHANGED); - } - } - return state & _BV(STATE_CHANGED); + // Ignore everything if we are locked out + if (millis() - previous_millis >= interval_millis) { + bool currentState = digitalRead(pin); + if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { + previous_millis = millis(); + state ^= _BV(DEBOUNCED_STATE); + state |= _BV(STATE_CHANGED); + } + } + return state & _BV(STATE_CHANGED); #else - // Lire l'etat de l'interrupteur dans une variable temporaire. - bool currentState = digitalRead(pin); + // Lire l'etat de l'interrupteur dans une variable temporaire. + bool currentState = digitalRead(pin); state &= ~_BV(STATE_CHANGED); - // Redemarrer le compteur timeStamp tant et aussi longtemps que - // la lecture ne se stabilise pas. - if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) { - previous_millis = millis(); - state ^= _BV(UNSTABLE_STATE); - } else if ( millis() - previous_millis >= interval_millis ) { - // Rendu ici, la lecture est stable - // Est-ce que la lecture est différente de l'etat emmagasine de l'interrupteur? - if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { - previous_millis = millis(); - state ^= _BV(DEBOUNCED_STATE); - state |= _BV(STATE_CHANGED); - } - } + // Redemarrer le compteur timeStamp tant et aussi longtemps que + // la lecture ne se stabilise pas. + if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) { + previous_millis = millis(); + state ^= _BV(UNSTABLE_STATE); + } else + if ( millis() - previous_millis >= interval_millis ) { + // Rendu ici, la lecture est stable + // Est-ce que la lecture est différente de l'etat emmagasine de l'interrupteur? + if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { + previous_millis = millis(); + state ^= _BV(DEBOUNCED_STATE); + state |= _BV(STATE_CHANGED); + } + } - return state & _BV(STATE_CHANGED); + return state & _BV(STATE_CHANGED); #endif } bool Bounce::read() { - return state & _BV(DEBOUNCED_STATE); + return state & _BV(DEBOUNCED_STATE); } bool Bounce::rose() { - return ( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED)); + return ( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED)); } bool Bounce::fell() { - return !( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED)); + return !( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED)); } diff --git a/Bounce2/Bounce2.h b/Bounce2/Bounce2.h index df9402c..2d44f7c 100644 --- a/Bounce2/Bounce2.h +++ b/Bounce2/Bounce2.h @@ -1,33 +1,30 @@ - /* -The MIT License (MIT) + The MIT License (MIT) -Copyright (c) 2013 thomasfredericks + Copyright (c) 2013 thomasfredericks -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ - - -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * - Main code by Thomas O Fredericks (tof@t-o-f.info) - Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway -* * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * + Main code by Thomas O Fredericks (tof@t-o-f.info) + Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway + * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #ifdef BOUNCE_LOCK #error You are using the invalid BOUNCE_LOCK-OUT define. Please update your sources to use BOUNCE_LOCK_OUT @@ -44,33 +41,35 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. class Bounce { + public: + // Create an instance of the bounce library + Bounce(); -public: - // Create an instance of the bounce library - Bounce(); - // Attach to a pin (and also sets initial state) - void attach(int pin); - // Sets the debounce interval - void interval(uint16_t interval_millis); - // Updates the pin - // Returns 1 if the state changed - // Returns 0 if the state did not change - bool update(); - // Returns the updated pin state - bool read(); - // Returns the falling pin state - bool fell(); - // Returns the rising pin state - bool rose(); + // Attach to a pin (and also sets initial state) + void attach(int pin); - -protected: - unsigned long previous_millis; - uint16_t interval_millis; - uint8_t state; - uint8_t pin; + // Sets the debounce interval + void interval(uint16_t interval_millis); + + // Updates the pin + // Returns 1 if the state changed + // Returns 0 if the state did not change + bool update(); + + // Returns the updated pin state + bool read(); + + // Returns the falling pin state + bool fell(); + + // Returns the rising pin state + bool rose(); + + protected: + unsigned long previous_millis; + uint16_t interval_millis; + uint8_t state; + uint8_t pin; }; #endif - - -- 2.54.0 From 68133b66f4f2536b27ef879a4f8604a90895b545 Mon Sep 17 00:00:00 2001 From: Christian Iversen Date: Sat, 11 Oct 2014 18:45:06 +0200 Subject: [PATCH 4/4] * Whitespace fix --- Bounce2/Bounce2.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Bounce2/Bounce2.cpp b/Bounce2/Bounce2.cpp index c20abd8..cc9afff 100644 --- a/Bounce2/Bounce2.cpp +++ b/Bounce2/Bounce2.cpp @@ -64,14 +64,14 @@ bool Bounce::update() state ^= _BV(UNSTABLE_STATE); } else if ( millis() - previous_millis >= interval_millis ) { - // Rendu ici, la lecture est stable - // Est-ce que la lecture est différente de l'etat emmagasine de l'interrupteur? - if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { - previous_millis = millis(); - state ^= _BV(DEBOUNCED_STATE); - state |= _BV(STATE_CHANGED); + // Rendu ici, la lecture est stable + // Est-ce que la lecture est différente de l'etat emmagasine de l'interrupteur? + if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) { + previous_millis = millis(); + state ^= _BV(DEBOUNCED_STATE); + state |= _BV(STATE_CHANGED); + } } - } return state & _BV(STATE_CHANGED); #endif -- 2.54.0