Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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;
Expand Down Expand Up @@ -72,4 +78,4 @@ bool Button::pressed()
bool Button::released()
{
return (read() == RELEASED && has_changed());
}
}
4 changes: 3 additions & 1 deletion Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -23,11 +24,12 @@ class Button
const static bool RELEASED = HIGH;

private:
void (*_on_change_callback)(void);
uint8_t _pin;
uint16_t _delay;
bool _state;
uint32_t _ignore_until;
bool _has_changed;
};

#endif
#endif
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.
19 changes: 19 additions & 0 deletions examples/event_driven/event_driven.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <Button.h>

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();
}
2 changes: 1 addition & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ has_changed KEYWORD2
# Constants (LITERAL1)
#######################################
PRESSED LITERAL1
RELEASED LITERAL1
RELEASED LITERAL1