diff --git a/Button.cpp b/Button.cpp index 746ce21..30061e0 100644 --- a/Button.cpp +++ b/Button.cpp @@ -21,6 +21,11 @@ void Button::begin() pinMode(_pin, INPUT_PULLUP); } +void Button::begin(void (*on_change_callback)(void)) +{ + _on_change_callback = on_change_callback; +} + // // public methods // @@ -39,6 +44,7 @@ bool Button::read() _ignore_until = millis() + _delay; _state = !_state; _has_changed = true; + if (_on_change_callback != nullptr) _on_change_callback(); } return _state; @@ -72,4 +78,4 @@ bool Button::pressed() bool Button::released() { return (read() == RELEASED && has_changed()); -} +} \ No newline at end of file diff --git a/Button.h b/Button.h index 6a23b29..5469064 100644 --- a/Button.h +++ b/Button.h @@ -13,6 +13,7 @@ class Button public: Button(uint8_t pin, uint16_t debounce_ms = 100); void begin(); + void begin(void (*)(void)); bool read(); bool toggled(); bool pressed(); @@ -23,6 +24,7 @@ class Button const static bool RELEASED = HIGH; private: + void (*_on_change_callback)(void); uint8_t _pin; uint16_t _delay; bool _state; @@ -30,4 +32,4 @@ class Button bool _has_changed; }; -#endif +#endif \ No newline at end of file diff --git a/README.md b/README.md index fcdfb6d..4c298e9 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Features * Super simple API. * Handles debouncing. * Sets the pin mode automatically. +* On change callback for event-driven code. * Lets you write code that triggers: ** based on the pin state (high or low) ** when a button is pressed @@ -81,6 +82,9 @@ Creates a new Button. **void begin()** Call this in your `setup` method to setup the button. All it does is set the correct pin mode. +**void begin(void ((void (*on_change_callback)(void))** +Call this in your `setup` method to setup the button. All it does is set the correct pin mode. This also adds a callback fuinction you define as a paramter that executes when the button state changes if you fancy event-driven code. + **bool pressed()** Returns true when _and only when_ the button is pressed. Until the button is released (in the debounced-sense of the word) this function won't return true again. So in effect, it returns true only while you are pressing the button, or to put it another way, it fires on a rising edge. @@ -111,5 +115,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/examples/event_driven/event_driven.ino b/examples/event_driven/event_driven.ino new file mode 100644 index 0000000..ff40200 --- /dev/null +++ b/examples/event_driven/event_driven.ino @@ -0,0 +1,19 @@ +#include + +Button button1(2); // Connect your button between pin 2 and GND + +void onChange() +{ + if (button1.read() == Button::PRESSED) + Serial.println("Button 3 has been pressed"); + else + Serial.println("Button 3 has been released"); +} + +void setup() { + Serial.begin(9600); +} + +void loop () { + button1.read(); +} \ No newline at end of file diff --git a/keywords.txt b/keywords.txt index 1372b4a..bd8bacd 100644 --- a/keywords.txt +++ b/keywords.txt @@ -22,4 +22,4 @@ has_changed KEYWORD2 # Constants (LITERAL1) ####################################### PRESSED LITERAL1 -RELEASED LITERAL1 +RELEASED LITERAL1 \ No newline at end of file