OverviewFilesCommitsIssuesDocsReleasesPeers

cthuloid commits / 0b54b1b

lightswitch proof of concept
0b54b1b0e4b0234d6e3a67f978be7bb3192d8b8a
by John Zorko <jzorko@trulia.com> · 2019-04-15T02:26:23.000Z

Changed files (3) +216 -72

esp32/esp32-rotary-encoder/lightswitch_poc/lightswitch_poc.ino +108 -0
……@@ -0,0 +1,108 @@
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#include <WiFi.h>
10#include <WiFiMulti.h>
11
12long int rotValue=0, swValue=0;
13uint8_t state=0;
14
15// 10.0.0.30 1337
16// 10.0.0.31 1337
17
18WiFiClient client;
19bool connected, pressed, onOrOff;
20WiFiMulti wifi;
21
22#define ROTARY_PINA 2
23#define ROTARY_PINB 4
24#define ROTARY_PINSW 5
25
26portMUX_TYPE gpioMux = portMUX_INITIALIZER_UNLOCKED;
27
28void IRAM_ATTR isrAB() {
29 uint8_t s = state & 3;
30
31 portENTER_CRITICAL_ISR(&gpioMux);
32 if (digitalRead(ROTARY_PINA)) s |= 4;
33 if (digitalRead(ROTARY_PINB)) s |= 8;
34 switch (s) {
35 case 0: case 5: case 10: case 15:
36 break;
37 case 1: case 7: case 8: case 14:
38 rotValue++; break;
39 case 2: case 4: case 11: case 13:
40 rotValue--; break;
41 case 3: case 12:
42 rotValue += 2; break;
43 default:
44 rotValue -= 2; break;
45 }
46 state = (s >> 2);
47 portEXIT_CRITICAL_ISR(&gpioMux);
48
49}
50
51
52void IRAM_ATTR isrSWAll() {
53
54 portENTER_CRITICAL_ISR(&gpioMux);
55 swValue++;
56 portEXIT_CRITICAL_ISR(&gpioMux);
57
58 pressed = true;
59}
60
61void setup(){
62 pinMode(ROTARY_PINA, INPUT_PULLUP);
63 pinMode(ROTARY_PINB, INPUT_PULLUP);
64 pinMode(ROTARY_PINSW, INPUT_PULLUP);
65
66 attachInterrupt(ROTARY_PINA, isrAB, CHANGE);
67 attachInterrupt(ROTARY_PINB, isrAB, CHANGE);
68 attachInterrupt(ROTARY_PINSW, isrSWAll, CHANGE);
69 Serial.begin(115200);
70
71 wifi.addAP("ubitron", "superduper");
72
73 Serial.println();
74 Serial.println();
75 Serial.print("Waiting for WiFi... ");
76
77 while(wifi.run() != WL_CONNECTED) {
78 Serial.print(".");
79 delay(500);
80 }
81
82 connected = client.connect("10.0.0.63", 1337);
83
84 onOrOff = false;
85}
86
87
88void loop(){
89 Serial.print("isrSWAll ");
90 Serial.print(swValue);
91 Serial.print(" rotValue ");
92 Serial.println(rotValue);
93
94 if (pressed) {
95 if (onOrOff) {
96 Serial.print("RED\n");
97 client.println("pwm2 1 0 10000 30000");
98 } else {
99 Serial.print("BLUE\n");
100 client.println("pwm2 1 40000 10000 10000");
101 }
102
103 pressed = false;
104 onOrOff = ! onOrOff;
105 }
106
107 delay(1000);
108}
esp32/esp32-rotary-encoder/rotary-encoder.ino +0 -72
……@@ -1,72 +0,0 @@
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
esp32/esp32-rotary-encoder/rotary-encoder/rotary-encoder.ino +108 -0
……@@ -0,0 +1,108 @@
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#include <WiFi.h>
10#include <WiFiMulti.h>
11
12long int rotValue=0, swValue=0;
13uint8_t state=0;
14
15// 10.0.0.30 1337
16// 10.0.0.31 1337
17
18WiFiClient client;
19bool connected, pressed, onOrOff;
20WiFiMulti wifi;
21
22#define ROTARY_PINA 2
23#define ROTARY_PINB 4
24#define ROTARY_PINSW 5
25
26portMUX_TYPE gpioMux = portMUX_INITIALIZER_UNLOCKED;
27
28void IRAM_ATTR isrAB() {
29 uint8_t s = state & 3;
30
31 portENTER_CRITICAL_ISR(&gpioMux);
32 if (digitalRead(ROTARY_PINA)) s |= 4;
33 if (digitalRead(ROTARY_PINB)) s |= 8;
34 switch (s) {
35 case 0: case 5: case 10: case 15:
36 break;
37 case 1: case 7: case 8: case 14:
38 rotValue++; break;
39 case 2: case 4: case 11: case 13:
40 rotValue--; break;
41 case 3: case 12:
42 rotValue += 2; break;
43 default:
44 rotValue -= 2; break;
45 }
46 state = (s >> 2);
47 portEXIT_CRITICAL_ISR(&gpioMux);
48
49}
50
51
52void IRAM_ATTR isrSWAll() {
53
54 portENTER_CRITICAL_ISR(&gpioMux);
55 swValue++;
56 portEXIT_CRITICAL_ISR(&gpioMux);
57
58 pressed = true;
59}
60
61void setup(){
62 pinMode(ROTARY_PINA, INPUT_PULLUP);
63 pinMode(ROTARY_PINB, INPUT_PULLUP);
64 pinMode(ROTARY_PINSW, INPUT_PULLUP);
65
66 attachInterrupt(ROTARY_PINA, isrAB, CHANGE);
67 attachInterrupt(ROTARY_PINB, isrAB, CHANGE);
68 attachInterrupt(ROTARY_PINSW, isrSWAll, CHANGE);
69 Serial.begin(115200);
70
71 wifi.addAP("ubitron", "superduper");
72
73 Serial.println();
74 Serial.println();
75 Serial.print("Waiting for WiFi... ");
76
77 while(wifi.run() != WL_CONNECTED) {
78 Serial.print(".");
79 delay(500);
80 }
81
82 connected = client.connect("10.0.0.63", 1337);
83
84 onOrOff = false;
85}
86
87
88void loop(){
89 Serial.print("isrSWAll ");
90 Serial.print(swValue);
91 Serial.print(" rotValue ");
92 Serial.println(rotValue);
93
94 if (pressed) {
95 if (onOrOff) {
96 Serial.print("RED\n");
97 client.println("pwm2 1 0 10000 30000");
98 } else {
99 Serial.print("BLUE\n");
100 client.println("pwm2 1 40000 10000 10000");
101 }
102
103 pressed = false;
104 onOrOff = ! onOrOff;
105 }
106
107 delay(1000);
108}