forked from vitotai/BrewPiLess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotaryEncoder.cpp
418 lines (366 loc) · 11.6 KB
/
RotaryEncoder.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
/*
* Copyright 2012-2013 BrewPi/Elco Jacobs.
*
* This file is part of BrewPi.
*
* BrewPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BrewPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BrewPi. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Brewpi.h"
#include "RotaryEncoder.h"
#include "Pins.h"
#include <limits.h>
#include "Ticks.h"
#include "Display.h"
#include "Brewpi.h"
#include "TempControl.h"
#ifdef RotaryViaPCF8574
#include <pcf8574_esp.h>
#endif
RotaryEncoder rotaryEncoder;
int16_t RotaryEncoder::maximum;
int16_t RotaryEncoder::minimum;
volatile int16_t RotaryEncoder::steps;
volatile bool RotaryEncoder::pushFlag;
#ifndef ESP8266
#if BREWPI_STATIC_CONFIG!=BREWPI_SHIELD_DIY
#if rotarySwitchPin != 7
#error Review interrupt vectors when not using pin 7 for menu push
#endif
#if rotaryAPin != 8
#error Review interrupt vectors when not using pin 8 for menu right
#endif
#if rotaryBPin != 9
#error Review interrupt vectors when not using pin 9 for menu left
#endif
#else
#if rotarySwitchPin != 0
#error Review interrupt vectors when not using pin 0 for menu push
#endif
#if rotaryAPin != 2
#error Review interrupt vectors when not using pin 2 for menu right
#endif
#if rotaryBPin != 1
#error Review interrupt vectors when not using pin 1 for menu left
#endif
#endif
#if BREWPI_ROTARY_ENCODER
#if BREWPI_BOARD!=BREWPI_BOARD_LEONARDO && BREWPI_BOARD!=BREWPI_BOARD_STANDARD
#error Rotary encoder code is not compatible with boards other than leonardo or uno yet.
#endif
#endif
#endif
// Implementation based on work of Ben Buxton:
/* 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.
*/
/*
* 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
// #define HALF_STEP
// Use the half-step state table (emits a code at 00 and 11)
#define HS_R_CCW_BEGIN 0x1
#define HS_R_CW_BEGIN 0x2
#define HS_R_START_M 0x3
#define HS_R_CW_BEGIN_M 0x4
#define HS_R_CCW_BEGIN_M 0x5
const uint8_t PROGMEM hs_ttable[7][4] = {
// R_START (00)
{HS_R_START_M, HS_R_CW_BEGIN, HS_R_CCW_BEGIN, R_START},
// HS_R_CCW_BEGIN
{HS_R_START_M | DIR_CCW, R_START, HS_R_CCW_BEGIN, R_START},
// HS_R_CW_BEGIN
{HS_R_START_M | DIR_CW, HS_R_CW_BEGIN, R_START, R_START},
// HS_R_START_M (11)
{HS_R_START_M, HS_R_CCW_BEGIN_M, HS_R_CW_BEGIN_M, R_START},
// HS_R_CW_BEGIN_M
{HS_R_START_M, HS_R_START_M, HS_R_CW_BEGIN_M, R_START | DIR_CW},
// HS_R_CCW_BEGIN_M
{HS_R_START_M, HS_R_CCW_BEGIN_M, HS_R_START_M, R_START | DIR_CCW},
{R_START, R_START, R_START, R_START}
};
// 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
const uint8_t PROGMEM 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},
};
#if BREWPI_ROTARY_ENCODER
#ifdef ESP8266
#ifdef RotaryViaPCF8574
PCF8574 pcf8574(PCF8574_ADDRESS,PIN_SDA, PIN_SCL);
static void isr_iochange(void) {
rotaryEncoder.process();
}
#else //#ifdef RotaryViaPCF8574
static void isr_rotary(void) { rotaryEncoder.process(); }
static void isr_push(void) {
if(! digitalRead(rotarySwitchPin))
rotaryEncoder.setPushed();
}
#endif //#ifdef RotaryViaPCF8574
#else //#ifdef ESP8266
#include "util/atomic.h"
#include "FastDigitalPin.h"
#if BREWPI_STATIC_CONFIG==BREWPI_SHIELD_DIY
ISR(INT2_vect) {
rotaryEncoder.setPushed();
}
ISR(INT3_vect) {
rotaryEncoder.process();
}
ISR(INT1_vect) {
rotaryEncoder.process();
}
#elif BREWPI_BOARD == BREWPI_BOARD_LEONARDO
ISR(INT6_vect){
rotaryEncoder.setPushed();
}
ISR(PCINT0_vect){
rotaryEncoder.process();
}
#elif BREWPI_BOARD == BREWPI_BOARD_STANDARD
ISR(PCINT2_vect){
if(!bitRead(PIND,7)){
// high to low transition
rotaryEncoder.setPushed();
}
}
ISR(PCINT0_vect){
rotaryEncoder.process();
}
#else
#error board/processor not supported by rotary encoder code. Disable or fix the rotary encoder.
#endif
#endif //#ifdef ESP8266
void RotaryEncoder::process(void){
static uint8_t state=R_START;
// Grab state of input pins.
#ifdef ESP8266
#ifdef RotaryViaPCF8574
uint8_t p=pcf8574.read8();
// push
if ((p & ( 1<<rotarySwitchPin)) == 0){
rotaryEncoder.setPushed();
return;
}
uint8_t currPinA = (p & ( 1<<rotaryAPin)) >> rotaryAPin ;
uint8_t currPinB = (p & ( 1<<rotaryBPin)) >> rotaryBPin;
#else //#ifdef RotaryViaPCF8574
uint8_t currPinA = ! digitalRead(rotaryAPin);
uint8_t currPinB = ! digitalRead(rotaryBPin);
#endif //#ifdef RotaryViaPCF8574
#else // #ifdef ESP8266
#if BREWPI_STATIC_CONFIG == BREWPI_SHIELD_DIY
uint8_t currPinA = !bitRead(PIND,2);
uint8_t currPinB = !bitRead(PIND,3);
#elif BREWPI_BOARD == BREWPI_BOARD_LEONARDO
uint8_t currPinA = !bitRead(PINB,4);
uint8_t currPinB = !bitRead(PINB,5);
#elif BREWPI_BOARD == BREWPI_BOARD_STANDARD
uint8_t currPinA = !bitRead(PINB,0);
uint8_t currPinB = !bitRead(PINB,1);
#endif
#endif //#ifdef ESP8266
unsigned char pinstate = (currPinB << 1) | currPinA;
// Determine new state from the pins and state table.
if(tempControl.cc.rotaryHalfSteps){
state = pgm_read_byte(&(hs_ttable[state & 0xf][pinstate]));
}
else{
state = pgm_read_byte(&(ttable[state & 0xf][pinstate]));
}
// Get emit bits, ie the generated event.
uint8_t dir = state & 0x30;
if(dir){
int16_t s = steps; // steps is volatile - save a copy here to avoid multiple fetches
s = (dir==DIR_CW) ? s+1 : s-1;
if (s > maximum)
s = minimum;
else if (s < minimum)
s = maximum;
steps = s;
display.resetBacklightTimer();
}
}
#endif // BREWPI_ROTARY_ENCODER
void RotaryEncoder::setPushed(void){
pushFlag = true;
display.resetBacklightTimer();
}
void RotaryEncoder::init(void){
#if BREWPI_ROTARY_ENCODER
#ifdef ESP8266
#define BREWPI_INPUT_PULLUP (USE_INTERNAL_PULL_UP_RESISTORS ? INPUT_PULLUP : INPUT)
#ifdef RotaryViaPCF8574
pinMode(PCF8574_INT, INPUT_PULLUP);
attachInterrupt(PCF8574_INT, isr_iochange, FALLING);
#else //#ifdef RotaryViaPCF8574
pinMode(rotaryAPin, INPUT_PULLUP);
pinMode(rotaryBPin, INPUT_PULLUP);
pinMode(rotarySwitchPin, INPUT_PULLUP);
attachInterrupt(rotaryAPin, isr_rotary, CHANGE);
attachInterrupt(rotaryBPin, isr_rotary, CHANGE);
attachInterrupt(rotarySwitchPin, isr_push, CHANGE);
#endif
#else //#ifdef ESP8266
#define BREWPI_INPUT_PULLUP (USE_INTERNAL_PULL_UP_RESISTORS ? INPUT_PULLUP : INPUT)
fastPinMode(rotaryAPin, BREWPI_INPUT_PULLUP);
fastPinMode(rotaryBPin, BREWPI_INPUT_PULLUP);
fastPinMode(rotarySwitchPin, BREWPI_INPUT_PULLUP);
#if BREWPI_STATIC_CONFIG==BREWPI_SHIELD_DIY
EICRA |= (1<<ISC21) | (1<<ISC10) | (1<<ISC30);; // any logical change for encoder pins, falling edge for switch
EIMSK |= (1<<INT2) | (1<<INT1) | (1<<INT3); // enable interrupts for each pin
#elif BREWPI_BOARD == BREWPI_BOARD_LEONARDO
// falling edge interrupt for switch on INT6
EICRB |= (1<<ISC61) | (0<<ISC60);
// enable interrupt for INT6
EIMSK |= (1<<INT6);
// enable pin change interrupts
PCICR |= (1<<PCIE0);
// enable pin change interrupt on Arduino pin 8 and 9
PCMSK0 |= (1<<PCINT5) | (1<<PCINT4);
#elif BREWPI_BOARD == BREWPI_BOARD_STANDARD
// enable PCINT0 (PCINT0 and PCINT1 pin) and PCINT2 vector (PCINT23 pin)
PCICR |= (1<<PCIE2) | (1<<PCIE0);
// enable mask bits for PCINT0 and PCINT1
PCMSK0 |= (1<<PCINT0) | (1<<PCINT1);
// enable mask bit for PCINT23
PCMSK2 |= (1<<PCINT23);
#endif
#endif // #ifdef ESP8266
#endif //#if BREWPI_ROTARY_ENCODER
}
void RotaryEncoder::setRange(int16_t start, int16_t minVal, int16_t maxVal){
#if BREWPI_ROTARY_ENCODER
#ifdef ESP8266
noInterrupts();
#else
ATOMIC_BLOCK(ATOMIC_RESTORESTATE){
#endif
// this part cannot be interrupted
// Multiply by two to convert to half steps
steps = start;
minimum = minVal;
maximum = maxVal; // +1 to make sure that one step is still two half steps at overflow
#ifdef ESP8266
interrupts();
#else
}
#endif
#endif
}
bool RotaryEncoder::changed(void){
// returns one if the value changed since the last call of changed.
static int16_t prevValue = 0;
int16_t r = read();
if(r != prevValue){
prevValue = r;
return 1;
}
if(pushFlag == true){
return 1;
}
return 0;
}
int16_t RotaryEncoder::read(void){
#if BREWPI_ROTARY_ENCODER
#ifdef ESP8266
return steps;
#else
ATOMIC_BLOCK(ATOMIC_RESTORESTATE){
return steps;
}
#endif
#endif
return 0;
}