forked from DustinWatts/FreeTouchDeck
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfigLoad.cpp
636 lines (594 loc) · 21.4 KB
/
ConfigLoad.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
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
#include "ConfigLoad.h"
#include "FTButton.h"
#include "System.h"
#include "Storage.h"
#include "ConfigHelper.h"
#include "FTAction.h"
namespace FreeTouchDeck
{
FTAction *saveConfigAction = new FTAction(ParametersList_t({"SAVECONFIG"}));
using namespace fs;
const char *defaultDeviceName = "FreeTouchDeck";
const char *defaultManufacturerName = "Made by me";
Config generalconfig;
static const char *module = "ConfigLoad";
const char *generalConfigFile = "/config/general.json";
void HasConfigElementChanged(const char *name, const char *currentVal, const char *newVal, bool &result, const char * module)
{
if (strcmp(currentVal, newVal) != 0)
{
LOC_LOGI(module, "Configuration element %s change from %s to %s", name, currentVal, newVal);
result = true;
}
}
void HasConfigElementChanged(const char *name, bool currentVal, bool newVal, bool &result, const char * module)
{
if (currentVal != newVal)
{
LOC_LOGI(module, "Configuration element %s change from %s to %s", name, currentVal ? "TRUE" : "FALSE", newVal ? "TRUE" : "FALSE");
result = true;
}
}
void HasConfigElementChanged(const char *name, LogLevels currentVal, LogLevels newVal, bool &result, const char * module )
{
if (currentVal != newVal)
{
LOC_LOGI(module, "Configuration element %s change from %d to %d", name, currentVal, newVal);
result = true;
}
}
#define HAS_CONFIG_ELEMENT_CHANGED(e) HasConfigElementChanged(QUOTE(e), currentConfig.e, newConfig.e, result, module)
bool WasConfigChanged(Config ¤tConfig, Config &newConfig)
{
bool result = false;
HAS_CONFIG_ELEMENT_CHANGED(menuButtonColour);
HAS_CONFIG_ELEMENT_CHANGED(functionButtonColour);
HAS_CONFIG_ELEMENT_CHANGED(backgroundColour);
HAS_CONFIG_ELEMENT_CHANGED(latchedColour);
HAS_CONFIG_ELEMENT_CHANGED(DefaultOutline);
HAS_CONFIG_ELEMENT_CHANGED(DefaultTextColor);
HAS_CONFIG_ELEMENT_CHANGED(DefaultTextSize);
HAS_CONFIG_ELEMENT_CHANGED(colscount);
HAS_CONFIG_ELEMENT_CHANGED(rowscount);
HAS_CONFIG_ELEMENT_CHANGED(sleepenable);
HAS_CONFIG_ELEMENT_CHANGED(keyDelay);
HAS_CONFIG_ELEMENT_CHANGED(sleeptimer);
HAS_CONFIG_ELEMENT_CHANGED(beep);
HAS_CONFIG_ELEMENT_CHANGED(flip_touch_axis);
HAS_CONFIG_ELEMENT_CHANGED(reverse_x_touch);
HAS_CONFIG_ELEMENT_CHANGED(reverse_y_touch);
HAS_CONFIG_ELEMENT_CHANGED(screenrotation);
HAS_CONFIG_ELEMENT_CHANGED(deviceName);
HAS_CONFIG_ELEMENT_CHANGED(manufacturer);
HAS_CONFIG_ELEMENT_CHANGED(helperdelay);
HAS_CONFIG_ELEMENT_CHANGED(ledBrightness);
HAS_CONFIG_ELEMENT_CHANGED(LogLevel);
return result;
}
void SetGeneralConfigDefaults()
{
generalconfig.menuButtonColour = 0x009bf4;
generalconfig.functionButtonColour = 0x00efcb;
generalconfig.latchedColour = 0xfe0149;
generalconfig.backgroundColour = TFT_BLACK;
generalconfig.sleepenable = false;
generalconfig.sleeptimer = 60;
generalconfig.beep = false;
#ifdef DEFAULT_LOG_LEVEL
generalconfig.LogLevel = DEFAULT_LOG_LEVEL;
#else
generalconfig.LogLevel = LogLevels::INFO;
#endif
generalconfig.helperdelay = 0;
generalconfig.screenrotation = SCREEN_ROTATION;
generalconfig.flip_touch_axis = FLIP_TOUCH_AXIS;
generalconfig.reverse_x_touch = INVERSE_X_TOUCH;
generalconfig.reverse_y_touch = INVERSE_Y_TOUCH;
generalconfig.rowscount = 3;
generalconfig.colscount = 3;
generalconfig.DefaultOutline = 0xffffff;
generalconfig.DefaultTextColor = 0xffffff;
generalconfig.keyDelay = 20;
generalconfig.DefaultTextSize = KEY_TEXTSIZE;
generalconfig.ledBrightness = 255;
FREE_AND_NULL(generalconfig.deviceName);
generalconfig.deviceName = ps_strdup(defaultDeviceName);
FREE_AND_NULL(generalconfig.manufacturer);
generalconfig.manufacturer = ps_strdup(defaultManufacturerName);
}
bool GetValueOrDefault(cJSON *value, char **valuePointer, const char *defaultValue)
{
bool success = false;
char *tempValue = NULL;
FREE_AND_NULL((*valuePointer));
if (value && cJSON_IsString(value))
{
tempValue = cJSON_GetStringValue(value);
if (tempValue && strlen(tempValue) > 0)
{
LOC_LOGV(module, "Assigning value %s", tempValue);
(*valuePointer) = ps_strdup(tempValue);
success = true;
}
else
{
LOC_LOGV(module, "Empty string value was found");
}
}
else
{
if (!value)
{
LOC_LOGV(module, "Value was not found");
}
else if (!cJSON_IsString(value))
{
LOC_LOGV(module, "Value is not a string");
}
else
{
LOC_LOGV(module, "Unknown value type");
}
}
if (!(*valuePointer))
{
if (defaultValue)
{
LOC_LOGV(module, "Copying default value [%s]", defaultValue);
(*valuePointer) = ps_strdup(defaultValue);
}
else
{
(*valuePointer) = NULL;
}
}
return success;
}
bool GetValueOrDefault(cJSON *value, std::string &valuePointer, const char *defaultValue)
{
bool success = false;
char *tempValue = NULL;
LOC_LOGV(module, "Clearing string value");
valuePointer.clear();
if (value && cJSON_IsString(value))
{
tempValue = cJSON_GetStringValue(value);
if (tempValue && strlen(tempValue) > 0)
{
LOC_LOGV(module, "Assigning value %s", tempValue);
valuePointer = tempValue;
success = true;
}
else
{
LOC_LOGV(module, "Empty string value was found");
}
}
else
{
if (!value)
{
LOC_LOGV(module, "Value was not found");
}
else if (!cJSON_IsString(value))
{
LOC_LOGV(module, "Value is not a string");
}
else
{
LOC_LOGV(module, "Unknown value type");
}
}
if (valuePointer.empty())
{
if (defaultValue)
{
LOC_LOGV(module, "Copying default value [%s]", defaultValue);
valuePointer = defaultValue;
}
}
return success;
}
bool GetValueOrDefault(cJSON *value, uint16_t *valuePointer, uint16_t defaultValue)
{
uint16_t tempValue = 0;
bool success = false;
if (value && cJSON_IsNumber(value))
{
LOC_LOGV(module, "Value %d found, and it is a number", value->valueint);
(*valuePointer) = (uint16_t)value->valuedouble;
success = true;
}
else
{
if (!value)
{
LOC_LOGV(module, "Numeric value not found");
}
else
{
LOC_LOGV(module, "Value was found, but is not a number");
}
(*valuePointer) = defaultValue;
}
return success;
}
bool GetValueOrDefault(cJSON *value, uint8_t *valuePointer, uint8_t defaultValue)
{
uint16_t tempValue = 0;
bool success = false;
if (value && cJSON_IsNumber(value))
{
LOC_LOGV(module, "Value %d found, and it is a number", value->valueint);
success = true;
(*valuePointer) = (uint8_t)value->valueint;
}
else
{
if (!value)
{
LOC_LOGV(module, "Numeric value not found");
}
else
{
LOC_LOGV(module, "Value was found, but is not a number");
}
(*valuePointer) = defaultValue;
}
return success;
}
bool GetValueOrDefault(cJSON *value, bool *valuePointer, bool defaultValue)
{
bool tempValue = 0;
bool success = false;
if (value && cJSON_IsBool(value))
{
LOC_LOGV(module, "Value found, and it is a boolean", value->valueint);
success = true;
(*valuePointer) = cJSON_IsTrue(value);
}
else
{
if (!value)
{
LOC_LOGV(module, "Value was not found");
}
else
{
LOC_LOGV(module, "Value was found but is not a boolean");
}
(*valuePointer) = defaultValue;
}
return success;
}
bool GetValueOrDefault(cJSON *doc, const char *name, char **valuePointer, const char *defaultValue)
{
LOC_LOGV(module, "Looking for char value %s", name);
if (!GetValueOrDefault(cJSON_GetObjectItem(doc, name), valuePointer, defaultValue))
{
DumpCJson(doc);
}
}
bool GetValueOrDefault(cJSON *doc, const char *name, std::string &valuePointer, const char *defaultValue)
{
LOC_LOGV(module, "Looking for char value %s", name);
if (!GetValueOrDefault(cJSON_GetObjectItem(doc, name), valuePointer, defaultValue))
{
DumpCJson(doc);
}
}
bool GetValueOrDefault(cJSON *doc, const char *name, uint16_t *valuePointer, uint16_t defaultValue)
{
LOC_LOGV(module, "Looking for uint16_t value %s", name);
if (!GetValueOrDefault(cJSON_GetObjectItem(doc, name), valuePointer, defaultValue))
{
DumpCJson(doc);
}
}
bool GetValueOrDefault(cJSON *doc, const char *name, uint8_t *valuePointer, uint8_t defaultValue)
{
LOC_LOGV(module, "Looking for uint8_t value %s", name);
if (!GetValueOrDefault(cJSON_GetObjectItem(doc, name), valuePointer, defaultValue))
{
DumpCJson(doc);
}
}
void GetValueOrDefault(cJSON *doc, const char *name, bool *valuePointer, bool defaultValue)
{
LOC_LOGV(module, "Looking for boolean value %s", name);
if (!GetValueOrDefault(cJSON_GetObjectItem(doc, name), valuePointer, defaultValue))
{
DumpCJson(doc);
}
}
bool GetColorOrDefault(cJSON *doc, const char *name, uint16_t *valuePointer, uint16_t defaultValue)
{
LOC_LOGV(module, "Looking for uint16_t value %s", name);
char *color = NULL;
cJSON *colorJson = cJSON_GetObjectItem(doc, name);
if (!colorJson || !cJSON_IsString(colorJson) || ISNULLSTRING(cJSON_GetStringValue(colorJson)))
{
*valuePointer = defaultValue;
return false;
}
else
{
*valuePointer = convertHTMLtoRGB888(cJSON_GetStringValue(colorJson));
return true;
}
}
bool GetColorOrDefault(cJSON *doc, const char *name, uint32_t *valuePointer, uint32_t defaultValue)
{
LOC_LOGV(module, "Looking for uint16_t value %s", name);
char *color = NULL;
cJSON *colorJson = cJSON_GetObjectItem(doc, name);
if (!colorJson || !cJSON_IsString(colorJson) || ISNULLSTRING(cJSON_GetStringValue(colorJson)))
{
*valuePointer = defaultValue;
return false;
}
else
{
*valuePointer = convertHTMLtoRGB888(cJSON_GetStringValue(colorJson));
return true;
}
}
/**
* @brief This function loads the menu configuration.
*
* @param String the config to be loaded
*
* @return none
*
* @note Options for values are: colors, homescreen, menu1, menu2, menu3
menu4, and menu5
*/
bool loadConfig(const char *name, bool saveifchanged)
{
Config currentConfig;
memcpy(¤tConfig, &generalconfig, sizeof(currentConfig));
if (ISNULLSTRING(name))
{
LOC_LOGE(module, "Invalid configuration file passed. ");
return false;
}
LOC_LOGI(module, "Loading configuration from %s - %s", STRING_OR_DEFAULT(ftdfs->Name, "?"), STRING_OR_DEFAULT(name, ""));
File configfile = ftdfs->open(name, "r");
if (!configfile && configfile.size() == 0)
{
LOC_LOGE(module, "Could not find file %s", name);
return false;
}
size_t bufferSize = configfile.size() + 1;
char *buffer = (char *)malloc_fn(bufferSize);
if (!buffer)
{
LOC_LOGE(module, "Could not allocate %d bytes for reading file %s", bufferSize, name);
return false;
}
size_t bytesRead = configfile.readBytes(buffer, bufferSize);
if (bytesRead != configfile.size())
{
LOC_LOGE(module, "Could not fully read %s config in buffer. Read %d of %d bytes.", name, bytesRead, configfile.size());
configfile.close();
ShowDir(ftdfs);
drawErrorMessage(false, module, "Could not fully read %s config in buffer. Read %d of %d bytes.", name, bytesRead, configfile.size());
return false;
}
configfile.close();
LOC_LOGD(module, "Parsing configuration file:\n%s", buffer);
cJSON *doc = cJSON_Parse(buffer);
if (!doc)
{
const char *error = cJSON_GetErrorPtr();
drawErrorMessage(!ftdfs->External, module, "Unable to parse json string while loading file %s. Error: %s\nContent:\n%s", name, error, buffer);
FREE_AND_NULL(buffer);
return false;
}
else
{
if (generalconfig.LogLevel >= LogLevels::VERBOSE)
{
char *docstr = cJSON_Print(doc);
if (docstr)
{
LOC_LOGD(module, "Configuration is : \n%s", docstr);
FREE_AND_NULL(docstr);
}
}
}
FREE_AND_NULL(buffer);
GetValueOrDefault(cJSON_GetObjectItem(doc, "manufacturer"), &generalconfig.manufacturer, defaultManufacturerName);
LOC_LOGD(module, "Manufacturer name : %s", generalconfig.manufacturer);
GetValueOrDefault(cJSON_GetObjectItem(doc, "devicename"), &generalconfig.deviceName, defaultDeviceName);
LOC_LOGD(module, "Device name : %s", generalconfig.deviceName);
// Parsing colors
// Get the color for the menu and back home buttons.
GetColorOrDefault(doc, "menubuttoncolor", &generalconfig.menuButtonColour, 0x9bf4);
LOC_LOGD(module, "menuButtonColour : %06x", generalconfig.menuButtonColour);
// Get the color for the function buttons.
GetColorOrDefault(doc, "functionbuttoncolor", &generalconfig.functionButtonColour, 0x00efcb);
LOC_LOGD(module, "functionbuttoncolor : %06x", generalconfig.functionButtonColour);
GetColorOrDefault(doc, "latchcolor", &generalconfig.latchedColour, 0xfe0149);
LOC_LOGD(module, "latchcolor : %06x", generalconfig.latchedColour);
GetColorOrDefault(doc, "backgroundcolor", &generalconfig.backgroundColour, 0x00);
LOC_LOGD(module, "backgroundColour : %06x", generalconfig.backgroundColour);
GetColorOrDefault(doc, "outline", &generalconfig.DefaultOutline, TFT_WHITE);
LOC_LOGD(module, "outline : %06x", generalconfig.DefaultOutline);
GetColorOrDefault(doc, "textcolor", &generalconfig.DefaultTextColor, TFT_WHITE);
LOC_LOGD(module, "textcolor : %06x", generalconfig.DefaultTextColor);
// Loading general settings
GetValueOrDefault(cJSON_GetObjectItem(doc, "sleepenable"), &generalconfig.sleepenable, false);
GetValueOrDefault(cJSON_GetObjectItem(doc, "sleeptimer"), &generalconfig.sleeptimer, 60);
GetValueOrDefault(cJSON_GetObjectItem(doc, "beep"), &generalconfig.beep, false);
uint8_t logLevel;
GetValueOrDefault(cJSON_GetObjectItem(doc, "loglevel"), &logLevel, static_cast<uint8_t>(LogLevels::INFO));
generalconfig.LogLevel = static_cast<LogLevels>(logLevel);
GetValueOrDefault(cJSON_GetObjectItem(doc, "helperdelay"), &generalconfig.helperdelay, 0);
GetValueOrDefault(cJSON_GetObjectItem(doc, "screenrotation"), &generalconfig.screenrotation, SCREEN_ROTATION);
GetValueOrDefault(cJSON_GetObjectItem(doc, "flip_touch_axis"), &generalconfig.flip_touch_axis, FLIP_TOUCH_AXIS);
GetValueOrDefault(cJSON_GetObjectItem(doc, "reverse_x_touch"), &generalconfig.reverse_x_touch, INVERSE_X_TOUCH);
GetValueOrDefault(cJSON_GetObjectItem(doc, "reverse_y_touch"), &generalconfig.reverse_y_touch, INVERSE_Y_TOUCH);
GetValueOrDefault(cJSON_GetObjectItem(doc, "rowscount"), &generalconfig.rowscount, 3);
GetValueOrDefault(cJSON_GetObjectItem(doc, "colscount"), &generalconfig.colscount, 3);
GetValueOrDefault(cJSON_GetObjectItem(doc, "keydelay"), &generalconfig.keyDelay, 0);
GetValueOrDefault(cJSON_GetObjectItem(doc, "ledbrightness"), (uint8_t *)&generalconfig.ledBrightness, 255);
GetValueOrDefault(cJSON_GetObjectItem(doc, "textsize"), &generalconfig.DefaultTextSize, KEY_TEXTSIZE);
cJSON_Delete(doc);
if (generalconfig.LogLevel >= LogLevels::VERBOSE)
{
LOC_LOGD(module, "Configuration after load");
saveConfig(true);
}
if (WasConfigChanged(currentConfig, generalconfig))
{
LOC_LOGI(module, "Queuing saving of the updated configuration");
}
FTButton::BackButton->BackgroundColor = generalconfig.functionButtonColour;
FTButton::BackButton->TextColor = generalconfig.DefaultTextColor;
FTButton::BackButton->TextSize = generalconfig.DefaultTextSize;
FTButton::BackButton->Outline = generalconfig.DefaultOutline;
FTButton::HomeButton->BackgroundColor = generalconfig.functionButtonColour;
FTButton::HomeButton->TextColor = generalconfig.DefaultTextColor;
FTButton::HomeButton->TextSize = generalconfig.DefaultTextSize;
FTButton::HomeButton->Outline = generalconfig.DefaultOutline;
return true;
}
void QueueSaving()
{
QueueAction(saveConfigAction);
}
bool loadGeneralConfig()
{
if (ftdfs->External)
{
LOC_LOGD(module, "Booting from external storage. Checking if config exists");
bool needsInit = false;
if (!ftdfs->exists(generalConfigFile))
{
PrintScreenMessage(true, "Reading from %s, config file not found", ftdfs->Name);
needsInit = true;
}
else if (GetFileSize(generalConfigFile, ftdfs) == 0)
{
PrintScreenMessage(false, "Configuration file is empty on device %s", ftdfs->Name);
needsInit = true;
}
if (needsInit)
{
PrintScreenMessage(false, "Initializing %s from internal storage", ftdfs->Name);
if (!InitializeStorage())
{
drawErrorMessage(true, module, "Could not initialize storage %s", ftdfs->Name);
}
else
{
PrintScreenMessage(false, "Finished initializing storage %s", ftdfs->Name);
ftdfs->end();
WaitTouchReboot();
}
}
}
if (!loadConfig(generalConfigFile))
{
drawErrorMessage(true, module, "Error loading configuration from %s", ftdfs->Name);
}
return true;
}
cJSON *GetConfigJson()
{
cJSON *doc = cJSON_CreateObject();
if (!doc)
{
return NULL;
}
cJSON_AddBoolToObject(doc, "sleepenable", generalconfig.sleepenable);
cJSON_AddBoolToObject(doc, "flip_touch_axis", generalconfig.flip_touch_axis);
cJSON_AddBoolToObject(doc, "reverse_x_touch", generalconfig.reverse_x_touch);
cJSON_AddBoolToObject(doc, "reverse_y_touch", generalconfig.reverse_y_touch);
cJSON_AddBoolToObject(doc, "beep", generalconfig.beep);
cJSON_AddStringToObject(doc, "menubuttoncolor", convertRGB888oHTMLRGB888(generalconfig.menuButtonColour));
cJSON_AddStringToObject(doc, "functionbuttoncolor", convertRGB888oHTMLRGB888(generalconfig.functionButtonColour));
cJSON_AddStringToObject(doc, "latchcolor", convertRGB888oHTMLRGB888(generalconfig.latchedColour));
cJSON_AddStringToObject(doc, "backgroundcolor", convertRGB888oHTMLRGB888(generalconfig.backgroundColour));
cJSON_AddStringToObject(doc, "outline", convertRGB888oHTMLRGB888(generalconfig.DefaultOutline));
cJSON_AddStringToObject(doc, "textcolor", convertRGB888oHTMLRGB888(generalconfig.DefaultTextColor));
// Loading general settings
cJSON_AddNumberToObject(doc, "loglevel", static_cast<int>(generalconfig.LogLevel));
cJSON_AddNumberToObject(doc, "screenrotation", generalconfig.screenrotation);
cJSON_AddNumberToObject(doc, "sleeptimer", generalconfig.sleeptimer);
if (!ISNULLSTRING(generalconfig.manufacturer))
cJSON_AddStringToObject(doc, "manufacturer", generalconfig.manufacturer);
if (!ISNULLSTRING(generalconfig.deviceName))
cJSON_AddStringToObject(doc, "devicename", generalconfig.deviceName);
cJSON_AddNumberToObject(doc, "helperdelay", generalconfig.helperdelay);
cJSON_AddNumberToObject(doc, "rowscount", generalconfig.rowscount);
cJSON_AddNumberToObject(doc, "colscount", generalconfig.colscount);
cJSON_AddNumberToObject(doc, "keydelay", generalconfig.keyDelay);
cJSON_AddNumberToObject(doc, "ledbrightness", generalconfig.ledBrightness);
cJSON_AddNumberToObject(doc, "textsize", generalconfig.DefaultTextSize);
return doc;
}
bool saveConfig(bool serial)
{
cJSON *doc = GetConfigJson();
SaveJsonToFile("/config/general.json", doc);
if (serial)
{
PrintMemInfo(__FUNCTION__, __LINE__);
ShowFileContent("/config/general.json", ftdfs);
PrintMemInfo(__FUNCTION__, __LINE__);
}
return true;
}
/**
* @brief This function opens wificonfig.json and fills the wificonfig
* struct accordingly.
*
* @param none
*
* @return True when succeeded. False otherwise.
*
* @note This is also where the sleep configuration lives.
*/
bool loadWifiConfig()
{
char buffer[501] = {0};
if (!ftdfs->exists("/config/wificonfig.json"))
{
LOC_LOGD(module, "Config file not found!");
return false;
}
File configfile = ftdfs->open("/config/wificonfig.json", FILE_READ);
if (!configfile && configfile.size() == 0)
{
LOC_LOGE(module, "Could not find file /config/wificonfig.json");
return false;
}
size_t bytesRead = configfile.readBytes(buffer, sizeof(buffer));
if (bytesRead != configfile.size())
{
drawErrorMessage(false, module, "Could not fully read wifi config in buffer. Read %d of %d bytes.", bytesRead, configfile.size());
return false;
}
configfile.close();
cJSON *doc = cJSON_Parse(buffer);
if (!doc)
{
const char *error = cJSON_GetErrorPtr();
drawErrorMessage(false, module, "Unable to parse json string : %s", error);
return false;
}
else
{
GetValueOrDefault(doc, "ssid", &wificonfig.ssid, "FAILED");
GetValueOrDefault(doc, "password", &wificonfig.password, "FAILED");
GetValueOrDefault(doc, "wifimode", &wificonfig.wifimode, "FAILED");
GetValueOrDefault(doc, "wifihostname", &wificonfig.hostname, "freetouchdeck");
GetValueOrDefault(doc, "attempts", &wificonfig.attempts, 10);
GetValueOrDefault(doc, "attemptdelay", &wificonfig.attemptdelay, 500);
cJSON_Delete(doc);
}
return true;
}
}