-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
687 lines (582 loc) · 21.3 KB
/
Controller.cpp
File metadata and controls
687 lines (582 loc) · 21.3 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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#include "Controller.h"
Controller::Controller(ESP8266WebServer* server, Animation** pCurrAnim, Background** pCurrBg) : server(server), pCurrAnim(pCurrAnim), pCurrBg(pCurrBg) {
this->setupHandlers();
}
// LEDs
bool Controller::getLedsOn() {
return this->ledsOn;
}
void Controller::setLedsOn(bool on) {
this->ledsOn = on;
}
void Controller::initBg() {
Background* bg = NULL;
String bg_default = String(this->getBgDefault());
if(bg_default == "rainbow") {
bg = new RainbowBackground(scale16by8(this->getBgRainbowSpeed(), 38), false); // Convert from 100% speed to 15bpm (38/256 ~ 15/100)
}
else if(bg_default == "cycle") {
bg = new CycleBackground(scale16by8(this->getBgCycleSpeed(), 38)); // Convert from 100% speed to 15bpm (38/256 ~ 15/100)
}
else if(bg_default == "off") {
bg = new OffBackground();
}
else if(bg_default == "color") {
bg = new ColorBackground(CRGB(this->getBgColorColor()));
}
else if(bg_default == "beat") {
bg = new BeatBackground(CRGB(this->getBgBeatColor()), scale16by8(this->getBgBeatSpeed(), 38)); // Convert from 100% speed to 15bpm (38/256 ~ 15/100)
}
this->startBackground(bg);
}
void Controller::startAnimation(Animation* newAnim) {
if(*(this->pCurrAnim) != NULL) { // Interrupt currently running animation
delete *(this->pCurrAnim);
*(this->pCurrAnim) = NULL;
}
*(this->pCurrAnim) = newAnim;
}
void Controller::startBackground(Background* newBg) {
if(*(this->pCurrBg) != NULL) { // Interrupt currently running background
delete *(this->pCurrBg);
*(this->pCurrBg) = NULL;
}
*(this->pCurrBg) = newBg;
}
// Wifi
void Controller::setupHandlers() {
this->server->on("/", [this](){ this->handleRoot(); });
this->server->on("/alert/sweep", [this](){ this->handleAlert("sweep"); });
this->server->on("/alert/fill", [this](){ this->handleAlert("fill"); });
this->server->on("/alert/flash", [this](){ this->handleAlert("flash"); });
this->server->on("/alert/intro", [this](){ this->handleAlert("intro"); });
this->server->on("/bg/rainbow", [this](){ this->handleBg("rainbow"); });
this->server->on("/bg/cycle", [this](){ this->handleBg("cycle"); });
this->server->on("/bg/off", [this](){ this->handleBg("off"); });
this->server->on("/bg/color", [this](){ this->handleBg("color"); });
this->server->on("/bg/beat", [this](){ this->handleBg("beat"); });
this->server->on("/settings", [this](){ this->handleSettings(); });
this->server->on("/settings/alert", HTTP_GET, [this](){ this->handleSettingsAlert(); });
this->server->on("/settings/alert", HTTP_POST, [this](){ this->saveSettingsAlert(); });
this->server->on("/settings/bg", HTTP_GET, [this](){ this->handleSettingsBg(); });
this->server->on("/settings/bg", HTTP_POST, [this](){ this->saveSettingsBg(); });
this->server->on("/off", [this](){ this->handleOff(); });
this->server->on("/on", [this](){ this->handleOn(); });
}
void Controller::handleRoot() {
Serial.println("handleRoot()");
// Headers
String page = FPSTR(HTTP_AGS_HEAD);
page.replace("{v}", "Settings");
page += FPSTR(HTTP_AGS_SCRIPT);
page += FPSTR(HTTP_AGS_STYLE);
page += FPSTR(HTTP_AGS_HEAD_END);
// Content
page += FPSTR(HTTP_AGS_TITLE);
page.replace("{t}", AP_NAME);
page += String(F("<h3>Home page</h3>"));
String item = FPSTR(HTTP_AGS_HOME);
item.replace("{ip}", WiFi.localIP().toString());
page += item;
// Footer
String stickyFooter = FPSTR(HTTP_AGS_FOOTER);
stickyFooter.replace("{v}", VERSION);
page += stickyFooter;
page += FPSTR(HTTP_AGS_END);
this->server->sendHeader("Content-Length", String(page.length()));
this->server->send(200, "text/html", page);
}
void Controller::handleAlert(String name) {
Serial.println("handleAlert() " + name);
if(name == "") {
this->server->send(204);
return;
}
else if((name == "sweep") || (name == "fill") || (name == "flash")) {
// Default parameters
uint32_t speed = 100;
CRGB color(0xFF0000);
if(name == "sweep") {
speed = this->getAlertSweepSpeed();
}
else if(name == "fill") {
speed = this->getAlertFillSpeed();
}
else if(name == "flash") {
speed = this->getAlertFlashSpeed();
}
// Requested parameters (if they exist)
if(this->server->arg("c") != "") {
uint32_t colorCode = parseColorCode(this->server->arg("c"));
if(colorCode <= 0xFFFFFF) {
color = CRGB(colorCode);
}
}
if(this->server->arg("s") != "") {
uint32_t number = parseNumber(this->server->arg("s"));
if((number >= 10) && (number <= 500)) {
speed = number;
}
}
// Run the animation
if(name == "sweep") {
uint32_t total_ms = (uint32_t)6500 * 100 / speed;
this->startAnimation(new SweepAnimation(total_ms, color));
}
else if(name == "fill") {
uint32_t total_ms = (uint32_t)3250 * 100 / speed;
this->startAnimation(new FillAnimation(total_ms, color));
}
else if(name == "flash") {
uint32_t total_ms = (uint32_t)3250 * 100 / speed;
this->startAnimation(new FlashAnimation(total_ms, color));
}
}
else if(name == "intro") {
// Default parameters
CRGB color(this->getAlertIntroColor());
bool glitchy = this->getAlertIntroGlitchy();
// Requested parameters (if they exist)
if(this->server->arg("c") != "") {
uint32_t colorCode = parseColorCode(this->server->arg("c"));
if(colorCode <= 0xFFFFFF) {
color = CRGB(colorCode);
}
}
if(this->server->arg("g") == "true") {
glitchy = true;
}
// Run the animation
this->startAnimation(new IntroAnimation(glitchy, color));
}
this->server->send(204);
}
void Controller::handleBg(String name) {
Serial.println("handleBg() " + name);
if(name == "") {
this->server->send(204);
return;
}
// Check name validity
bool validBgName = false;
for(uint8_t i = 0; i < Background::num; i++) { // Check background names
if(Background::name[i] == name) {
validBgName = true;
break;
}
}
if(!validBgName) {
this->server->send(400, "text/plain", "400: Invalid Request");
return;
}
if(name == "off") {
// Run the background
this->startBackground(new OffBackground());
}
else if((name == "rainbow") || (name == "cycle")) {
// Default parameters
uint32_t speed = 100;
if(name == "rainbow") {
speed = this->getBgRainbowSpeed();
}
else if(name == "cycle") {
speed = this->getBgCycleSpeed();
}
// Requested parameters (if they exist)
if(this->server->arg("s") != "") {
uint32_t number = parseNumber(this->server->arg("s"));
if((number >= 10) && (number <= 500)) {
speed = number;
}
}
// Run the background
if(name == "rainbow") {
uint8_t bpm = scale16by8(speed, 38); // Convert from 100% speed to 15bpm (38/256 ~ 15/100)
this->startBackground(new RainbowBackground(bpm, false));
}
else if(name == "cycle") {
uint8_t bpm = scale16by8(speed, 38); // Convert from 100% speed to 15bpm (38/256 ~ 15/100)
this->startBackground(new CycleBackground(bpm));
}
}
else if(name == "color") {
// Default parameters
CRGB color(this->getBgColorColor());
// Requested parameters (if they exist)
if(this->server->arg("c") != "") {
uint32_t colorCode = parseColorCode(this->server->arg("c"));
if(colorCode <= 0xFFFFFF) {
color = CRGB(colorCode);
}
}
// Run the background
this->startBackground(new ColorBackground(color));
}
else if(name == "beat") {
// Default parameters
uint32_t speed = this->getBgBeatSpeed();
CRGB color(this->getBgBeatColor());
// Requested parameters (if they exist)
if(this->server->arg("s") != "") {
uint32_t number = parseNumber(this->server->arg("s"));
if((number >= 10) && (number <= 500)) {
speed = number;
}
}
if(this->server->arg("c") != "") {
uint32_t colorCode = parseColorCode(this->server->arg("c"));
if(colorCode <= 0xFFFFFF) {
color = CRGB(colorCode);
}
}
// Run the background
uint8_t bpm = scale16by8(speed, 38); // Convert from 100% speed to 15bpm (38/256 ~ 15/100)
this->startBackground(new BeatBackground(color, bpm));
}
// Copy over to cfg
strcpy(this->cfg.bg_default, name.c_str());
this->server->send(204);
}
void Controller::handleSettings() {
Serial.println("handleSettings()");
// Headers
String page = FPSTR(HTTP_AGS_HEAD);
page.replace("{v}", "Settings");
page += FPSTR(HTTP_AGS_SCRIPT);
page += FPSTR(HTTP_AGS_STYLE);
page += FPSTR(HTTP_AGS_HEAD_END);
// Content
page += FPSTR(HTTP_AGS_TITLE);
page.replace("{t}", AP_NAME);
page += String(F("<h3>Settings</h3>"));
String buttons = FPSTR(HTTP_AGS_SETTINGS_OPTIONS);
buttons.replace("{v}", this->getLedsOn() ? "Disable LEDs" : "Enable LEDs");
buttons.replace("{i}", this->getLedsOn() ? "/off" : "/on");
page += buttons;
// Footer
String stickyFooter = FPSTR(HTTP_AGS_FOOTER);
stickyFooter.replace("{v}", VERSION);
page += stickyFooter;
page += FPSTR(HTTP_AGS_END);
this->server->sendHeader("Content-Length", String(page.length()));
this->server->send(200, "text/html", page);
}
void Controller::handleSettingsAlert() {
Serial.println("handleSettingsAlert()");
// Headers
String page = FPSTR(HTTP_AGS_HEAD);
page.replace("{v}", "Alerts settings");
page += FPSTR(HTTP_AGS_SCRIPT);
page += FPSTR(HTTP_AGS_STYLE);
page += FPSTR(HTTP_AGS_HEAD_END);
// Content
page += FPSTR(HTTP_AGS_TITLE);
page.replace("{t}", AP_NAME);
page += String(F("<h3>Alerts settings</h3>"));
String item = FPSTR(HTTP_AGS_SETTINGS_FORM_START);
item.replace("{a}", "/settings/alert"); // Submit action
item.replace("{cs}", "\"ic\""); // Color fields to convert at submit
page += item;
item = FPSTR(HTTP_AGS_SETTINGS_ALERT_SWEEP);
item.replace("{s}", String(this->getAlertSweepSpeed()));
page += item;
item = FPSTR(HTTP_AGS_SETTINGS_ALERT_FILL);
item.replace("{s}", String(this->getAlertFillSpeed()));
page += item;
item = FPSTR(HTTP_AGS_SETTINGS_ALERT_FLASH);
item.replace("{s}", String(this->getAlertFlashSpeed()));
page += item;
item = FPSTR(HTTP_AGS_SETTINGS_ALERT_INTRO);
item.replace("{g}", this->getAlertIntroGlitchy() ? "checked" : "");
item.replace("{c}", colorCodeToHex(this->getAlertIntroColor()));
page += item;
page += FPSTR(HTTP_AGS_SETTINGS_FORM_END);
// Footer
item = FPSTR(HTTP_AGS_FOOTER);
item.replace("{v}", VERSION);
page += item;
page += FPSTR(HTTP_AGS_END);
this->server->sendHeader("Content-Length", String(page.length()));
this->server->send(200, "text/html", page);
}
void Controller::saveSettingsAlert() {
Serial.println("saveSettingsAlert()");
if(!this->server->hasArg("ss") ||
!this->server->hasArg("fs") ||
!this->server->hasArg("fls") ||
!this->server->hasArg("ic") ||
this->server->arg("ss") == NULL ||
this->server->arg("fs") == NULL ||
this->server->arg("fls") == NULL ||
this->server->arg("ic") == NULL) {
this->server->send(400, "text/plain", "400: Invalid Request");
return;
}
// Parse into temporary memory
Config tmp;
tmp.alert_sweep_speed = parseNumber(this->server->arg("ss"));
tmp.alert_fill_speed = parseNumber(this->server->arg("fs"));
tmp.alert_flash_speed = parseNumber(this->server->arg("fls"));
tmp.alert_intro_color = parseColorCode(this->server->arg("ic"));
tmp.alert_intro_glitchy = this->server->arg("intro_glitchy") == "on" ? true : false;
Serial.println("tmp alert config: ");
printConfig(tmp);
// Copy over to cfg
this->cfg.alert_sweep_speed = tmp.alert_sweep_speed;
this->cfg.alert_fill_speed = tmp.alert_fill_speed;
this->cfg.alert_flash_speed = tmp.alert_flash_speed;
this->cfg.alert_intro_color = tmp.alert_intro_color;
this->cfg.alert_intro_glitchy = tmp.alert_intro_glitchy;
// Save to SPIFFS
if(!this->saveConfig()) {
this->server->send(500, "text/plain", "500: Failed to save config to SPIFFS");
return;
}
this->server->sendHeader("Location", "/settings", true);
this->server->send(303, "text/plain", "");
}
void Controller::handleSettingsBg() {
Serial.println("handleSettingsBg()");
// Headers
String page = FPSTR(HTTP_AGS_HEAD);
page.replace("{v}", "Backgrounds settings");
page += FPSTR(HTTP_AGS_SCRIPT);
page += FPSTR(HTTP_AGS_STYLE);
page += FPSTR(HTTP_AGS_HEAD_END);
// Content
page += FPSTR(HTTP_AGS_TITLE);
page.replace("{t}", AP_NAME);
page += String(F("<h3>Backgrounds settings</h3>"));
String item = FPSTR(HTTP_AGS_SETTINGS_FORM_START);
item.replace("{a}", "/settings/bg"); // Submit action
item.replace("{cs}", "\"cc\", \"bc\""); // Color fields to convert at submit
page += item;
page += FPSTR(HTTP_AGS_SETTINGS_BG_DEFAULT_START);
for(uint8_t i = 0; i < Background::num; i++) { // Add all backgrounds
item = FPSTR(HTTP_AGS_SETTINGS_BG_DEFAULT_OPTION);
item.replace("{v}", Background::name[i]);
item.replace("{n}", Background::prettyName[i]);
if(Background::name[i] == this->getBgDefault())
item.replace("{s}", "selected");
page += item;
}
page += FPSTR(HTTP_AGS_SETTINGS_BG_DEFAULT_END);
item = FPSTR(HTTP_AGS_SETTINGS_BG_RAINBOW);
item.replace("{s}", String(this->getBgRainbowSpeed()));
page += item;
item = FPSTR(HTTP_AGS_SETTINGS_BG_CYCLE);
item.replace("{s}", String(this->getBgCycleSpeed()));
page += item;
page += FPSTR(HTTP_AGS_SETTINGS_BG_OFF);
item = FPSTR(HTTP_AGS_SETTINGS_BG_COLOR);
item.replace("{c}", colorCodeToHex(this->getBgColorColor()));
page += item;
item = FPSTR(HTTP_AGS_SETTINGS_BG_BEAT);
item.replace("{c}", colorCodeToHex(this->getBgBeatColor()));
item.replace("{s}", String(this->getBgBeatSpeed()));
page += item;
page += FPSTR(HTTP_AGS_SETTINGS_FORM_END);
// Footer
item = FPSTR(HTTP_AGS_FOOTER);
item.replace("{v}", VERSION);
page += item;
page += FPSTR(HTTP_AGS_END);
this->server->sendHeader("Content-Length", String(page.length()));
this->server->send(200, "text/html", page);
}
void Controller::saveSettingsBg() {
Serial.println("saveSettingsBg()");
if(!this->server->hasArg("dbg") ||
!this->server->hasArg("rs") ||
!this->server->hasArg("cs") ||
!this->server->hasArg("cc") ||
!this->server->hasArg("bs") ||
!this->server->hasArg("bc") ||
this->server->arg("dbg") == NULL ||
this->server->arg("rs") == NULL ||
this->server->arg("cs") == NULL ||
this->server->arg("cc") == NULL ||
this->server->arg("bs") == NULL ||
this->server->arg("bc") == NULL) {
this->server->send(400, "text/plain", "400: Invalid Request");
return;
}
bool validBgName = false;
for(uint8_t i = 0; i < Background::num; i++) { // Check background names
if(Background::name[i] == this->server->arg("dbg")) {
validBgName = true;
break;
}
}
if(!validBgName) {
this->server->send(400, "text/plain", "400: Invalid Request");
return;
}
// Parse into temporary memory
Config tmp;
strcpy(tmp.bg_default, this->server->arg("dbg").c_str());
tmp.bg_rainbow_speed = parseNumber(this->server->arg("rs"));
tmp.bg_cycle_speed = parseNumber(this->server->arg("cs"));
tmp.bg_color_color = parseColorCode(this->server->arg("cc"));
tmp.bg_beat_speed = parseNumber(this->server->arg("bs"));
tmp.bg_beat_color = parseColorCode(this->server->arg("bc"));
Serial.println("tmp bg config: ");
printConfig(tmp);
// Copy over to cfg
strcpy(this->cfg.bg_default, tmp.bg_default);
this->cfg.bg_rainbow_speed = tmp.bg_rainbow_speed;
this->cfg.bg_cycle_speed = tmp.bg_cycle_speed;
this->cfg.bg_color_color = tmp.bg_color_color;
this->cfg.bg_beat_speed = tmp.bg_beat_speed;
this->cfg.bg_beat_color = tmp.bg_beat_color;
// Save to SPIFFS
if(!this->saveConfig()) {
this->server->send(500, "text/plain", "500: Failed to save config to SPIFFS");
return;
}
this->initBg(); // Restart the default bg (in case the user tried another bg)
this->server->sendHeader("Location", "/settings", true);
this->server->send(303, "text/plain", "");
}
void Controller::handleOff() {
Serial.println("handleOff()");
this->setLedsOn(false);
this->server->sendHeader("Location", "settings", true);
this->server->send(303, "text/plain", "");
}
void Controller::handleOn() {
Serial.println("handleOn()");
this->setLedsOn(true);
this->server->sendHeader("Location", "settings", true);
this->server->send(303, "text/plain", "");
}
bool Controller::loadConfig() {
File configFile = LittleFS.open("/config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}
size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
// We don't use String here because ArduinoJson library requires the input
// buffer to be mutable. If you don't use ArduinoJson, you may as well
// use configFile.readString instead.
configFile.readBytes(buf.get(), size);
StaticJsonDocument<512> doc;
auto error = deserializeJson(doc, buf.get());
if (error) {
Serial.println("Failed to parse config file");
return false;
}
this->cfg.alert_sweep_speed = doc["alert_sweep_speed"];
this->cfg.alert_fill_speed = doc["alert_fill_speed"];
this->cfg.alert_flash_speed = doc["alert_flash_speed"];
this->cfg.alert_intro_color = doc["alert_intro_color"];
this->cfg.alert_intro_glitchy = doc["alert_intro_glitchy"];
this->cfg.bg_rainbow_speed = doc["bg_rainbow_speed"];
this->cfg.bg_cycle_speed = doc["bg_cycle_speed"];
this->cfg.bg_color_color = doc["bg_color_color"];
this->cfg.bg_beat_speed = doc["bg_beat_speed"];
this->cfg.bg_beat_color = doc["bg_beat_color"];
strcpy(this->cfg.bg_default, doc["bg_default"].as<const char*>());
Serial.println("Loaded: ");
printConfig(this->cfg);
return true;
}
bool Controller::saveConfig() {
StaticJsonDocument<512> doc;
Serial.println("Saving: ");
printConfig(this->cfg);
doc["alert_sweep_speed"] = this->cfg.alert_sweep_speed;
doc["alert_fill_speed"] = this->cfg.alert_fill_speed;
doc["alert_flash_speed"] = this->cfg.alert_flash_speed;
doc["alert_intro_color"] = this->cfg.alert_intro_color;
doc["alert_intro_glitchy"] = this->cfg.alert_intro_glitchy;
doc["bg_rainbow_speed"] = this->cfg.bg_rainbow_speed;
doc["bg_cycle_speed"] = this->cfg.bg_cycle_speed;
doc["bg_color_color"] = this->cfg.bg_color_color;
doc["bg_beat_speed"] = this->cfg.bg_beat_speed;
doc["bg_beat_color"] = this->cfg.bg_beat_color;
doc["bg_default"] = this->cfg.bg_default;
File configFile = LittleFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config file for writing");
return false;
}
serializeJson(doc, configFile);
return true;
}
uint16_t Controller::getAlertSweepSpeed() {
return this->cfg.alert_sweep_speed;
}
uint16_t Controller::getAlertFillSpeed() {
return this->cfg.alert_fill_speed;
}
uint16_t Controller::getAlertFlashSpeed() {
return this->cfg.alert_flash_speed;
}
uint32_t Controller::getAlertIntroColor() {
return this->cfg.alert_intro_color;
}
bool Controller::getAlertIntroGlitchy() {
return this->cfg.alert_intro_glitchy;
}
uint16_t Controller::getBgRainbowSpeed() {
return this->cfg.bg_rainbow_speed;
}
uint16_t Controller::getBgCycleSpeed() {
return this->cfg.bg_cycle_speed;
}
uint32_t Controller::getBgColorColor() {
return this->cfg.bg_color_color;
}
uint16_t Controller::getBgBeatSpeed() {
return this->cfg.bg_beat_speed;
}
uint32_t Controller::getBgBeatColor() {
return this->cfg.bg_beat_color;
}
char* Controller::getBgDefault() {
return this->cfg.bg_default;
}
uint32_t parseColorCode(String c) {
char pColor[] = "0xFF0000"; // C char array
c.toCharArray(pColor, 9);
if(pColor[0] == '0' && pColor[1] == 'x') {
return strtol(pColor, NULL, 16);
}
else {
return (uint32_t)-1;
}
}
uint32_t parseNumber(String c) {
char pNumber[11] = ""; // C char array up to 32bit digit
c.toCharArray(pNumber, 11);
return strtol(pNumber, NULL, 10);
}
String colorCodeToHex(uint32_t c) {
if(c > 0xFFFFFF) return String("Invalid color code: ") + String(c);
char buf[10];
sprintf(buf, "#%06X", c);
return String(buf);
}
void printConfig(Config &cfg) {
Serial.println("Config [");
Serial.print(" alert_sweep_speed\t"); Serial.println(cfg.alert_sweep_speed);
Serial.print(" alert_fill_speed\t"); Serial.println(cfg.alert_fill_speed);
Serial.print(" alert_flash_speed\t"); Serial.println(cfg.alert_flash_speed);
Serial.print(" alert_intro_color\t"); Serial.println(colorCodeToHex(cfg.alert_intro_color));
Serial.print(" alert_intro_glitchy\t"); Serial.println(cfg.alert_intro_glitchy ? "true" : "false");
Serial.print(" bg_rainbow_speed\t"); Serial.println(cfg.bg_rainbow_speed);
Serial.print(" bg_cycle_speed\t"); Serial.println(cfg.bg_cycle_speed);
Serial.print(" bg_color_color\t"); Serial.println(colorCodeToHex(cfg.bg_color_color));
Serial.print(" bg_beat_speed\t"); Serial.println(cfg.bg_beat_speed);
Serial.print(" bg_beat_color\t"); Serial.println(colorCodeToHex(cfg.bg_beat_color));
Serial.print(" bg_default\t"); Serial.println(cfg.bg_default);
Serial.println("]");
}