-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrotary.cpp
324 lines (300 loc) · 10.4 KB
/
rotary.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
/* Rotary encoder handler for arduino. v1.1
*
* Copyright 2011 Ben Buxton. Licenced under the GNU GPL Version 3.
* Contact: bb@cactii.net
*
* A typical mechanical rotary encoder emits a two bit gray code
* on 3 output pins. Every step in the output (often accompanied
* by a physical 'click') generates a specific sequence of output
* codes on the pins.
*
* There are 3 pins used for the rotary encoding - one common and
* two 'bit' pins.
*
* The following is the typical sequence of code on the output when
* moving from one step to the next:
*
* Position Bit1 Bit2
* ----------------------
* Step1 0 0
* 1/4 1 0
* 1/2 1 1
* 3/4 0 1
* Step2 0 0
*
* From this table, we can see that when moving from one 'click' to
* the next, there are 4 changes in the output code.
*
* - From an initial 0 - 0, Bit1 goes high, Bit0 stays low.
* - Then both bits are high, halfway through the step.
* - Then Bit1 goes low, but Bit2 stays high.
* - Finally at the end of the step, both bits return to 0.
*
* Detecting the direction is easy - the table simply goes in the other
* direction (read up instead of down).
*
* To decode this, we use a simple state machine. Every time the output
* code changes, it follows state, until finally a full steps worth of
* code is received (in the correct order). At the final 0-0, it returns
* a value indicating a step in one direction or the other.
*
* It's also possible to use 'half-step' mode. This just emits an event
* at both the 0-0 and 1-1 positions. This might be useful for some
* encoders where you want to detect all positions.
*
* If an invalid state happens (for example we go from '0-1' straight
* to '1-0'), the state machine resets to the start until 0-0 and the
* next valid codes occur.
*
* The biggest advantage of using a state machine over other algorithms
* is that this has inherent debounce built in. Other algorithms emit spurious
* output with switch bounce, but this one will simply flip between
* sub-states until the bounce settles, then continue along the state
* machine.
* A side effect of debounce is that fast rotations can cause steps to
* be skipped. By not requiring debounce, fast rotations can be accurately
* measured.
* Another advantage is the ability to properly handle bad state, such
* as due to EMI, etc.
* It is also a lot simpler than others - a static state table and less
* than 10 lines of logic.
*/
/* Modified 5/22/2015 by Phill Fisk
* Added methods and constructor to handle button-equiped rotary encoders
* Added support methods to return clockwise and counter-clockwise defines
*/
#include "Arduino.h"
#include "rotary.h"
/*
* The below state table has, for each state (row), the new state
* to set based on the next encoder output. From left to right in,
* the table, the encoder outputs are 00, 01, 10, 11, and the value
* in that position is the new state to set.
*/
#define R_START 0x0
#ifdef HALF_STEP
// Use the half-step state table (emits a code at 00 and 11)
#define R_CCW_BEGIN 0x1
#define R_CW_BEGIN 0x2
#define R_START_M 0x3
#define R_CW_BEGIN_M 0x4
#define R_CCW_BEGIN_M 0x5
//original code from Buxtronixs
//const unsigned char ttable[6][4] = {
// R_START (00)
//{R_START_M, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CCW_BEGIN
//{R_START_M | DIR_CCW, R_START, R_CCW_BEGIN, R_START},
// R_CW_BEGIN
//{R_START_M | DIR_CW, R_CW_BEGIN, R_START, R_START},
// R_START_M (11)
//{R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, R_START},
// R_CW_BEGIN_M
//{R_START_M, R_START_M, R_CW_BEGIN_M, R_START | DIR_CW},
// R_CCW_BEGIN_M
//{R_START_M, R_CCW_BEGIN_M, R_START_M, R_START | DIR_CCW},
//};
/* Modified 5/04/2019 by Carlos Siles
* Modified table to follow sequence 00>10>11>01>01
*/
const unsigned char ttable[6][4] = {
// R_START (00)
{R_START, R_CCW_BEGIN, R_CW_BEGIN, R_START_M},
// R_CCW_BEGIN
{R_START, R_CCW_BEGIN, R_START, R_START_M | DIR_CCW},
// R_CW_BEGIN
{ R_START, R_START, R_CW_BEGIN , R_START_M | DIR_CW},
// R_START_M (11)
{R_START, R_CW_BEGIN_M, R_CCW_BEGIN_M, R_START_M},
// R_CW_BEGIN_M
{R_START | DIR_CW, R_CW_BEGIN_M, R_START_M, R_START_M},
// R_CCW_BEGIN_M
{R_START | DIR_CCW, R_START_M, R_CCW_BEGIN_M, R_START_M},
};
#else
// Use the full-step state table (emits a code at 00 only)
#define R_CW_FINAL 0x1
#define R_CW_BEGIN 0x2
#define R_CW_NEXT 0x3
#define R_CCW_BEGIN 0x4
#define R_CCW_FINAL 0x5
#define R_CCW_NEXT 0x6
//original code from Buxtronixs
//const unsigned char ttable[7][4] = {
// R_START
//{R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CW_FINAL
//{R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW},
// R_CW_BEGIN
//{R_CW_NEXT, R_CW_BEGIN, R_START, R_START},
// R_CW_NEXT
//{R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, R_START},
// R_CCW_BEGIN
//{R_CCW_NEXT, R_START, R_CCW_BEGIN, R_START},
// R_CCW_FINAL
//{R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW},
// R_CCW_NEXT
//{R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
//};
/* Modified 5/04/2019 by Carlos Siles
* Modified table to follow sequence 00>10>11>01>01
*/
const unsigned char ttable[7][4] = {
// R_START
{R_START, R_CCW_BEGIN, R_CW_BEGIN, R_START},
// R_CW_FINAL
{R_START | DIR_CW, R_CW_FINAL, R_START, R_CW_NEXT},
// R_CW_BEGIN
{R_START, R_START, R_CW_BEGIN, R_CW_NEXT},
// R_CW_NEXT
{R_START, R_CW_FINAL, R_CW_BEGIN, R_CW_NEXT},
// R_CCW_BEGIN
{R_START, R_CCW_BEGIN, R_START, R_CCW_NEXT},
// R_CCW_FINAL
{R_START | DIR_CCW, R_START, R_CCW_FINAL, R_CCW_NEXT},
// R_CCW_NEXT
{R_START, R_CCW_BEGIN, R_CCW_FINAL, R_CCW_NEXT},
};
#endif
/*
* Constructor. Each arg is the pin number for each encoder contact.
*/
Rotary::Rotary(char _pin1, char _pin2) {
// Moved to init method()
init(_pin1, _pin2);
}
/*
* Overloaded constructor, added to support rotary encoders that
* also include a button
*/
Rotary::Rotary(char _pin1, char _pin2, char _buttonPin) {
// intiialize the rotary encoder button
buttonTimer = 0;
buttonPin = _buttonPin;
pinMode(buttonPin, INPUT);
#ifdef ENABLE_PULLUPS
digitalWrite(buttonPin, HIGH);
#endif
// run the original initialization
init(_pin1, _pin2);
}
/*
* Moved constructor initialization logic here to support multiple constructors
*/
void Rotary::init(char _pin1, char _pin2) {
// Assign variables.
pin1 = _pin1;
pin2 = _pin2;
// Set pins to input.
pinMode(pin1, INPUT);
pinMode(pin2, INPUT);
#ifdef ENABLE_PULLUPS
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
#endif
// Initialise state.
state = R_START;
}
unsigned char Rotary::process() {
// Grab state of input pins.
unsigned char pinstate = (digitalRead(pin2) << 1) | digitalRead(pin1);
// Determine new state from the pins and state table.
state = ttable[state & 0xf][pinstate];
// Return emit bits, ie the generated event.
return state & 0x30;
}
/*
* Added to return clockwise def. makes sketch easier to read
* (just check against this method for movement)
*/
unsigned char Rotary::clockwise() {
return DIR_CW;
}
/*
* Added to return counter-clockwise def. makes sketch easier to read
* (just check against this method for movement)
*/
unsigned char Rotary::counterClockwise() {
return DIR_CCW;
}
/*
* Reads the rotary encoder button, and returns true if the
* button has been pressed and released, based on the debounce dealy passed in
*
* Presuming the button is wired with one lead to a GPIO pin and the other pin
* to ground, the button will read high when open (not pressed) and low when
* closed (pressed). In other words, a transition of high -> low -> high (1 -> 0 -> 1)
* will indicate a button "press". The debounce delay is configurable (passed in)
*/
bool Rotary::buttonPressedReleased(short debounce_delay) {
if (buttonState == BUTTON_PRESSED) { // If the button has been pressed
if (millis() - buttonTimer > debounce_delay) { // and the debounce timer has expired
if (digitalRead(buttonPin)) { // and the pin reads HIGH (open)
buttonState |= BUTTON_RELEASED; // then the button has been pressed and released
}
}
}
else { // if the button hasn't been pressed yet, read the pin
if (!digitalRead(buttonPin)) { // if the pin is LOW (closed), then the button has been pushed in
buttonState |= BUTTON_PRESSED; // set the state
buttonTimer = millis(); // and start the timer
}
}
// Check to see if the button has been pressed and released
if (buttonState == (BUTTON_PRESSED | BUTTON_RELEASED)) {
buttonState = 0x00; // Reset the button state before returning true
return true;
}
else {
return false;
}
}
/*
* Reads the encoder button, and returns true if the button has been
* pressed and held down for a given amount of time.
*/
bool Rotary::buttonPressedHeld(short delay_millis) {
// Is the button closed (being pressed)?
if (!digitalRead(buttonPin)) {
// If this is the first time we've checked the button, set the state and start the timer
if (buttonState != BUTTON_PRESSED) {
buttonState = BUTTON_PRESSED;
buttonTimer = millis();
}
else {
// Otherwise, check the timer to see if it's expired
if (millis() - buttonTimer > delay_millis) {
// the wait timer has expired, reset the button state
buttonState = BUTTON_RESET;
// and return true indicating the button has been held down for long enough
return true;
}
}
}
else {
// button is open (not being pressed) was it already pressed?
if (buttonState == BUTTON_PRESSED) {
// it was pressed and released too quickly, reset it
buttonState = BUTTON_RESET;
}
}
return false;
}
/*
* Reads the encoder button and returns the current state (pressed OR released)
* Does not return the composite state (just for checking the state right now)
*/
unsigned char Rotary::readButton() {
if (digitalRead(buttonPin)) {
return BUTTON_RELEASED;
}
else {
return BUTTON_PRESSED;
}
}
/*
* Resets the state of the button
*/
void Rotary::resetButton() {
buttonState = BUTTON_RESET;
}