![]() |
InputEvents v1.4.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
|
The EventEncoderButton class combines the EventEncoder class with the EventButton class and is used for the common encoder buttons.
This class provides all the EventEncoder & EventButton events with the addition of CHANGED_PRESSED
& CHANGED_RELEASED
which is fired when the encoder is turned while pressed.
The following InputEventTypes are fired by EventEncoderButton:
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.Note: When the encoder is not pressed and turned, its button behaves just like a regular EventButton but if the encoder is turned while pressed, the following changes to events occur:
- on release, the button's
RELEASED
event is translated to aCHANGED_RELEASED
event if aPRESSED_CHANGED
event has been fired- the
LONG_PRESS
,CLICKED
andLONG_CLICKED
are not fired.
#include <EventEncoderButton.h>
Public Member Functions | |
Constructors | |
EventEncoderButton (EncoderAdapter *encoderAdapter, byte buttonPin, bool useDefaultDebouncer=true) | |
Construct an EventEncoderButton input from an EncoderAdapter and a pin. More... | |
EventEncoderButton (EncoderAdapter *encoderAdapter, PinAdapter *_pinAdapter, bool useDefaultDebouncer=true) | |
Construct a new EventButton with a PinAdapter and optionally use the default debouncer. More... | |
EventEncoderButton (EncoderAdapter *encoderAdapter, 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 EventEncoderButton. More... | |
void | setCallback (CallbackFunction f) |
Set the Callback function. More... | |
template<typename T > | |
void | setCallback (T *instance, void(T::*method)(InputEventType, EventEncoderButton &)) |
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 underlying encoder library and button pin. More... | |
Getting the State | |
These methods return the current state of the input (as set during the last update() ) | |
int16_t | increment () |
Returns a positive (CW) or negative (CCW) integer. More... | |
int32_t | position () |
The current position of the encoder. Can be reset with resetPosition() | |
int32_t | pressedPosition () |
The current pressed position of the encoder. Can be reset with resetPressedPosition() | |
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 pressed. | |
uint32_t | currentDuration () |
Duration in milliseconds of the current button state. More... | |
uint32_t | previousDuration () |
Duration in milliseconds of the previous button state. More... | |
Setting limits for position() and pressedPosition(). | |
High and low limits can be set for both position() and pressedPosition().
There are two behaviours defined by wrapMinMaxPosition() and wrapMinMaxPressedPosition(). The default (false) is to not wrap. | |
void | setMinPosition (int32_t minPosition=0) |
Set a lower limit for the encoder position. More... | |
void | setMaxPosition (int32_t maxPosition=0) |
Set an upper limit for the encoder position. More... | |
void | wrapMinMaxPosition (bool wrap) |
Wrap the max -> min -> max ... (default is to not wrap) More... | |
void | setMinPressedPosition (int32_t minPressedPosition=0) |
Set a lower limit for the encoder pressed position. More... | |
void | setMaxPressedPosition (int32_t maxPressedPosition=0) |
Set an upper limit for the encoder pressed position. More... | |
void | wrapMinMaxPressedPosition (bool wrap) |
Wrap the max -> min -> max ... (default is to not wrap) More... | |
Other Configuration Settings | |
void | setRateLimit (long ms) |
Encoder callbacks are normally fired on every loop() but for MPG style encoders this can fire a huge number of events (that may swamp a serial connection). More... | |
void | setPositionDivider (uint8_t divider=4) |
Quadrature encoders usually have four states for each 'click' of the rotary control, although some have just 2. More... | |
uint8_t | getPositionDivider () |
Get the currently set position divider value. More... | |
void | resetPosition (int32_t pos=0) |
Reset the counted position of the EventEncoderButton. More... | |
void | resetPressedPosition (int32_t pos) |
Reset the counted pressed position of the EventEncoderButton. More... | |
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' for the button. 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, EventEncoderButton &ie)> | CallbackFunction |
If std::function is supported, this creates the callback type. | |
typedef void(* | CallbackFunction) (InputEventType et, EventEncoderButton &) |
Used to create the callback type as pointer if std::function is not supported. | |
Protected Member Functions | |
void | invoke (InputEventType et) override |
To be overriden by derived classes. More... | |
void | onEnabled () override |
Can be ovrriden by derived classes but base method must be called. More... | |
void | onDisabled () override |
Can be ovrriden by derived classes but base method must be called. More... | |
void | onIdle () override |
Can be ovrriden by derived classes but base method must be called. More... | |
void | onInputCallback (InputEventType et, EventInputBase &ie) |
![]() | |
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. | |
EventEncoder | encoder |
the EventEncoder instance | |
EventButton | button |
the EventButton onstance | |
![]() | |
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... :-/. | |
EventEncoderButton::EventEncoderButton | ( | EncoderAdapter * | encoderAdapter, |
byte | buttonPin, | ||
bool | useDefaultDebouncer = true |
||
) |
Construct an EventEncoderButton input from an EncoderAdapter and a pin.
Please see EncoderAdapter Notes
Note: The EncoderAdapter's begin() method will be called from the EventEncoderButton's begin() method.
encoderAdapter | Pass a previously created EncoderAdapter by reference. |
buttonPin | The pin for the button |
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
EventEncoderButton::EventEncoderButton | ( | EncoderAdapter * | encoderAdapter, |
PinAdapter * | _pinAdapter, | ||
bool | useDefaultDebouncer = true |
||
) |
Construct a new EventButton with a PinAdapter and optionally use the default debouncer.
encoderAdapter | Pass a previously created EncoderAdapter by reference. |
pinAdapter |
EventEncoderButton::EventEncoderButton | ( | EncoderAdapter * | encoderAdapter, |
PinAdapter * | _pinAdapter, | ||
DebounceAdapter * | debounceAdapter | ||
) |
Construct a new EventButton with a PinAdapter and a DebounceAdapter.
encoderAdapter | Pass a previously created EncoderAdapter by reference. |
pinAdapter | |
debounceAdapter |
|
virtual |
unsigned char EventEncoderButton::clickCount | ( | ) |
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 EventEncoderButton::currentDuration | ( | ) |
Duration in milliseconds of the current button state.
void EventEncoderButton::enableLongPressRepeat | ( | bool | repeat = true | ) |
Choose whether to repeat the long press callback. (true by default)
repeat | Pass true to repeat, false to not repeat. |
uint8_t EventEncoderButton::getPositionDivider | ( | ) |
Get the currently set position divider value.
|
inline |
Returns a positive (CW) or negative (CCW) integer.
The returned value normally 1 or -1 but if your loop() has lots of processing, your Arduino is slow or you setRateLimit() this will report the actual number of increments made by the encoder since the update().
|
overrideprotectedvirtual |
To be overriden by derived classes.
et | Enum of type InputEventType |
Implements EventInputBase.
uint8_t EventEncoderButton::longPressCount | ( | ) |
The number of times the long press has occurred during a button press.
This is oncremented 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 |
Can be ovrriden by derived classes but base method must be called.
Reimplemented from EventInputBase.
|
overrideprotectedvirtual |
Can be ovrriden by derived classes but base method must be called.
Reimplemented from EventInputBase.
|
inlineoverrideprotectedvirtual |
Can be ovrriden by derived classes but base method must be called.
Reimplemented from EventInputBase.
|
protected |
Convert the encoder & button callbacks to a EventEncoderButton callbacks
uint32_t EventEncoderButton::previousDuration | ( | ) |
Duration in milliseconds of the previous button state.
|
inline |
Reset the counted position of the EventEncoderButton.
Note: This does not reset the position on the underlying encoder libraries may only allow a 'reset' to 0, not the setting of a specific value.
It also does not check if the passed position is within any min/max limits you may have set.
|
inline |
Reset the counted pressed position of the EventEncoderButton.
Note: This only resets the EventEncoderButton pressedPosition, it does not change the underlying encoder library position.
It also does not check if the passed position is within any min/max limits you may have set.
|
inline |
Set the Callback function.
f | A function of type EventEncoderButton::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 EventEncoderButton::CallbackFunction type. |
bool EventEncoderButton::setDebounceInterval | ( | uint16_t | intervalMs = 10 | ) |
Set the DebounceAdapter debounce interval. Default is 10ms.
void EventEncoderButton::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 |
void EventEncoderButton::setLongClickDuration | ( | uint16_t | longDurationMs = 750 | ) |
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 |
void EventEncoderButton::setLongPressInterval | ( | uint16_t | intervalMs = 500 | ) |
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). |
void EventEncoderButton::setMaxPosition | ( | int32_t | maxPosition = 0 | ) |
Set an upper limit for the encoder position.
If position() is higher than the passed value, then it will be set to maxPosition.
maxPosition | The highest position required |
void EventEncoderButton::setMaxPressedPosition | ( | int32_t | maxPressedPosition = 0 | ) |
Set an upper limit for the encoder pressed position.
If either setMinPressedPosition() or setMaxPressedPosition() are non zero, the limits will be applied.
If wrapMinMaxPressedPosition() is set to true, the pressedPosition() will be wrapped from max to min or min to max.
maxPressedPosition | The highest pressed position required |
void EventEncoderButton::setMinPosition | ( | int32_t | minPosition = 0 | ) |
Set a lower limit for the encoder position.
If position() is lower than the passed value, then it will be set to minPosition.
minPosition | The lowest position required |
void EventEncoderButton::setMinPressedPosition | ( | int32_t | minPressedPosition = 0 | ) |
Set a lower limit for the encoder pressed position.
If pressedPosition() is higher than the passed value, then it will be set to maxPressedPosition.
minPressedPosition | The lowest pressed position required |
void EventEncoderButton::setMultiClickInterval | ( | uint16_t | intervalMs = 250 | ) |
Set the multi click interval.
intervalMs | The interval in milliseconds between double, triple or multi clicks |
void EventEncoderButton::setPositionDivider | ( | uint8_t | divider = 4 | ) |
Quadrature encoders usually have four states for each 'click' of the rotary control, although some have just 2.
Normally we only want to fire an event once per click so the default positionDivider is 4. You can set this to any positive integer eg 8 would increment the position every 2 clicks. Affects pressed+turning too.
void EventEncoderButton::setPressedState | ( | bool | state = LOW | ) |
Set the pin state that represents 'pressed' for the button. 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 |
void EventEncoderButton::setRateLimit | ( | long | ms | ) |
Encoder callbacks are normally fired on every loop() but for MPG style encoders this can fire a huge number of events (that may swamp a serial connection).
The encoder interupts will sitll be called but this will limit the call back firing to every set ms - read the EncoderButton.increment() for lossless counting of encoder. Set to zero (default) for no rate limit.
ms | Number of milliseconds between each update() |
|
overridevirtual |
Unset a previously set callback function or method.
Must be called before the set function or method is destoyed.
Reimplemented from EventInputBase.
void EventEncoderButton::update | ( | ) |
Update the state from the underlying encoder library and button pin.
Must be called from within loop()
|
inline |
Wrap the max -> min -> max ... (default is to not wrap)
If pressedPosition() is lower than the passed value, then it will be set to minPressedPosition.
wrap | Pass true to wrap and false for default behaviour. |
|
inline |
Wrap the max -> min -> max ... (default is to not wrap)
If wrapMinMaxPressedPosition() is set to false (the default), the pressedPosition() will be stopped at the min or max limit.
If wrapMinMaxPressedPosition() is set to true, the pressedPosition() will be wrapped from max -> min -> max ...
wrap | Pass true to wrap and false for default behaviour. |