OverviewFilesCommitsIssuesDocsReleasesPeers

cthuloid commits / 2853829

adding in initial rotary encoder sketch
2853829811747820c00adbdf9403d109281c5046
by plankatron <fleeky@gmail.com> · 2019-04-14T22:24:44.000Z

Changed files (1) +72 -0

esp32/esp32-rotary-encoder/rotary-encoder.ino +72 -0
……@@ -0,0 +1,72 @@
1// KY-040 ... ESP32
2// CLK ... PIN 4
3// DT ... PIN 2
4// SW ... PIN 5
5// + ... 3.3V
6// GND ... GND
7
8#include <Arduino.h>
9
10long int rotValue=0, swValue=0;
11uint8_t state=0;
12
13
14#define ROTARY_PINA 2
15#define ROTARY_PINB 4
16#define ROTARY_PINSW 5
17
18portMUX_TYPE gpioMux = portMUX_INITIALIZER_UNLOCKED;
19
20void IRAM_ATTR isrAB() {
21 uint8_t s = state & 3;
22
23 portENTER_CRITICAL_ISR(&gpioMux);
24 if (digitalRead(ROTARY_PINA)) s |= 4;
25 if (digitalRead(ROTARY_PINB)) s |= 8;
26 switch (s) {
27 case 0: case 5: case 10: case 15:
28 break;
29 case 1: case 7: case 8: case 14:
30 rotValue++; break;
31 case 2: case 4: case 11: case 13:
32 rotValue--; break;
33 case 3: case 12:
34 rotValue += 2; break;
35 default:
36 rotValue -= 2; break;
37 }
38 state = (s >> 2);
39 portEXIT_CRITICAL_ISR(&gpioMux);
40
41}
42
43
44void IRAM_ATTR isrSWAll() {
45
46 portENTER_CRITICAL_ISR(&gpioMux);
47 swValue++;
48 portEXIT_CRITICAL_ISR(&gpioMux);
49
50}
51
52void setup(){
53 pinMode(ROTARY_PINA, INPUT_PULLUP);
54 pinMode(ROTARY_PINB, INPUT_PULLUP);
55 pinMode(ROTARY_PINSW, INPUT_PULLUP);
56
57 attachInterrupt(ROTARY_PINA, isrAB, CHANGE);
58 attachInterrupt(ROTARY_PINB, isrAB, CHANGE);
59 attachInterrupt(ROTARY_PINSW, isrSWAll, CHANGE);
60 Serial.begin(115200);
61}
62
63
64void loop(){
65 Serial.print("isrSWAll ");
66 Serial.print(swValue);
67 Serial.print(" rotValue ");
68 Serial.println(rotValue);
69 delay(1000);
70}
71
72