From bc2b91dc8aa08c7cbeec85ab4841aad591f3a05f Mon Sep 17 00:00:00 2001 From: elC0mpa Date: Mon, 12 Aug 2019 13:26:10 +0200 Subject: [PATCH 1/3] Interrupts support --- src/EasyButton.cpp | 49 ++++++++++++++++++++++++++++++++++------------ src/EasyButton.h | 11 +++++++---- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/EasyButton.cpp b/src/EasyButton.cpp index 0e8f892..a5e92e2 100644 --- a/src/EasyButton.cpp +++ b/src/EasyButton.cpp @@ -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. @@ -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. @@ -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)); +} \ No newline at end of file diff --git a/src/EasyButton.h b/src/EasyButton.h index 61bc671..eb69fc9 100644 --- a/src/EasyButton.h +++ b/src/EasyButton.h @@ -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. @@ -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 \ No newline at end of file From cfae017bc4b8103870e2616e79e679cc73287074 Mon Sep 17 00:00:00 2001 From: elC0mpa Date: Mon, 12 Aug 2019 13:40:46 +0200 Subject: [PATCH 2/3] Interrupts example --- examples/Interrupts/Interrupts.ino | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 examples/Interrupts/Interrupts.ino diff --git a/examples/Interrupts/Interrupts.ino b/examples/Interrupts/Interrupts.ino new file mode 100644 index 0000000..46edc2d --- /dev/null +++ b/examples/Interrupts/Interrupts.ino @@ -0,0 +1,54 @@ +/* + Name: MultipleButtons.ino + Created: 8/11/2019 11:45:52 AM + Author: José Gabriel Companioni Benítez (elC0mpa) + Description: Example to demostrate how to use the library with more than one button +*/ + +#include +#include + +#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: +} From 58d241285c3145b97152991131eec3d4fba8484f Mon Sep 17 00:00:00 2001 From: elC0mpa Date: Mon, 12 Aug 2019 14:03:22 +0200 Subject: [PATCH 3/3] added Interrupts example --- examples/Interrupts/Interrupts.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Interrupts/Interrupts.ino b/examples/Interrupts/Interrupts.ino index 46edc2d..685c684 100644 --- a/examples/Interrupts/Interrupts.ino +++ b/examples/Interrupts/Interrupts.ino @@ -1,7 +1,7 @@ /* Name: MultipleButtons.ino Created: 8/11/2019 11:45:52 AM - Author: José Gabriel Companioni Benítez (elC0mpa) + Author: José Gabriel Companioni Benítez (https://github.com/elC0mpa) Description: Example to demostrate how to use the library with more than one button */