![]() |
InputEvents v1.4.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
|
The EventButton class is for momentary inputs. The momentary switch (button) must be wired between the pin and GND.
EventButton is probably the easiest way to implement a button - a single function and four lines of code gives you information on all types of user inputs.
The following InputEventTypes are fired by EventButton:
ENABLED
& DISABLED
) has been fired for a specified time. Each input can define its own idle timeout. Default is 10 seconds.RELEASED
if not LONG_CLICKED
and button is pressed and released once.RELEASED
if not LONG_CLICKED
and button is pressed and released twice.RELEASED
if not LONG_CLICKED
and button is pressed and released more than twice (no limit!). The method clickCount() returns the number of clicks.#include <EventButton.h>
Public Member Functions | |
Constructors | |
EventButton (byte buttonPin, bool useDefaultDebouncer=true) | |
Construct an EventButton with a GPIO pin. More... | |
EventButton (PinAdapter *_pinAdapter, bool useDefaultDebouncer=true) | |
Construct a new EventButton with a PinAdapter and optionally use the default debouncer. More... | |
EventButton (PinAdapter *_pinAdapter, DebounceAdapter *debounceAdapter) | |
Construct a new EventButton with a PinAdapter and a DebounceAdapter. More... | |
Common Methods | |
These methods are common to all InputEvent classes. Additional methods for input enable, timeout, event blocking and user ID/value are also inherited from the EventInputBase class. | |
void | begin () |
Initialise the EventButton. More... | |
void | setCallback (CallbackFunction f) |
Set the Callback function. More... | |
template<typename T > | |
void | setCallback (T *instance, void(T::*method)(InputEventType, EventButton &)) |
Set the Callback function to a class method. More... | |
void | unsetCallback () override |
Unset a previously set callback function or method. More... | |
void | update () |
Update the state from the pin input. More... | |
Getting the State | |
These methods return the current state of the input (as set during the last update() ) | |
uint8_t | clickCount () |
The number of clicks that have been fired in the MULTI_CLICKED event. More... | |
uint8_t | longPressCount () |
The number of times the long press has occurred during a button press. More... | |
bool | isPressed () |
Returns true if button is currently pressed. | |
uint32_t | currentDuration () |
Duration in milliseconds of the current state. More... | |
uint32_t | previousDuration () |
Duration in milliseconds of the previous state. More... | |
Other Configuration Settings | |
void | enableLongPressRepeat (bool repeat=true) |
Choose whether to repeat the long press callback. (true by default) More... | |
void | setLongClickDuration (uint16_t longDurationMs=750) |
Set the number of milliseconds that define the first long click duration. More... | |
void | setLongPressInterval (uint16_t intervalMs=500) |
Set the number of milliseconds that define the subbsequent long click intervals. More... | |
void | setMultiClickInterval (uint16_t intervalMs=250) |
Set the multi click interval. More... | |
void | setDebouncer (DebounceAdapter *debounceAdapter) |
Set the debouncer. Note: When planning to use setDebouncer() you must ensure useDefaultDebouncer is set to false in the button or switch constructor. Previously set debouncers are not deleted. More... | |
bool | setDebounceInterval (uint16_t intervalMs=10) |
Set the DebounceAdapter debounce interval. Default is 10ms. More... | |
void | setPressedState (bool state=LOW) |
Set the pin state that represents 'pressed'. By default this is LOW (ie pulled down for pressed). More... | |
![]() | |
bool | isCallbackSet () |
Returns true if the callback is set. | |
void | update () |
Update the state of the input. More... | |
bool | isEnabled () |
Returns true if input is enabled. More... | |
void | enable (bool e=true) |
Enable or disable an input. More... | |
void | setIdleTimeout (unsigned int timeoutMs=10000) |
Set the idle timeout in milliseconds (default 10000, 10 seconds) More... | |
unsigned long | msSinceLastEvent () |
Returns the number of ms since any event was fired for this input. | |
bool | isIdle () |
Return true if no activity for longer than setIdleTimeout - irrespective of whether the idle (or changed) callback has been fired. More... | |
void | resetIdleTimer () |
Reset the idle timer. The IDLE event will fire setIdleTimeout ms after this is called. More... | |
void | blockEvent (InputEventType et) |
Stop an event from firing. More... | |
void | allowEvent (InputEventType et) |
Allow a a previously blocked event tto fire. More... | |
void | blockAllEvents () |
Stop all events from firing - usually used in conjunction with allowEvent() | |
void | allowAllEvents () |
Clear all blocked events. | |
bool | isEventAllowed (InputEventType et) |
Returns true if the event is not blocked. | |
void | setInputId (uint8_t id) |
Set the input ID (for use by user, not used internally). Not unique, default is 0. | |
uint8_t | getInputId () |
Get the input ID (for use by user, not used internally). Not unique, default is 0. | |
void | setInputValue (uint8_t val) |
Set the input value (for use by user, not used internally). Not unique, default is 0. | |
uint8_t | getInputValue () |
Get the input value (for use by user, not used internally). Not unique, default is 0. | |
Protected Types | |
typedef std::function< void(InputEventType et, EventButton &ie)> | CallbackFunction |
If std::function is supported, this creates the callback type. | |
typedef void(* | CallbackFunction) (InputEventType et, EventButton &) |
Used to create the callback type as pointer if std::function is not supported. | |
Protected Member Functions | |
void | invoke (InputEventType et) override |
Override of the EventInputBase::invoke() virtual method. More... | |
void | onDisabled () override |
Override base method to reset click and pressed counts. More... | |
void | changeState (bool newState) |
Change the button state and flag as changed. More... | |
bool | changedState () |
Returns true if either pinAdapter, press() or release() changed the button state. More... | |
bool | changedPinState () |
Returns true if pinAdapter read() has changed since last call. More... | |
bool | releasing () |
Returns true if state has changed and previous state is pressedState. More... | |
bool | pressing () |
Returns true if state has changed and previous state is not pressedState. More... | |
![]() | |
bool | isInvokable (InputEventType et) |
virtual void | invoke (InputEventType et)=0 |
To be overriden by derived classes. More... | |
virtual void | onEnabled () |
Can be ovrriden by derived classes but base method must be called. More... | |
virtual void | onDisabled () |
Can be ovrriden by derived classes but base method must be called. More... | |
virtual void | onIdle () |
Can be ovrriden by derived classes but base method must be called. More... | |
Protected Attributes | |
CallbackFunction | callbackFunction = nullptr |
The callback function member. | |
![]() | |
uint8_t | input_id = 0 |
Input ID, not used internally. | |
uint8_t | input_value = 0 |
Input value, not used internally. | |
bool | _enabled = true |
Input enabled flag. | |
bool | idleFlagged = true |
True if input is idle. | |
unsigned long | lastEventMs = millis() |
number of milliseconds since the last event | |
unsigned long | idleTimeout = 10000 |
The idle timeout in milliseconds. | |
bool | callbackIsSet = false |
Required because in C/C++ callback has to be defined in derived classes... :-/. | |
EventButton::EventButton | ( | byte | pin, |
bool | useDefaultDebouncer = true |
||
) |
Construct an EventButton with a GPIO pin.
buttonPin | Any type of pin and optionally use the default debouncer (default true). By default button contact should pull down to to GND when pressed. This behaviour can be reversed with setPressedState() |
GPLv2 Licence https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
Copyright (c) 2024 Philip Fletcher phili.nosp@m.p.fl.nosp@m.etche.nosp@m.r@st.nosp@m.utchb.nosp@m.ury..nosp@m.com
EventButton::EventButton | ( | PinAdapter * | _pinAdapter, |
bool | useDefaultDebouncer = true |
||
) |
Construct a new EventButton with a PinAdapter and optionally use the default debouncer.
pinAdapter |
EventButton::EventButton | ( | PinAdapter * | _pinAdapter, |
DebounceAdapter * | debounceAdapter | ||
) |
Construct a new EventButton with a PinAdapter and a DebounceAdapter.
pinAdapter | |
debounceAdapter |
|
virtual |
|
protected |
Returns true if pinAdapter read() has changed since last call.
|
protected |
Returns true if either pinAdapter, press() or release() changed the button state.
|
protected |
Change the button state and flag as changed.
newState |
|
inline |
The number of clicks that have been fired in the MULTI_CLICKED event.
This is also set do CLICK and DOUBLE_CLICKED and is reset to zero after any CLICKED event is fired.
uint32_t EventButton::currentDuration | ( | ) |
Duration in milliseconds of the current state.
|
inline |
Choose whether to repeat the long press callback. (true by default)
repeat | Pass true to repeat, false to not repeat. |
|
overrideprotectedvirtual |
Override of the EventInputBase::invoke()
virtual method.
et | Enum of type InputEventType |
Implements EventInputBase.
|
inline |
The number of times the long press has occurred during a button press.
This is incremented even if the setLongPressRepeat is false, so can be read in the LONC_CLICKED event. This is reset to zero after LONG_CLICKED
is fired.
|
overrideprotectedvirtual |
Override base method to reset click and pressed counts.
Reimplemented from EventInputBase.
|
inlineprotected |
Returns true if state has changed and previous state is not pressedState.
|
inline |
Duration in milliseconds of the previous state.
|
inlineprotected |
Returns true if state has changed and previous state is pressedState.
|
inline |
Set the Callback function.
f | A function of type EventButton::CallbackFunction type. |
|
inline |
Set the Callback function to a class method.
Note: This method is only available if std:function
is supported.
instance | The instance of a class implementing a CallbackFunction method. |
method | The class method of type EventButton::CallbackFunction type. |
bool EventButton::setDebounceInterval | ( | uint16_t | intervalMs = 10 | ) |
Set the DebounceAdapter debounce interval. Default is 10ms.
void EventButton::setDebouncer | ( | DebounceAdapter * | debounceAdapter | ) |
Set the debouncer. Note: When planning to use setDebouncer()
you must ensure useDefaultDebouncer
is set to false
in the button or switch constructor. Previously set debouncers are not deleted.
debounceAdapter |
|
inline |
Set the number of milliseconds that define the first long click duration.
The LONG_PRESS event will fire after this duration, then repeat at the interval setLongPressInterval(ms) (default 500ms) if enableLongPressRepeat() is true. After the button is released, a LONG_CLICK event will fire.
longDurationMs | Default 750ms |
|
inline |
Set the number of milliseconds that define the subbsequent long click intervals.
If enableLongPressRepeat() is true a LONG_PRESS event will fire every intervalMs after the first LONG_PRESS has fired.
intervalMs | The interval in milliseconds (default is 500ms). |
|
inline |
Set the multi click interval.
intervalMs | The interval in milliseconds between double, triple or multi clicks |
|
inline |
Set the pin state that represents 'pressed'. By default this is LOW
(ie pulled down for pressed).
If you set this value to HIGH
, you must also pass INPUT_PULLDOWN
to the GpioPinAdapter constructor.
If your board does not support INPUT_PULLDOWN
, pass INPUT
and use an external resistor.
state | Either LOW (default) or HIGH |
|
overridevirtual |
Unset a previously set callback function or method.
Must be called before the set function or method is destoyed.
Reimplemented from EventInputBase.
void EventButton::update | ( | ) |
Update the state from the pin input.
Must be called from within loop()