From 1af864b0bd6b167a8b63380408ae85540fb117b3 Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Tue, 19 May 2020 14:06:37 -0400 Subject: [PATCH] Added a Button class --- .../bounceMore.ino} | 0 .../bounceTwo.ino} | 0 examples/buttonClass/buttonClass.ino | 61 +++++++++++++++++++ keywords.txt | 6 +- src/Bounce2.h | 48 +++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) rename examples/{bounce_multiple/bounce_multiple.ino => bounceMore/bounceMore.ino} (100%) rename examples/{bounce2buttons/bounce2buttons.ino => bounceTwo/bounceTwo.ino} (100%) create mode 100644 examples/buttonClass/buttonClass.ino diff --git a/examples/bounce_multiple/bounce_multiple.ino b/examples/bounceMore/bounceMore.ino similarity index 100% rename from examples/bounce_multiple/bounce_multiple.ino rename to examples/bounceMore/bounceMore.ino diff --git a/examples/bounce2buttons/bounce2buttons.ino b/examples/bounceTwo/bounceTwo.ino similarity index 100% rename from examples/bounce2buttons/bounce2buttons.ino rename to examples/bounceTwo/bounceTwo.ino diff --git a/examples/buttonClass/buttonClass.ino b/examples/buttonClass/buttonClass.ino new file mode 100644 index 0000000..56d5a57 --- /dev/null +++ b/examples/buttonClass/buttonClass.ino @@ -0,0 +1,61 @@ + + + +/* + DESCRIPTION + ==================== + Simple example of the Button class from the Bounce library. + The Button class matches an electrical state to a physical action. + that switches the debug LED when + either of 2 buttons are pressed. + */ + +// Include the Bounce2 library found here : +// https://github.com/thomasfredericks/Bounce2 +#include + +#define BUTTON_PIN_A 2 +#define BUTTON_PIN_B 3 + + +#define LED_PIN 13 + +// Instantiate a Button object +Button buttonA = Button(); + +// Instantiate another Button object +Button buttonB = Button(); + +void setup() { + + // SETUP BUTTON A + buttonA.attach( BUTTON_PIN_A , INPUT_PULLUP ); + buttonA.interval(5); // interval in ms + buttonA.setPressedState(LOW); // INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON + + // SETUP BUTTON B + buttonB.attach( BUTTON_PIN_B , INPUT_PULLUP ); + buttonB.interval(5); // interval in ms + buttonB.setPressedState(LOW); // INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON + + + // SETUP LED + pinMode(LED_PIN,OUTPUT); + +} + +void loop() { + // UPDATE THE BUTTONS + buttonA.update(); + buttonB.update(); + + + // TURN ON LED IF EITHER BUTTON IS PRESSED + if ( buttonA.pressed() || buttonB.pressed() ) { + digitalWrite(LED_PIN, HIGH ); + } + else { + digitalWrite(LED_PIN, LOW ); + } + +} diff --git a/keywords.txt b/keywords.txt index 3269e8f..8f2c4de 100644 --- a/keywords.txt +++ b/keywords.txt @@ -7,7 +7,8 @@ ####################################### Bounce KEYWORD1 -Bounce2 KEYWORD1 +Bounce2 KEYWORD1 +Button KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) @@ -20,6 +21,9 @@ attach KEYWORD2 rose KEYWORD2 fell KEYWORD2 duration KEYWORD2 +pressed KEYWORD2 +released KEYWORD2 +setPressedState KEYWORD2 ####################################### # Instances (KEYWORD2) diff --git a/src/Bounce2.h b/src/Bounce2.h index d634890..c264b21 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -208,6 +208,54 @@ class Bounce public: bool changed( ) { return getStateFlag(CHANGED_STATE); } +}; + +/** + The Button class. + */ +class Button : public Bounce{ +protected: + bool stateForPressed = 0; +public: + /*! + @brief Create an instance of the Button class. + + @code + + // Create an instance of the Button class. + Button() button; + + @endcode +*/ + Button(){ } + + /*! + @brief Set the electrical state (HIGH/LOW) that corresponds to a physical press. + + @param state + The electrical state (HIGH/LOW) that corresponds to a physical press. + +*/ + void setPressedState(bool state){ + stateForPressed = state; + } + + /*! + @brief Returns true if the button was physically pressed +*/ + bool pressed() { + return changed() && (read() == stateForPressed); + }; + + /*! + @brief Returns true if the button was physically released +*/ + bool released() { + return changed() && (read() != stateForPressed); + }; + + + }; #endif