forked from soligen2010/encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClickEncoder.cpp
313 lines (242 loc) · 8.42 KB
/
ClickEncoder.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
// ----------------------------------------------------------------------------
// Rotary Encoder Driver with Acceleration
// Supports Click, DoubleClick, Long Click
//
// (c) 2010 [email protected]
// (c) 2014 [email protected]
//
// Timer-based rotary encoder logic by Peter Dannegger
// http://www.mikrocontroller.net/articles/Drehgeber
// ----------------------------------------------------------------------------
#include "ClickEncoder.h"
// ----------------------------------------------------------------------------
// Button configuration (values for 1ms timer service calls)
//
#define ENC_BUTTONINTERVAL 10 // check button every x milliseconds, also debouce time
// ----------------------------------------------------------------------------
// Acceleration configuration (for 1000Hz calls to ::service())
//
#define ENC_ACCEL_TOP 3072 // max. acceleration: *12 (val >> 8)
#define ENC_ACCEL_INC 25
#define ENC_ACCEL_DEC 2
// ----------------------------------------------------------------------------
#if ENC_DECODER != ENC_NORMAL
# if ENC_HALFSTEP
// decoding table for hardware with flaky notch (half resolution)
const int8_t ClickEncoder::table[16] __attribute__((__progmem__)) = {
0, 0, -1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, -1, 0, 0
};
# else
// decoding table for normal hardware
const int8_t ClickEncoder::table[16] __attribute__((__progmem__)) = {
0, 1, -1, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, -1, 1, 0
};
# endif
#endif
// ----------------------------------------------------------------------------
ClickEncoder::ClickEncoder(int8_t A, int8_t B, int8_t BTN, uint8_t stepsPerNotch, bool active)
: doubleClickEnabled(true),buttonHeldEnabled(true), accelerationEnabled(true),
delta(0), last(0), acceleration(0),
button(Open), steps(stepsPerNotch),
pinA(A), pinB(B), pinBTN(BTN), pinsActive(active)
#ifndef WITHOUT_BUTTON
, analogInput(false)
#endif
{
pinMode_t configType = (pinsActive == LOW) ? INPUT_PULLUP : INPUT;
if (pinA >= 0) {pinMode(pinA, configType);}
if (pinB >= 0) {pinMode(pinB, configType);}
#ifndef WITHOUT_BUTTON
if (pinBTN >= 0) {pinMode(pinBTN, configType);}
#endif
if (digitalRead(pinA) == pinsActive) {
last = 3;
}
if (digitalRead(pinB) == pinsActive) {
last ^=1;
}
}
// ----------------------------------------------------------------------------
#ifndef WITHOUT_BUTTON
// Depricated. Use DigitalButton instead
ClickEncoder::ClickEncoder(int8_t BTN, bool active)
: doubleClickEnabled(true),buttonHeldEnabled(true), accelerationEnabled(true),
delta(0), last(0), acceleration(0),
button(Open), steps(1), analogInput(false),
pinA(-1), pinB(-1), pinBTN(BTN), pinsActive(active)
{
pinMode_t configType = (pinsActive == LOW) ? INPUT_PULLUP : INPUT;
if (pinBTN >= 0) {pinMode(pinBTN, configType);}
}
// ----------------------------------------------------------------------------
// Constructor for using digital input as a button
DigitalButton::DigitalButton(int8_t BTN, bool active) : ClickEncoder(BTN, active)
{
}
// ----------------------------------------------------------------------------
// Constructor for using analog input range as a button
AnalogButton::AnalogButton(int8_t BTN, int16_t rangeLow, int16_t rangeHigh) : ClickEncoder(BTN, (bool)false)
{
pinMode(pinBTN, INPUT);
anlogActiveRangeLow = rangeLow;
anlogActiveRangeHigh = rangeHigh;
analogInput = true;
if (anlogActiveRangeLow > anlogActiveRangeHigh) { // swap values if provided in the wrong order
int16_t t = anlogActiveRangeLow;
anlogActiveRangeLow = anlogActiveRangeHigh;
anlogActiveRangeHigh = t;
}
}
#endif
// ----------------------------------------------------------------------------
// call this every 1 millisecond via timer ISR
//
void ClickEncoder::service(void)
{
bool moved = false;
if (pinA >= 0 && pinB >= 0) {
if (accelerationEnabled) { // decelerate every tick
acceleration -= ENC_ACCEL_DEC;
if (acceleration & 0x8000) { // handle overflow of MSB is set
acceleration = 0;
}
}
#if ENC_DECODER == ENC_FLAKY
last = (last << 2) & 0x0F;
if (digitalRead(pinA) == pinsActive) {
last |= 2;
}
if (digitalRead(pinB) == pinsActive) {
last |= 1;
}
int8_t tbl = pgm_read_byte(&table[last]);
if (tbl) {
delta += tbl;
moved = true;
}
#elif ENC_DECODER == ENC_NORMAL
int8_t curr = 0;
if (digitalRead(pinA) == pinsActive) {
curr = 3;
}
if (digitalRead(pinB) == pinsActive) {
curr ^= 1;
}
int8_t diff = last - curr;
if (diff & 1) { // bit 0 = step
last = curr;
delta += (diff & 2) - 1; // bit 1 = direction (+/-)
moved = true;
}
#else
# error "Error: define ENC_DECODER to ENC_NORMAL or ENC_FLAKY"
#endif
if (accelerationEnabled && moved) {
// increment accelerator if encoder has been moved
if (acceleration <= (ENC_ACCEL_TOP - ENC_ACCEL_INC)) {
acceleration += ENC_ACCEL_INC;
}
}
}
// handle button
//
#ifndef WITHOUT_BUTTON
unsigned long currentMillis = millis();
unsigned long millisSinceLastCheck = currentMillis - lastButtonCheck;
if ((pinBTN > 0 || (pinBTN == 0 && buttonOnPinZeroEnabled)) // check button only, if a pin has been provided
&& (millisSinceLastCheck >= ENC_BUTTONINTERVAL)) // checking button is sufficient every 10-30ms
{
lastButtonCheck = currentMillis;
bool pinRead = getPinState();
if (pinRead == !pinsActive) { // key is now up
if (keyDownTicks > 1) { //Make sure key was down through 1 complete tick to prevent random transients from registering as click
if (button == Held) {
button = Released;
doubleClickTicks = 0;
}
else {
#define ENC_SINGLECLICKONLY 1
if (doubleClickTicks > ENC_SINGLECLICKONLY) { // prevent trigger in single click mode
if (doubleClickTicks < (buttonDoubleClickTime)) {
button = DoubleClicked;
doubleClickTicks = 0;
}
}
else {
doubleClickTicks = (doubleClickEnabled) ? (buttonDoubleClickTime) : ENC_SINGLECLICKONLY;
}
}
}
keyDownTicks = 0;
}
if (pinRead == pinsActive) { // key is down
if ((keyDownTicks > (buttonHoldTime)) && (buttonHeldEnabled)) {
button = Held;
}
keyDownTicks += millisSinceLastCheck;
}
if (doubleClickTicks > 0) {
doubleClickTicks -= min(millisSinceLastCheck, doubleClickTicks);
if (doubleClickTicks == 0) {
button = Clicked;
}
}
}
#endif // WITHOUT_BUTTON
}
// ----------------------------------------------------------------------------
int16_t ClickEncoder::getValue(void)
{
int16_t val;
int16_t r = 0;
noInterrupts();
val = delta;
delta = val % steps;
int16_t accel = ((accelerationEnabled) ? (acceleration >> 8) : 0);
interrupts();
val /= steps;
if (val < 0) {
r -= 1 + accel;
}
else if (val > 0) {
r += 1 + accel;
}
return r;
}
// ----------------------------------------------------------------------------
// This is used to reset the encoder. If the encoder gets 'between' dedents
// (for example only needs 3 more dedents for next click instead of 4)
// it could cause changing direction of encoder to do nothing on first turn
// this could happen if teh encoder misses a reansition, or if the encoder
// is being turned during initialization
// This routine resets the encoder to re-sync it with the dedents
void ClickEncoder::resetEncoder(void)
{
noInterrupts();
delta = 0;
acceleration = 0;
interrupts();
}
// ----------------------------------------------------------------------------
#ifndef WITHOUT_BUTTON
ClickEncoder::Button ClickEncoder::getButton(void)
{
noInterrupts();
ClickEncoder::Button ret = button;
if (button != ClickEncoder::Held && ret != ClickEncoder::Open) {
button = ClickEncoder::Open; // reset
}
interrupts();
return ret;
}
bool ClickEncoder::getPinState() {
bool pinState;
if (analogInput) {
int16_t pinValue = analogRead(pinBTN);
pinState = ((pinValue >= anlogActiveRangeLow) && (pinValue <= anlogActiveRangeHigh)) ? LOW : HIGH; // set result to LOW (button pressed) if analog input is in range
} else {
pinState = digitalRead(pinBTN);
}
return pinState;
}
#endif