InputEvents v1.6.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
EventEncoderButton.h
1/*
2 *
3 * GPLv2 Licence https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
4 *
5 * Copyright (c) 2024 Philip Fletcher <philip.fletcher@stutchbury.com>
6 *
7 */
8
9#include "InputEvents.h"
10
11
12#ifndef EVENT_ENCODER_BUTTON_H
13#define EVENT_ENCODER_BUTTON_H
14
15
16#include "Arduino.h"
17#include "EventEncoder.h"
18#include "EventButton.h"
19
46
47 protected:
48
49 #if defined(FUNCTIONAL_SUPPORTED)
53 typedef std::function<void(InputEventType et, EventEncoderButton &ie)> CallbackFunction;
54 #else
59 #endif
60
65
66public:
67
69
82 EventEncoderButton(IEncoderAdapter *encoderAdapter, byte buttonPin, bool useDefaultDebouncer=true);
83
90 EventEncoderButton(IEncoderAdapter *encoderAdapter, PinAdapter* _pinAdapter, bool useDefaultDebouncer=true);
91
99 EventEncoderButton(IEncoderAdapter *encoderAdapter, PinAdapter* _pinAdapter, DebounceAdapter* debounceAdapter);
100
102
104
115 void begin();
116
124 callbackIsSet = true;
125 }
126
136 #if defined(FUNCTIONAL_SUPPORTED)
137 // Method to set callback with instance and class method
138 template <typename T>
139 void setCallback(T* instance, void (T::*method)(InputEventType, EventEncoderButton&)) {
140 // Wrap the method call in a lambda
141 callbackFunction = [instance, method](InputEventType et, EventEncoderButton &ie) {
142 (instance->*method)(et, ie); // Call the member function on the instance
143 };
144 callbackIsSet = true;
145 }
146 #endif
147
153 void unsetCallback() override;
154
160 void update();
161
168 void resetState();
169
171
173
185 int16_t increment() { return currentIncrement; }
186
190 int32_t position() { return currentPosition; }
191
195 int32_t pressedPosition() { return currentPressedPosition; }
196
203 uint8_t clickCount();
204
205
212 uint8_t longPressCount();
213
217 bool isPressed();
218
224 uint32_t currentDuration();
225
231 uint32_t previousDuration();
232
234
236
255 void setMinPosition(int32_t minPosition=0);
256
264 void setMaxPosition(int32_t maxPosition=0);
265
275 void wrapMinMaxPosition(bool wrap) { wrapMinMaxPos = wrap; }
276
284 void setMinPressedPosition(int32_t minPressedPosition=0);
294 void setMaxPressedPosition(int32_t maxPressedPosition=0);
295
305 void wrapMinMaxPressedPosition(bool wrap) { wrapMinMaxPressedPos = wrap; }
307
309
324 void setRateLimit(long ms);
325
335 void setPositionDivider(uint8_t divider=4);
336
342 uint8_t getPositionDivider();
343
344
351 void resetPosition(int32_t pos = 0) {
352 currentPosition = pos;
353 previousPosition = currentPosition;
354 }
355
362 void resetPressedPosition(int32_t pos) {
363 currentPressedPosition = pos;
364 previousPressedPosition = currentPressedPosition;
365 }
366
372 void enableLongPressRepeat(bool repeat=true);
373
380 void setLongClickDuration(uint16_t longDurationMs=750);
381
388 void setLongPressInterval(uint16_t intervalMs=500);
389
395 void setMultiClickInterval(uint16_t intervalMs=250);
396
402 void setDebouncer(DebounceAdapter* debounceAdapter);
403
410 bool setDebounceInterval(uint16_t intervalMs=10);
411
421 void setPressedState(bool state = LOW);
423
424protected:
425 void invoke(InputEventType et) override;
426 void onEnabled() override;
427 void onDisabled() override;
428 void onIdle() override {/* Do nothing. Fire idle callback from either encoder or button but only if both are idle.*/ }
429
436
437private:
438 int16_t currentIncrement = 0;
439
440 int32_t currentPosition = 0;
441 int32_t previousPosition = 0;
442
443 int32_t currentPressedPosition = 0;
444 int32_t previousPressedPosition = 0;
445
446 bool encodingPressed = false;
447 uint16_t encodingPressedCount = 0;
448
449 int32_t minPos=0;
450 int32_t maxPos=0;
451 bool wrapMinMaxPos = false;
452
453 int32_t minPressedPos=0;
454 int32_t maxPressedPos=0;
455 bool wrapMinMaxPressedPos = false;
456
457 void setCallbacks();
458 bool onEncoderChanged();
459
461#ifndef FUNCTIONAL_SUPPORTED
462private:
463 static void encoderCallback(InputEventType et, EventEncoder &enc) {
464 EventEncoderButton *instance = static_cast<EventEncoderButton *>(enc.getOwner());
465 if (instance) instance->onInputCallback(et, enc);
466 }
467
468 static void buttonCallback(InputEventType et, EventButton &btn) {
469 EventEncoderButton *instance = static_cast<EventEncoderButton *>(btn.getOwner());
470 if (instance) instance->onInputCallback(et, btn);
471 }
472#endif
474
475};
476
477
478#endif
479
Contains InputEventType enums and some defines.
InputEventType
The size of the InputEventType enum.
Definition: InputEvents.h:46
This is the interface/base class for debounce adapters.
Definition: DebounceAdapter.h:11
The EventButton class is for momentary inputs. The momentary switch (button) must be wired between th...
Definition: EventButton.h:35
The EventEncoderButton class combines the EventEncoder class with the EventButton class and is used f...
Definition: EventEncoderButton.h:45
EventEncoderButton(IEncoderAdapter *encoderAdapter, byte buttonPin, bool useDefaultDebouncer=true)
Construct an EventEncoderButton input from an IEncoderAdapter and a pin.
Definition: EventEncoderButton.cpp:12
CallbackFunction callbackFunction
The callback function member.
Definition: EventEncoderButton.h:64
int32_t position()
The current position of the encoder. Can be reset with resetPosition()
Definition: EventEncoderButton.h:190
void resetPressedPosition(int32_t pos)
Reset the counted pressed position of the EventEncoderButton.
Definition: EventEncoderButton.h:362
void setLongPressInterval(uint16_t intervalMs=500)
Set the number of milliseconds that define the subbsequent long click intervals.
Definition: EventEncoderButton.cpp:199
bool setDebounceInterval(uint16_t intervalMs=10)
Set the DebounceAdapter debounce interval. Default is 10ms.
Definition: EventEncoderButton.cpp:189
void setCallback(CallbackFunction f)
Set the Callback function.
Definition: EventEncoderButton.h:122
void onEnabled() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventEncoderButton.cpp:60
uint32_t currentDuration()
Duration in milliseconds of the current button state.
Definition: EventEncoderButton.cpp:203
void setMinPosition(int32_t minPosition=0)
Set a lower limit for the encoder position.
Definition: EventEncoderButton.cpp:147
void setPressedState(bool state=LOW)
Set the pin state that represents 'pressed' for the button. By default this is LOW (ie pulled down fo...
Definition: EventEncoderButton.cpp:191
void wrapMinMaxPressedPosition(bool wrap)
Wrap the max -> min -> max ... (default is to not wrap)
Definition: EventEncoderButton.h:305
void setMultiClickInterval(uint16_t intervalMs=250)
Set the multi click interval.
Definition: EventEncoderButton.cpp:193
void invoke(InputEventType et) override
To be overriden by derived classes.
Definition: EventEncoderButton.cpp:54
void enableLongPressRepeat(bool repeat=true)
Choose whether to repeat the long press callback. (true by default)
Definition: EventEncoderButton.cpp:197
void onInputCallback(InputEventType et, EventInputBase &ie)
Definition: EventEncoderButton.cpp:72
void setPositionDivider(uint8_t divider=4)
Quadrature encoders usually have four states for each 'click' of the rotary control,...
Definition: EventEncoderButton.cpp:181
uint8_t getPositionDivider()
Get the currently set position divider value.
Definition: EventEncoderButton.cpp:183
uint32_t previousDuration()
Duration in milliseconds of the previous button state.
Definition: EventEncoderButton.cpp:205
void onDisabled() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventEncoderButton.cpp:66
EventEncoder encoder
the EventEncoder instance
Definition: EventEncoderButton.h:430
void begin()
Initialise the EventEncoderButton.
Definition: EventEncoderButton.cpp:39
bool isPressed()
Returns true if button is pressed.
Definition: EventEncoderButton.cpp:185
void setCallback(T *instance, void(T::*method)(InputEventType, EventEncoderButton &))
Set the Callback function to a class method.
Definition: EventEncoderButton.h:139
EventButton button
the EventButton onstance
Definition: EventEncoderButton.h:431
void setRateLimit(long ms)
Encoder callbacks are normally fired on every loop() but for MPG style encoders this can fire a huge ...
Definition: EventEncoderButton.cpp:179
void resetPosition(int32_t pos=0)
Reset the counted position of the EventEncoderButton.
Definition: EventEncoderButton.h:351
void setMaxPressedPosition(int32_t maxPressedPosition=0)
Set an upper limit for the encoder pressed position.
Definition: EventEncoderButton.cpp:171
void onIdle() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventEncoderButton.h:428
void unsetCallback() override
Unset a previously set callback function or method.
Definition: EventEncoderButton.cpp:44
void setMinPressedPosition(int32_t minPressedPosition=0)
Set a lower limit for the encoder pressed position.
Definition: EventEncoderButton.cpp:163
void setDebouncer(DebounceAdapter *debounceAdapter)
Set the debouncer. Note: When planning to use setDebouncer() you must ensure useDefaultDebouncer is s...
Definition: EventEncoderButton.cpp:187
std::function< void(InputEventType et, EventEncoderButton &ie)> CallbackFunction
If std::function is supported, this creates the callback type.
Definition: EventEncoderButton.h:53
void setMaxPosition(int32_t maxPosition=0)
Set an upper limit for the encoder position.
Definition: EventEncoderButton.cpp:155
void wrapMinMaxPosition(bool wrap)
Wrap the max -> min -> max ... (default is to not wrap)
Definition: EventEncoderButton.h:275
int32_t pressedPosition()
The current pressed position of the encoder. Can be reset with resetPressedPosition()
Definition: EventEncoderButton.h:195
uint8_t longPressCount()
The number of times the long press has occurred during a button press.
Definition: EventEncoderButton.cpp:207
void resetState()
Resets the state of the input: silently enables if disabled, clears any blocked events,...
Definition: EventEncoderButton.cpp:209
void update()
Update the state from the underlying encoder library and button pin.
Definition: EventEncoderButton.cpp:49
uint8_t clickCount()
The number of clicks that have been fired in the MULTI_CLICKED event.
Definition: EventEncoderButton.cpp:201
void setLongClickDuration(uint16_t longDurationMs=750)
Set the number of milliseconds that define the first long click duration.
Definition: EventEncoderButton.cpp:195
int16_t increment()
Returns a positive (CW) or negative (CCW) integer.
Definition: EventEncoderButton.h:185
The EventEncoder class is for quadrature encoder inputs providing the position & encoder increment,...
Definition: EventEncoder.h:29
The common base for InputEvents input classes.
Definition: EventInputBase.h:26
bool callbackIsSet
Required because in C/C++ callback has to be defined in derived classes... :-/.
Definition: EventInputBase.h:193
A lightweight adapter abstract class for encoders.
Definition: IEncoderAdapter.h:19
The interface specification for button, encoder button and switch pins.
Definition: PinAdapter.h:8