Skip to content

Commit

Permalink
Added an onPressedContinues callback feature
Browse files Browse the repository at this point in the history
Added example
fastbike committed Sep 22, 2022
1 parent 4e81841 commit ed3ad10
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Name: PressedForDurationContinues.ino
Created: 9/5/2018 10:49:52 AM
Author: Evert Arias
Description: Example to demostrate how to use the library to detect a pressed for a given duration on a button and to fire the additional feedback while the button is held longer
*/

#include <EasyButton.h>

// Arduino pin where the button is connected to.
#define BUTTON_PIN 2

#define BAUDRATE 115200

// Instance of the button.
EasyButton button(BUTTON_PIN);

// Callback function to be called when the button is pressed.
void onPressedForDuration()
{
Serial.println("Button pressed starts");
}

void onPressedContinues()
{
Serial.println("Button pressed continues");
}


void setup()
{
// Initialize Serial for debuging purposes.
Serial.begin(BAUDRATE);

Serial.println();
Serial.println(">>> EasyButton pressedFor example <<<");

// Initialize the button.
button.begin();
// Add the callback function to be called when the button is pressed for at least the given time.
button.onPressedFor(2000, onPressedForDuration);

// Add the callback function to be called when the button is pressed for longer
button.onPressedContinues(250, onPressedContinues);

}

void loop()
{
// Continuously read the status of the button.
button.read();
}
19 changes: 19 additions & 0 deletions src/EasyButtonBase.cpp
Original file line number Diff line number Diff line change
@@ -18,6 +18,12 @@ void EasyButtonBase::onPressedFor(uint32_t duration, EasyButtonBase::callback_t
_pressed_for_callback = callback;
}

void EasyButtonBase::onPressedContinues(uint32_t period, EasyButtonBase::callback_t callback)
{
_held_continues_period = period;
_pressed_continues_callback = callback;
}

#ifndef EASYBUTTON_DO_NOT_USE_SEQUENCES
void EasyButtonBase::onSequence(uint8_t sequences, uint32_t duration, EasyButtonBase::callback_t callback)
{
@@ -83,6 +89,19 @@ void EasyButtonBase::_checkPressedTime()
// Set as called.
_held_callback_called = true;
_pressed_for_callback();
_last_continues = read_started_ms;
}
}

if (_current_state && (read_started_ms - _last_continues) >= _held_continues_period && _pressed_continues_callback)
{
// Call the callback function for a long press continues event if it exist and the pressedFor has been called
if (_pressed_continues_callback && _held_callback_called)
{
_pressed_continues_callback();
_last_continues = read_started_ms;
}

}

}
5 changes: 5 additions & 0 deletions src/EasyButtonBase.h
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ class EasyButtonBase
virtual bool read() = 0; // 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 onPressedContinues(uint32_t period, callback_t callback); // Call a callback function when the button continues to be pressed
#ifndef EASYBUTTON_DO_NOT_USE_SEQUENCES
void onSequence(uint8_t sequences, uint32_t duration, callback_t callback); // Call a callback function when the given sequence has matched.
#endif
@@ -58,19 +59,23 @@ class EasyButtonBase
uint16_t _sequences_count;
#endif
uint32_t _held_threshold; // Held threshold.
uint32_t _held_continues_period; // Held Continues period.

#ifndef EASYBUTTON_DO_NOT_USE_SEQUENCES
callback_t _pressed_sequence_callbacks[MAX_SEQUENCES];
#endif
callback_t _pressed_callback; // Callback function for pressed events.
callback_t _pressed_for_callback; // Callback function for pressedFor events.
callback_t _pressed_continues_callback; // Callback function for pressedContinues events.

bool _held_callback_called; // Indicate if button long press has been notified.
bool _active_low; // Inverts button logic. If true, low = pressed else high = pressed.
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 _last_continues; // Time the last continues was fired
bool _was_btn_held; // Indicate if button was held.

// Common functions

0 comments on commit ed3ad10

Please sign in to comment.