-
Notifications
You must be signed in to change notification settings - Fork 0
/
boblight.cpp
465 lines (425 loc) · 11.9 KB
/
boblight.cpp
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include <stdint.h>
#include <math.h>
#include <Arduino.h>
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h> // Base ESP8266 includes
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <pgmspace.h>
#include <FastLED.h>
#include "eepromdata.h"
#include "boblight.h"
extern CRGB *leds[];
extern uint16_t numLEDs[];
uint8_t bobStrip = 0; // Default: first zone
light_t lights[MAX_LEDS];
uint16_t numLights = 0;
// strings (to reduce IRAM pressure)
static const char _CONFIG[] PROGMEM = "/config.bob";
void fillBobLights(int bottom, int left, int top, int right, float pct_scan)
{
/*
# boblight
# Copyright (C) Bob 2009
#
# makeboblight.sh created by Adam Boeglin <[email protected]>
#
# boblight is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# boblight is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
*/
int lightcount = 0;
int total = top+left+right+bottom;
int bcount;
if ( total > MAX_LEDS ) {
#if DEBUG
Serial.println(F("Too many lights."));
#endif
return;
}
if ( bottom > 0 )
{
bcount = 1;
float brange = 100.0/bottom;
float bcurrent = 50.0;
if ( bottom < top )
{
int diff = top - bottom;
brange = 100.0/top;
bcurrent -= (diff/2)*brange;
}
while ( bcount <= bottom/2 )
{
float btop = bcurrent - brange;
String name = "b"+String(bcount);
strncpy(lights[lightcount].lightname, name.c_str(), 4);
lights[lightcount].hscan[0] = btop;
lights[lightcount].hscan[1] = bcurrent;
lights[lightcount].vscan[0] = 100 - pct_scan;
lights[lightcount].vscan[1] = 100;
lightcount+=1;
bcurrent = btop;
bcount+=1;
}
}
if ( left > 0 )
{
int lcount = 1;
float lrange = 100.0/left;
float lcurrent = 100.0;
while (lcount <= left )
{
float ltop = lcurrent - lrange;
String name = "l"+String(lcount);
strncpy(lights[lightcount].lightname, name.c_str(), 4);
lights[lightcount].hscan[0] = 0;
lights[lightcount].hscan[1] = pct_scan;
lights[lightcount].vscan[0] = ltop;
lights[lightcount].vscan[1] = lcurrent;
lightcount+=1;
lcurrent = ltop;
lcount+=1;
}
}
if ( top > 0 )
{
int tcount = 1;
float trange = 100.0/top;
float tcurrent = 0;
while ( tcount <= top )
{
float ttop = tcurrent + trange;
String name = "t"+String(tcount);
strncpy(lights[lightcount].lightname, name.c_str(), 4);
lights[lightcount].hscan[0] = tcurrent;
lights[lightcount].hscan[1] = ttop;
lights[lightcount].vscan[0] = 0;
lights[lightcount].vscan[1] = pct_scan;
lightcount+=1;
tcurrent = ttop;
tcount+=1;
}
}
if ( right > 0 )
{
int rcount = 1;
float rrange = 100.0/right;
float rcurrent = 0;
while ( rcount <= right )
{
float rtop = rcurrent + rrange;
String name = "r"+String(rcount);
strncpy(lights[lightcount].lightname, name.c_str(), 4);
lights[lightcount].hscan[0] = 100-pct_scan;
lights[lightcount].hscan[1] = 100;
lights[lightcount].vscan[0] = rcurrent;
lights[lightcount].vscan[1] = rtop;
lightcount+=1;
rcurrent = rtop;
rcount+=1;
}
}
if ( bottom > 0 )
{
float brange = 100.0/bottom;
float bcurrent = 100;
if ( bottom < top )
{
brange = 100.0/top;
}
while ( bcount <= bottom )
{
float btop = bcurrent - brange;
String name = "b"+String(bcount);
strncpy(lights[lightcount].lightname, name.c_str(), 4);
lights[lightcount].hscan[0] = btop;
lights[lightcount].hscan[1] = bcurrent;
lights[lightcount].vscan[0] = 100 - pct_scan;
lights[lightcount].vscan[1] = 100;
lightcount+=1;
bcurrent = btop;
bcount+=1;
}
}
numLights = lightcount;
#if DEBUG
Serial.println(F("Fill light data: "));
char tmp[64];
sprintf(tmp, " lights %d\n", numLights);
Serial.print(tmp);
for (int i=0; i<numLights; i++)
{
sprintf(tmp, " light %s scan %2.1f %2.1f %2.1f %2.1f\n", lights[i].lightname, lights[i].vscan[0], lights[i].vscan[1], lights[i].hscan[0], lights[i].hscan[1]);
Serial.print(tmp);
}
#endif
}
bool saveBobConfig()
{
//read configuration from FS json
if ( SPIFFS.begin() )
{
String conf_str = FPSTR(_CONFIG);
String tmp_str;
//file exists, reading and loading
File configFile = SPIFFS.open(conf_str, "w");
if ( configFile )
{
int b=0,r=0,t=0,l=0;
float pct=0;
DynamicJsonDocument doc(256);
doc["lights"] = numLights;
for ( int i=0; i<numLights; i++ )
{
switch ( lights[i].lightname[0] )
{
case 'b' : b++; if (!pct) pct = lights[i].vscan[1] - lights[i].vscan[0]; break;
case 'l' : l++; if (!pct) pct = lights[i].hscan[1] - lights[i].hscan[0]; break;
case 't' : t++; if (!pct) pct = lights[i].vscan[1] - lights[i].vscan[0]; break;
case 'r' : r++; if (!pct) pct = lights[i].hscan[1] - lights[i].hscan[0]; break;
}
}
doc["top"] = t;
doc["bottom"] = b;
doc["left"] = l;
doc["right"] = r;
doc["pct"] = pct;
serializeJson(doc, configFile);
configFile.close();
#if DEBUG
Serial.println(F("Save config: "));
serializeJson(doc, Serial);
Serial.println();
#endif
return true;
}
else
{
#if DEBUG
Serial.println(F("Failed to create config file."));
#endif
}
}
else
{
#if DEBUG
Serial.println(F("Failed to initialize SPIFFS."));
#endif
}
//end save
return false;
}
bool loadBobConfig()
{
//read configuration from FS json
if ( SPIFFS.begin() )
{
String conf_str = FPSTR(_CONFIG);
String tmp_str;
if ( SPIFFS.exists(conf_str) )
{
#if DEBUG
Serial.println(F("Reading SPIFFS data."));
#endif
//file exists, reading and loading
File configFile = SPIFFS.open(conf_str, "r");
if ( configFile )
{
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
char *buf = (char*)malloc(size+1);
configFile.readBytes(buf, size);
buf[size] = '\0';
#if DEBUG
Serial.println(size,DEC);
Serial.println(buf);
#endif
DynamicJsonDocument doc(256);
DeserializationError error = deserializeJson(doc, buf);
if ( !error && doc["lights"] != nullptr )
{
numLights = doc["lights"];
fillBobLights(doc["bottom"], doc["left"], doc["top"], doc["right"], doc["pct"]);
}
else
{
#if DEBUG
Serial.println(F("Error in config file."));
Serial.println(error.c_str());
#endif
}
free(buf);
return true;
}
}
}
//end read
return false;
}
void BobSync()
{
yield(); // allow other tasks
FastLED[bobStrip].showLeds(255);
}
void BobClear()
{
fill_solid(leds[bobStrip], numLights, CRGB::Black);
BobSync();
}
void setBobColor(uint16_t led, uint8_t red, uint8_t green, uint8_t blue)
{
CRGB lcolor;
lcolor.r = red;
lcolor.g = green;
lcolor.b = blue;
leds[bobStrip][led] = lcolor;
}
// main boblight handling
void pollBob() {
//check if there are any new clients
if (bob.hasClient())
{
//find free/disconnected spot
if (!bobClient || !bobClient.connected())
{
if (bobClient) bobClient.stop();
bobClient = bob.available();
}
//no free/disconnected spot so reject
WiFiClient bobClient = bob.available();
bobClient.stop();
BobClear();
}
//check clients for data
if (bobClient && bobClient.connected())
{
if (bobClient.available())
{
//get data from the client
while (bobClient.available())
{
String input = bobClient.readStringUntil('\n');
//Serial.print("Client: "); Serial.println(input);
if (input.startsWith("hello"))
{
#if DEBUG
Serial.println(F("hello"));
#endif
bobClient.print("hello\n");
}
else if (input.startsWith("ping"))
{
#if DEBUG
Serial.println(F("ping 1"));
#endif
bobClient.print("ping 1\n");
}
else if (input.startsWith("get version"))
{
#if DEBUG
Serial.println(F("version 5"));
#endif
bobClient.print("version 5\n");
}
else if (input.startsWith("get lights"))
{
char tmp[64];
String answer = "";
sprintf(tmp, "lights %d\n", numLights);
#if DEBUG
Serial.println(tmp);
#endif
answer.concat(tmp);
for (int i=0; i<numLights; i++)
{
sprintf_P(tmp, PSTR("light %s scan %2.1f %2.1f %2.1f %2.1f\n"), lights[i].lightname, lights[i].vscan[0], lights[i].vscan[1], lights[i].hscan[0], lights[i].hscan[1]);
#if DEBUG
Serial.print(tmp);
#endif
answer.concat(tmp);
}
bobClient.print(answer);
}
else if (input.startsWith("set priority"))
{
#if DEBUG
Serial.println(F("set priority not implemented"));
#endif
// not implemented
}
else if (input.startsWith("set light "))
{ // <id> <cmd in rgb, speed, interpolation> <value> ...
input.remove(0,10);
String tmp = input.substring(0,input.indexOf(' '));
int light_id = -1;
for ( uint16_t i=0; i<numLights; i++ )
{
if ( strncmp(lights[i].lightname, tmp.c_str(), 4) == 0 )
{
light_id = i;
break;
}
}
if ( light_id == -1 )
return;
/*
#if DEBUG
Serial.print(F("light found "));
Serial.println(light_id,DEC);
#endif
*/
input.remove(0,input.indexOf(' ')+1);
if ( input.startsWith("rgb ") )
{
input.remove(0,4);
tmp = input.substring(0,input.indexOf(' '));
uint8_t red = (uint8_t)(255.0f*tmp.toFloat());
input.remove(0,input.indexOf(' ')+1); // remove first float value
tmp = input.substring(0,input.indexOf(' '));
uint8_t green = (uint8_t)(255.0f*tmp.toFloat());
input.remove(0,input.indexOf(' ')+1); // remove second float value
tmp = input.substring(0,input.indexOf(' '));
uint8_t blue = (uint8_t)(255.0f*tmp.toFloat());
/*
#if DEBUG
Serial.println(F("color set "));
Serial.print(red,HEX);
Serial.print(green,HEX);
Serial.print(blue,HEX);
Serial.println();
#endif
*/
setBobColor(light_id, red, green, blue);
} // currently no support for interpolation or speed, we just ignore this
}
else if (input.startsWith("sync"))
{
/*
#if DEBUG
Serial.println(F("sync"));
#endif
*/
BobSync();
}
else
{
// Client sent gibberish
#if DEBUG
Serial.println(F("Client sent gibberish."));
#endif
bobClient.stop();
bobClient = bob.available();
BobClear();
}
}
}
}
}