Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support std::function for callback. #2

Merged
merged 1 commit into from
Oct 29, 2018
Merged
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
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