InputEvents v1.4.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
EventAnalog.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
10#ifndef EVENT_ANALOG_H
11#define EVENT_ANALOG_H
12
13#include "Arduino.h"
14#include "EventInputBase.h"
15
31
32protected:
33
34 #if defined(FUNCTIONAL_SUPPORTED)
38 typedef std::function<void(InputEventType et, EventAnalog &ie)> CallbackFunction;
39 #else
44 #endif
45
50
56 void invoke(InputEventType et) override;
57
58public:
59
61
70 EventAnalog(byte analogPin, uint8_t adcBits=10);
72
73
75
85 void begin();
86
94 callbackIsSet = true;
95 }
96
105 #if defined(FUNCTIONAL_SUPPORTED)
106 // Method to set callback with instance and class method
107 template <typename T>
108 void setCallback(T* instance, void (T::*method)(InputEventType, EventAnalog&)) {
109 // Wrap the method call in a lambda
110 callbackFunction = [instance, method](InputEventType et, EventAnalog &ie) {
111 (instance->*method)(et, ie); // Call the member function on the instance
112 };
113 callbackIsSet = true;
114 }
115 #endif
116
121 void unsetCallback() override;
122
126 void update();
130
141 int16_t position() { return _reversePosition ? currentPos*-1 : currentPos; }
142
148 int16_t previousPosition() { return _reversePosition ? previousPos*-1 : previousPos; }
149
150
157 bool hasChanged() { return _hasChanged; }
159
160
161
163
177 void setNumIncrements(uint8_t numIncr=10);
178
184 void setNumNegativeIncrements(uint8_t numIncr=10);
185
191 void setNumPositiveIncrements(uint8_t numIncr=10);
193
194
196
207 void setStartValue(uint16_t val);
208
213 void setStartValue();
214
222 void setMinValue(uint16_t val);
223
231 void setMaxValue(uint16_t val);
233
235
247 void setStartBoundary(uint16_t width=200);
248
256 void setEndBoundary(uint16_t width=100);
258
260
269 void setRateLimit(uint16_t ms) { rateLimit = ms; }
270
277 void enableAutoCalibrate(bool enable=true) { autoCalibrate = enable; }
278
285 void reversePosition(bool rev=true) { _reversePosition = rev; }
286
293 bool isPositionReversed() { return _reversePosition; }
295
296 protected:
297
301 void setSliceNeg() { sliceNeg = max(((startVal-startBoundary-minVal-endBoundary)/negativeIncrements),1); }//Never allow 0
302
306 void setSlicePos() { slicePos = max(((maxVal-endBoundary-startBoundary-startVal)/positiveIncrements),1); }//Never allow 0
307
308private:
309 byte analogPin = 0;
310 int16_t startVal = 0;
311 int16_t readVal = startVal;
312 int16_t currentVal = startVal;
313 int16_t previousVal = currentVal;
314 int16_t minVal = 100;
315 int16_t maxVal = 980;
316 int16_t startBoundary = 0;
317 int16_t endBoundary = 0;
318 int16_t adcMax = 1023;
319
320
321 int16_t negativeIncrements = 25;
322 int16_t positiveIncrements = 25;
323 int16_t sliceNeg = 20;
324 int16_t slicePos = 20;
325
326 int16_t readPos = 0;
327 int16_t currentPos = 0;
328 int16_t previousPos = 0;
329 bool _reversePosition = false;
330
331 bool autoCalibrate = true;
332 bool _hasChanged = false;
333 bool _started = false;
334
335 uint16_t rateLimit = 0;
336 unsigned long rateLimitCounter = 0;
337
338 void setReadPos(int16_t offset);
339 void setInitialReadPos();
340
341
342
343
344};
345
346#endif
347
348
349
350
InputEventType
A list of all events that can be fired by InputEvents classes.
Definition: InputEvents.h:55
The EventAnalog class is for analog inputs - slice an analog range into configurable number of increm...
Definition: EventAnalog.h:30
void setRateLimit(uint16_t ms)
Limit the rate at which events are fired.
Definition: EventAnalog.h:269
void setSlicePos()
Set the size of each positive slice/increment.
Definition: EventAnalog.h:306
void setSliceNeg()
Set the size of each negative slice/increment.
Definition: EventAnalog.h:301
int16_t previousPosition()
Returns the previous position - this is not the analog value but the mapped position of the increment...
Definition: EventAnalog.h:148
void invoke(InputEventType et) override
Override of the EventInputBase::invoke() virtual method.
Definition: EventAnalog.cpp:35
void setNumPositiveIncrements(uint8_t numIncr=10)
Normally increments are set with setNumIncrements() but you can also set the negative and positive si...
Definition: EventAnalog.cpp:150
void setMaxValue(uint16_t val)
The maximmum ADC value that can be read by the analog pin.
Definition: EventAnalog.cpp:118
void unsetCallback() override
Unset a previously set callback function or method.
Definition: EventAnalog.cpp:30
void setNumIncrements(uint8_t numIncr=10)
Split the analog range into this number of slices. The default is 25.
Definition: EventAnalog.cpp:138
void setCallback(T *instance, void(T::*method)(InputEventType, EventAnalog &))
Set the Callback function to a class method.
Definition: EventAnalog.h:108
std::function< void(InputEventType et, EventAnalog &ie)> CallbackFunction
If std::function is supported, this creates the callback type.
Definition: EventAnalog.h:38
void update()
Update the state from the analog input. Must be called from within loop() in order to update state fr...
Definition: EventAnalog.cpp:41
int16_t position()
Returns the current position - this is not the analog value but the mapped position of the increments...
Definition: EventAnalog.h:141
void setEndBoundary(uint16_t width=100)
Used primarily for joysticks.
Definition: EventAnalog.cpp:133
void reversePosition(bool rev=true)
Reverse the increments.
Definition: EventAnalog.h:285
bool hasChanged()
Returns true if the position has changed since previous update()
Definition: EventAnalog.h:157
void setCallback(CallbackFunction f)
Set the Callback function.
Definition: EventAnalog.h:92
void setMinValue(uint16_t val)
The minimum ADC value that can be read by the analog pin.
Definition: EventAnalog.cpp:113
void enableAutoCalibrate(bool enable=true)
If enableAutoCalibrate is set to true (the default), will do auto calibration, setting the minValue a...
Definition: EventAnalog.h:277
EventAnalog(byte analogPin, uint8_t adcBits=10)
Construct an EventAnalog input.
Definition: EventAnalog.cpp:12
CallbackFunction callbackFunction
The callback function member.
Definition: EventAnalog.h:49
void setNumNegativeIncrements(uint8_t numIncr=10)
Normally increments are set with setNumIncrements() but you can also set the negative and positive si...
Definition: EventAnalog.cpp:145
bool isPositionReversed()
Returns true if position is reversed.
Definition: EventAnalog.h:293
void setStartBoundary(uint16_t width=200)
Used primarily for joysticks.
Definition: EventAnalog.cpp:128
void begin()
Initialise the EventAnalog. Must be called from within setup()
Definition: EventAnalog.cpp:19
void setStartValue()
Will set the start value as the current position.
Definition: EventAnalog.cpp:123
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
void enable(bool e=true)
Enable or disable an input.
Definition: EventInputBase.cpp:73