-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomm_ws.h
More file actions
221 lines (195 loc) · 6.75 KB
/
comm_ws.h
File metadata and controls
221 lines (195 loc) · 6.75 KB
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
#ifndef COMM_WS_H
#define COMM_WS_H
#include <ArduinoJson.h>
#include <WebSocketsServer.h>
WebSocketsServer webSocket = WebSocketsServer(81);
extern const bool SERIAL_DEBUG;
extern void setZygoteDMX( uint8_t f, uint8_t r, uint8_t g, uint8_t b, uint8_t w );
extern bool savePresets();
extern bool loadPresets();
/*
JSON object:
let state = {
speed: 127,
delay: 0,
hue: 160,
mode: 0,
hue_cycle: 0,
brightness: 255,
white: 0,
preset: 0,
num_devices: 8
};
local struct:
struct State = {
uint8_t speed = 127;
uint8_t delay = 0;
uint8_t hue = 160;
uint8_t mode = 0;
uint8_t hue_cycle = 0;
uint8_t brightness = 255;
uint8_t white = 0;
uint8_t preset = 0;
uint8_t num_devices = 8;
} state;
*/
/* --------- JSON Deserialize --------- */
/* (read JSON from websocket callback. Works for Presets (ARRAY) and State (SINGLE OBJECT)) */
const uint16_t jsonReceiveSize = 256;
bool deserializeJSON(uint8_t * json) {
StaticJsonBuffer<jsonReceiveSize*3> jsonBuffer; // actually only needs to be about 240 bytes, extra reserved for future use (or just because I have the space..). (http://arduinojson.org/assistant/)
JsonVariant variant = jsonBuffer.parse(json);
if (variant.is<JsonArray>()) {
JsonArray& array = variant;
if (!array.success()) {
if (SERIAL_DEBUG) Serial.printf("[JSON] parseArray() failed\n");
return false;
}
else {
if (SERIAL_DEBUG) Serial.printf("[JSON] JSON Array test values: ");
for (int i = 0; i < NUM_PRESETS; i++) {
presets[i].speed = array[i]["speed"];
presets[i].delay = array[i]["delay"];
presets[i].hue = array[i]["hue"];
uint8_t _mode = array[i]["mode"];
presets[i].mode = (Mode) _mode;
presets[i].hue_cycle = array[i]["hue_cycle"];
presets[i].brightness = array[i]["brightness"];
presets[i].white = array[i]["white"];
presets[i].preset = array[i]["preset"];
presets[i].num_devices = array[i]["num_devices"];
}
if (SERIAL_DEBUG) Serial.println();
}
if (array.success()) { // this means we parsed a new preset array!
savePresetFlag = true; // so save it!
return true;
}
else {
return false;
}
}
else if (variant.is<JsonObject>()) {
JsonObject& root = variant;
if (!root.success()) {
if (SERIAL_DEBUG) Serial.printf("[JSON] parseObject() failed\n");
return false;
}
else {
// SDC(/LFO):
state.speed = root["speed"];
sdc_speed = (256 - state.speed)/5.0f;
state.delay = root["delay"];
state.hue = root["hue"];
static uint8_t previousHue;
if (state.hue != previousHue) {
for (int i = 0; i < state.num_devices; i++) {
leds[i].h = state.hue;
}
previousHue = state.hue;
}
// MODE:
uint8_t _mode = root["mode"];
state.mode = (Mode) _mode;
// HUE CYCLE:
state.hue_cycle = root["hue_cycle"];
if (state.hue_cycle == 0) hueCycleSpeed = 0;
else hueCycleSpeed = map(state.hue_cycle, 0, 255, 175, 1);
// BRIGHTNESS, WHITE:
state.brightness = root["brightness"];
state.white = root["white"];
for (int i = 0; i < state.num_devices; i++) {
whiteLeds[i].r = root["white"]; // using red channel of fastLED for white control
}
// PRESET:
state.preset = root["preset"];
// NUMBER OF DEVICES:
state.num_devices = root["num_devices"];
}
return root.success();
}
else {
if (SERIAL_DEBUG) Serial.printf("[JSON] variant not Object or Array\n");
return false;
}
}
/* --------- JSON Serialize State --------- */
/* (prep current state to send over websockets. Called when a client connects ) */
const uint16_t jsonSendSize = 256;
void serializeJSON_state(char * json) {
// send state:
StaticJsonBuffer<jsonSendSize> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["speed"] = state.speed;
root["delay"] = state.delay;
root["hue"] = state.hue;
root["mode"] = state.mode;
root["hue_cycle"] = state.hue_cycle;
root["brightness"] = state.brightness;
root["white"] = state.white;
root["preset"] = state.preset;
root["num_devices"] = state.num_devices;
root.printTo(json, jsonSendSize);
//if (SERIAL_DEBUG) Serial.println(json);
if (SERIAL_DEBUG) root.prettyPrintTo(Serial);
}
/* --------- JSON Serialize Presets --------- */
/* (prep current presets to send over websockets. Called when a client connects ) */
void serializeJSON_presets(char * json) {
// send presets:
DynamicJsonBuffer jsonBuffer; // switched to dynamic array - static array causing problems in for-loops
JsonArray& array = jsonBuffer.createArray();
for (int i = 0; i < NUM_PRESETS; i++) {
//StaticJsonBuffer<jsonSendSize> jsonBufferTemp;
JsonObject& root = jsonBuffer.createObject();
root["speed"] = presets[i].speed;
root["delay"] = presets[i].delay;
root["hue"] = presets[i].hue;
root["mode"] = presets[i].mode;
root["hue_cycle"] = presets[i].hue_cycle;
root["brightness"] = presets[i].brightness;
root["white"] = presets[i].white;
root["preset"] = presets[i].preset;
root["num_devices"] = presets[i].num_devices;
array.add(root);
}
array.printTo(json, jsonBuffer.size());
if (SERIAL_DEBUG) array.prettyPrintTo(Serial);
}
/*---------- Websocket Event ------------*/
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
switch (type) {
case WStype_ERROR:
if (SERIAL_DEBUG) Serial.printf("[ws] ERROR!\n");
break;
case WStype_DISCONNECTED:
if (SERIAL_DEBUG) Serial.printf("[ws] [%u] Disconnected!\n", num);
break;
case WStype_CONNECTED: {
// num is client ID (because I am a websocket server!)
IPAddress ip = webSocket.remoteIP(num);
if (SERIAL_DEBUG) Serial.printf("[ws] [%u] Connected from url: %u.%u.%u.%u%s replying...\n", num, ip[0], ip[1], ip[2], ip[3], payload);
loadPresets();
yield();
char presetJSON[jsonSendSize*3];
serializeJSON_presets(presetJSON);
webSocket.sendTXT(num, presetJSON);
char connJSON[jsonSendSize];
serializeJSON_state(connJSON);
webSocket.sendTXT(num, connJSON); // send to same client (num) that connected to me!
}
break;
case WStype_TEXT: {
if (SERIAL_DEBUG) Serial.printf("[ws] [%u] got text: %s\n", num, payload);
deserializeJSON(payload);
}
break;
case WStype_BIN:
if (SERIAL_DEBUG) Serial.printf("[ws] got binary (length): %zu\n", length);
hexdump(payload, length);
break;
default:
break;
}
}
#endif /* COMM_WS_H */