Skip to content

Commit

Permalink
Merge pull request #18 from elC0mpa/master
Browse files Browse the repository at this point in the history
Interrupts support
  • Loading branch information
evert-arias authored Aug 13, 2019
2 parents ee401d2 + 58d2412 commit 7ca0b11
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 17 deletions.
54 changes: 54 additions & 0 deletions examples/Interrupts/Interrupts.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Name: MultipleButtons.ino
Created: 8/11/2019 11:45:52 AM
Author: José Gabriel Companioni Benítez (https://github.com/elC0mpa)
Description: Example to demostrate how to use the library with more than one button
*/

#include <Arduino.h>
#include <EasyButton.h>

#define BUTTON_PIN 2
#define BAUDRATE 9600

EasyButton button(BUTTON_PIN);


void buttonPressed()
{
Serial.println("Button is pressed");
}

void buttonPressedTwoSeconds()
{
Serial.println("Button pressed for two seconds");
}

void buttonSequence()
{
Serial.println("Sequence");
}

void buttonPressedReleased()
{
button.read();
}

void setup() {
// put your setup code here, to run once:
Serial.begin(BAUDRATE);
button.begin();
if (button.supportsInterrupt())
{
Serial.println("Button will be used through external interrupts");
button.enableInterrupt(buttonPressedReleased);
}

button.onPressed(buttonPressed);
button.onPressedFor(2000, buttonPressedTwoSeconds);
button.onSequence(3, 5000, buttonSequence);
}

void loop() {
// put your main code here, to run repeatedly:
}
49 changes: 36 additions & 13 deletions src/EasyButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,31 @@ bool EasyButton::read()
}

// detect change on button's state.
if (read_started_ms - _last_change < _db_time) {
// button's state has not changed.
if (read_started_ms - _last_change < _db_time)
{//true -> debounce time has not ellapsed
_changed = false;
}
else {
// button's state has changed.
else
{// debounce time ellapsed
_last_state = _current_state; // save last state.
_current_state = pinVal; // assign new state as current state from pin's value.
_changed = (_current_state != _last_state); // report state change if current state vary from last state.
// if state has changed since last read.
if (_changed) {
if (_changed)
{// state change
// save current millis as last change time.
_last_change = read_started_ms;
}
}

// call the callback functions when conditions met.
if (!_current_state && _changed) {
// if (!_current_state && _changed) {
if (wasReleased()) {
// button was released.
if (!_was_btn_held) {
if (_short_press_count == 0) {
if (!_was_btn_held)
{
if (_short_press_count == 0)
{
_first_press_time = read_started_ms;
}
// increment presses counter.
Expand All @@ -112,21 +116,25 @@ bool EasyButton::read()
_pressed_callback();
}

if (_short_press_count == _press_sequences && _press_sequence_duration >= (read_started_ms - _first_press_time)) {
if (_pressed_sequence_callback) {
if (_short_press_count == _press_sequences && _press_sequence_duration >= (read_started_ms - _first_press_time))
{//true-> pressed_sequence
if (_pressed_sequence_callback)
{
_pressed_sequence_callback();
}
_short_press_count = 0;
_first_press_time = 0;
_first_press_time = 0;//MOdificar
}
// if sequence timeout, reset short presses counters.
else if (_press_sequence_duration <= (read_started_ms - _first_press_time)) {
else if (_press_sequence_duration <= (read_started_ms - _first_press_time))
{
_short_press_count = 0;
_first_press_time = 0;
}
}
// button was not held.
else {
else
{
_was_btn_held = false;
}
// since button released, reset _pressed_for_callbackCalled value.
Expand Down Expand Up @@ -155,3 +163,18 @@ bool EasyButton::_readPin()
{
return digitalRead(_pin);
}

bool EasyButton::supportsInterrupt()
{
return (digitalPinToInterrupt(_pin) != NOT_AN_INTERRUPT);
}

void EasyButton::enableInterrupt(EasyButton::callback_t callback)
{
attachInterrupt(digitalPinToInterrupt(_pin), callback, CHANGE);
}

void EasyButton::disableInterrupt()
{
detachInterrupt(digitalPinToInterrupt(_pin));
}
11 changes: 7 additions & 4 deletions src/EasyButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ class EasyButton
// PUBLIC FUNCTIONS
virtual void begin(); // Initialize a button object and the pin it's connected to.
bool read(); // Returns the current debounced button state, true for pressed, false for released.

void onPressed(callback_t callback); // Call a callback function when the button has been pressed and released.
void onPressedFor(uint32_t duration, callback_t callback); // Call a callback function when the button has been held for at least the given number of milliseconds.
void onSequence(uint8_t sequences, uint32_t duration, callback_t callback); // Call a callback function when the given sequence has matched.
void enableInterrupt(callback_t callback); // Call a callback function when the button is pressed or released
void disableInterrupt();
bool supportsInterrupt(); // Returns true if the button pin is an external interrupt pin
bool isPressed(); // Returns true if the button state was pressed at the last read.
bool isReleased(); // Returns true if the button state was released at the last read.
bool wasPressed(); // Returns true if the button state at the last read was pressed.
Expand All @@ -60,12 +64,11 @@ class EasyButton
uint32_t _time; // Time of current state.
uint32_t _last_change; // Time of last state change.
// CALLBACKS
callback_t _pressed_callback; // Callback function for pressed events.
callback_t _pressed_callback; // Callback function for pressed events.
callback_t _pressed_for_callback; // Callback function for pressedFor events.
callback_t _pressed_sequence_callback; // Callback function for pressedSequence events.
callback_t _pressed_sequence_callback; // Callback function for pressedSequence events.

virtual bool _readPin(); // Abstracts the pin value reading.
};

#endif

#endif

0 comments on commit 7ca0b11

Please sign in to comment.