forked from OpenStickCommunity/GP2040-CE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebconfig.cpp
More file actions
2777 lines (2450 loc) · 135 KB
/
webconfig.cpp
File metadata and controls
2777 lines (2450 loc) · 135 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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "config.pb.h"
#include "base64.h"
#include "hardware/adc.h"
#include "helper.h"
#include "drivermanager.h"
#include "storagemanager.h"
#include "eventmanager.h"
#include "layoutmanager.h"
#include "peripheralmanager.h"
#include "animationstorage.h"
#include "system.h"
#include "config_utils.h"
#include "types.h"
#include "version.h"
#include <cstring>
#include <string>
#include <vector>
#include <memory>
#include <set>
#include <pico/types.h>
// for hall-effect calibration
#include "hardware/adc.h"
// HTTPD Includes
#include <ArduinoJson.h>
#include "rndis.h"
#include "fs.h"
#include "fscustom.h"
#include "fsdata.h"
#include "lwip/apps/httpd.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "addons/input_macro.h"
#define PATH_CGI_ACTION "/cgi/action"
#define LWIP_HTTPD_POST_MAX_PAYLOAD_LEN (1024 * 16)
extern struct fsdata_file file__index_html[];
const static char* spaPaths[] = { "/backup", "/display-config", "/led-config", "/pin-mapping", "/settings", "/reset-settings", "/add-ons", "/custom-theme", "/macro", "/peripheral-mapping" };
const static char* excludePaths[] = { "/css", "/images", "/js", "/static" };
const static uint32_t rebootDelayMs = 500;
static string http_post_uri;
static char http_post_payload[LWIP_HTTPD_POST_MAX_PAYLOAD_LEN];
static uint16_t http_post_payload_len = 0;
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T, typename K>
static void __attribute__((noinline)) readDoc(T& var, const DynamicJsonDocument& doc, const K& key)
{
var = doc[key];
}
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T, typename K0, typename K1>
static void __attribute__((noinline)) readDoc(T& var, const DynamicJsonDocument& doc, const K0& key0, const K1& key1)
{
var = doc[key0][key1];
}
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T, typename K0, typename K1, typename K2>
static void __attribute__((noinline)) readDoc(T& var, const DynamicJsonDocument& doc, const K0& key0, const K1& key1, const K2& key2)
{
var = doc[key0][key1][key2];
}
// Don't inline this function, we do not want to consume stack space in the calling function
static bool __attribute__((noinline)) hasValue(const DynamicJsonDocument& doc, const char* key0, const char* key1)
{
return doc[key0][key1] != nullptr;
}
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T>
static void __attribute__((noinline)) docToValue(T& value, const DynamicJsonDocument& doc, const char* key)
{
if (doc[key] != nullptr)
{
value = doc[key];
}
}
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T>
static void __attribute__((noinline)) docToValue(T& value, const DynamicJsonDocument& doc, const char* key0, const char* key1)
{
if (doc[key0][key1] != nullptr)
{
value = doc[key0][key1];
}
}
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T>
static void __attribute__((noinline)) docToValue(T& value, const DynamicJsonDocument& doc, const char* key0, const char* key1, const char* key2)
{
if (doc[key0][key1][key2] != nullptr)
{
value = doc[key0][key1][key2];
}
}
// Don't inline this function, we do not want to consume stack space in the calling function
static void __attribute__((noinline)) cleanAddonGpioMappings(Pin_t& addonPin, Pin_t oldAddonPin)
{
GpioMappingInfo* gpioMappings = Storage::getInstance().getGpioMappings().pins;
ProfileOptions& profiles = Storage::getInstance().getProfileOptions();
// if the new addon pin value is valid, mark it assigned in GpioMappings
if (isValidPin(addonPin))
{
gpioMappings[addonPin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[0].pins[addonPin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[1].pins[addonPin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[2].pins[addonPin].action = GpioAction::ASSIGNED_TO_ADDON;
} else {
// -1 is our de facto value for "not assigned" in addons
addonPin = -1;
}
// either way now, the addon's pin config is set to its real value, if the
// old value is a real pin (and different), we should unset it
if (isValidPin(oldAddonPin) && oldAddonPin != addonPin)
{
gpioMappings[oldAddonPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[0].pins[oldAddonPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[1].pins[oldAddonPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[2].pins[oldAddonPin].action = GpioAction::NONE;
}
}
// Don't inline this function, we do not want to consume stack space in the calling function
static void __attribute__((noinline)) docToPin(Pin_t& pin, const DynamicJsonDocument& doc, const char* key)
{
Pin_t oldPin = pin;
if (doc.containsKey(key))
{
pin = doc[key];
cleanAddonGpioMappings(pin, oldPin);
}
}
// Don't inline this function, we do not want to consume stack space in the calling function
static void __attribute__((noinline)) docToPin(Pin_t& pin, const DynamicJsonDocument& doc, const char* key0, const char* key1)
{
Pin_t oldPin = pin;
if (doc.containsKey(key0) && doc[key0].containsKey(key1))
{
pin = doc[key0][key1];
cleanAddonGpioMappings(pin, oldPin);
}
}
// Don't inline this function, we do not want to consume stack space in the calling function
static void __attribute__((noinline)) docToPin(Pin_t& pin, const DynamicJsonDocument& doc, const char* key0, const char* key1, const char* key2)
{
Pin_t oldPin = pin;
if (doc.containsKey(key0) && doc[key0].containsKey(key1) && doc[key0][key1].containsKey(key2))
{
pin = doc[key0][key1][key2];
cleanAddonGpioMappings(pin, oldPin);
}
}
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T, typename K>
static void __attribute__((noinline)) writeDoc(DynamicJsonDocument& doc, const K& key, const T& var)
{
doc[key] = var;
}
// Don't inline this function, we do not want to consume stack space in the calling function
// Web-config frontend compatibility workaround
template <typename K>
static void __attribute__((noinline)) writeDoc(DynamicJsonDocument& doc, const K& key, const bool& var)
{
doc[key] = var ? 1 : 0;
}
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T, typename K0, typename K1>
static void __attribute__((noinline)) writeDoc(DynamicJsonDocument& doc, const K0& key0, const K1& key1, const T& var)
{
doc[key0][key1] = var;
}
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T, typename K0, typename K1, typename K2>
static void __attribute__((noinline)) writeDoc(DynamicJsonDocument& doc, const K0& key0, const K1& key1, const K2& key2, const T& var)
{
doc[key0][key1][key2] = var;
}
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T, typename K0, typename K1, typename K2, typename K3>
static void __attribute__((noinline)) writeDoc(DynamicJsonDocument& doc, const K0& key0, const K1& key1, const K2& key2, const K3& key3, const T& var)
{
doc[key0][key1][key2][key3] = var;
}
// Don't inline this function, we do not want to consume stack space in the calling function
template <typename T, typename K0, typename K1, typename K2, typename K3, typename K4>
static void __attribute__((noinline)) writeDoc(DynamicJsonDocument& doc, const K0& key0, const K1& key1, const K2& key2, const K3& key3, const K4& key4, const T& var)
{
doc[key0][key1][key2][key3][key4] = var;
}
static int32_t cleanPin(int32_t pin) { return isValidPin(pin) ? pin : -1; }
enum class HttpStatusCode
{
_200,
_400,
_500,
};
struct DataAndStatusCode
{
DataAndStatusCode(string&& data, HttpStatusCode statusCode) :
data(std::move(data)),
statusCode(statusCode)
{}
string data;
HttpStatusCode statusCode;
};
// **** WEB SERVER Overrides and Special Functionality ****
int set_file_data(fs_file* file, const DataAndStatusCode& dataAndStatusCode)
{
static string returnData;
const char* statusCodeStr = "";
switch (dataAndStatusCode.statusCode)
{
case HttpStatusCode::_200: statusCodeStr = "200 OK"; break;
case HttpStatusCode::_400: statusCodeStr = "400 Bad Request"; break;
case HttpStatusCode::_500: statusCodeStr = "500 Internal Server Error"; break;
}
returnData.clear();
returnData.append("HTTP/1.0 ");
returnData.append(statusCodeStr);
returnData.append("\r\n");
returnData.append(
"Server: GP2040-CE " GP2040VERSION "\r\n"
"Content-Type: application/json\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Content-Length: "
);
returnData.append(std::to_string(dataAndStatusCode.data.length()));
returnData.append("\r\n\r\n");
returnData.append(dataAndStatusCode.data);
file->data = returnData.c_str();
file->len = returnData.size();
file->index = file->len;
file->http_header_included = file->http_header_included;
file->pextension = NULL;
return 1;
}
int set_file_data(fs_file *file, string&& data)
{
if (data.empty())
return 0;
return set_file_data(file, DataAndStatusCode(std::move(data), HttpStatusCode::_200));
}
DynamicJsonDocument get_post_data()
{
DynamicJsonDocument doc(LWIP_HTTPD_POST_MAX_PAYLOAD_LEN);
deserializeJson(doc, http_post_payload, http_post_payload_len);
return doc;
}
void save_hotkey(HotkeyEntry* hotkey, const DynamicJsonDocument& doc, const string hotkey_key)
{
readDoc(hotkey->auxMask, doc, hotkey_key, "auxMask");
uint32_t buttonsMask = doc[hotkey_key]["buttonsMask"];
uint32_t dpadMask = 0;
if (buttonsMask & GAMEPAD_MASK_DU) {
dpadMask |= GAMEPAD_MASK_UP;
}
if (buttonsMask & GAMEPAD_MASK_DD) {
dpadMask |= GAMEPAD_MASK_DOWN;
}
if (buttonsMask & GAMEPAD_MASK_DL) {
dpadMask |= GAMEPAD_MASK_LEFT;
}
if (buttonsMask & GAMEPAD_MASK_DR) {
dpadMask |= GAMEPAD_MASK_RIGHT;
}
buttonsMask &= ~(GAMEPAD_MASK_DU | GAMEPAD_MASK_DD | GAMEPAD_MASK_DL | GAMEPAD_MASK_DR);
hotkey->dpadMask = dpadMask;
hotkey->buttonsMask = buttonsMask;
readDoc(hotkey->action, doc, hotkey_key, "action");
}
void load_hotkey(const HotkeyEntry* hotkey, DynamicJsonDocument& doc, const string hotkey_key)
{
writeDoc(doc, hotkey_key, "auxMask", hotkey->auxMask);
uint32_t buttonsMask = hotkey->buttonsMask;
if (hotkey->dpadMask & GAMEPAD_MASK_UP) {
buttonsMask |= GAMEPAD_MASK_DU;
}
if (hotkey->dpadMask & GAMEPAD_MASK_DOWN) {
buttonsMask |= GAMEPAD_MASK_DD;
}
if (hotkey->dpadMask & GAMEPAD_MASK_LEFT) {
buttonsMask |= GAMEPAD_MASK_DL;
}
if (hotkey->dpadMask & GAMEPAD_MASK_RIGHT) {
buttonsMask |= GAMEPAD_MASK_DR;
}
writeDoc(doc, hotkey_key, "buttonsMask", buttonsMask);
writeDoc(doc, hotkey_key, "action", hotkey->action);
}
// LWIP callback on HTTP POST to validate the URI
err_t httpd_post_begin(void *connection, const char *uri, const char *http_request,
uint16_t http_request_len, int content_len, char *response_uri,
uint16_t response_uri_len, uint8_t *post_auto_wnd)
{
LWIP_UNUSED_ARG(http_request);
LWIP_UNUSED_ARG(http_request_len);
LWIP_UNUSED_ARG(content_len);
LWIP_UNUSED_ARG(response_uri);
LWIP_UNUSED_ARG(response_uri_len);
LWIP_UNUSED_ARG(post_auto_wnd);
if (!uri || strncmp(uri, "/api", 4) != 0) {
return ERR_ARG;
}
http_post_uri = uri;
http_post_payload_len = 0;
memset(http_post_payload, 0, LWIP_HTTPD_POST_MAX_PAYLOAD_LEN);
return ERR_OK;
}
// LWIP callback on HTTP POST to for receiving payload
err_t httpd_post_receive_data(void *connection, struct pbuf *p)
{
LWIP_UNUSED_ARG(connection);
// Cache the received data to http_post_payload
while (p != NULL)
{
if (http_post_payload_len + p->len <= LWIP_HTTPD_POST_MAX_PAYLOAD_LEN)
{
MEMCPY(http_post_payload + http_post_payload_len, p->payload, p->len);
http_post_payload_len += p->len;
}
else // Buffer overflow
{
http_post_payload_len = 0xffff;
break;
}
p = p->next;
}
// Need to release memory here or will leak
pbuf_free(p);
// If the buffer overflows, error out
if (http_post_payload_len == 0xffff) {
return ERR_BUF;
}
return ERR_OK;
}
// LWIP callback to set the HTTP POST response_uri, which can then be looked up via the fs_custom callbacks
void httpd_post_finished(void *connection, char *response_uri, uint16_t response_uri_len)
{
LWIP_UNUSED_ARG(connection);
if (http_post_payload_len != 0xffff) {
strncpy(response_uri, http_post_uri.c_str(), response_uri_len);
response_uri[response_uri_len - 1] = '\0';
}
}
void addUsedPinsArray(DynamicJsonDocument& doc)
{
auto usedPins = doc.createNestedArray("usedPins");
GpioMappingInfo* gpioMappings = Storage::getInstance().getGpioMappings().pins;
for (unsigned int pin = 0; pin < NUM_BANK0_GPIOS; pin++) {
// NOTE: addons in webconfig break by seeing their own pins here; if/when they
// are refactored to ignore their own pins from this list, we can include them
if (gpioMappings[pin].action != GpioAction::NONE &&
gpioMappings[pin].action != GpioAction::ASSIGNED_TO_ADDON) {
usedPins.add(pin);
}
}
}
std::string serialize_json(DynamicJsonDocument &doc)
{
std::string data;
serializeJson(doc, data);
return data;
}
std::string getUsedPins()
{
const size_t capacity = JSON_OBJECT_SIZE(100);
DynamicJsonDocument doc(capacity);
addUsedPinsArray(doc);
return serialize_json(doc);
}
std::string setDisplayOptions(DisplayOptions& displayOptions)
{
DynamicJsonDocument doc = get_post_data();
readDoc(displayOptions.enabled, doc, "enabled");
readDoc(displayOptions.flip, doc, "flipDisplay");
readDoc(displayOptions.invert, doc, "invertDisplay");
readDoc(displayOptions.buttonLayout, doc, "buttonLayout");
readDoc(displayOptions.buttonLayoutRight, doc, "buttonLayoutRight");
readDoc(displayOptions.splashMode, doc, "splashMode");
readDoc(displayOptions.splashChoice, doc, "splashChoice");
readDoc(displayOptions.splashDuration, doc, "splashDuration");
readDoc(displayOptions.displaySaverTimeout, doc, "displaySaverTimeout");
readDoc(displayOptions.displaySaverMode, doc, "displaySaverMode");
readDoc(displayOptions.buttonLayoutOrientation, doc, "buttonLayoutOrientation");
readDoc(displayOptions.turnOffWhenSuspended, doc, "turnOffWhenSuspended");
readDoc(displayOptions.inputMode, doc, "inputMode");
readDoc(displayOptions.turboMode, doc, "turboMode");
readDoc(displayOptions.dpadMode, doc, "dpadMode");
readDoc(displayOptions.socdMode, doc, "socdMode");
readDoc(displayOptions.macroMode, doc, "macroMode");
readDoc(displayOptions.profileMode, doc, "profileMode");
readDoc(displayOptions.inputHistoryEnabled, doc, "inputHistoryEnabled");
readDoc(displayOptions.inputHistoryLength, doc, "inputHistoryLength");
readDoc(displayOptions.inputHistoryCol, doc, "inputHistoryCol");
readDoc(displayOptions.inputHistoryRow, doc, "inputHistoryRow");
readDoc(displayOptions.buttonLayoutCustomOptions.paramsLeft.layout, doc, "buttonLayoutCustomOptions", "params", "layout");
readDoc(displayOptions.buttonLayoutCustomOptions.paramsLeft.common.startX, doc, "buttonLayoutCustomOptions", "params", "startX");
readDoc(displayOptions.buttonLayoutCustomOptions.paramsLeft.common.startY, doc, "buttonLayoutCustomOptions", "params", "startY");
readDoc(displayOptions.buttonLayoutCustomOptions.paramsLeft.common.buttonRadius, doc, "buttonLayoutCustomOptions", "params", "buttonRadius");
readDoc(displayOptions.buttonLayoutCustomOptions.paramsLeft.common.buttonPadding, doc, "buttonLayoutCustomOptions", "params", "buttonPadding");
readDoc(displayOptions.buttonLayoutCustomOptions.paramsRight.layout, doc, "buttonLayoutCustomOptions", "paramsRight", "layout");
readDoc(displayOptions.buttonLayoutCustomOptions.paramsRight.common.startX, doc, "buttonLayoutCustomOptions", "paramsRight", "startX");
readDoc(displayOptions.buttonLayoutCustomOptions.paramsRight.common.startY, doc, "buttonLayoutCustomOptions", "paramsRight", "startY");
readDoc(displayOptions.buttonLayoutCustomOptions.paramsRight.common.buttonRadius, doc, "buttonLayoutCustomOptions", "paramsRight", "buttonRadius");
readDoc(displayOptions.buttonLayoutCustomOptions.paramsRight.common.buttonPadding, doc, "buttonLayoutCustomOptions", "paramsRight", "buttonPadding");
return serialize_json(doc);
}
std::string setDisplayOptions()
{
std::string response = setDisplayOptions(Storage::getInstance().getDisplayOptions());
EventManager::getInstance().triggerEvent(new GPStorageSaveEvent(true));
return response;
}
std::string setPreviewDisplayOptions()
{
std::string response = setDisplayOptions(Storage::getInstance().getDisplayOptions());
return response;
}
std::string getDisplayOptions() // Manually set Document Attributes for the display
{
const size_t capacity = JSON_OBJECT_SIZE(100);
DynamicJsonDocument doc(capacity);
const DisplayOptions& displayOptions = Storage::getInstance().getDisplayOptions();
writeDoc(doc, "enabled", displayOptions.enabled ? 1 : 0);
writeDoc(doc, "flipDisplay", displayOptions.flip);
writeDoc(doc, "invertDisplay", displayOptions.invert ? 1 : 0);
writeDoc(doc, "buttonLayout", displayOptions.buttonLayout);
writeDoc(doc, "buttonLayoutRight", displayOptions.buttonLayoutRight);
writeDoc(doc, "splashMode", displayOptions.splashMode);
writeDoc(doc, "splashChoice", displayOptions.splashChoice);
writeDoc(doc, "splashDuration", displayOptions.splashDuration);
writeDoc(doc, "displaySaverTimeout", displayOptions.displaySaverTimeout);
writeDoc(doc, "displaySaverMode", displayOptions.displaySaverMode);
writeDoc(doc, "buttonLayoutOrientation", displayOptions.buttonLayoutOrientation);
writeDoc(doc, "turnOffWhenSuspended", displayOptions.turnOffWhenSuspended);
writeDoc(doc, "inputMode", displayOptions.inputMode);
writeDoc(doc, "turboMode", displayOptions.turboMode);
writeDoc(doc, "dpadMode", displayOptions.dpadMode);
writeDoc(doc, "socdMode", displayOptions.socdMode);
writeDoc(doc, "macroMode", displayOptions.macroMode);
writeDoc(doc, "profileMode", displayOptions.profileMode);
writeDoc(doc, "inputHistoryEnabled", displayOptions.inputHistoryEnabled);
writeDoc(doc, "inputHistoryLength", displayOptions.inputHistoryLength);
writeDoc(doc, "inputHistoryCol", displayOptions.inputHistoryCol);
writeDoc(doc, "inputHistoryRow", displayOptions.inputHistoryRow);
writeDoc(doc, "buttonLayoutCustomOptions", "params", "layout", displayOptions.buttonLayoutCustomOptions.paramsLeft.layout);
writeDoc(doc, "buttonLayoutCustomOptions", "params", "startX", displayOptions.buttonLayoutCustomOptions.paramsLeft.common.startX);
writeDoc(doc, "buttonLayoutCustomOptions", "params", "startY", displayOptions.buttonLayoutCustomOptions.paramsLeft.common.startY);
writeDoc(doc, "buttonLayoutCustomOptions", "params", "buttonRadius", displayOptions.buttonLayoutCustomOptions.paramsLeft.common.buttonRadius);
writeDoc(doc, "buttonLayoutCustomOptions", "params", "buttonPadding", displayOptions.buttonLayoutCustomOptions.paramsLeft.common.buttonPadding);
writeDoc(doc, "buttonLayoutCustomOptions", "paramsRight", "layout", displayOptions.buttonLayoutCustomOptions.paramsRight.layout);
writeDoc(doc, "buttonLayoutCustomOptions", "paramsRight", "startX", displayOptions.buttonLayoutCustomOptions.paramsRight.common.startX);
writeDoc(doc, "buttonLayoutCustomOptions", "paramsRight", "startY", displayOptions.buttonLayoutCustomOptions.paramsRight.common.startY);
writeDoc(doc, "buttonLayoutCustomOptions", "paramsRight", "buttonRadius", displayOptions.buttonLayoutCustomOptions.paramsRight.common.buttonRadius);
writeDoc(doc, "buttonLayoutCustomOptions", "paramsRight", "buttonPadding", displayOptions.buttonLayoutCustomOptions.paramsRight.common.buttonPadding);
return serialize_json(doc);
}
std::string getSplashImage()
{
const DisplayOptions& displayOptions = Storage::getInstance().getDisplayOptions();
const size_t capacity = JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(displayOptions.splashImage.size);
DynamicJsonDocument doc(capacity);
JsonArray splashImageArray = doc.createNestedArray("splashImage");
copyArray(displayOptions.splashImage.bytes, displayOptions.splashImage.size, splashImageArray);
return serialize_json(doc);
}
std::string setSplashImage()
{
DynamicJsonDocument doc = get_post_data();
DisplayOptions& displayOptions = Storage::getInstance().getDisplayOptions();
std::string decoded;
std::string base64String = doc["splashImage"];
Base64::Decode(base64String, decoded);
const size_t length = std::min(decoded.length(), sizeof(displayOptions.splashImage.bytes));
memcpy(displayOptions.splashImage.bytes, decoded.data(), length);
displayOptions.splashImage.size = length;
EventManager::getInstance().triggerEvent(new GPStorageSaveEvent(true));
return serialize_json(doc);
}
std::string setProfileOptions()
{
DynamicJsonDocument doc = get_post_data();
ProfileOptions& profileOptions = Storage::getInstance().getProfileOptions();
GpioMappings& coreMappings = Storage::getInstance().getGpioMappings();
JsonObject options = doc.as<JsonObject>();
JsonArray alts = options["alternativePinMappings"];
int altsIndex = 0;
char pinName[6];
for (JsonObject alt : alts) {
for (Pin_t pin = 0; pin < (Pin_t)NUM_BANK0_GPIOS; pin++) {
snprintf(pinName, 6, "pin%0*d", 2, pin);
// setting a pin shouldn't change a new existing addon/reserved pin
// but if the profile definition is new, we should still capture the addon/reserved state
if (profileOptions.gpioMappingsSets[altsIndex].pins[pin].action != GpioAction::ASSIGNED_TO_ADDON &&
profileOptions.gpioMappingsSets[altsIndex].pins[pin].action != GpioAction::RESERVED &&
(GpioAction)alt[pinName]["action"] != GpioAction::RESERVED &&
(GpioAction)alt[pinName]["action"] != GpioAction::ASSIGNED_TO_ADDON) {
profileOptions.gpioMappingsSets[altsIndex].pins[pin].action = (GpioAction)alt[pinName]["action"];
profileOptions.gpioMappingsSets[altsIndex].pins[pin].customButtonMask = (uint32_t)alt[pinName]["customButtonMask"];
profileOptions.gpioMappingsSets[altsIndex].pins[pin].customDpadMask = (uint32_t)alt[pinName]["customDpadMask"];
} else if ((coreMappings.pins[pin].action == GpioAction::RESERVED &&
(GpioAction)alt[pinName]["action"] == GpioAction::RESERVED) ||
(coreMappings.pins[pin].action == GpioAction::ASSIGNED_TO_ADDON &&
(GpioAction)alt[pinName]["action"] == GpioAction::ASSIGNED_TO_ADDON)) {
profileOptions.gpioMappingsSets[altsIndex].pins[pin].action = (GpioAction)alt[pinName]["action"];
}
}
profileOptions.gpioMappingsSets[altsIndex].pins_count = NUM_BANK0_GPIOS;
size_t profileLabelSize = sizeof(profileOptions.gpioMappingsSets[altsIndex].profileLabel);
strncpy(profileOptions.gpioMappingsSets[altsIndex].profileLabel, alt["profileLabel"], profileLabelSize - 1);
profileOptions.gpioMappingsSets[altsIndex].profileLabel[profileLabelSize - 1] = '\0';
profileOptions.gpioMappingsSets[altsIndex].enabled = alt["enabled"];
profileOptions.gpioMappingsSets_count = ++altsIndex;
if (altsIndex > 4) break;
}
EventManager::getInstance().triggerEvent(new GPStorageSaveEvent(true));
return serialize_json(doc);
}
std::string getProfileOptions()
{
const size_t capacity = JSON_OBJECT_SIZE(500);
DynamicJsonDocument doc(capacity);
const auto writePinDoc = [&](const int item, const char* key, const GpioMappingInfo& value) -> void
{
writeDoc(doc, "alternativePinMappings", item, key, "action", value.action);
writeDoc(doc, "alternativePinMappings", item, key, "customButtonMask", value.customButtonMask);
writeDoc(doc, "alternativePinMappings", item, key, "customDpadMask", value.customDpadMask);
};
ProfileOptions& profileOptions = Storage::getInstance().getProfileOptions();
// return an empty list if no profiles are currently set, since we no longer populate by default
if (profileOptions.gpioMappingsSets_count == 0) {
doc.createNestedArray("alternativePinMappings");
}
for (int i = 0; i < profileOptions.gpioMappingsSets_count; i++) {
// this looks duplicative, but something in arduinojson treats the doc
// field string by reference so you can't be "clever" and do an snprintf
// thing or else you only send the last field in the JSON
writePinDoc(i, "pin00", profileOptions.gpioMappingsSets[i].pins[0]);
writePinDoc(i, "pin01", profileOptions.gpioMappingsSets[i].pins[1]);
writePinDoc(i, "pin02", profileOptions.gpioMappingsSets[i].pins[2]);
writePinDoc(i, "pin03", profileOptions.gpioMappingsSets[i].pins[3]);
writePinDoc(i, "pin04", profileOptions.gpioMappingsSets[i].pins[4]);
writePinDoc(i, "pin05", profileOptions.gpioMappingsSets[i].pins[5]);
writePinDoc(i, "pin06", profileOptions.gpioMappingsSets[i].pins[6]);
writePinDoc(i, "pin07", profileOptions.gpioMappingsSets[i].pins[7]);
writePinDoc(i, "pin08", profileOptions.gpioMappingsSets[i].pins[8]);
writePinDoc(i, "pin09", profileOptions.gpioMappingsSets[i].pins[9]);
writePinDoc(i, "pin10", profileOptions.gpioMappingsSets[i].pins[10]);
writePinDoc(i, "pin11", profileOptions.gpioMappingsSets[i].pins[11]);
writePinDoc(i, "pin12", profileOptions.gpioMappingsSets[i].pins[12]);
writePinDoc(i, "pin13", profileOptions.gpioMappingsSets[i].pins[13]);
writePinDoc(i, "pin14", profileOptions.gpioMappingsSets[i].pins[14]);
writePinDoc(i, "pin15", profileOptions.gpioMappingsSets[i].pins[15]);
writePinDoc(i, "pin16", profileOptions.gpioMappingsSets[i].pins[16]);
writePinDoc(i, "pin17", profileOptions.gpioMappingsSets[i].pins[17]);
writePinDoc(i, "pin18", profileOptions.gpioMappingsSets[i].pins[18]);
writePinDoc(i, "pin19", profileOptions.gpioMappingsSets[i].pins[19]);
writePinDoc(i, "pin20", profileOptions.gpioMappingsSets[i].pins[20]);
writePinDoc(i, "pin21", profileOptions.gpioMappingsSets[i].pins[21]);
writePinDoc(i, "pin22", profileOptions.gpioMappingsSets[i].pins[22]);
writePinDoc(i, "pin23", profileOptions.gpioMappingsSets[i].pins[23]);
writePinDoc(i, "pin24", profileOptions.gpioMappingsSets[i].pins[24]);
writePinDoc(i, "pin25", profileOptions.gpioMappingsSets[i].pins[25]);
writePinDoc(i, "pin26", profileOptions.gpioMappingsSets[i].pins[26]);
writePinDoc(i, "pin27", profileOptions.gpioMappingsSets[i].pins[27]);
writePinDoc(i, "pin28", profileOptions.gpioMappingsSets[i].pins[28]);
writePinDoc(i, "pin29", profileOptions.gpioMappingsSets[i].pins[29]);
writeDoc(doc, "alternativePinMappings", i, "profileLabel", profileOptions.gpioMappingsSets[i].profileLabel);
doc["alternativePinMappings"][i]["enabled"] = profileOptions.gpioMappingsSets[i].enabled;
}
return serialize_json(doc);
}
std::string setGamepadOptions()
{
DynamicJsonDocument doc = get_post_data();
GamepadOptions& gamepadOptions = Storage::getInstance().getGamepadOptions();
readDoc(gamepadOptions.dpadMode, doc, "dpadMode");
readDoc(gamepadOptions.inputMode, doc, "inputMode");
readDoc(gamepadOptions.inputDeviceType, doc, "inputDeviceType");
readDoc(gamepadOptions.socdMode, doc, "socdMode");
readDoc(gamepadOptions.switchTpShareForDs4, doc, "switchTpShareForDs4");
readDoc(gamepadOptions.lockHotkeys, doc, "lockHotkeys");
readDoc(gamepadOptions.fourWayMode, doc, "fourWayMode");
readDoc(gamepadOptions.profileNumber, doc, "profileNumber");
readDoc(gamepadOptions.debounceDelay, doc, "debounceDelay");
readDoc(gamepadOptions.inputModeB1, doc, "inputModeB1");
readDoc(gamepadOptions.inputModeB2, doc, "inputModeB2");
readDoc(gamepadOptions.inputModeB3, doc, "inputModeB3");
readDoc(gamepadOptions.inputModeB4, doc, "inputModeB4");
readDoc(gamepadOptions.inputModeL1, doc, "inputModeL1");
readDoc(gamepadOptions.inputModeL2, doc, "inputModeL2");
readDoc(gamepadOptions.inputModeR1, doc, "inputModeR1");
readDoc(gamepadOptions.inputModeR2, doc, "inputModeR2");
readDoc(gamepadOptions.ps4AuthType, doc, "ps4AuthType");
readDoc(gamepadOptions.ps5AuthType, doc, "ps5AuthType");
readDoc(gamepadOptions.xinputAuthType, doc, "xinputAuthType");
readDoc(gamepadOptions.ps4ControllerIDMode, doc, "ps4ControllerIDMode");
readDoc(gamepadOptions.usbDescOverride, doc, "usbDescOverride");
readDoc(gamepadOptions.miniMenuGamepadInput, doc, "miniMenuGamepadInput");
// Copy USB descriptor strings
size_t strSize = sizeof(gamepadOptions.usbDescManufacturer);
strncpy(gamepadOptions.usbDescManufacturer, doc["usbDescManufacturer"], strSize - 1);
gamepadOptions.usbDescManufacturer[strSize - 1] = '\0';
strSize = sizeof(gamepadOptions.usbDescProduct);
strncpy(gamepadOptions.usbDescProduct, doc["usbDescProduct"], strSize - 1);
gamepadOptions.usbDescProduct[strSize - 1] = '\0';
strSize = sizeof(gamepadOptions.usbDescVersion);
strncpy(gamepadOptions.usbDescVersion, doc["usbDescVersion"], strSize - 1);
gamepadOptions.usbDescVersion[strSize - 1] = '\0';
readDoc(gamepadOptions.usbOverrideID, doc, "usbOverrideID");
readDoc(gamepadOptions.usbVendorID, doc, "usbVendorID");
readDoc(gamepadOptions.usbProductID, doc, "usbProductID");
HotkeyOptions& hotkeyOptions = Storage::getInstance().getHotkeyOptions();
save_hotkey(&hotkeyOptions.hotkey01, doc, "hotkey01");
save_hotkey(&hotkeyOptions.hotkey02, doc, "hotkey02");
save_hotkey(&hotkeyOptions.hotkey03, doc, "hotkey03");
save_hotkey(&hotkeyOptions.hotkey04, doc, "hotkey04");
save_hotkey(&hotkeyOptions.hotkey05, doc, "hotkey05");
save_hotkey(&hotkeyOptions.hotkey06, doc, "hotkey06");
save_hotkey(&hotkeyOptions.hotkey07, doc, "hotkey07");
save_hotkey(&hotkeyOptions.hotkey08, doc, "hotkey08");
save_hotkey(&hotkeyOptions.hotkey09, doc, "hotkey09");
save_hotkey(&hotkeyOptions.hotkey10, doc, "hotkey10");
save_hotkey(&hotkeyOptions.hotkey11, doc, "hotkey11");
save_hotkey(&hotkeyOptions.hotkey12, doc, "hotkey12");
save_hotkey(&hotkeyOptions.hotkey13, doc, "hotkey13");
save_hotkey(&hotkeyOptions.hotkey14, doc, "hotkey14");
save_hotkey(&hotkeyOptions.hotkey15, doc, "hotkey15");
save_hotkey(&hotkeyOptions.hotkey16, doc, "hotkey16");
ForcedSetupOptions& forcedSetupOptions = Storage::getInstance().getForcedSetupOptions();
readDoc(forcedSetupOptions.mode, doc, "forcedSetupMode");
EventManager::getInstance().triggerEvent(new GPStorageSaveEvent(true));
return serialize_json(doc);
}
std::string getGamepadOptions()
{
const size_t capacity = JSON_OBJECT_SIZE(500);
DynamicJsonDocument doc(capacity);
GamepadOptions& gamepadOptions = Storage::getInstance().getGamepadOptions();
writeDoc(doc, "dpadMode", gamepadOptions.dpadMode);
writeDoc(doc, "inputMode", gamepadOptions.inputMode);
writeDoc(doc, "inputDeviceType", gamepadOptions.inputDeviceType);
writeDoc(doc, "socdMode", gamepadOptions.socdMode);
writeDoc(doc, "switchTpShareForDs4", gamepadOptions.switchTpShareForDs4 ? 1 : 0);
writeDoc(doc, "lockHotkeys", gamepadOptions.lockHotkeys ? 1 : 0);
writeDoc(doc, "fourWayMode", gamepadOptions.fourWayMode ? 1 : 0);
writeDoc(doc, "profileNumber", gamepadOptions.profileNumber);
writeDoc(doc, "debounceDelay", gamepadOptions.debounceDelay);
writeDoc(doc, "inputModeB1", gamepadOptions.inputModeB1);
writeDoc(doc, "inputModeB2", gamepadOptions.inputModeB2);
writeDoc(doc, "inputModeB3", gamepadOptions.inputModeB3);
writeDoc(doc, "inputModeB4", gamepadOptions.inputModeB4);
writeDoc(doc, "inputModeL1", gamepadOptions.inputModeL1);
writeDoc(doc, "inputModeL2", gamepadOptions.inputModeL2);
writeDoc(doc, "inputModeR1", gamepadOptions.inputModeR1);
writeDoc(doc, "inputModeR2", gamepadOptions.inputModeR2);
writeDoc(doc, "ps4AuthType", gamepadOptions.ps4AuthType);
writeDoc(doc, "ps5AuthType", gamepadOptions.ps5AuthType);
writeDoc(doc, "xinputAuthType", gamepadOptions.xinputAuthType);
writeDoc(doc, "ps4ControllerIDMode", gamepadOptions.ps4ControllerIDMode);
writeDoc(doc, "usbDescOverride", gamepadOptions.usbDescOverride);
writeDoc(doc, "usbDescManufacturer", gamepadOptions.usbDescManufacturer);
writeDoc(doc, "usbDescProduct", gamepadOptions.usbDescProduct);
writeDoc(doc, "usbDescVersion", gamepadOptions.usbDescVersion);
writeDoc(doc, "usbOverrideID", gamepadOptions.usbOverrideID);
writeDoc(doc, "miniMenuGamepadInput", gamepadOptions.miniMenuGamepadInput);
// Write USB Vendor ID and Product ID as 4 character hex strings with 0 padding
char usbVendorStr[5];
snprintf(usbVendorStr, 5, "%04X", gamepadOptions.usbVendorID);
writeDoc(doc, "usbVendorID", usbVendorStr);
char usbProductStr[5];
snprintf(usbProductStr, 5, "%04X", gamepadOptions.usbProductID);
writeDoc(doc, "usbProductID", usbProductStr);
writeDoc(doc, "fnButtonPin", -1);
GpioMappingInfo* gpioMappings = Storage::getInstance().getGpioMappings().pins;
for (unsigned int pin = 0; pin < NUM_BANK0_GPIOS; pin++) {
if (gpioMappings[pin].action == GpioAction::BUTTON_PRESS_FN) {
writeDoc(doc, "fnButtonPin", pin);
}
}
HotkeyOptions& hotkeyOptions = Storage::getInstance().getHotkeyOptions();
load_hotkey(&hotkeyOptions.hotkey01, doc, "hotkey01");
load_hotkey(&hotkeyOptions.hotkey02, doc, "hotkey02");
load_hotkey(&hotkeyOptions.hotkey03, doc, "hotkey03");
load_hotkey(&hotkeyOptions.hotkey04, doc, "hotkey04");
load_hotkey(&hotkeyOptions.hotkey05, doc, "hotkey05");
load_hotkey(&hotkeyOptions.hotkey06, doc, "hotkey06");
load_hotkey(&hotkeyOptions.hotkey07, doc, "hotkey07");
load_hotkey(&hotkeyOptions.hotkey08, doc, "hotkey08");
load_hotkey(&hotkeyOptions.hotkey09, doc, "hotkey09");
load_hotkey(&hotkeyOptions.hotkey10, doc, "hotkey10");
load_hotkey(&hotkeyOptions.hotkey11, doc, "hotkey11");
load_hotkey(&hotkeyOptions.hotkey12, doc, "hotkey12");
load_hotkey(&hotkeyOptions.hotkey13, doc, "hotkey13");
load_hotkey(&hotkeyOptions.hotkey14, doc, "hotkey14");
load_hotkey(&hotkeyOptions.hotkey15, doc, "hotkey15");
load_hotkey(&hotkeyOptions.hotkey16, doc, "hotkey16");
ForcedSetupOptions& forcedSetupOptions = Storage::getInstance().getForcedSetupOptions();
writeDoc(doc, "forcedSetupMode", forcedSetupOptions.mode);
return serialize_json(doc);
}
std::string setLedOptions()
{
DynamicJsonDocument doc = get_post_data();
const auto readIndex = [&](int32_t& var, const char* key0, const char* key1)
{
var = -1;
if (hasValue(doc, key0, key1))
{
readDoc(var, doc, key0, key1);
}
};
LEDOptions& ledOptions = Storage::getInstance().getLedOptions();
docToPin(ledOptions.dataPin, doc, "dataPin");
readDoc(ledOptions.ledFormat, doc, "ledFormat");
readDoc(ledOptions.ledLayout, doc, "ledLayout");
readDoc(ledOptions.ledsPerButton, doc, "ledsPerButton");
readDoc(ledOptions.brightnessMaximum, doc, "brightnessMaximum");
readDoc(ledOptions.brightnessSteps, doc, "brightnessSteps");
readDoc(ledOptions.turnOffWhenSuspended, doc, "turnOffWhenSuspended");
readIndex(ledOptions.indexUp, "ledButtonMap", "Up");
readIndex(ledOptions.indexDown, "ledButtonMap", "Down");
readIndex(ledOptions.indexLeft, "ledButtonMap", "Left");
readIndex(ledOptions.indexRight, "ledButtonMap", "Right");
readIndex(ledOptions.indexB1, "ledButtonMap", "B1");
readIndex(ledOptions.indexB2, "ledButtonMap", "B2");
readIndex(ledOptions.indexB3, "ledButtonMap", "B3");
readIndex(ledOptions.indexB4, "ledButtonMap", "B4");
readIndex(ledOptions.indexL1, "ledButtonMap", "L1");
readIndex(ledOptions.indexR1, "ledButtonMap", "R1");
readIndex(ledOptions.indexL2, "ledButtonMap", "L2");
readIndex(ledOptions.indexR2, "ledButtonMap", "R2");
readIndex(ledOptions.indexS1, "ledButtonMap", "S1");
readIndex(ledOptions.indexS2, "ledButtonMap", "S2");
readIndex(ledOptions.indexL3, "ledButtonMap", "L3");
readIndex(ledOptions.indexR3, "ledButtonMap", "R3");
readIndex(ledOptions.indexA1, "ledButtonMap", "A1");
readIndex(ledOptions.indexA2, "ledButtonMap", "A2");
readDoc(ledOptions.pledType, doc, "pledType");
docToPin(ledOptions.pledPin1, doc, "pledPin1");
docToPin(ledOptions.pledPin2, doc, "pledPin2");
docToPin(ledOptions.pledPin3, doc, "pledPin3");
docToPin(ledOptions.pledPin4, doc, "pledPin4");
readDoc(ledOptions.pledIndex1, doc, "pledIndex1");
readDoc(ledOptions.pledIndex2, doc, "pledIndex2");
readDoc(ledOptions.pledIndex3, doc, "pledIndex3");
readDoc(ledOptions.pledIndex4, doc, "pledIndex4");
readDoc(ledOptions.pledColor, doc, "pledColor");
readDoc(ledOptions.caseRGBType, doc, "caseRGBType");
readDoc(ledOptions.caseRGBIndex, doc, "caseRGBIndex");
readDoc(ledOptions.caseRGBCount, doc, "caseRGBCount");
EventManager::getInstance().triggerEvent(new GPStorageSaveEvent(true));
return serialize_json(doc);
}
std::string getLedOptions()
{
const size_t capacity = JSON_OBJECT_SIZE(500);
DynamicJsonDocument doc(capacity);
const LEDOptions& ledOptions = Storage::getInstance().getLedOptions();
writeDoc(doc, "dataPin", cleanPin(ledOptions.dataPin));
writeDoc(doc, "ledFormat", ledOptions.ledFormat);
writeDoc(doc, "ledLayout", ledOptions.ledLayout);
writeDoc(doc, "ledsPerButton", ledOptions.ledsPerButton);
writeDoc(doc, "brightnessMaximum", ledOptions.brightnessMaximum);
writeDoc(doc, "brightnessSteps", ledOptions.brightnessSteps);
writeDoc(doc, "turnOffWhenSuspended", ledOptions.turnOffWhenSuspended);
const auto writeIndex = [&](const char* key0, const char* key1, int var)
{
if (var < 0)
{
writeDoc(doc, key0, key1, nullptr);
}
else
{
writeDoc(doc, key0, key1, var);
}
};
writeIndex("ledButtonMap", "Up", ledOptions.indexUp);
writeIndex("ledButtonMap", "Down", ledOptions.indexDown);
writeIndex("ledButtonMap", "Left", ledOptions.indexLeft);
writeIndex("ledButtonMap", "Right", ledOptions.indexRight);
writeIndex("ledButtonMap", "B1", ledOptions.indexB1);
writeIndex("ledButtonMap", "B2", ledOptions.indexB2);
writeIndex("ledButtonMap", "B3", ledOptions.indexB3);
writeIndex("ledButtonMap", "B4", ledOptions.indexB4);
writeIndex("ledButtonMap", "L1", ledOptions.indexL1);
writeIndex("ledButtonMap", "R1", ledOptions.indexR1);
writeIndex("ledButtonMap", "L2", ledOptions.indexL2);
writeIndex("ledButtonMap", "R2", ledOptions.indexR2);
writeIndex("ledButtonMap", "S1", ledOptions.indexS1);
writeIndex("ledButtonMap", "S2", ledOptions.indexS2);
writeIndex("ledButtonMap", "L3", ledOptions.indexL3);
writeIndex("ledButtonMap", "R3", ledOptions.indexR3);
writeIndex("ledButtonMap", "A1", ledOptions.indexA1);
writeIndex("ledButtonMap", "A2", ledOptions.indexA2);
writeDoc(doc, "pledType", ledOptions.pledType);
writeDoc(doc, "pledPin1", ledOptions.pledPin1);
writeDoc(doc, "pledPin2", ledOptions.pledPin2);
writeDoc(doc, "pledPin3", ledOptions.pledPin3);
writeDoc(doc, "pledPin4", ledOptions.pledPin4);
writeDoc(doc, "pledIndex1", ledOptions.pledIndex1);
writeDoc(doc, "pledIndex2", ledOptions.pledIndex2);
writeDoc(doc, "pledIndex3", ledOptions.pledIndex3);
writeDoc(doc, "pledIndex4", ledOptions.pledIndex4);
writeDoc(doc, "pledColor", ((RGB)ledOptions.pledColor).value(LED_FORMAT_RGB));
writeDoc(doc, "caseRGBType", ledOptions.caseRGBType);
writeDoc(doc, "caseRGBIndex", ledOptions.caseRGBIndex);
writeDoc(doc, "caseRGBCount", ledOptions.caseRGBCount);
return serialize_json(doc);
}
std::string getButtonLayoutDefs()
{
const size_t capacity = JSON_OBJECT_SIZE(500);
DynamicJsonDocument doc(capacity);
uint16_t layoutCtr = 0;
for (layoutCtr = _ButtonLayout_MIN; layoutCtr < _ButtonLayout_ARRAYSIZE; layoutCtr++) {
LayoutManager::LayoutList leftLayout = LayoutManager::getInstance().getLeftLayout((ButtonLayout)layoutCtr);
if ((leftLayout.size() > 0) || (layoutCtr == ButtonLayout::BUTTON_LAYOUT_BLANKA)) writeDoc(doc, "buttonLayout", LayoutManager::getInstance().getButtonLayoutName((ButtonLayout)layoutCtr), layoutCtr);
}
for (layoutCtr = _ButtonLayoutRight_MIN; layoutCtr < _ButtonLayoutRight_ARRAYSIZE; layoutCtr++) {
LayoutManager::LayoutList rightLayout = LayoutManager::getInstance().getRightLayout((ButtonLayoutRight)layoutCtr);
if ((rightLayout.size() > 0) || (layoutCtr == ButtonLayoutRight::BUTTON_LAYOUT_BLANKB)) writeDoc(doc, "buttonLayoutRight", LayoutManager::getInstance().getButtonLayoutRightName((ButtonLayoutRight)layoutCtr), layoutCtr);
}
return serialize_json(doc);
}
std::string getButtonLayouts()
{
const size_t capacity = JSON_OBJECT_SIZE(500);
DynamicJsonDocument doc(capacity);
const LEDOptions& ledOptions = Storage::getInstance().getLedOptions();
const DisplayOptions& displayOptions = Storage::getInstance().getDisplayOptions();
uint16_t elementCtr = 0;
LayoutManager::LayoutList layoutA = LayoutManager::getInstance().getLayoutA();
LayoutManager::LayoutList layoutB = LayoutManager::getInstance().getLayoutB();
writeDoc(doc, "ledLayout", "id", ledOptions.ledLayout);
writeDoc(doc, "ledLayout", "indexUp", ledOptions.indexUp);
writeDoc(doc, "ledLayout", "indexDown", ledOptions.indexDown);
writeDoc(doc, "ledLayout", "indexLeft", ledOptions.indexLeft);
writeDoc(doc, "ledLayout", "indexRight", ledOptions.indexRight);
writeDoc(doc, "ledLayout", "indexB1", ledOptions.indexB1);
writeDoc(doc, "ledLayout", "indexB2", ledOptions.indexB2);
writeDoc(doc, "ledLayout", "indexB3", ledOptions.indexB3);
writeDoc(doc, "ledLayout", "indexB4", ledOptions.indexB4);
writeDoc(doc, "ledLayout", "indexL1", ledOptions.indexL1);
writeDoc(doc, "ledLayout", "indexR1", ledOptions.indexR1);
writeDoc(doc, "ledLayout", "indexL2", ledOptions.indexL2);
writeDoc(doc, "ledLayout", "indexR2", ledOptions.indexR2);
writeDoc(doc, "ledLayout", "indexS1", ledOptions.indexS1);
writeDoc(doc, "ledLayout", "indexS2", ledOptions.indexS2);
writeDoc(doc, "ledLayout", "indexL3", ledOptions.indexL3);
writeDoc(doc, "ledLayout", "indexR3", ledOptions.indexR3);
writeDoc(doc, "ledLayout", "indexA1", ledOptions.indexA1);
writeDoc(doc, "ledLayout", "indexA2", ledOptions.indexA2);
writeDoc(doc, "displayLayouts", "buttonLayoutId", displayOptions.buttonLayout);
for (elementCtr = 0; elementCtr < layoutA.size(); elementCtr++) {
const size_t elementSize = JSON_OBJECT_SIZE(12);
DynamicJsonDocument ele(elementSize);
writeDoc(ele, "elementType", layoutA[elementCtr].elementType);
writeDoc(ele, "parameters", "x1", layoutA[elementCtr].parameters.x1);
writeDoc(ele, "parameters", "y1", layoutA[elementCtr].parameters.y1);
writeDoc(ele, "parameters", "x2", layoutA[elementCtr].parameters.x2);
writeDoc(ele, "parameters", "y2", layoutA[elementCtr].parameters.y2);
writeDoc(ele, "parameters", "stroke", layoutA[elementCtr].parameters.stroke);
writeDoc(ele, "parameters", "fill", layoutA[elementCtr].parameters.fill);
writeDoc(ele, "parameters", "value", layoutA[elementCtr].parameters.value);
writeDoc(ele, "parameters", "shape", layoutA[elementCtr].parameters.shape);
writeDoc(ele, "parameters", "angleStart", layoutA[elementCtr].parameters.angleStart);
writeDoc(ele, "parameters", "angleEnd", layoutA[elementCtr].parameters.angleEnd);
writeDoc(ele, "parameters", "closed", layoutA[elementCtr].parameters.closed);
writeDoc(doc, "displayLayouts", "buttonLayout", std::to_string(elementCtr), ele);
}
writeDoc(doc, "displayLayouts", "buttonLayoutRightId", displayOptions.buttonLayoutRight);
for (elementCtr = 0; elementCtr < layoutB.size(); elementCtr++) {
const size_t elementSize = JSON_OBJECT_SIZE(12);
DynamicJsonDocument ele(elementSize);
writeDoc(ele, "elementType", layoutB[elementCtr].elementType);
writeDoc(ele, "parameters", "x1", layoutB[elementCtr].parameters.x1);
writeDoc(ele, "parameters", "y1", layoutB[elementCtr].parameters.y1);
writeDoc(ele, "parameters", "x2", layoutB[elementCtr].parameters.x2);
writeDoc(ele, "parameters", "y2", layoutB[elementCtr].parameters.y2);
writeDoc(ele, "parameters", "stroke", layoutB[elementCtr].parameters.stroke);
writeDoc(ele, "parameters", "fill", layoutB[elementCtr].parameters.fill);
writeDoc(ele, "parameters", "value", layoutB[elementCtr].parameters.value);
writeDoc(ele, "parameters", "shape", layoutB[elementCtr].parameters.shape);
writeDoc(ele, "parameters", "angleStart", layoutB[elementCtr].parameters.angleStart);
writeDoc(ele, "parameters", "angleEnd", layoutB[elementCtr].parameters.angleEnd);
writeDoc(ele, "parameters", "closed", layoutB[elementCtr].parameters.closed);
writeDoc(doc, "displayLayouts", "buttonLayoutRight", std::to_string(elementCtr), ele);
}
return serialize_json(doc);