From 878f86bf1cffb836853b6c6e487b9dda28c49ad2 Mon Sep 17 00:00:00 2001 From: James Myatt Date: Tue, 25 Aug 2020 13:54:33 +0100 Subject: [PATCH 1/3] Add isPressed and getPressedState methods --- src/Bounce2.h | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index 44d43d4..4032a70 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -267,22 +267,34 @@ protected: stateForPressed = state; } + /*! + @brief Get the electrical state (HIGH/LOW) that corresponds to a physical press. + */ + inline bool getPressedState() { + return stateForPressed; + }; + + /*! + @brief Returns true if the button is currently physically pressed. + */ + inline bool isPressed() { + return read() == getPressedState(); + }; + /*! @brief Returns true if the button was physically pressed */ - bool pressed() { - return changed() && (read() == stateForPressed); + inline bool pressed() { + return changed() && isPressed(); }; /*! @brief Returns true if the button was physically released */ - bool released() { - return changed() && (read() != stateForPressed); + inline bool released() { + return changed() && !isPressed(); }; - - }; #endif -- 2.54.0 From 9babb4fc3857ca2959d5c88617c0e499f9da6f9c Mon Sep 17 00:00:00 2001 From: James Myatt Date: Tue, 25 Aug 2020 13:59:55 +0100 Subject: [PATCH 2/3] Reuse state attribute for pressed state --- src/Bounce2.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index 4032a70..ddd2035 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -67,7 +67,7 @@ private: static const uint8_t UNSTABLE_STATE = 0b00000010; static const uint8_t CHANGED_STATE = 0b00000100; -private: +protected: inline void changeState(); inline void setStateFlag(const uint8_t flag) {state |= flag;} inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;} @@ -241,8 +241,9 @@ protected: @brief The Debouncer:Bounce:Button class. The Button class matches an electrical state to a physical action. */ class Button : public Bounce{ -protected: - bool stateForPressed = 1; // + private: + // Note : this is private as it might change in the future + static const uint8_t PRESSED_STATE = 0b00001000; public: /*! @brief Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical level. @@ -254,7 +255,10 @@ protected: @endcode */ - Button(){ } + Button(){ + // Default to pressed state is HIGH + setStateFlag(PRESSED_STATE); + } /*! @brief Set the electrical state (HIGH/LOW) that corresponds to a physical press. By default, the pressed state is matched to a HIGH electrical level. @@ -264,14 +268,17 @@ protected: */ void setPressedState(bool state){ - stateForPressed = state; + if (state) + setStateFlag(PRESSED_STATE); + else + unsetStateFlag(PRESSED_STATE); } /*! @brief Get the electrical state (HIGH/LOW) that corresponds to a physical press. */ inline bool getPressedState() { - return stateForPressed; + return getStateFlag(PRESSED_STATE); }; /*! -- 2.54.0 From 83bc18e24592bac9247781ea7bd56702965494c1 Mon Sep 17 00:00:00 2001 From: James Myatt Date: Wed, 26 Aug 2020 13:53:35 +0100 Subject: [PATCH 3/3] Revert "Reuse state attribute for pressed state" This reverts commit 9babb4fc3857ca2959d5c88617c0e499f9da6f9c. --- src/Bounce2.h | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/Bounce2.h b/src/Bounce2.h index ddd2035..4032a70 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -67,7 +67,7 @@ private: static const uint8_t UNSTABLE_STATE = 0b00000010; static const uint8_t CHANGED_STATE = 0b00000100; -protected: +private: inline void changeState(); inline void setStateFlag(const uint8_t flag) {state |= flag;} inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;} @@ -241,9 +241,8 @@ protected: @brief The Debouncer:Bounce:Button class. The Button class matches an electrical state to a physical action. */ class Button : public Bounce{ - private: - // Note : this is private as it might change in the future - static const uint8_t PRESSED_STATE = 0b00001000; +protected: + bool stateForPressed = 1; // public: /*! @brief Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical level. @@ -255,10 +254,7 @@ class Button : public Bounce{ @endcode */ - Button(){ - // Default to pressed state is HIGH - setStateFlag(PRESSED_STATE); - } + Button(){ } /*! @brief Set the electrical state (HIGH/LOW) that corresponds to a physical press. By default, the pressed state is matched to a HIGH electrical level. @@ -268,17 +264,14 @@ class Button : public Bounce{ */ void setPressedState(bool state){ - if (state) - setStateFlag(PRESSED_STATE); - else - unsetStateFlag(PRESSED_STATE); + stateForPressed = state; } /*! @brief Get the electrical state (HIGH/LOW) that corresponds to a physical press. */ inline bool getPressedState() { - return getStateFlag(PRESSED_STATE); + return stateForPressed; }; /*! -- 2.54.0