-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrequest_handlers.h
304 lines (269 loc) · 9.67 KB
/
request_handlers.h
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
// ***************************************************************************
// Request handlers
// ***************************************************************************
void getArgs() {
if (server.arg("rgb") != "") {
uint32_t rgb = (uint32_t) strtol(server.arg("rgb").c_str(), NULL, 16);
main_color.red = ((rgb >> 16) & 0xFF);
main_color.green = ((rgb >> 8) & 0xFF);
main_color.blue = ((rgb >> 0) & 0xFF);
} else {
main_color.red = server.arg("r").toInt();
main_color.green = server.arg("g").toInt();
main_color.blue = server.arg("b").toInt();
}
FPS = server.arg("d").toInt();
if (main_color.red > 255) {
main_color.red = 255;
}
if (main_color.green > 255) {
main_color.green = 255;
}
if (main_color.blue > 255) {
main_color.blue = 255;
}
if (main_color.red < 0) {
main_color.red = 0;
}
if (main_color.green < 0) {
main_color.green = 0;
}
if (main_color.blue < 0) {
main_color.blue = 0;
}
if (server.arg("d") == "") {
FPS = 40;
}
DBG_OUTPUT_PORT.print("Mode: ");
DBG_OUTPUT_PORT.print(mode);
DBG_OUTPUT_PORT.print(", Color: ");
DBG_OUTPUT_PORT.print(main_color.red);
DBG_OUTPUT_PORT.print(", ");
DBG_OUTPUT_PORT.print(main_color.green);
DBG_OUTPUT_PORT.print(", ");
DBG_OUTPUT_PORT.print(main_color.blue);
DBG_OUTPUT_PORT.print(", Delay:");
DBG_OUTPUT_PORT.print(FPS);
DBG_OUTPUT_PORT.print(", Brightness:");
DBG_OUTPUT_PORT.println(brightness);
DBG_OUTPUT_PORT.print(", show_length:");
DBG_OUTPUT_PORT.println(show_length);
}
void handleMinimalUpload() {
char temp[1500];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf ( temp, 1500,
"<!DOCTYPE html>\
<html>\
<head>\
<title>ESP8266 Upload</title>\
<meta charset=\"utf-8\">\
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
</head>\
<body>\
<form action=\"/edit\" method=\"post\" enctype=\"multipart/form-data\">\
<input type=\"file\" name=\"data\">\
<input type=\"text\" name=\"path\" value=\"/\">\
<button>Upload</button>\
</form>\
</body>\
</html>",
hr, min % 60, sec % 60
);
server.send ( 200, "text/html", temp );
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) {
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
server.send ( 404, "text/plain", message );
}
char* listStatusJSON() {
char json[255];
snprintf(json, sizeof(json), "{\"mode\":%d, \"FPS\":%d,\"show_length\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}", mode, FPS, show_length, brightness, main_color.red, main_color.green, main_color.blue);
return json;
}
void getStatusJSON() {
server.send ( 200, "application/json", listStatusJSON() );
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch (type) {
case WStype_DISCONNECTED:
DBG_OUTPUT_PORT.printf("WS: [%u] Disconnected!\n", num);
break;
case WStype_CONNECTED: {
IPAddress ip = webSocket.remoteIP(num);
DBG_OUTPUT_PORT.printf("WS: [%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
// send message to client
webSocket.sendTXT(num, "Connected");
}
break;
case WStype_TEXT:
DBG_OUTPUT_PORT.printf("WS: [%u] get Text: %s\n", num, payload);
// # ==> Set main color
if (payload[0] == '#') {
// decode rgb data
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
main_color.red = ((rgb >> 16) & 0xFF);
main_color.green = ((rgb >> 8) & 0xFF);
main_color.blue = ((rgb >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("Set main color to: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue);
webSocket.sendTXT(num, "OK");
}
// # ==> Set glitter color
if (payload[0] == 'G') {
// decode rgb data
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
glitter_color.red = ((rgb >> 16) & 0xFF);
glitter_color.green = ((rgb >> 8) & 0xFF);
glitter_color.blue = ((rgb >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("Set glitter color to: [%u] [%u] [%u]\n", glitter_color.red, glitter_color.green, glitter_color.blue);
webSocket.sendTXT(num, "OK");
}
// # ==> Set delay
if (payload[0] == '?') {
// decode delay data
uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
FPS = ((d >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("WS: Set FPS: [%u]\n", FPS);
webSocket.sendTXT(num, "OK");
}
// # ==> Set brightness
if (payload[0] == '%') {
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
brightness = ((b >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("WS: Set brightness to: [%u]\n", brightness);
FastLED.setBrightness(brightness);
webSocket.sendTXT(num, "OK");
}
// # ==> Set show_length
if (payload[0] == '^') {
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
show_length = ((b >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("WS: Set show_length to: [%u]\n", show_length);
webSocket.sendTXT(num, "OK");
}
// # ==> Set fade to black speed
if (payload[0] == '_') {
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
ftb_speed = ((b >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("WS: Set fade to black speed to: [%u]\n", ftb_speed);
webSocket.sendTXT(num, "OK");
}
// # ==> Set fade glitter density
if (payload[0] == '+') {
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
glitter_density = ((b >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("WS: Set fade to glitter density to: [%u]\n", glitter_density);
webSocket.sendTXT(num, "OK");
}
// * ==> Set main color and light all LEDs (Shortcut)
if (payload[0] == '*') {
// decode rgb data
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
main_color.red = ((rgb >> 16) & 0xFF);
main_color.green = ((rgb >> 8) & 0xFF);
main_color.blue = ((rgb >> 0) & 0xFF);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(main_color.red, main_color.green, main_color.blue);
}
FastLED.show();
DBG_OUTPUT_PORT.printf("WS: Set all leds to main color: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue);
exit_func = true;
mode = ALL;
webSocket.sendTXT(num, "OK");
}
// ! ==> Set single LED in given color
if (payload[0] == '!') {
// decode led index
uint64_t rgb = (uint64_t) strtol((const char *) &payload[1], NULL, 16);
uint8_t led = ((rgb >> 24) & 0xFF);
if (led < NUM_LEDS) {
ledstates[led].red = ((rgb >> 16) & 0xFF);
ledstates[led].green = ((rgb >> 8) & 0xFF);
ledstates[led].blue = ((rgb >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("WS: Set single led [%u] to [%u] [%u] [%u]!\n", led, ledstates[led].red, ledstates[led].green, ledstates[led].blue);
for (uint8_t i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(ledstates[i].red, ledstates[i].green, ledstates[i].blue);
}
FastLED.show();
}
exit_func = true;
mode = ALL;
webSocket.sendTXT(num, "OK");
}
// ! ==> Activate mode
if (payload[0] == '=') {
// we get mode data
String str_mode = String((char *) &payload[0]);
exit_func = true;
if (str_mode.startsWith("=off")) {
mode = OFF;
}
if (str_mode.startsWith("=all")) {
mode = ALL;
}
if (str_mode.startsWith("=mixedshow")) {
mode = MIXEDSHOW;
}
if (str_mode.startsWith("=rainbow")) {
mode = RAINBOW;
}
if (str_mode.startsWith("=confetti")) {
mode = CONFETTI;
}
if (str_mode.startsWith("=sinelon")) {
mode = SINELON;
}
if (str_mode.startsWith("=juggle")) {
mode = JUGGLE;
}
if (str_mode.startsWith("=bpm")) {
mode = BPM;
}
if (str_mode.startsWith("=palette_anims")) {
mode = PALETTE_ANIMS;
}
if (str_mode.startsWith("=ripple")) {
mode = RIPPLE;
}
if (str_mode.startsWith("=comet")) {
mode = COMET;
}
if (str_mode.startsWith("=theaterchase")) {
mode = THEATERCHASE;
}
if (str_mode.startsWith("=add_glitter")) {
GLITTER_ON = true;
}
if (str_mode.startsWith("=stop_glitter")) {
GLITTER_ON = false;
}
DBG_OUTPUT_PORT.printf("Activated mode [%u]!\n", mode);
webSocket.sendTXT(num, "OK");
}
// $ ==> Get status Info.
if (payload[0] == '$') {
DBG_OUTPUT_PORT.printf("Get status info.");
String json = listStatusJSON();
DBG_OUTPUT_PORT.println(json);
webSocket.sendTXT(num, json);
}
break;
}
}
void checkForRequests() {
webSocket.loop();
server.handleClient();
}