Skip to content

Commit

Permalink
Update preprocessor macros and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Evert Arias committed Apr 3, 2020
1 parent 5496efd commit 1a99099
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
24 changes: 12 additions & 12 deletions src/EasyButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ void EasyButton::begin()
{
pinMode(_pin, _pu_enabled ? INPUT_PULLUP : INPUT);
_current_state = _readPin();
if (_invert) _current_state = !_current_state;
if (_invert)
_current_state = !_current_state;
_time = millis();
_last_state = _current_state;
_changed = false;
Expand All @@ -31,7 +32,7 @@ void EasyButton::onPressedFor(uint32_t duration, EasyButton::callback_t callback

void EasyButton::onSequence(uint8_t sequences, uint32_t duration, EasyButton::callback_t callback)
{
if(_sequences_count < 5)
if (_sequences_count < 5)
{
Sequence sequence(sequences, duration);
sequence.enable();
Expand Down Expand Up @@ -80,11 +81,11 @@ bool EasyButton::read(int read_type)
pinVal = !pinVal;

if (read_started_ms - _last_change < _db_time)
{ //true -> debounce time has not ellapsed
{ //true -> debounce time has not ellapsed
_changed = false;
}
else
{ //true -> debounce time ellapsed
{ //true -> 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.
Expand All @@ -105,7 +106,7 @@ bool EasyButton::read(int read_type)

for (size_t i = 0; i < MAX_SEQUENCES; i++)
{
if(_sequences[i].newPress(read_started_ms))
if (_sequences[i].newPress(read_started_ms))
{
callback_t function = _pressed_sequence_callbacks[i];
function();
Expand All @@ -120,9 +121,8 @@ bool EasyButton::read(int read_type)
// since button released, reset _pressed_for_callbackCalled value.
_held_callback_called = false;
}
else if(isPressed() && read_type == EASYBUTTON_TYPE_POLL)
else if (isPressed() && read_type == EASYBUTTON_READ_TYPE_POLL)
_checkPressedTime();


_time = read_started_ms;

Expand Down Expand Up @@ -164,11 +164,11 @@ void EasyButton::_checkPressedTime()
_was_btn_held = true;

// reset short presses counters.
for(Sequence seq:_sequences)
{
seq.reset();
}
for (Sequence seq : _sequences)
{
seq.reset();
}

// call the callback function for a long press event if it exist and if it has not been called yet.
if (_pressed_for_callback && !_held_callback_called)
{
Expand Down
61 changes: 32 additions & 29 deletions src/EasyButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,65 @@
#include "FunctionalInterrupt.h"
#endif

#define EASYBUTTON_TYPE_INTERRUPT 0
#define EASYBUTTON_TYPE_POLL 1
#define EASYBUTTON_READ_TYPE_INTERRUPT 0
#define EASYBUTTON_READ_TYPE_POLL 1

#define MAX_SEQUENCES 5

class EasyButton
{
friend class EasyButtonTouch;

public:
#ifdef EASYBUTTON_FUNCTIONAL_SUPPORT
typedef std::function<void()> callback_t;
#else
typedef void(*callback_t)();
typedef void (*callback_t)();
#endif
EasyButton(uint8_t pin, uint32_t debounce_time = 35, bool pullup_enable = true, bool invert = true) : _pin(pin), _db_time(debounce_time), _invert(invert), _pu_enabled(pullup_enable) {}
EasyButton(uint8_t pin, uint32_t debounce_time = 35, bool pullup_enable = true, bool invert = true) : _pin(pin), _db_time(debounce_time), _invert(invert), _pu_enabled(pullup_enable)
{
}
~EasyButton() {}
// PUBLIC FUNCTIONS
virtual void begin(); // Initialize a button object and the pin it's connected to.
bool read(int read_type = EASYBUTTON_TYPE_POLL); // Returns the current debounced button state, true for pressed, false for released.
bool read(int read_type = EASYBUTTON_READ_TYPE_POLL); // Returns the current debounced button state, true for pressed, false for released.
void update(); // Update button pressed time, only needed when using interrupts
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 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.
bool wasReleased(); // Returns true if the button state at the last read was released.
bool pressedFor(uint32_t duration); // Returns true if the button state at the last read was pressed, and has been in that state for at least the given number of milliseconds.
bool releasedFor(uint32_t duration); // Returns true if the button state at the last read was released, and has been in that state for at least the given number of milliseconds.
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.
bool wasReleased(); // Returns true if the button state at the last read was released.
bool pressedFor(uint32_t duration); // Returns true if the button state at the last read was pressed, and has been in that state for at least the given number of milliseconds.
bool releasedFor(uint32_t duration); // Returns true if the button state at the last read was released, and has been in that state for at least the given number of milliseconds.
private:
// PRIVATE VARIABLES
Sequence _sequences[MAX_SEQUENCES];
uint16_t _sequences_count;
callback_t _pressed_sequence_callbacks[MAX_SEQUENCES];

uint32_t _held_threshold; // Held threshold.
bool _was_btn_held; // Indicate if button was held.
bool _held_callback_called; // Indicate if button long press has been notified.
uint8_t _pin; // Arduino pin number where the Button is connected to.
uint32_t _db_time; // Debounce time (ms).
bool _invert; // Inverts button logic. If true, low = pressed else high = pressed.
bool _pu_enabled; // Internal pullup resistor enabled.
bool _current_state; // Current button state, true = pressed.
bool _last_state; // Previous button state, true = pressed.
bool _changed; // Has the state change since last read.
uint32_t _time; // Time of current state.
uint32_t _last_change; // Time of last state change.
uint32_t _held_threshold; // Held threshold.
bool _was_btn_held; // Indicate if button was held.
bool _held_callback_called; // Indicate if button long press has been notified.
uint8_t _pin; // Arduino pin number where the Button is connected to.
uint32_t _db_time; // Debounce time (ms).
bool _invert; // Inverts button logic. If true, low = pressed else high = pressed.
bool _pu_enabled; // Internal pullup resistor enabled.
bool _current_state; // Current button state, true = pressed.
bool _last_state; // Previous button state, true = pressed.
bool _changed; // Has the state change since last read.
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_for_callback; // Callback function for pressedFor events.
callback_t _pressed_callback; // Callback function for pressed events.
callback_t _pressed_for_callback; // Callback function for pressedFor events.

virtual bool _readPin(); // Abstracts the pin value reading.
void _checkPressedTime(); // Verify if pressed_for_callback should be called
virtual bool _readPin(); // Abstracts the pin value reading.
void _checkPressedTime(); // Verify if pressed_for_callback should be called
};

#endif

0 comments on commit 1a99099

Please sign in to comment.