From 8c852c10416a2f3172390ddc339b555c47eb2602 Mon Sep 17 00:00:00 2001 From: ElectronicsArchiver <85485984+ElectronicsArchiver@users.noreply.github.com> Date: Wed, 25 May 2022 21:21:29 -0400 Subject: [PATCH 1/9] Split Implementation Into Class Files --- src/Bounce.cpp | 20 ++++++++++++++++++++ src/{Bounce2.cpp => Debounce.cpp} | 20 -------------------- 2 files changed, 20 insertions(+), 20 deletions(-) create mode 100644 src/Bounce.cpp rename src/{Bounce2.cpp => Debounce.cpp} (93%) diff --git a/src/Bounce.cpp b/src/Bounce.cpp new file mode 100644 index 0000000..e78b81b --- /dev/null +++ b/src/Bounce.cpp @@ -0,0 +1,20 @@ +//////////// +// BOUNCE // +//////////// + + +Bounce::Bounce() + : pin(0) +{} + +void Bounce::attach(int pin) { + this->pin = pin; + + // SET INITIAL STATE + begin(); +} + +void Bounce::attach(int pin, int mode){ + setPinMode(pin, mode); + this->attach(pin); +} diff --git a/src/Bounce2.cpp b/src/Debounce.cpp similarity index 93% rename from src/Bounce2.cpp rename to src/Debounce.cpp index 6a755ea..b2b89bd 100644 --- a/src/Bounce2.cpp +++ b/src/Debounce.cpp @@ -130,26 +130,6 @@ bool Debouncer::fell() const return !getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE); } -//////////// -// BOUNCE // -//////////// - - -Bounce::Bounce() - : pin(0) -{} - -void Bounce::attach(int pin) { - this->pin = pin; - - // SET INITIAL STATE - begin(); -} - -void Bounce::attach(int pin, int mode){ - setPinMode(pin, mode); - this->attach(pin); -} From 66e8e385ae3a79fc6a0acc2b3d42d87d920fac7d Mon Sep 17 00:00:00 2001 From: ElectronicsArchiver <85485984+ElectronicsArchiver@users.noreply.github.com> Date: Wed, 25 May 2022 21:39:13 -0400 Subject: [PATCH 2/9] Fixed Comments --- src/Bounce.cpp | 16 +++++++--- src/Debounce.cpp | 83 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 77 insertions(+), 22 deletions(-) diff --git a/src/Bounce.cpp b/src/Bounce.cpp index e78b81b..83ec937 100644 --- a/src/Bounce.cpp +++ b/src/Bounce.cpp @@ -1,7 +1,14 @@ -//////////// -// BOUNCE // -//////////// +/* + * Check Bounce2.h for licensing / authors. + */ + +#include "Bounce2.h" + + +/* + * Bounce + */ Bounce::Bounce() : pin(0) @@ -10,7 +17,8 @@ Bounce::Bounce() void Bounce::attach(int pin) { this->pin = pin; - // SET INITIAL STATE + // Set Initial State + begin(); } diff --git a/src/Debounce.cpp b/src/Debounce.cpp index b2b89bd..2bbae76 100644 --- a/src/Debounce.cpp +++ b/src/Debounce.cpp @@ -1,11 +1,14 @@ -// Please read Bounce2.h for information about the liscence and authors +/* + * Check Bounce2.h for licensing / authors. + */ #include "Bounce2.h" -////////////// -// DEBOUNCE // -////////////// + +/* + * Debounce + */ Debouncer::Debouncer():previous_millis(0) , interval_millis(10) @@ -36,6 +39,7 @@ bool Debouncer::update() #ifdef BOUNCE_LOCK_OUT // Ignore everything if we are locked out + if (millis() - previous_millis >= interval_millis) { bool currentState = readCurrentState(); if ( currentState != getStateFlag(DEBOUNCED_STATE) ) { @@ -46,44 +50,81 @@ bool Debouncer::update() #elif defined BOUNCE_WITH_PROMPT_DETECTION - // Read the state of the switch port into a temporary variable. + + /* + * Switch Port State 🠖 Temporary Variable + */ + bool readState = readCurrentState(); + // Check if the button state has changed + if ( readState != getStateFlag(DEBOUNCED_STATE) ) { - // We have seen a change from the current button state. + + + /* + * Enough time has passed + * ⤷ New state changes are allowed + * + * Set Flags: + * - STATE_CHANGED + * - DEBOUNCED_STATE + */ if ( millis() - previous_millis >= interval_millis ) { - // We have passed the time threshold, so a new change of state is allowed. - // set the STATE_CHANGED flag and the new DEBOUNCED_STATE. - // This will be prompt as long as there has been greater than interval_misllis ms since last change of input. - // Otherwise debounced state will not change again until bouncing is stable for the timeout period. changeState(); } } - // If the readState is different from previous readState, reset the debounce timer - as input is still unstable - // and we want to prevent new button state changes until the previous one has remained stable for the timeout. + + /* + * If readState ≠ to it's previous state + * ⤷ Reset the debounce timer + * + * This is done, as input is still unstable + * and we want to prevent new button state + * changes until the previous state has + * remained stable for the timeout. + */ + if ( readState != getStateFlag(UNSTABLE_STATE) ) { - // Update Unstable Bit to macth readState + + // Update unstable bit to match readState + toggleStateFlag(UNSTABLE_STATE); previous_millis = millis(); } #else - // Read the state of the switch in a temporary variable. + + /* + * Switch Port State 🠖 Temporary Variable + */ + bool currentState = readCurrentState(); - // If the reading is different from last reading, reset the debounce counter + /* + * If the reading ≠ to it's previous state + * ⤷ Reset the debounce counter + */ + if ( currentState != getStateFlag(UNSTABLE_STATE) ) { previous_millis = millis(); toggleStateFlag(UNSTABLE_STATE); } else if ( millis() - previous_millis >= interval_millis ) { - // We have passed the threshold time, so the input is now stable - // If it is different from last state, set the STATE_CHANGED flag + + /* + * We have passed the threshold time + * ⤷ The input is now stable + * + * If it is ≠ to it's previous state + * ⤷ Set the STATE_CHANGED flag + */ + if (currentState != getStateFlag(DEBOUNCED_STATE) ) { previous_millis = millis(); @@ -99,7 +140,13 @@ bool Debouncer::update() } -// WIP HELD + +// Work In Progress +/* + * Work In Progress + * HELD + */ + unsigned long Debouncer::previousDuration() const { return durationOfPreviousState; } From 0a51c7679ad0f19c2b9df56e029c6ab499115785 Mon Sep 17 00:00:00 2001 From: ElectronicsArchiver <85485984+ElectronicsArchiver@users.noreply.github.com> Date: Wed, 25 May 2022 21:44:07 -0400 Subject: [PATCH 3/9] Formatted Code To Use Same Style --- src/Bounce.cpp | 18 +++-- src/Debounce.cpp | 192 ++++++++++++++++++++++++----------------------- 2 files changed, 108 insertions(+), 102 deletions(-) diff --git a/src/Bounce.cpp b/src/Bounce.cpp index 83ec937..9f51117 100644 --- a/src/Bounce.cpp +++ b/src/Bounce.cpp @@ -11,18 +11,22 @@ */ Bounce::Bounce() - : pin(0) -{} + : pin(0) {} -void Bounce::attach(int pin) { - this->pin = pin; + +void Bounce::attach(int pin){ + + this -> pin = pin; // Set Initial State begin(); } -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); } diff --git a/src/Debounce.cpp b/src/Debounce.cpp index 2bbae76..4b678f3 100644 --- a/src/Debounce.cpp +++ b/src/Debounce.cpp @@ -14,134 +14,134 @@ Debouncer::Debouncer():previous_millis(0) , interval_millis(10) , state(0) {} -void Debouncer::interval(uint16_t interval_millis) -{ - this->interval_millis = interval_millis; +void Debouncer::interval(uint16_t interval_millis){ + this -> interval_millis = interval_millis; } -void Debouncer::begin() { - state = 0; - if (readCurrentState()) { + +void Debouncer::begin(){ + + state = 0; + + if(readCurrentState()) setStateFlag(DEBOUNCED_STATE | UNSTABLE_STATE); - } #ifdef BOUNCE_LOCK_OUT - previous_millis = 0; -#else - previous_millis = millis(); -#endif + previous_millis = 0; + #else + previous_millis = millis(); + #endif } -bool Debouncer::update() -{ + +bool Debouncer::update(){ unsetStateFlag(CHANGED_STATE); -#ifdef BOUNCE_LOCK_OUT + + #ifdef BOUNCE_LOCK_OUT - // Ignore everything if we are locked out + // 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(); + if (millis() - previous_millis >= interval_millis) { + bool currentState = readCurrentState(); + if ( currentState != getStateFlag(DEBOUNCED_STATE) ) { + previous_millis = millis(); + changeState(); + } } - } - + -#elif defined BOUNCE_WITH_PROMPT_DETECTION + #elif defined BOUNCE_WITH_PROMPT_DETECTION - /* - * Switch Port State 🠖 Temporary Variable - */ + /* + * Switch Port State 🠖 Temporary Variable + */ - bool readState = readCurrentState(); + bool readState = readCurrentState(); - // Check if the button state has changed + // Check if the button state has changed - if ( readState != getStateFlag(DEBOUNCED_STATE) ) { + if(readState != getStateFlag(DEBOUNCED_STATE)){ - /* - * Enough time has passed - * ⤷ New state changes are allowed - * - * Set Flags: - * - STATE_CHANGED - * - DEBOUNCED_STATE - */ + /* + * Enough time has passed + * ⤷ New state changes are allowed + * + * Set Flags: + * - STATE_CHANGED + * - DEBOUNCED_STATE + */ - if ( millis() - previous_millis >= interval_millis ) { - changeState(); - } - } + if(millis() - previous_millis >= interval_millis){ + changeState(); + } + } - /* - * If readState ≠ to it's previous state - * ⤷ Reset the debounce timer - * - * This is done, as input is still unstable - * and we want to prevent new button state - * changes until the previous state has - * remained stable for the timeout. - */ + /* + * If readState ≠ to it's previous state + * ⤷ Reset the debounce timer + * + * This is done, as input is still unstable + * and we want to prevent new button state + * changes until the previous state has + * remained stable for the timeout. + */ - if ( readState != getStateFlag(UNSTABLE_STATE) ) { + if(readState != getStateFlag(UNSTABLE_STATE)){ - // Update unstable bit to match readState + // Update unstable bit to match readState - toggleStateFlag(UNSTABLE_STATE); - previous_millis = millis(); - } - + toggleStateFlag(UNSTABLE_STATE); + previous_millis = millis(); + } + -#else + #else - /* - * Switch Port State 🠖 Temporary Variable - */ + /* + * Switch Port State 🠖 Temporary Variable + */ - bool currentState = readCurrentState(); - + bool currentState = readCurrentState(); + - /* - * If the reading ≠ to it's previous state - * ⤷ Reset the debounce counter - */ + /* + * If the reading ≠ to it's previous state + * ⤷ Reset the debounce counter + */ - if ( currentState != getStateFlag(UNSTABLE_STATE) ) { - previous_millis = millis(); - toggleStateFlag(UNSTABLE_STATE); - } else - if ( millis() - previous_millis >= interval_millis ) { + if(currentState != getStateFlag(UNSTABLE_STATE)){ + previous_millis = millis(); + toggleStateFlag(UNSTABLE_STATE); + } else + if(millis() - previous_millis >= interval_millis){ /* * We have passed the threshold time * ⤷ The input is now stable - * + * * If it is ≠ to it's previous state * ⤷ Set the STATE_CHANGED flag */ - if (currentState != getStateFlag(DEBOUNCED_STATE) ) { + if(currentState != getStateFlag(DEBOUNCED_STATE)){ + previous_millis = millis(); - - - changeState(); + + changeState(); } } -#endif - - return changed(); + #endif + return changed(); } -// Work In Progress /* * Work In Progress * HELD @@ -151,32 +151,34 @@ unsigned long Debouncer::previousDuration() const { return durationOfPreviousState; } + unsigned long Debouncer::currentDuration() const { - return (millis() - stateChangeLastTime); + return millis() - stateChangeLastTime; } -inline void Debouncer::changeState() { + +inline void Debouncer::changeState(){ + toggleStateFlag(DEBOUNCED_STATE); setStateFlag(CHANGED_STATE) ; - durationOfPreviousState = millis() - stateChangeLastTime; + + durationOfPreviousState = millis() - stateChangeLastTime; stateChangeLastTime = millis(); } -bool Debouncer::read() const -{ - return getStateFlag(DEBOUNCED_STATE); -} - -bool Debouncer::rose() const -{ - return getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE); -} -bool Debouncer::fell() const -{ - return !getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE); +bool Debouncer::read() const { + return getStateFlag(DEBOUNCED_STATE); } +bool Debouncer::rose() const { + return getStateFlag(DEBOUNCED_STATE) + && getStateFlag(CHANGED_STATE); +} +bool Debouncer::fell() const { + return ! getStateFlag(DEBOUNCED_STATE) + && getStateFlag(CHANGED_STATE); +} From db9a24919fe52356867ba3b405cb022fb7ebe273 Mon Sep 17 00:00:00 2001 From: ElectronicsArchiver <85485984+ElectronicsArchiver@users.noreply.github.com> Date: Wed, 25 May 2022 21:54:43 -0400 Subject: [PATCH 4/9] Untangled Macro Use --- src/Debounce.cpp | 164 ++++++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 72 deletions(-) diff --git a/src/Debounce.cpp b/src/Debounce.cpp index 4b678f3..c82af69 100644 --- a/src/Debounce.cpp +++ b/src/Debounce.cpp @@ -34,114 +34,134 @@ void Debouncer::begin(){ } +#ifdef BOUNCE_LOCK_OUT + + bool Debouncer::update(){ unsetStateFlag(CHANGED_STATE); - #ifdef BOUNCE_LOCK_OUT - - // 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(); - } + // 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(); } - + } - #elif defined BOUNCE_WITH_PROMPT_DETECTION + return changed(); +} - /* - * Switch Port State 🠖 Temporary Variable - */ - bool readState = readCurrentState(); +#elif defined BOUNCE_WITH_PROMPT_DETECTION + +bool Debouncer::update(){ - // Check if the button state has changed + unsetStateFlag(CHANGED_STATE); - if(readState != getStateFlag(DEBOUNCED_STATE)){ + /* + * Switch Port State 🠖 Temporary Variable + */ - /* - * Enough time has passed - * ⤷ New state changes are allowed - * - * Set Flags: - * - STATE_CHANGED - * - DEBOUNCED_STATE - */ + bool readState = readCurrentState(); - if(millis() - previous_millis >= interval_millis){ - changeState(); - } - } + // Check if the button state has changed + + if(readState != getStateFlag(DEBOUNCED_STATE)){ /* - * If readState ≠ to it's previous state - * ⤷ Reset the debounce timer + * Enough time has passed + * ⤷ New state changes are allowed * - * This is done, as input is still unstable - * and we want to prevent new button state - * changes until the previous state has - * remained stable for the timeout. + * Set Flags: + * - STATE_CHANGED + * - DEBOUNCED_STATE */ - if(readState != getStateFlag(UNSTABLE_STATE)){ + if(millis() - previous_millis >= interval_millis) + changeState(); + } - // Update unstable bit to match readState - toggleStateFlag(UNSTABLE_STATE); - previous_millis = millis(); - } + /* + * If readState ≠ to it's previous state + * ⤷ Reset the debounce timer + * + * This is done, as input is still unstable + * and we want to prevent new button state + * changes until the previous state has + * remained stable for the timeout. + */ + + if(readState != getStateFlag(UNSTABLE_STATE)){ + + // Update unstable bit to match readState + + toggleStateFlag(UNSTABLE_STATE); + previous_millis = millis(); + } + return changed(); +} + + +#else + + +bool Debouncer::update(){ + + unsetStateFlag(CHANGED_STATE); + + + /* + * Switch Port State 🠖 Temporary Variable + */ + + bool currentState = readCurrentState(); - #else - /* - * Switch Port State 🠖 Temporary Variable - */ + /* + * If the reading ≠ to it's previous state + * ⤷ Reset the debounce counter + */ - bool currentState = readCurrentState(); - + if(currentState != getStateFlag(UNSTABLE_STATE)){ + previous_millis = millis(); + toggleStateFlag(UNSTABLE_STATE); + } else + if(millis() - previous_millis >= interval_millis){ /* - * If the reading ≠ to it's previous state - * ⤷ Reset the debounce counter + * We have passed the threshold time + * ⤷ The input is now stable + * + * If it is ≠ to it's previous state + * ⤷ Set the STATE_CHANGED flag */ - if(currentState != getStateFlag(UNSTABLE_STATE)){ + if(currentState != getStateFlag(DEBOUNCED_STATE)){ + previous_millis = millis(); - toggleStateFlag(UNSTABLE_STATE); - } else - if(millis() - previous_millis >= interval_millis){ - - /* - * We have passed the threshold time - * ⤷ The input is now stable - * - * If it is ≠ to it's previous state - * ⤷ Set the STATE_CHANGED flag - */ - - if(currentState != getStateFlag(DEBOUNCED_STATE)){ - - previous_millis = millis(); - - changeState(); - } + + changeState(); } + } - - #endif - - return changed(); + return changed(); } +#endif + + + /* * Work In Progress * HELD From eed0ef8022170244036b97e1ddb8413715672557 Mon Sep 17 00:00:00 2001 From: ElectronicsArchiver <85485984+ElectronicsArchiver@users.noreply.github.com> Date: Wed, 25 May 2022 21:59:08 -0400 Subject: [PATCH 5/9] Abstracted Threshold Check --- src/Bounce2.h | 1 + src/Debounce.cpp | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index 7dea71e..1a4d7ee 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -74,6 +74,7 @@ class Debouncer 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) const {return((state & flag) != 0);} + inline bool thresholdPassed(); public: /*! diff --git a/src/Debounce.cpp b/src/Debounce.cpp index c82af69..7da28bd 100644 --- a/src/Debounce.cpp +++ b/src/Debounce.cpp @@ -43,7 +43,7 @@ bool Debouncer::update(){ // Ignore everything if we are locked out - if(millis() - previous_millis >= interval_millis){ + if(thresholdPassed()){ bool currentState = readCurrentState(); @@ -85,7 +85,7 @@ bool Debouncer::update(){ * - DEBOUNCED_STATE */ - if(millis() - previous_millis >= interval_millis) + if(thresholdPassed()) changeState(); } @@ -136,7 +136,7 @@ bool Debouncer::update(){ previous_millis = millis(); toggleStateFlag(UNSTABLE_STATE); } else - if(millis() - previous_millis >= interval_millis){ + if(thresholdPassed()){ /* * We have passed the threshold time @@ -202,3 +202,8 @@ bool Debouncer::fell() const { return ! getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE); } + + +inline bool Debouncer::thresholdPassed(){ + return millis() - previous_millis >= interval_millis; +} \ No newline at end of file From 9231e6ed198eea1daa40922dbb51804f17d1d296 Mon Sep 17 00:00:00 2001 From: ElectronicsArchiver <85485984+ElectronicsArchiver@users.noreply.github.com> Date: Wed, 25 May 2022 22:02:06 -0400 Subject: [PATCH 6/9] Abstracted Updating Time --- src/Bounce2.h | 1 + src/Debounce.cpp | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index 1a4d7ee..38b5735 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -75,6 +75,7 @@ class Debouncer inline void toggleStateFlag(const uint8_t flag) {state ^= flag;} inline bool getStateFlag(const uint8_t flag) const {return((state & flag) != 0);} inline bool thresholdPassed(); + inline void updateTime(); public: /*! diff --git a/src/Debounce.cpp b/src/Debounce.cpp index 7da28bd..ecac2f3 100644 --- a/src/Debounce.cpp +++ b/src/Debounce.cpp @@ -29,7 +29,7 @@ void Debouncer::begin(){ #ifdef BOUNCE_LOCK_OUT previous_millis = 0; #else - previous_millis = millis(); + updateTime(); #endif } @@ -48,7 +48,7 @@ bool Debouncer::update(){ bool currentState = readCurrentState(); if(currentState != getStateFlag(DEBOUNCED_STATE)){ - previous_millis = millis(); + updateTime(); changeState(); } } @@ -105,7 +105,7 @@ bool Debouncer::update(){ // Update unstable bit to match readState toggleStateFlag(UNSTABLE_STATE); - previous_millis = millis(); + updateTime(); } return changed(); @@ -133,7 +133,7 @@ bool Debouncer::update(){ */ if(currentState != getStateFlag(UNSTABLE_STATE)){ - previous_millis = millis(); + updateTime(); toggleStateFlag(UNSTABLE_STATE); } else if(thresholdPassed()){ @@ -147,9 +147,7 @@ bool Debouncer::update(){ */ if(currentState != getStateFlag(DEBOUNCED_STATE)){ - - previous_millis = millis(); - + updateTime(); changeState(); } } @@ -206,4 +204,8 @@ bool Debouncer::fell() const { inline bool Debouncer::thresholdPassed(){ return millis() - previous_millis >= interval_millis; +} + +inline void Debouncer::updateTime(){ + previous_millis = millis(); } \ No newline at end of file From 537be6a0a55f9961786fdb518df032b0c78254db Mon Sep 17 00:00:00 2001 From: ElectronicsArchiver <85485984+ElectronicsArchiver@users.noreply.github.com> Date: Wed, 25 May 2022 22:07:45 -0400 Subject: [PATCH 7/9] Formatted Variable Names --- src/Debounce.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Debounce.cpp b/src/Debounce.cpp index ecac2f3..7e7c3aa 100644 --- a/src/Debounce.cpp +++ b/src/Debounce.cpp @@ -69,12 +69,12 @@ bool Debouncer::update(){ * Switch Port State 🠖 Temporary Variable */ - bool readState = readCurrentState(); + const bool state = readCurrentState(); // Check if the button state has changed - if(readState != getStateFlag(DEBOUNCED_STATE)){ + if(state != getStateFlag(DEBOUNCED_STATE)){ /* * Enough time has passed @@ -100,7 +100,7 @@ bool Debouncer::update(){ * remained stable for the timeout. */ - if(readState != getStateFlag(UNSTABLE_STATE)){ + if(state != getStateFlag(UNSTABLE_STATE)){ // Update unstable bit to match readState @@ -124,7 +124,7 @@ bool Debouncer::update(){ * Switch Port State 🠖 Temporary Variable */ - bool currentState = readCurrentState(); + const bool state = readCurrentState(); /* @@ -132,10 +132,14 @@ bool Debouncer::update(){ * ⤷ Reset the debounce counter */ - if(currentState != getStateFlag(UNSTABLE_STATE)){ + if(state != getStateFlag(UNSTABLE_STATE)){ + updateTime(); toggleStateFlag(UNSTABLE_STATE); - } else + + return changed(); + } + if(thresholdPassed()){ /* @@ -146,7 +150,7 @@ bool Debouncer::update(){ * ⤷ Set the STATE_CHANGED flag */ - if(currentState != getStateFlag(DEBOUNCED_STATE)){ + if(state != getStateFlag(DEBOUNCED_STATE)){ updateTime(); changeState(); } From 5b0eea1f2d6007ff7ec0b661337a31eb96f8f57d Mon Sep 17 00:00:00 2001 From: ElectronicsArchiver <85485984+ElectronicsArchiver@users.noreply.github.com> Date: Wed, 25 May 2022 22:12:33 -0400 Subject: [PATCH 8/9] Abstracted State Checking --- src/Bounce2.h | 6 ++++-- src/Debounce.cpp | 24 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index 38b5735..f241616 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -74,8 +74,10 @@ class Debouncer 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) const {return((state & flag) != 0);} - inline bool thresholdPassed(); - inline void updateTime(); + inline bool thresholdPassed() const; + inline void updateTime() const; + inline bool isDebouncing(const bool state) const; + inline bool isUnstable(const bool state) const; public: /*! diff --git a/src/Debounce.cpp b/src/Debounce.cpp index 7e7c3aa..b970040 100644 --- a/src/Debounce.cpp +++ b/src/Debounce.cpp @@ -45,9 +45,9 @@ bool Debouncer::update(){ if(thresholdPassed()){ - bool currentState = readCurrentState(); + const bool state = readCurrentState(); - if(currentState != getStateFlag(DEBOUNCED_STATE)){ + if(!isDebouncing(state)){ updateTime(); changeState(); } @@ -74,7 +74,7 @@ bool Debouncer::update(){ // Check if the button state has changed - if(state != getStateFlag(DEBOUNCED_STATE)){ + if(!isDebouncing(state)){ /* * Enough time has passed @@ -100,7 +100,7 @@ bool Debouncer::update(){ * remained stable for the timeout. */ - if(state != getStateFlag(UNSTABLE_STATE)){ + if(!isUnstable(state)){ // Update unstable bit to match readState @@ -132,7 +132,7 @@ bool Debouncer::update(){ * ⤷ Reset the debounce counter */ - if(state != getStateFlag(UNSTABLE_STATE)){ + if(!isUnstable(state)){ updateTime(); toggleStateFlag(UNSTABLE_STATE); @@ -150,7 +150,7 @@ bool Debouncer::update(){ * ⤷ Set the STATE_CHANGED flag */ - if(state != getStateFlag(DEBOUNCED_STATE)){ + if(!isDebouncing(state)){ updateTime(); changeState(); } @@ -206,10 +206,18 @@ bool Debouncer::fell() const { } -inline bool Debouncer::thresholdPassed(){ +inline bool Debouncer::thresholdPassed() const { return millis() - previous_millis >= interval_millis; } -inline void Debouncer::updateTime(){ +inline void Debouncer::updateTime() const { previous_millis = millis(); +} + +inline bool Debouncer::isDebouncing(const bool state) const { + return state == getStateFlag(DEBOUNCED_STATE); +} + +inline bool Debouncer::isUnstable(const bool state) const { + return state == getStateFlag(UNSTABLE_STATE); } \ No newline at end of file From e95b40b776f3fb21ad57fec1decaca2a72486876 Mon Sep 17 00:00:00 2001 From: ElectronicsArchiver <85485984+ElectronicsArchiver@users.noreply.github.com> Date: Wed, 25 May 2022 22:15:22 -0400 Subject: [PATCH 9/9] Fixed Declaration --- src/Bounce2.h | 2 +- src/Debounce.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index f241616..d57b5fa 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -69,13 +69,13 @@ class Debouncer // Note : this is private as it migh change in the futur private: + inline void updateTime(); inline void changeState(); inline void setStateFlag(const uint8_t flag) {state |= flag;} 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) const {return((state & flag) != 0);} inline bool thresholdPassed() const; - inline void updateTime() const; inline bool isDebouncing(const bool state) const; inline bool isUnstable(const bool state) const; diff --git a/src/Debounce.cpp b/src/Debounce.cpp index b970040..0811d63 100644 --- a/src/Debounce.cpp +++ b/src/Debounce.cpp @@ -210,7 +210,7 @@ inline bool Debouncer::thresholdPassed() const { return millis() - previous_millis >= interval_millis; } -inline void Debouncer::updateTime() const { +inline void Debouncer::updateTime(){ previous_millis = millis(); }