-
Notifications
You must be signed in to change notification settings - Fork 16
/
NewEncoder.cpp
424 lines (378 loc) · 11.2 KB
/
NewEncoder.cpp
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* NewEncoder.cpp
*/
#include "NewEncoder.h"
#define A_PIN_FALLING 0b00
#define A_PIN_RISING 0b01
#define B_PIN_FALLING 0b10
#define B_PIN_FALLING 0b10
#define B_PIN_RISING 0b11
// Define states and transition table for "one pulse per detent" type encoder
#define START_STATE 0b011
#define CW_STATE_1 0b010
#define CW_STATE_2 0b000
#define CW_STATE_3 0b001
#define CCW_STATE_1 0b101
#define CCW_STATE_2 0b100
#define CCW_STATE_3 0b110
const NewEncoder::encoderStateTransition NewEncoder::fullPulseTransitionTable[] = {
{ CW_STATE_2, CW_STATE_3, CW_STATE_2, CW_STATE_1 }, // cwState2 = 0b000
{ CW_STATE_2, CW_STATE_3, CW_STATE_3, START_STATE | INCREMENT_DELTA }, // cwState3 = 0b001
{ CW_STATE_1, START_STATE, CW_STATE_2, CW_STATE_1 }, // cwState1 = 0b010
{ CW_STATE_1, START_STATE, CCW_STATE_1, START_STATE }, // startState = 0b011
{ CCW_STATE_2, CCW_STATE_1, CCW_STATE_2, CCW_STATE_3 }, // ccwState2 = 0b100
{ CCW_STATE_2, CCW_STATE_1, CCW_STATE_1, START_STATE }, // ccwState1 = 0b101
{ CCW_STATE_3, START_STATE | DECREMENT_DELTA, CCW_STATE_2, CCW_STATE_3 }, // ccwState3 = 0b110
{ START_STATE, START_STATE, START_STATE, START_STATE } // 0b111 illegal state should never be in it
};
// Define states and transition table for "one pulse per two detents" type encoder
#define DETENT_0 0b000
#define DETENT_1 0b111
#define DEBOUNCE_0 0b010
#define DEBOUNCE_1 0b001
#define DEBOUNCE_2 0b101
#define DEBOUNCE_3 0b110
const NewEncoder::encoderStateTransition NewEncoder::halfPulseTransitionTable[] = {
{ DETENT_0, DEBOUNCE_1, DETENT_0, DEBOUNCE_0 }, // DETENT_0 0b000
{ DETENT_0, DEBOUNCE_1, DEBOUNCE_1, DETENT_1 | INCREMENT_DELTA }, // DEBOUNCE_1 0b001
{ DEBOUNCE_0, DETENT_1 | DECREMENT_DELTA, DETENT_0, DEBOUNCE_0 }, // DEBOUNCE_0 0b010
{ DETENT_1, DETENT_1, DETENT_1, DETENT_1 }, // 0b011 - illegal state should never be in it
{ DETENT_0, DETENT_0, DETENT_0, DETENT_0 }, // 0b100 - illegal state should never be in it
{ DETENT_0 | DECREMENT_DELTA, DEBOUNCE_2, DEBOUNCE_2, DETENT_1 }, // DEBOUNCE_2 0b101
{ DEBOUNCE_3, DETENT_1, DETENT_0 | INCREMENT_DELTA, DEBOUNCE_3 }, // DEBOUNCE_3 0b110
{ DEBOUNCE_3, DETENT_1, DEBOUNCE_2, DETENT_1 } // DETENT_1 0b111
};
#ifndef USE_FUNCTIONAL_ISR
NewEncoder::isrInfo NewEncoder::_isrTable[CORE_NUM_INTERRUPT];
#endif
NewEncoder::NewEncoder(uint8_t aPin, uint8_t bPin, int16_t minValue,
int16_t maxValue, int16_t initalValue, uint8_t type) {
active = false;
configure(aPin, bPin, minValue, maxValue, initalValue, type);
}
NewEncoder::NewEncoder() {
active = false;
configured = false;
_aPin_register = nullptr;
_bPin_register = nullptr;
}
NewEncoder::~NewEncoder() {
end();
}
void NewEncoder::end() {
if (!active) {
return;
}
active = false;
int16_t _interruptA = digitalPinToInterrupt(_aPin);
int16_t _interruptB = digitalPinToInterrupt(_bPin);
detachInterrupt(_interruptA);
detachInterrupt(_interruptB);
}
void NewEncoder::configure(uint8_t aPin, uint8_t bPin, int16_t minValue,
int16_t maxValue, int16_t initalValue, uint8_t type) {
if (active) {
end();
}
_aPin = aPin;
_bPin = bPin;
_minValue = minValue;
_maxValue = maxValue;
_aPin_register = PIN_TO_BASEREG(aPin);
_bPin_register = PIN_TO_BASEREG(bPin);
_aPin_bitmask = PIN_TO_BITMASK(aPin);
_bPin_bitmask = PIN_TO_BITMASK(bPin);
if (initalValue > _maxValue) {
initalValue = _maxValue;
} else if (initalValue < _minValue) {
initalValue = _minValue;
}
liveState.currentValue = initalValue;
liveState.currentClick = NoClick;
memcpy((void*) &localState, (void*) &liveState, sizeof(EncoderState));
stateChanged = false;
if (type == HALF_PULSE) {
tablePtr = halfPulseTransitionTable;
} else {
tablePtr = fullPulseTransitionTable;
}
configured = true;
}
bool NewEncoder::begin() {
if (active) {
return false;
}
if (!configured) {
return false;
}
if (_aPin == _bPin) {
return false;
}
using InterruptNumberType = decltype(NOT_AN_INTERRUPT);
InterruptNumberType _interruptA = static_cast<InterruptNumberType>(digitalPinToInterrupt(_aPin));
InterruptNumberType _interruptB = static_cast<InterruptNumberType>(digitalPinToInterrupt(_bPin));
if (_interruptA == _interruptB) {
return false;
}
if (_interruptA == NOT_AN_INTERRUPT) {
return false;
}
if (_interruptB == NOT_AN_INTERRUPT) {
return false;
}
if (_minValue >= _maxValue) {
return false;
}
pinMode(_aPin, INPUT_PULLUP);
pinMode(_bPin, INPUT_PULLUP);
delay(2); // Seems to help ensure first reading after pinMode is correct
_aPinValue = DIRECT_PIN_READ(_aPin_register, _aPin_bitmask);
_bPinValue = DIRECT_PIN_READ(_bPin_register, _bPin_bitmask);
currentStateVariable = (_bPinValue << 1) | _aPinValue;
if ((tablePtr == halfPulseTransitionTable) && (currentStateVariable == (DETENT_1 & 0b11))) {
currentStateVariable = DETENT_1;
}
#ifndef USE_FUNCTIONAL_ISR
_isrTable[_interruptA].objectPtr = this;
_isrTable[_interruptA].functPtr = &NewEncoder::aPinChange;
auto isrA = getIsr(_interruptA);
if (isrA == nullptr) {
return false;
}
_isrTable[_interruptB].objectPtr = this;
_isrTable[_interruptB].functPtr = &NewEncoder::bPinChange;
auto isrB = getIsr(_interruptB);
if (isrB == nullptr) {
return false;
}
attachInterrupt(_interruptA, isrA, CHANGE);
attachInterrupt(_interruptB, isrB, CHANGE);
#else
auto aPinIsr = [this] {
this->aPinChange();
};
attachInterrupt(_interruptA, aPinIsr, CHANGE);
auto bPinIsr = [this] {
this->bPinChange();
};
attachInterrupt(_interruptB, bPinIsr, CHANGE);
#endif
active = true;
return true;
}
bool NewEncoder::getState(EncoderState &state) {
bool localStateChanged = stateChanged;
if (localStateChanged) {
noInterrupts();
memcpy((void*) &localState, (void*) &liveState, sizeof(EncoderState));
stateChanged = false;
interrupts();
} else {
localState.currentClick = NoClick;
}
memcpy((void*) &state, (void*) &localState, sizeof(EncoderState));
return localStateChanged;
}
bool NewEncoder::getAndSet(int16_t val, EncoderState &Oldstate, EncoderState &Newstate) {
bool changed;
if (val < _minValue) {
val = _minValue;
} else if (val > _maxValue) {
val = _maxValue;
}
noInterrupts();
changed = stateChanged;
stateChanged = false;
memcpy((void*) &Oldstate, (void*) &liveState, sizeof(EncoderState));
if (!changed) {
Oldstate.currentClick = NoClick;
}
liveState.currentValue = val;
liveState.currentClick = NoClick;
memcpy((void*) &localState, (void*) &liveState, sizeof(EncoderState));
interrupts();
memcpy((void*) &Newstate, (void*) &localState, sizeof(EncoderState));
return changed;
}
bool NewEncoder::newSettings(int16_t newMin, int16_t newMax, int16_t newCurrent, EncoderState &state) {
if (newMax <= newMin) {
return false;
}
if (newCurrent < newMin) {
newCurrent = newMin;
}
if (newCurrent > newMax) {
newCurrent = newMax;
}
noInterrupts();
stateChanged = false;
liveState.currentValue = newCurrent;
liveState.currentClick = NoClick;
_minValue = newMin;
_maxValue = newMax;
memcpy((void*) &localState, (void*) &liveState, sizeof(EncoderState));
interrupts();
memcpy((void*) &state, (void*) &localState, sizeof(EncoderState));
return true;
}
bool NewEncoder::enabled() const {
return active;
}
void NewEncoder::attachCallback(EncoderCallBack cback, void *uPtr) {
callBackPtr = cback;
userPointer = uPtr;
}
int16_t NewEncoder::setValue(int16_t val) {
if (val < _minValue) {
val = _minValue;
} else if (val > _maxValue) {
val = _maxValue;
}
#if defined(__AVR__)
noInterrupts(); // 16-bit access not atomic on 8-bit processor
#endif
liveState.currentValue = val;
#if defined(__AVR__)
interrupts();
#endif
return val;
}
int16_t NewEncoder::operator =(int16_t val) {
if (val < _minValue) {
val = _minValue;
} else if (val > _maxValue) {
val = _maxValue;
}
#if defined(__AVR__)
noInterrupts(); // 16-bit access not atomic on 8-bit processor
#endif
liveState.currentValue = val;
#if defined(__AVR__)
interrupts();
#endif
return val;
}
int16_t NewEncoder::getValue() {
#if defined(__AVR__)
int16_t val;
noInterrupts(); // 16-bit access not atomic on 8-bit processor
val = liveState.currentValue;
interrupts();
return val;
#else
return liveState.currentValue;
#endif
}
NewEncoder::operator int16_t() const {
#if defined(__AVR__)
int16_t val;
noInterrupts(); // 16-bit access not atomic on 8-bit processor
val = liveState.currentValue;
interrupts();
return val;
#else
return liveState.currentValue;
#endif
}
int16_t NewEncoder::getAndSet(int16_t val) {
int16_t localCurrentValue;
if (val < _minValue) {
val = _minValue;
} else if (val > _maxValue) {
val = _maxValue;
}
noInterrupts()
;
localCurrentValue = liveState.currentValue;
liveState.currentValue = val;
interrupts()
;
return localCurrentValue;
}
bool NewEncoder::upClick() {
if (clickUp) {
clickUp = false;
return true;
} else {
return false;
}
}
bool NewEncoder::downClick() {
if (clickDown) {
clickDown = false;
return true;
} else {
return false;
}
}
bool NewEncoder::newSettings(int16_t newMin, int16_t newMax, int16_t newCurrent) {
bool success = false;
#if defined(__AVR__)
noInterrupts(); // 16-bit access not atomic on 8-bit processor
#endif
if (active) {
if (newMax > newMin) {
if (newCurrent < newMin) {
newCurrent = newMin;
} else if (newCurrent > newMax) {
newCurrent = newMax;
}
liveState.currentValue = newCurrent;
_minValue = newMin;
_maxValue = newMax;
success = true;
}
}
#if defined(__AVR__)
interrupts();
#endif
return success;
}
void ESP_ISR NewEncoder::aPinChange() {
uint8_t newPinValue = DIRECT_PIN_READ(_aPin_register, _aPin_bitmask);
if (newPinValue == _aPinValue) {
return;
}
_aPinValue = newPinValue;
pinChangeHandler(0b00 | _aPinValue); // Falling aPin == 0b00, Rising aPin = 0b01;
}
void ESP_ISR NewEncoder::bPinChange() {
uint8_t newPinValue = DIRECT_PIN_READ(_bPin_register, _bPin_bitmask);
if (newPinValue == _bPinValue) {
return;
}
_bPinValue = newPinValue;
pinChangeHandler(0b10 | _bPinValue); // Falling bPin == 0b10, Rising bPin = 0b11;
}
void ESP_ISR NewEncoder::pinChangeHandler(uint8_t index) {
uint8_t newStateVariable;
newStateVariable = NewEncoder::tablePtr[currentStateVariable][index];
currentStateVariable = newStateVariable & STATE_MASK;
if ((newStateVariable & DELTA_MASK) != 0) {
if ((newStateVariable & DELTA_MASK) == INCREMENT_DELTA) {
clickUp = true;
clickDown = false;
} else {
clickUp = false;
clickDown = true;
}
updateValue(newStateVariable);
if (callBackPtr != nullptr) {
callBackPtr(this, &liveState, userPointer);
}
}
}
void ESP_ISR NewEncoder::updateValue(uint8_t updatedStateVariable) {
if ((updatedStateVariable & DELTA_MASK) == INCREMENT_DELTA) {
liveState.currentClick = UpClick;
if (liveState.currentValue < _maxValue) {
liveState.currentValue++;
}
} else if ((updatedStateVariable & DELTA_MASK) == DECREMENT_DELTA) {
liveState.currentClick = DownClick;
if (liveState.currentValue > _minValue) {
liveState.currentValue--;
}
}
stateChanged = true;
}