-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathencoder.c
279 lines (251 loc) · 9.03 KB
/
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
#include "Arduino.h"
#include "wvr_pins.h"
#include "ws_log.h"
#include "encoder.h"
#include "rotary_encoder.h"
#define ENABLE_HALF_STEPS false // Set to true to enable tracking of rotary encoder at half step resolution
#define RESET_AT 0 // Set to a positive non-zero number to reset the position if this value is exceeded
#define FLIP_DIRECTION false // Set to true to reverse the clockwise/counterclockwise sense
static const char* tag = "encoder";
rotary_encoder_info_t info = { 0 };
QueueHandle_t encoder_event_queue;
static uint32_t enc_a_gpio = 0;
static uint32_t enc_b_gpio = 0;
void on_encoder_default(bool down)
{
ESP_LOGI(tag, "%s", down ? "down" : "up");
}
static void encoder_task(void* arg)
{
for(;;)
{
rotary_encoder_event_t event = { 0 };
if (xQueueReceive(encoder_event_queue, &event, portMAX_DELAY))
{
on_encoder(event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE);
// ESP_LOGI(tag, "Event: position %d, direction %s", event.state.position,
// event.state.direction ? (event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET");
}
}
}
void encoder_init(int enc_a, int enc_b)
{
on_encoder = on_encoder_default;
ESP_ERROR_CHECK(rotary_encoder_init(&info, (gpio_num_t)enc_a, (gpio_num_t)enc_b));
ESP_ERROR_CHECK(rotary_encoder_enable_half_steps(&info, ENABLE_HALF_STEPS));
#ifdef FLIP_DIRECTION
ESP_ERROR_CHECK(rotary_encoder_flip_direction(&info));
#endif
encoder_event_queue = rotary_encoder_create_queue();
ESP_ERROR_CHECK(rotary_encoder_set_queue(&info, encoder_event_queue));
xTaskCreate(encoder_task, "encoder_task", 2048, NULL, 2, NULL);
}
// static void gpio_isr_handler(void* arg)
// {
// uint32_t gpio_num = (uint32_t) arg;
// xQueueSendFromISR(encoder_event_queue, &gpio_num, NULL);
// }
// static void enc_a_handler(void)
// {
// xQueueSendFromISR(encoder_event_queue, &enc_a_gpio, NULL);
// }
// static void enc_b_handler(void)
// {
// xQueueSendFromISR(encoder_event_queue, &enc_b_gpio, NULL);
// }
// static int8_t out = 1;
// static void emit(char dir){
// // log_d("step %s",dir?"left":"right");
// // out += (dir?-1:1);
// // log_i("%d",out);
// on_encoder((bool)dir);
// }
// static void encoder_task(void* arg)
// {
// char dir = L;
// // 0 none
// // 1 dir set
// // 2 dir conf
// // 31 1st up (waiting for 2nd)
// // 32 2st up (waiting for 1st)
// // 4 both up (done)
// char step = 0;
// uint32_t io_num;
// for(;;) {
// if(xQueueReceive(encoder_event_queue, &io_num, portMAX_DELAY)) {
// if(GPIO_MASK & (1ULL<<io_num))
// {
// int val = gpio_get_level(io_num);
// int pin = (io_num == enc_b_gpio) ? L : R;
// log_d("got %s %u",(pin == L)?"L":"R", val);
// if(step == 0)
// {
// if(val == 0)
// {
// dir = pin;
// step = 1;
// log_d("goto step 1 dir set %s",dir?"R":"L");
// }
// else
// {
// log_d("do nothing");
// }
// }
// else if(step == 1)
// {
// if(val == 0)
// {
// if(pin != dir)
// {
// step = 2;
// log_d("goto step 2");
// }
// else
// {
// log_d("do nothing");
// }
// }
// else
// {
// if(pin == dir)
// {
// // step = 0;
// // log_d("goto step 0");
// step = 31;
// log_d("goto step 3");
// }
// else
// {
// log_d("do nothing");
// }
// }
// }
// else if(step == 2)
// {
// if(val == 1)
// {
// if(pin == dir)
// {
// step = 31;
// log_d("goto step 31");
// }
// else
// {
// step = 32;
// log_d("goto step 32");
// }
// }
// else
// {
// if(pin == dir)
// {
// // step = 1;
// // log_d("goto step 1");
// log_d("do nothing");
// }
// else
// {
// log_d("do nothing");
// }
// }
// }
// else if(step == 31)
// {
// if(val == 1)
// {
// if(pin != dir)
// {
// emit(dir);
// step=0;
// }
// else
// {
// log_d("do nothin");
// }
// }
// else
// {
// if(pin == dir)
// {
// step=2;
// log_d("goto step 2");
// }
// else
// {
// log_d("do nothing");
// // step=2;
// // log_d("goto step 2");
// }
// }
// }
// else if(step == 32)
// {
// if(val == 1)
// {
// if(pin == dir)
// {
// emit(dir);
// step=0;
// }
// else
// {
// log_d("do nothin");
// }
// }
// else
// {
// if(pin != dir)
// {
// step=2;
// log_d("goto step 2");
// }
// else
// {
// log_d("do nothing");
// // step=2;
// // log_d("goto step 2");
// }
// }
// }
// }
// }
// }
// }
// // void encoder_init(void){
// void encoder_init(int enc_a, int enc_b){
// // using the esp-idf interrupt style functions breaks the arduino ones !?!?!?!
// // gpio_config_t io_conf;
// // //interrupt of any edge
// // io_conf.intr_type = GPIO_PIN_INTR_ANYEDGE;
// // //bit mask of the pins, use GPIO4/5 here
// // io_conf.pin_bit_mask = (GPIO_MASK);
// // //set as input mode
// // io_conf.mode = GPIO_MODE_INPUT;
// // //disable pull-down mode
// // io_conf.pull_down_en = 0;
// // //enable pull-up mode
// // io_conf.pull_up_en = 1;
// // gpio_config(&io_conf);
// // //create a queue to handle gpio event from isr
// // encoder_event_queue = xQueueCreate(10, sizeof(uint32_t));
// // //start gpio task
// // xTaskCreatePinnedToCore(encoder_task, "encoder_task", 2048, NULL, 10, NULL, 0);
// // //install gpio isr service
// // gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
// // //hook isr handler for specific gpio pin
// // gpio_isr_handler_add(ROT_ENC_A_GPIO, gpio_isr_handler, (void*) ROT_ENC_A_GPIO);
// // gpio_isr_handler_add(ROT_ENC_B_GPIO, gpio_isr_handler, (void*) ROT_ENC_B_GPIO);
// log_i("encoder INIT");
// enc_a_gpio = enc_a;
// enc_b_gpio = enc_b;
// GPIO_MASK = ((1ULL<<enc_a) | (1ULL<<enc_b));
// pinMode(enc_a, INPUT_PULLUP);
// pinMode(enc_b, INPUT_PULLUP);
// // pinMode(enc_a, INPUT);
// // pinMode(enc_b, INPUT);
// encoder_event_queue = xQueueCreate(10, sizeof(uint32_t));
// xTaskCreatePinnedToCore(encoder_task, "encoder_task", 2048, NULL, 10, NULL, 0);
// on_encoder = on_encoder_default;
// attachInterrupt(enc_a, enc_a_handler, CHANGE);
// attachInterrupt(enc_b, enc_b_handler, CHANGE);
// }