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..ce3674e 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -93,11 +93,19 @@ 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); + + /** + @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); + /** @brief Sets the debounce interval in milliseconds. @@ -117,10 +125,19 @@ class Bounce @return True if the pin changed state. */ - bool update(); + bool update(); + + /*! + @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. */