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 - - // 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(); + #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(); + } } - } - + -#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 + */ + + if(millis() - previous_millis >= interval_millis){ + changeState(); + } + } /* - * Enough time has passed - * ⤷ New state changes are allowed + * If readState ≠ to it's previous state + * ⤷ Reset the debounce timer * - * Set Flags: - * - STATE_CHANGED - * - DEBOUNCED_STATE + * 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 ( millis() - previous_millis >= interval_millis ) { - changeState(); - } - } + if(readState != getStateFlag(UNSTABLE_STATE)){ + // Update unstable bit to match readState - /* - * 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(); - } + 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); +}