-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathClickEncoder.cpp
218 lines (181 loc) · 5.82 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
// ----------------------------------------------------------------------------
// 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
#define ENC_DOUBLECLICKTIME 600 // second click within 600ms
#define ENC_HOLDTIME 1200 // report held button after 1.2s
// ----------------------------------------------------------------------------
// 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
# ifdef 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(uint8_t A, uint8_t B, uint8_t BTN, uint8_t stepsPerNotch, bool coderActive, bool btnActive)
: doubleClickEnabled(true), accelerationEnabled(true),
delta(0), last(0), acceleration(0),
button(Open), steps(stepsPerNotch),
pinA(A), pinB(B), pinBTN(BTN), coderPinsActive(coderActive), btnPinActive(btnActive)
{
uint8_t coderType = (coderPinsActive == LOW) ? INPUT_PULLUP : INPUT;
pinMode(pinA, coderType);
pinMode(pinB, coderType);
uint8_t btnType = (btnPinActive == LOW) ? INPUT_PULLUP : INPUT;
pinMode(pinBTN, btnType);
if (digitalRead(pinA) == coderPinsActive) {
last = 3;
}
if (digitalRead(pinB) == coderPinsActive) {
last ^=1;
}
}
// ----------------------------------------------------------------------------
// call this every 1 millisecond via timer ISR
//
void ClickEncoder::service(void)
{
bool moved = false;
unsigned long now = millis();
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) == coderPinsActive) {
last |= 2;
}
if (digitalRead(pinB) == coderPinsActive) {
last |= 1;
}
uint8_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) == coderPinsActive) {
curr = 3;
}
if (digitalRead(pinB) == coderPinsActive) {
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
if (pinBTN > -1 // check button only, if a pin has been provided
&& (now - lastButtonCheck) >= ENC_BUTTONINTERVAL) // checking button is sufficient every 10-30ms
{
lastButtonCheck = now;
if (digitalRead(pinBTN) == btnPinActive) { // key is down
keyDownTicks++;
if (keyDownTicks > (ENC_HOLDTIME / ENC_BUTTONINTERVAL)) {
button = Held;
}
}
if (digitalRead(pinBTN) == !btnPinActive) { // key is now up
if (keyDownTicks /*> ENC_BUTTONINTERVAL*/) {
if (button == Held) {
button = Released;
doubleClickTicks = 0;
}
else {
#define ENC_SINGLECLICKONLY 1
if (doubleClickTicks > ENC_SINGLECLICKONLY) { // prevent trigger in single click mode
if (doubleClickTicks < (ENC_DOUBLECLICKTIME / ENC_BUTTONINTERVAL)) {
button = DoubleClicked;
doubleClickTicks = 0;
}
}
else {
doubleClickTicks = (doubleClickEnabled) ? (ENC_DOUBLECLICKTIME / ENC_BUTTONINTERVAL) : ENC_SINGLECLICKONLY;
}
}
}
keyDownTicks = 0;
}
if (doubleClickTicks > 0) {
doubleClickTicks--;
if (--doubleClickTicks == 0) {
button = Clicked;
}
}
}
#endif // WITHOUT_BUTTON
}
// ----------------------------------------------------------------------------
int16_t ClickEncoder::getValue(void)
{
int16_t val;
cli();
val = delta;
if (steps == 2) delta = val & 1;
else if (steps == 4) delta = val & 3;
else delta = 0; // default to 1 step per notch
sei();
if (steps == 4) val >>= 2;
if (steps == 2) val >>= 1;
int16_t r = 0;
int16_t accel = ((accelerationEnabled) ? (acceleration >> 8) : 0);
if (val < 0) {
r -= 1 + accel;
}
else if (val > 0) {
r += 1 + accel;
}
return r;
}
// ----------------------------------------------------------------------------
#ifndef WITHOUT_BUTTON
ClickEncoder::Button ClickEncoder::getButton(void)
{
ClickEncoder::Button ret = button;
if (button != ClickEncoder::Held) {
button = ClickEncoder::Open; // reset
}
return ret;
}
#endif