InputEvents v1.6.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
EventEncoder.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#ifndef EVENT_ENCODER_H
12#define EVENT_ENCODER_H
13
14#include "Arduino.h"
15#include "EventInputBase.h"
16#include <EncoderAdapter/IEncoderAdapter.h>
17
30
31 protected:
32
33 #if defined(FUNCTIONAL_SUPPORTED)
37 typedef std::function<void(InputEventType et, EventEncoder &ie)> CallbackFunction;
38 #else
43 #endif
44
49
53 void readIncrement();
54
55public:
56
58
71 EventEncoder(IEncoderAdapter *encoderAdapter);
72
78
80
92 void begin();
93
101 callbackIsSet = true;
102 }
103
113 #if defined(FUNCTIONAL_SUPPORTED)
114 // Method to set callback with instance and class method
115 template <typename T>
116 void setCallback(T* instance, void (T::*method)(InputEventType, EventEncoder&)) {
117 // Wrap the method call in a lambda
118 callbackFunction = [instance, method](InputEventType et, EventEncoder &ie) {
119 (instance->*method)(et, ie); // Call the member function on the instance
120 };
121 callbackIsSet = true;
122 }
123 #endif
124
130 void unsetCallback() override;
131
137 void update();
139
141
153 int16_t increment() { return encoderIncrement; }
154
158 long position() { return currentPosition; }
160
162
174 void setRateLimit(long ms=0) { rateLimit = ms; }
175
185 void setPositionDivider(uint8_t divider=4) {
186 if ( divider > 0 ) {
187 positionDivider = divider;
188 }
189 }
190
196 uint8_t getPositionDivider() { return positionDivider; }
197
198
203 void resetPosition(long pos = 0) { currentPosition = pos; }
205
206protected:
207
208 void invoke(InputEventType et) override;
209 void onEnabled() override;
210
211private:
212
213 IEncoderAdapter *encoder;
214
215 uint8_t positionDivider = 4;
216 int32_t currentPosition = 0;
217 int32_t oldPosition = 0;
218 unsigned int rateLimit = 0;
219 unsigned long rateLimitCounter = 0;
220 int encoderIncrement = 0;
221
222};
223
224#endif
Contains InputEventType enums and some defines.
InputEventType
The size of the InputEventType enum.
Definition: InputEvents.h:46
The EventEncoder class is for quadrature encoder inputs providing the position & encoder increment,...
Definition: EventEncoder.h:29
void readIncrement()
Read and set the increment during update()
Definition: EventEncoder.cpp:66
long position()
The current position of the encoder. Can be reset with resetPosition()
Definition: EventEncoder.h:158
EventEncoder(IEncoderAdapter *encoderAdapter)
Construct an EventEncoder input from an IEncoderAdapter.
Definition: EventEncoder.cpp:18
void update()
Update the state from the underlying encoder library.
Definition: EventEncoder.cpp:50
void setPositionDivider(uint8_t divider=4)
Quadrature encoders usually have four states for each 'click' of the rotary control,...
Definition: EventEncoder.h:185
CallbackFunction callbackFunction
The callback function member.
Definition: EventEncoder.h:48
void setCallback(T *instance, void(T::*method)(InputEventType, EventEncoder &))
Set the Callback function to a class method.
Definition: EventEncoder.h:116
void onEnabled() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventEncoder.cpp:37
std::function< void(InputEventType et, EventEncoder &ie)> CallbackFunction
If std::function is supported, this creates the callback type.
Definition: EventEncoder.h:37
void invoke(InputEventType et) override
To be overriden by derived classes.
Definition: EventEncoder.cpp:30
void unsetCallback() override
Unset a previously set callback function or method.
Definition: EventEncoder.cpp:45
int16_t increment()
Returns a positive (CW) or negative (CCW) integer.
Definition: EventEncoder.h:153
void begin()
Initialise the EventEncoder.
Definition: EventEncoder.cpp:26
uint8_t getPositionDivider()
Get the currently set position divider value.
Definition: EventEncoder.h:196
void resetPosition(long pos=0)
Reset the counted position of the encoder.
Definition: EventEncoder.h:203
void setCallback(CallbackFunction f)
Set the Callback function.
Definition: EventEncoder.h:99
~EventEncoder()
Destroy the EventEncoder input.
Definition: EventEncoder.cpp:22
void setRateLimit(long ms=0)
Encoder callbacks are normally fired on every loop() but for MPG style encoders this can fire a huge ...
Definition: EventEncoder.h:174
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