InputEvents v1.4.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 EXCLUDE_EVENT_ENCODER
12
13#ifndef EVENT_ENCODER_H
14#define EVENT_ENCODER_H
15
16#include "Arduino.h"
17#include "EventInputBase.h"
18#include <EncoderAdapter.h>
19
32
33 protected:
34
35 #if defined(FUNCTIONAL_SUPPORTED)
39 typedef std::function<void(InputEventType et, EventEncoder &ie)> CallbackFunction;
40 #else
45 #endif
46
51
55 void readIncrement();
56
57public:
58
60
73 EventEncoder(EncoderAdapter *encoderAdapter);
74
80
82
94 void begin();
95
103 callbackIsSet = true;
104 }
105
115 #if defined(FUNCTIONAL_SUPPORTED)
116 // Method to set callback with instance and class method
117 template <typename T>
118 void setCallback(T* instance, void (T::*method)(InputEventType, EventEncoder&)) {
119 // Wrap the method call in a lambda
120 callbackFunction = [instance, method](InputEventType et, EventEncoder &ie) {
121 (instance->*method)(et, ie); // Call the member function on the instance
122 };
123 callbackIsSet = true;
124 }
125 #endif
126
132 void unsetCallback() override;
133
139 void update();
141
143
155 int16_t increment() { return encoderIncrement; }
156
160 long position() { return currentPosition; }
162
164
176 void setRateLimit(long ms=0) { rateLimit = ms; }
177
187 void setPositionDivider(uint8_t divider=4) {
188 if ( divider > 0 ) {
189 positionDivider = divider;
190 }
191 }
192
198 uint8_t getPositionDivider() { return positionDivider; }
199
200
205 void resetPosition(long pos = 0) { currentPosition = pos; }
207
208protected:
209
210 void invoke(InputEventType et) override;
211 void onEnabled() override;
212
213private:
214
215 EncoderAdapter *encoder;
216
217 uint8_t positionDivider = 4;
218 int32_t currentPosition = 0;
219 int32_t oldPosition = 0;
220 unsigned int rateLimit = 0;
221 unsigned long rateLimitCounter = 0;
222 int encoderIncrement = 0;
223
224};
225
226#endif
227
228#endif
Contains InputEventType enums and some defines.
InputEventType
A list of all events that can be fired by InputEvents classes.
Definition: InputEvents.h:55
The EventEncoder class is for quadrature encoder inputs providing the position & encoder increment,...
Definition: EventEncoder.h:31
void readIncrement()
Read and set the increment during update()
Definition: EventEncoder.cpp:71
long position()
The current position of the encoder. Can be reset with resetPosition()
Definition: EventEncoder.h:160
void update()
Update the state from the underlying encoder library.
Definition: EventEncoder.cpp:55
void setPositionDivider(uint8_t divider=4)
Quadrature encoders usually have four states for each 'click' of the rotary control,...
Definition: EventEncoder.h:187
CallbackFunction callbackFunction
The callback function member.
Definition: EventEncoder.h:50
void setCallback(T *instance, void(T::*method)(InputEventType, EventEncoder &))
Set the Callback function to a class method.
Definition: EventEncoder.h:118
void onEnabled() override
Can be ovrriden by derived classes but base method must be called.
Definition: EventEncoder.cpp:42
std::function< void(InputEventType et, EventEncoder &ie)> CallbackFunction
If std::function is supported, this creates the callback type.
Definition: EventEncoder.h:39
void invoke(InputEventType et) override
To be overriden by derived classes.
Definition: EventEncoder.cpp:35
void unsetCallback() override
Unset a previously set callback function or method.
Definition: EventEncoder.cpp:50
int16_t increment()
Returns a positive (CW) or negative (CCW) integer.
Definition: EventEncoder.h:155
void begin()
Initialise the EventEncoder.
Definition: EventEncoder.cpp:31
uint8_t getPositionDivider()
Get the currently set position divider value.
Definition: EventEncoder.h:198
void resetPosition(long pos=0)
Reset the counted position of the encoder.
Definition: EventEncoder.h:205
void setCallback(CallbackFunction f)
Set the Callback function.
Definition: EventEncoder.h:101
EventEncoder(EncoderAdapter *encoderAdapter)
Construct an EventEncoder input from an EncoderAdapter
Definition: EventEncoder.cpp:23
~EventEncoder()
Destroy the EventEncoder input.
Definition: EventEncoder.cpp:27
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:176
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