InputEvents v1.5.2
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 EXCLUDE_EVENT_ENCODER
13
14#ifndef EVENT_ENCODER_BUTTON_H
15#define EVENT_ENCODER_BUTTON_H
16
17
18#include "Arduino.h"
19#include "EventEncoder.h"
20#include "EventButton.h"
21
48
49 protected:
50
51 #if defined(FUNCTIONAL_SUPPORTED)
55 typedef std::function<void(InputEventType et, EventEncoderButton &ie)> CallbackFunction;
56 #else
61 #endif
62
67
68public:
69
71
84 EventEncoderButton(EncoderAdapter *encoderAdapter, byte buttonPin, bool useDefaultDebouncer=true);
85
92 EventEncoderButton(EncoderAdapter *encoderAdapter, PinAdapter* _pinAdapter, bool useDefaultDebouncer=true);
93
101 EventEncoderButton(EncoderAdapter *encoderAdapter, PinAdapter* _pinAdapter, DebounceAdapter* debounceAdapter);
102
104
106
117 void begin();
118
126 callbackIsSet = true;
127 }
128
138 #if defined(FUNCTIONAL_SUPPORTED)
139 // Method to set callback with instance and class method
140 template <typename T>
141 void setCallback(T* instance, void (T::*method)(InputEventType, EventEncoderButton&)) {
142 // Wrap the method call in a lambda
143 callbackFunction = [instance, method](InputEventType et, EventEncoderButton &ie) {
144 (instance->*method)(et, ie); // Call the member function on the instance
145 };
146 callbackIsSet = true;
147 }
148 #endif
149
155 void unsetCallback() override;
156
162 void update();
163
170 void resetState();
171
173
175
187 int16_t increment() { return currentIncrement; }
188
192 int32_t position() { return currentPosition; }
193
197 int32_t pressedPosition() { return currentPressedPosition; }
198
205 uint8_t clickCount();
206
207
214 uint8_t longPressCount();
215
219 bool isPressed();
220
226 uint32_t currentDuration();
227
233 uint32_t previousDuration();
234
236
238
257 void setMinPosition(int32_t minPosition=0);
258
266 void setMaxPosition(int32_t maxPosition=0);
267
277 void wrapMinMaxPosition(bool wrap) { wrapMinMaxPos = wrap; }
278
286 void setMinPressedPosition(int32_t minPressedPosition=0);
296 void setMaxPressedPosition(int32_t maxPressedPosition=0);
297
307 void wrapMinMaxPressedPosition(bool wrap) { wrapMinMaxPressedPos = wrap; }
309
311
326 void setRateLimit(long ms);
327
337 void setPositionDivider(uint8_t divider=4);
338
344 uint8_t getPositionDivider();
345
346
353 void resetPosition(int32_t pos = 0) {
354 currentPosition = pos;
355 previousPosition = currentPosition;
356 }
357
364 void resetPressedPosition(int32_t pos) {
365 currentPressedPosition = pos;
366 previousPressedPosition = currentPressedPosition;
367 }
368
374 void enableLongPressRepeat(bool repeat=true);
375
382 void setLongClickDuration(uint16_t longDurationMs=750);
383
390 void setLongPressInterval(uint16_t intervalMs=500);
391
397 void setMultiClickInterval(uint16_t intervalMs=250);
398
404 void setDebouncer(DebounceAdapter* debounceAdapter);
405
412 bool setDebounceInterval(uint16_t intervalMs=10);
413
423 void setPressedState(bool state = LOW);
425
426protected:
427 void invoke(InputEventType et) override;
428 void onEnabled() override;
429 void onDisabled() override;
430 void onIdle() override {/* Do nothing. Fire idle callback from either encoder or button but only if both are idle.*/ }
431
438
439private:
440 int16_t currentIncrement = 0;
441
442 int32_t currentPosition = 0;
443 int32_t previousPosition = 0;
444
445 int32_t currentPressedPosition = 0;
446 int32_t previousPressedPosition = 0;
447
448 bool encodingPressed = false;
449 uint16_t encodingPressedCount = 0;
450
451 int32_t minPos=0;
452 int32_t maxPos=0;
453 bool wrapMinMaxPos = false;
454
455 int32_t minPressedPos=0;
456 int32_t maxPressedPos=0;
457 bool wrapMinMaxPressedPos = false;
458
459 void setCallbacks();
460 bool onEncoderChanged();
461
463#ifndef FUNCTIONAL_SUPPORTED
464private:
465 static void encoderCallback(InputEventType et, EventEncoder &enc) {
466 EventEncoderButton *instance = static_cast<EventEncoderButton *>(enc.getOwner());
467 if (instance) instance->onInputCallback(et, enc);
468 }
469
470 static void buttonCallback(InputEventType et, EventButton &btn) {
471 EventEncoderButton *instance = static_cast<EventEncoderButton *>(btn.getOwner());
472 if (instance) instance->onInputCallback(et, btn);
473 }
474#endif
476
477};
478
479
480#endif
481#endif
482
Contains InputEventType enums and some defines.
InputEventType
The size of the InputEventType enum.
Definition: InputEvents.h:55
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:47
CallbackFunction callbackFunction
The callback function member.
Definition: EventEncoderButton.h:66
int32_t position()
The current position of the encoder. Can be reset with resetPosition()
Definition: EventEncoderButton.h:192
void resetPressedPosition(int32_t pos)
Reset the counted pressed position of the EventEncoderButton.
Definition: EventEncoderButton.h:364
void setLongPressInterval(uint16_t intervalMs=500)
Set the number of milliseconds that define the subbsequent long click intervals.
Definition: EventEncoderButton.cpp:202
bool setDebounceInterval(uint16_t intervalMs=10)
Set the DebounceAdapter debounce interval. Default is 10ms.
Definition: EventEncoderButton.cpp:192
void setCallback(CallbackFunction f)
Set the Callback function.
Definition: EventEncoderButton.h:124
void onEnabled() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventEncoderButton.cpp:63
uint32_t currentDuration()
Duration in milliseconds of the current button state.
Definition: EventEncoderButton.cpp:206
void setMinPosition(int32_t minPosition=0)
Set a lower limit for the encoder position.
Definition: EventEncoderButton.cpp:150
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:194
void wrapMinMaxPressedPosition(bool wrap)
Wrap the max -> min -> max ... (default is to not wrap)
Definition: EventEncoderButton.h:307
void setMultiClickInterval(uint16_t intervalMs=250)
Set the multi click interval.
Definition: EventEncoderButton.cpp:196
void invoke(InputEventType et) override
To be overriden by derived classes.
Definition: EventEncoderButton.cpp:57
void enableLongPressRepeat(bool repeat=true)
Choose whether to repeat the long press callback. (true by default)
Definition: EventEncoderButton.cpp:200
void onInputCallback(InputEventType et, EventInputBase &ie)
Definition: EventEncoderButton.cpp:75
void setPositionDivider(uint8_t divider=4)
Quadrature encoders usually have four states for each 'click' of the rotary control,...
Definition: EventEncoderButton.cpp:184
uint8_t getPositionDivider()
Get the currently set position divider value.
Definition: EventEncoderButton.cpp:186
uint32_t previousDuration()
Duration in milliseconds of the previous button state.
Definition: EventEncoderButton.cpp:208
EventEncoderButton(EncoderAdapter *encoderAdapter, byte buttonPin, bool useDefaultDebouncer=true)
Construct an EventEncoderButton input from an EncoderAdapter and a pin.
Definition: EventEncoderButton.cpp:15
void onDisabled() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventEncoderButton.cpp:69
EventEncoder encoder
the EventEncoder instance
Definition: EventEncoderButton.h:432
void begin()
Initialise the EventEncoderButton.
Definition: EventEncoderButton.cpp:42
bool isPressed()
Returns true if button is pressed.
Definition: EventEncoderButton.cpp:188
void setCallback(T *instance, void(T::*method)(InputEventType, EventEncoderButton &))
Set the Callback function to a class method.
Definition: EventEncoderButton.h:141
EventButton button
the EventButton onstance
Definition: EventEncoderButton.h:433
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:182
void resetPosition(int32_t pos=0)
Reset the counted position of the EventEncoderButton.
Definition: EventEncoderButton.h:353
void setMaxPressedPosition(int32_t maxPressedPosition=0)
Set an upper limit for the encoder pressed position.
Definition: EventEncoderButton.cpp:174
void onIdle() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventEncoderButton.h:430
void unsetCallback() override
Unset a previously set callback function or method.
Definition: EventEncoderButton.cpp:47
void setMinPressedPosition(int32_t minPressedPosition=0)
Set a lower limit for the encoder pressed position.
Definition: EventEncoderButton.cpp:166
void setDebouncer(DebounceAdapter *debounceAdapter)
Set the debouncer. Note: When planning to use setDebouncer() you must ensure useDefaultDebouncer is s...
Definition: EventEncoderButton.cpp:190
std::function< void(InputEventType et, EventEncoderButton &ie)> CallbackFunction
If std::function is supported, this creates the callback type.
Definition: EventEncoderButton.h:55
void setMaxPosition(int32_t maxPosition=0)
Set an upper limit for the encoder position.
Definition: EventEncoderButton.cpp:158
void wrapMinMaxPosition(bool wrap)
Wrap the max -> min -> max ... (default is to not wrap)
Definition: EventEncoderButton.h:277
int32_t pressedPosition()
The current pressed position of the encoder. Can be reset with resetPressedPosition()
Definition: EventEncoderButton.h:197
uint8_t longPressCount()
The number of times the long press has occurred during a button press.
Definition: EventEncoderButton.cpp:210
void resetState()
Resets the state of the input: silently enables if disabled, clears any blocked events,...
Definition: EventEncoderButton.cpp:212
void update()
Update the state from the underlying encoder library and button pin.
Definition: EventEncoderButton.cpp:52
uint8_t clickCount()
The number of clicks that have been fired in the MULTI_CLICKED event.
Definition: EventEncoderButton.cpp:204
void setLongClickDuration(uint16_t longDurationMs=750)
Set the number of milliseconds that define the first long click duration.
Definition: EventEncoderButton.cpp:198
int16_t increment()
Returns a positive (CW) or negative (CCW) integer.
Definition: EventEncoderButton.h:187
The EventEncoder class is for quadrature encoder inputs providing the position & encoder increment,...
Definition: EventEncoder.h:31
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
The interface specification for button, encoder button and switch pins.
Definition: PinAdapter.h:8