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] 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 @@ private: 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