Skip to content

Commit

Permalink
Merge pull request #2 from kekyo/applicable-lambda
Browse files Browse the repository at this point in the history
Support std::function for callback.
  • Loading branch information
evert-arias authored Oct 29, 2018
2 parents be2a42d + 84c8067 commit 108f4e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/EasyButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ void EasyButton::begin() {
_last_change = _time;
}

void EasyButton::onPressed(void(*callback)()) {
void EasyButton::onPressed(EasyButton::callback_t callback) {
mPressedCallback = callback;
}

void EasyButton::onPressedFor(uint32_t duration, void(*callback)()) {
void EasyButton::onPressedFor(uint32_t duration, EasyButton::callback_t callback) {
_held_threshold = duration;
mPressedForCallback = callback;
}

void EasyButton::onSequence(uint8_t sequences, uint32_t duration, void(*callback)())
void EasyButton::onSequence(uint8_t sequences, uint32_t duration, EasyButton::callback_t callback)
{
_press_sequences = sequences;
_press_sequence_duration = duration;
Expand Down
25 changes: 19 additions & 6 deletions src/EasyButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,30 @@

#include <Arduino.h>

#ifdef ESP8266
#define EASYBUTTON_FUNCTIONAL_SUPPORT 1
#endif

#ifdef EASYBUTTON_FUNCTIONAL_SUPPORT
#include <functional>
#endif

class EasyButton
{
public:
#ifdef EASYBUTTON_FUNCTIONAL_SUPPORT
typedef std::function<void()> callback_t;
#else
typedef void(*callback_t)();
#endif
EasyButton(uint8_t pin, uint32_t dbTime = 35, bool puEnable = true, bool invert = true) : _pin(pin), _db_time(dbTime), _pu_enabled(puEnable), _invert(invert) {}
~EasyButton() {};
// PUBLIC FUNCTIONS
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(void(*callback)()); // Call a callback function when the button has been pressed and released.
void onPressedFor(uint32_t duration, void(*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, void(*callback)()); // Call a callback function when the given sequence has matched.
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.
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 @@ -46,9 +59,9 @@ class EasyButton
uint32_t _time; // Time of current state.
uint32_t _last_change; // Time of last state change.
// CALLBACKS
void(*mPressedCallback)(); // Callback functionn for pressed events.
void(*mPressedForCallback)(); // Callback function for pressedFor events.
void(*mPressedSequenceCallback)(); // Callback function for pressedSequence events.
callback_t mPressedCallback; // Callback function for pressed events.
callback_t mPressedForCallback; // Callback function for pressedFor events.
callback_t mPressedSequenceCallback; // Callback function for pressedSequence events.
};

#endif
Expand Down

0 comments on commit 108f4e8

Please sign in to comment.