-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathESP32RotaryEncoder.h
More file actions
287 lines (243 loc) · 8.59 KB
/
ESP32RotaryEncoder.h
File metadata and controls
287 lines (243 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#ifndef _RotaryEncoder_h
#define _RotaryEncoder_h
#if defined( ARDUINO ) && ARDUINO >= 100
#include <Arduino.h>
#elif defined( WIRING )
#include <Wiring.h>
#else
#include <WProgram.h>
#include <pins_arduino.h>
#endif
#include <atomic>
class RotaryEncoder {
public:
static constexpr int8_t RE_DEFAULT_PIN = -1;
static constexpr uint8_t RE_DEFAULT_STEPS = 4;
static constexpr uint64_t RE_LOOP_INTERVAL = 100000U; // 0.1 seconds
typedef enum {
FLOATING,
HAS_PULLUP,
SW_FLOAT
} Type;
#if defined( ESP32 )
typedef std::function<void(long)> EncoderCallback;
typedef std::function<void(unsigned long)> ButtonCallback;
#else
typedef void (*EncoderCallback)(long);
typedef void (*ButtonCallback)(unsigned long);
#endif
/**
* @brief Construct a new Rotary Encoder instance
*
* @param encoderPinA The A pin on the encoder, sometimes marked "CLK"
* @param encoderPinB The B pin on the encoder, sometimes marked "DT"
* @param buttonPin Optional; the pushbutton pin, could be marked "SW"
* @param vccPin Optional; the voltage reference input, could be marked "+" or "V+" or "VCC"; defaults to -1, which is ignored
* @param encoderSteps Optional; the number of steps per detent; usually 4 (default), could be 2. A value of 0 will be imply the default.
*/
RotaryEncoder(
uint8_t encoderPinA,
uint8_t encoderPinB,
int8_t buttonPin = RE_DEFAULT_PIN,
int8_t vccPin = RE_DEFAULT_PIN,
uint8_t encoderSteps = RE_DEFAULT_STEPS
);
/**
* @brief Responsible for detaching interrupts and clearing the loop timer
*/
~RotaryEncoder();
/**
* @brief Enable the internal pull-up resistor for the encoder pin.
*
* By default, the encoder pin on the ESP32 is floating - which requires that
* the encoder module has its own pull-up resistors or external pull-ups are used.
*
* If the encoder module does not have its own pull-ups, calling this method
* will enable the internal pull-up in the ESP32.
*
* @note Call this before `begin()`. It has no effect after.
*/
void enableEncoderPinPullup() { encoderPinMode = INPUT_PULLUP; }
/**
* @brief Enable the internal pull-up resistor for the button pin.
*
* By default, the button pin on the ESP32 is floating - which requires that
* the button has its own pull-up resistors or external pull-ups are used.
*
* If the button does not have its own pull-ups, calling this method
* will enable the internal pull-up in the ESP32.
*
* @note Call this before `begin()`. It has no effect after.
*/
void enableButtonPinPullup() { buttonPinMode = INPUT_PULLUP; }
/**
* @brief Set the minimum and maximum values that the encoder will return.
*
* @note This is a convenience function that calls `setMinValue()`, `setMaxValue()`, and `setCircular()`
*
* @param minValue Minimum value (e.g. 0)
* @param maxValue Maximum value (e.g. 10)
* @param circleValues If true, turning past the maximum will wrap around to the minimum and vice-versa
* If false (default), turning past the minimum or maximum will return that boundary
*/
void setBoundaries( long minValue, long maxValue, bool circleValues = false );
/**
* @brief Set the minimum value that the encoder will return.
*
* @note Call this in `setup()`
*
* @param minValue Minimum value
*/
void setMinValue( long minValue );
/**
* @brief Set the maximum value that the encoder will return.
*
* @note Call this in `setup()`
*
* @param maxValue Maximum value
*/
void setMaxValue( long maxValue );
/**
* @brief Set whether the minimum or maximum value will wrap around to the other.
*
* @note Call this in `setup()`
*
* @param maxValue Maximum value
*/
void setCircular( bool circleValues );
/**
* @brief Set the amount of increment/decrement by which the value tracked by the encoder will change.
*
* @note Call this in `setup()`
*
* @param stepValue Step value
*/
void setStepValue( long stepValue );
/**
* @brief Set a function to fire every time the value tracked by the encoder changes.
*
* @note Call this in `setup()`. May be set/changed at runtime if needed.
*
* @param handler The function to call; it must accept one parameter
* of type long, which will be the current value
*/
void onTurned( EncoderCallback f );
/**
* @brief Set a function to fire every time the the pushbutton is pressed.
*
* @note Call this in `setup()`. May be set/changed at runtime if needed.
*
* @param handler The function to call; it must accept one parameter of type long, which
* will be the duration (in milliseconds) that the button was active
*/
void onPressed( ButtonCallback f );
/**
* @brief Sets up the GPIO pins specified in the constructor and attaches the ISR callback for the encoder.
*
* @note Call this in `setup()` after other "set" methods.
*/
void begin( bool useTimer = true );
/**
* @brief Enables the encoder knob and pushbutton if `disable()` was previously used.
*/
void enable();
/**
* @brief Disables the encoder knob and pushbutton.
*
* Knob rotation and button presses will have no effect until after `enable()` is called
*/
void disable();
/**
* @brief Confirms whether the encoder knob and pushbutton have been disabled.
*
*/
bool isEnabled();
/**
* @brief Check if the pushbutton has been pressed.
*
* @note Call this in `loop()` to fire a handler.
*
* @return true if the button was pressed since the last time it was checked,
* false if the button has not been pressed since the last time it was checked
*/
bool buttonPressed();
/**
* @brief Check if the value tracked by the encoder has changed.
*
* @note Call this in `loop()` to fire a handler for the new value.
*
* @return true if the value is different than the last time it was checked,
* false if the value is the same as the last time it was checked
*/
bool encoderChanged();
/**
* @brief Get the current value tracked by the encoder.
*
* @return A value between the minimum and maximum configured by `setBoundaries()`
*/
long getEncoderValue();
/**
* @brief Override the value tracked by the encoder.
*
* @note If the new value is outside the minimum or maximum configured
* by `setBoundaries()`, it will be adjusted accordingly
*
* @param newValue
*/
void setEncoderValue( long newValue );
/**
* @brief Reset the value tracked by the encoder.
*
* @note This will try to set the value to 0, but if the minimum and maximum configured
* by `setBoundaries()` does not include 0, then the minimum or maximum will be
* used instead
*/
void resetEncoderValue() { setEncoderValue( 0 ); }
private:
const char *LOG_TAG = "ESP32RotaryEncoder";
typedef enum {
LEFT = -1,
STILL = 0,
RIGHT = 1
} Rotation;
Rotation encoderStates[16] = {
STILL, LEFT, RIGHT, STILL,
RIGHT, STILL, STILL, LEFT,
LEFT, STILL, STILL, RIGHT,
STILL, RIGHT, LEFT, STILL
};
mutable portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
EncoderCallback callbackEncoderChanged = NULL;
ButtonCallback callbackButtonPressed = NULL;
int encoderPinMode = INPUT;
int buttonPinMode = INPUT;
const uint8_t encoderPinA;
const uint8_t encoderPinB;
const int8_t buttonPin;
const int8_t vccPin;
const uint8_t encoderSteps;
std::atomic<bool> _isEnabled{true};
long minEncoderValue = -1;
long maxEncoderValue = 1;
bool circleValues = false;
long stepValue = 1;
long currentValue;
unsigned long _lastRotaryInterruptTime;
uint8_t _previousAB;
int8_t _encoderPosition;
bool encoderChangedFlag;
long constrainValue( long value ) const;
bool buttonPressedFlag;
unsigned long _lastButtonInterruptTime;
unsigned long buttonPressedTime;
unsigned long buttonPressedDuration;
esp_timer_handle_t loopTimer;
void beginLoopTimer();
static void timerCallback( void *self );
void loop();
void attachInterrupts();
void detachInterrupts();
void _encoder_ISR();
void _button_ISR();
};
#endif