-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrotary_encoder.c
339 lines (312 loc) · 10.5 KB
/
rotary_encoder.c
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
/*
* Modified by Andew March to use Arduino interupt functions, using the esp-idf interrupt api breaks arduino
*
* Copyright (c) 2019 David Antliff
* Copyright 2011 Ben Buxton
*
* This file is part of the esp32-rotary-encoder component.
*
* esp32-rotary-encoder 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.
*
* esp32-rotary-encoder 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 esp32-rotary-encoder. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* @file rotary_encoder.c
* @brief Driver implementation for the ESP32-compatible Incremental Rotary Encoder component.
*
* Based on https://github.com/buxtronix/arduino/tree/master/libraries/Rotary
* Original header follows:
*
* Rotary encoder handler for arduino. v1.1
*
* Copyright 2011 Ben Buxton. Licenced under the GNU GPL Version 3.
* Contact: [email protected]
*
* 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.
*/
#include "Arduino.h"
#include "rotary_encoder.h"
#include "esp_log.h"
#include "driver/gpio.h"
#define TAG "rotary_encoder"
//#define ROTARY_ENCODER_DEBUG
// Use a single-item queue so that the last value can be easily overwritten by the interrupt handler
#define EVENT_QUEUE_LENGTH 1
#define TABLE_ROWS 7
#define DIR_NONE 0x0 // No complete step yet.
#define DIR_CW 0x10 // Clockwise step.
#define DIR_CCW 0x20 // Anti-clockwise step.
// Create the half-step state table (emits a code at 00 and 11)
#define R_START 0x0
#define H_CCW_BEGIN 0x1
#define H_CW_BEGIN 0x2
#define H_START_M 0x3
#define H_CW_BEGIN_M 0x4
#define H_CCW_BEGIN_M 0x5
DRAM_ATTR static const uint8_t _ttable_half[TABLE_ROWS][TABLE_COLS] = {
// 00 01 10 11 // BA
{H_START_M, H_CW_BEGIN, H_CCW_BEGIN, R_START}, // R_START (00)
{H_START_M | DIR_CCW, R_START, H_CCW_BEGIN, R_START}, // H_CCW_BEGIN
{H_START_M | DIR_CW, H_CW_BEGIN, R_START, R_START}, // H_CW_BEGIN
{H_START_M, H_CCW_BEGIN_M, H_CW_BEGIN_M, R_START}, // H_START_M (11)
{H_START_M, H_START_M, H_CW_BEGIN_M, R_START | DIR_CW}, // H_CW_BEGIN_M
{H_START_M, H_CCW_BEGIN_M, H_START_M, R_START | DIR_CCW}, // H_CCW_BEGIN_M
};
// Create the full-step state table (emits a code at 00 only)
# define F_CW_FINAL 0x1
# define F_CW_BEGIN 0x2
# define F_CW_NEXT 0x3
# define F_CCW_BEGIN 0x4
# define F_CCW_FINAL 0x5
# define F_CCW_NEXT 0x6
DRAM_ATTR static const uint8_t _ttable_full[TABLE_ROWS][TABLE_COLS] = {
// 00 01 10 11 // BA
{R_START, F_CW_BEGIN, F_CCW_BEGIN, R_START}, // R_START
{F_CW_NEXT, R_START, F_CW_FINAL, R_START | DIR_CW}, // F_CW_FINAL
{F_CW_NEXT, F_CW_BEGIN, R_START, R_START}, // F_CW_BEGIN
{F_CW_NEXT, F_CW_BEGIN, F_CW_FINAL, R_START}, // F_CW_NEXT
{F_CCW_NEXT, R_START, F_CCW_BEGIN, R_START}, // F_CCW_BEGIN
{F_CCW_NEXT, F_CCW_FINAL, R_START, R_START | DIR_CCW}, // F_CCW_FINAL
{F_CCW_NEXT, F_CCW_FINAL, F_CCW_BEGIN, R_START}, // F_CCW_NEXT
};
uint8_t _process(rotary_encoder_info_t * info)
{
uint8_t event = 0;
if (info != NULL)
{
// Get state of input pins.
uint8_t pin_state = (gpio_get_level(info->pin_b) << 1) | gpio_get_level(info->pin_a);
// Determine new state from the pins and state table.
#ifdef ROTARY_ENCODER_DEBUG
uint8_t old_state = info->table_state;
#endif
info->table_state = info->table[info->table_state & 0xf][pin_state];
// Return emit bits, i.e. the generated event.
event = info->table_state & 0x30;
#ifdef ROTARY_ENCODER_DEBUG
ESP_EARLY_LOGD(TAG, "BA %d%d, state 0x%02x, new state 0x%02x, event 0x%02x",
pin_state >> 1, pin_state & 1, old_state, info->table_state, event);
#endif
}
return event;
}
void _isr_rotenc(void * args)
{
rotary_encoder_info_t * info = (rotary_encoder_info_t *)args;
uint8_t event = _process(info);
bool send_event = false;
switch (event)
{
case DIR_CW:
++info->state.position;
info->state.direction = ROTARY_ENCODER_DIRECTION_CLOCKWISE;
send_event = true;
break;
case DIR_CCW:
--info->state.position;
info->state.direction = ROTARY_ENCODER_DIRECTION_COUNTER_CLOCKWISE;
send_event = true;
break;
default:
break;
}
if (send_event && info->queue)
{
rotary_encoder_event_t queue_event =
{
.state =
{
.position = info->state.position,
.direction = info->state.direction,
},
};
BaseType_t task_woken = pdFALSE;
xQueueOverwriteFromISR(info->queue, &queue_event, NULL);
if (task_woken)
{
portYIELD_FROM_ISR();
}
}
}
esp_err_t rotary_encoder_init(rotary_encoder_info_t * info, gpio_num_t pin_a, gpio_num_t pin_b)
{
esp_err_t err = ESP_OK;
if (info)
{
info->pin_a = pin_a;
info->pin_b = pin_b;
info->table = &_ttable_full[0]; //enable_half_step ? &_ttable_half[0] : &_ttable_full[0];
info->table_state = R_START;
info->state.position = 0;
info->state.direction = ROTARY_ENCODER_DIRECTION_NOT_SET;
// configure GPIOs
pinMode(pin_a, INPUT);
pinMode(pin_b, INPUT);
attachInterruptArg((uint8_t)pin_a, _isr_rotenc, (void*)info, CHANGE);
attachInterruptArg((uint8_t)pin_b, _isr_rotenc, (void*)info, CHANGE);
}
else
{
ESP_LOGE(TAG, "info is NULL");
err = ESP_ERR_INVALID_ARG;
}
return err;
}
esp_err_t rotary_encoder_enable_half_steps(rotary_encoder_info_t * info, bool enable)
{
esp_err_t err = ESP_OK;
if (info)
{
info->table = enable ? &_ttable_half[0] : &_ttable_full[0];
info->table_state = R_START;
}
else
{
ESP_LOGE(TAG, "info is NULL");
err = ESP_ERR_INVALID_ARG;
}
return err;
}
esp_err_t rotary_encoder_flip_direction(rotary_encoder_info_t * info)
{
esp_err_t err = ESP_OK;
if (info)
{
gpio_num_t temp = info->pin_a;
info->pin_a = info->pin_b;
info->pin_b = temp;
}
else
{
ESP_LOGE(TAG, "info is NULL");
err = ESP_ERR_INVALID_ARG;
}
return err;
}
esp_err_t rotary_encoder_uninit(rotary_encoder_info_t * info)
{
esp_err_t err = ESP_OK;
if (info)
{
gpio_isr_handler_remove(info->pin_a);
gpio_isr_handler_remove(info->pin_b);
}
else
{
ESP_LOGE(TAG, "info is NULL");
err = ESP_ERR_INVALID_ARG;
}
return err;
}
QueueHandle_t rotary_encoder_create_queue(void)
{
return xQueueCreate(EVENT_QUEUE_LENGTH, sizeof(rotary_encoder_event_t));
}
esp_err_t rotary_encoder_set_queue(rotary_encoder_info_t * info, QueueHandle_t queue)
{
esp_err_t err = ESP_OK;
if (info)
{
info->queue = queue;
}
else
{
ESP_LOGE(TAG, "info is NULL");
err = ESP_ERR_INVALID_ARG;
}
return err;
}
esp_err_t rotary_encoder_get_state(const rotary_encoder_info_t * info, rotary_encoder_state_t * state)
{
esp_err_t err = ESP_OK;
if (info && state)
{
// make a snapshot of the state
state->position = info->state.position;
state->direction = info->state.direction;
}
else
{
ESP_LOGE(TAG, "info and/or state is NULL");
err = ESP_ERR_INVALID_ARG;
}
return err;
}
esp_err_t rotary_encoder_reset(rotary_encoder_info_t * info)
{
esp_err_t err = ESP_OK;
if (info)
{
info->state.position = 0;
info->state.direction = ROTARY_ENCODER_DIRECTION_NOT_SET;
}
else
{
ESP_LOGE(TAG, "info is NULL");
err = ESP_ERR_INVALID_ARG;
}
return err;
}