InputEvents v1.4.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 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
165
167
179 int16_t increment() { return currentIncrement; }
180
184 int32_t position() { return currentPosition; }
185
189 int32_t pressedPosition() { return currentPressedPosition; }
190
197 uint8_t clickCount();
198
199
206 uint8_t longPressCount();
207
211 bool isPressed();
212
218 uint32_t currentDuration();
219
225 uint32_t previousDuration();
226
228
230
249 void setMinPosition(int32_t minPosition=0);
250
258 void setMaxPosition(int32_t maxPosition=0);
259
269 void wrapMinMaxPosition(bool wrap) { wrapMinMaxPos = wrap; }
270
278 void setMinPressedPosition(int32_t minPressedPosition=0);
288 void setMaxPressedPosition(int32_t maxPressedPosition=0);
289
299 void wrapMinMaxPressedPosition(bool wrap) { wrapMinMaxPressedPos = wrap; }
301
303
318 void setRateLimit(long ms);
319
329 void setPositionDivider(uint8_t divider=4);
330
336 uint8_t getPositionDivider();
337
338
345 void resetPosition(int32_t pos = 0) {
346 currentPosition = pos;
347 previousPosition = currentPosition;
348 }
349
356 void resetPressedPosition(int32_t pos) {
357 currentPressedPosition = pos;
358 previousPressedPosition = currentPressedPosition;
359 }
360
366 void enableLongPressRepeat(bool repeat=true);
367
374 void setLongClickDuration(uint16_t longDurationMs=750);
375
382 void setLongPressInterval(uint16_t intervalMs=500);
383
389 void setMultiClickInterval(uint16_t intervalMs=250);
390
396 void setDebouncer(DebounceAdapter* debounceAdapter);
397
404 bool setDebounceInterval(uint16_t intervalMs=10);
405
415 void setPressedState(bool state = LOW);
417
418protected:
419 void invoke(InputEventType et) override;
420 void onEnabled() override;
421 void onDisabled() override;
422 void onIdle() override {/* Do nothing. Fire idle callback from either encoder or button but only if both are idle.*/ }
423
430
431private:
432 int16_t currentIncrement = 0;
433
434 int32_t currentPosition = 0;
435 int32_t previousPosition = 0;
436
437 int32_t currentPressedPosition = 0;
438 int32_t previousPressedPosition = 0;
439
440 bool encodingPressed = false;
441 uint16_t encodingPressedCount = 0;
442
443 int32_t minPos=0;
444 int32_t maxPos=0;
445 bool wrapMinMaxPos = false;
446
447 int32_t minPressedPos=0;
448 int32_t maxPressedPos=0;
449 bool wrapMinMaxPressedPos = false;
450
451 void setCallbacks();
452 bool onEncoderChanged();
453
455#ifndef FUNCTIONAL_SUPPORTED
456private:
457 static void encoderCallback(InputEventType et, EventEncoder &enc) {
458 EventEncoderButton *instance = static_cast<EventEncoderButton *>(enc.getOwner());
459 if (instance) instance->onInputCallback(et, enc);
460 }
461
462 static void buttonCallback(InputEventType et, EventButton &btn) {
463 EventEncoderButton *instance = static_cast<EventEncoderButton *>(btn.getOwner());
464 if (instance) instance->onInputCallback(et, btn);
465 }
466#endif
468
469};
470
471
472#endif
473#endif
474
Contains InputEventType enums and some defines.
InputEventType
A list of all events that can be fired by InputEvents classes.
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:184
void resetPressedPosition(int32_t pos)
Reset the counted pressed position of the EventEncoderButton.
Definition: EventEncoderButton.h:356
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:299
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:424
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:425
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:345
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:422
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:269
int32_t pressedPosition()
The current pressed position of the encoder. Can be reset with resetPressedPosition()
Definition: EventEncoderButton.h:189
uint8_t longPressCount()
The number of times the long press has occurred during a button press.
Definition: EventEncoderButton.cpp:210
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:179
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:184
The interface specification for button, encoder button and switch pins.
Definition: PinAdapter.h:8