forked from forkineye/ESPixelStick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wshandler.h
294 lines (260 loc) · 9.17 KB
/
wshandler.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
/*
* ESPixelStick.h
*
* Project: ESPixelStick - An ESP8266 and E1.31 based pixel driver
* Copyright (c) 2016 Shelby Merrick
* http://www.forkineye.com
*
* This program is provided free for you to use in any way that you wish,
* subject to the laws and regulations where you are using it. Due diligence
* is strongly suggested before using this code. Please give credit where due.
*
* The Author makes no warranty of any kind, express or implied, with regard
* to this program or the documentation contained in this document. The
* Author shall not be liable in any event for incidental or consequential
* damages in connection with, or arising out of, the furnishing, performance
* or use of these programs.
*
*/
#ifndef WSHANDLER_H_
#define WSHANDLER_H_
/*
Packet Commands
E1 - Get Elements
G1 - Get Config
G2 - Get Config Status
T0 - Disable Testing
T1 - Static Testing
S1 - Set Network Config
S2 - Set Device Config
X1 - Get RSSI
X2 - Get E131 Status
X6 - Reboot
*/
EFUpdate efupdate;
void procX(uint8_t *data, AsyncWebSocketClient *client) {
switch (data[1]) {
case '1':
client->text("X1" + (String)WiFi.RSSI());
break;
case '2': {
uint32_t seqErrors = 0;
for (int i = 0; i < ((uniLast + 1) - config.universe); i++)
seqErrors =+ seqError[i];
client->text("X2" + (String)config.universe + ":" +
(String)uniLast + ":" +
(String)e131.stats.num_packets + ":" +
(String)seqErrors + ":" +
(String)e131.stats.packet_errors);
break;
}
case '6': // Init 6 baby, reboot!
reboot = true;
}
}
void procE(uint8_t *data, AsyncWebSocketClient *client) {
switch (data[1]) {
case '1':
// Create buffer and root object
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
#if defined (ESPS_MODE_PIXEL)
// Pixel Types
JsonObject &p_type = json.createNestedObject("p_type");
p_type["WS2811 800kHz"] = static_cast<uint8_t>(PixelType::WS2811);
p_type["GE Color Effects"] = static_cast<uint8_t>(PixelType::GECE);
// Pixel Colors
JsonObject &p_color = json.createNestedObject("p_color");
p_color["RGB"] = static_cast<uint8_t>(PixelColor::RGB);
p_color["GRB"] = static_cast<uint8_t>(PixelColor::GRB);
p_color["BRG"] = static_cast<uint8_t>(PixelColor::BRG);
p_color["RBG"] = static_cast<uint8_t>(PixelColor::RBG);
p_color["GBR"] = static_cast<uint8_t>(PixelColor::GBR);
p_color["BGR"] = static_cast<uint8_t>(PixelColor::BGR);
#elif defined (ESPS_MODE_SERIAL)
// Serial Protocols
JsonObject &s_proto = json.createNestedObject("s_proto");
s_proto["DMX512"] = static_cast<uint8_t>(SerialType::DMX512);
s_proto["Renard"] = static_cast<uint8_t>(SerialType::RENARD);
// Serial Baudrates
JsonObject &s_baud = json.createNestedObject("s_baud");
s_baud["38400"] = static_cast<uint32_t>(BaudRate::BR_38400);
s_baud["57600"] = static_cast<uint32_t>(BaudRate::BR_57600);
s_baud["115200"] = static_cast<uint32_t>(BaudRate::BR_115200);
s_baud["230400"] = static_cast<uint32_t>(BaudRate::BR_230400);
s_baud["250000"] = static_cast<uint32_t>(BaudRate::BR_250000);
s_baud["460800"] = static_cast<uint32_t>(BaudRate::BR_460800);
#endif
String response;
json.printTo(response);
client->text("E1" + response);
break;
}
}
void procG(uint8_t *data, AsyncWebSocketClient *client) {
switch (data[1]) {
case '1': {
String response;
serializeConfig(response, false, true);
client->text("G1" + response);
break;
}
case '2':
// Create buffer and root object
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
json["ssid"] = (String)WiFi.SSID();
json["ip"] = WiFi.localIP().toString();
json["mac"] = getMacAddress();
json["version"] = (String)VERSION;
json["testing"] = static_cast<uint8_t>(config.testmode);
String response;
json.printTo(response);
client->text("G2" + response);
break;
}
}
void procS(uint8_t *data, AsyncWebSocketClient *client) {
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.parseObject(reinterpret_cast<char*>(data + 2));
if (!json.success()) {
LOG_PORT.println(F("*** procS(): Parse Error ***"));
LOG_PORT.println(reinterpret_cast<char*>(data));
return;
}
switch (data[1]) {
case '1': // Set Network Config
dsNetworkConfig(json);
saveConfig();
client->text("S1");
break;
case '2': // Set Device Config
dsDeviceConfig(json);
saveConfig();
client->text("S2");
break;
}
}
/*
enum class TestMode : uint8_t {
DISABLED,
STATIC,
CHASE,
RAINBOW,
VIEW_STREAM
};
*/
void procT(uint8_t *data, AsyncWebSocketClient *client) {
switch(data[1]) {
case '0':
config.testmode = TestMode::DISABLED;
//clear whole string
#if defined(ESPS_MODE_PIXEL)
for(int y =0; y < config.channel_count; y++) pixels.setValue(y, 0);
#elif defined(ESPS_MODE_SERIAL)
for(int y =0; y < config.channel_count; y++) serial.setValue(y, 0);
#endif
break;
case '1': {//static color
config.testmode = TestMode::STATIC;
testing.step = 0;
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.parseObject(reinterpret_cast<char*>(data + 2));
testing.r = json["r"];
testing.g = json["g"];
testing.b = json["b"];
break;
}
case '2': {//chase
config.testmode = TestMode::CHASE;
testing.step = 0;
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.parseObject(reinterpret_cast<char*>(data + 2));
testing.r = json["r"];
testing.g = json["g"];
testing.b = json["b"];
break;
}
case '3': //rainbow
config.testmode = TestMode::RAINBOW;
testing.step = 0;
break;
case '4': {//view stream
config.testmode = TestMode::VIEW_STREAM;
#if defined(ESPS_MODE_PIXEL)
client->binary(pixels.getData(),config.channel_count);
#elif defined(ESPS_MODE_SERIAL)
if( config.serial_type == SerialType::DMX512)
client->binary(&serial.getData()[1],config.channel_count);
else
client->binary(&serial.getData()[2],config.channel_count);
#endif
break;
}
}
}
void handle_fw_upload(AsyncWebServerRequest *request, String filename,
size_t index, uint8_t *data, size_t len, bool final) {
if (!index) {
WiFiUDP::stopAll();
LOG_PORT.print(F("* Upload Started: "));
LOG_PORT.println(filename.c_str());
efupdate.begin();
}
if (!efupdate.process(data, len)) {
LOG_PORT.print(F("*** UPDATE ERROR: "));
LOG_PORT.println(String(efupdate.getError()));
}
if (efupdate.hasError())
request->send(200, "text/plain", "Update Error: " +
String(efupdate.getError()));
if (final) {
LOG_PORT.println(F("* Upload Finished."));
efupdate.end();
SPIFFS.begin();
saveConfig();
reboot = true;
}
}
void wsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
AwsEventType type, void * arg, uint8_t *data, size_t len) {
switch (type) {
case WS_EVT_DATA: {
AwsFrameInfo *info = static_cast<AwsFrameInfo*>(arg);
if (info->opcode == WS_TEXT) {
switch (data[0]) {
case 'X':
procX(data, client);
break;
case 'E':
procE(data, client);
break;
case 'G':
procG(data, client);
break;
case 'S':
procS(data, client);
break;
case 'T':
procT(data, client);
break;
}
} else {
LOG_PORT.println(F("-- binary message --"));
}
break;
}
case WS_EVT_CONNECT:
LOG_PORT.print(F("* WS Connect - "));
LOG_PORT.println(client->id());
break;
case WS_EVT_DISCONNECT:
LOG_PORT.print(F("* WS Disconnect - "));
LOG_PORT.println(client->id());
break;
case WS_EVT_ERROR:
LOG_PORT.println(F("** WS ERROR **"));
break;
}
}
#endif /* ESPIXELSTICK_H_ */