Repository navigation
Expand file tree
/
Copy pathGS32Config.cpp
More file actions
148 lines (138 loc) · 5.7 KB
/
Copy pathGS32Config.cpp
File metadata and controls
148 lines (138 loc) · 5.7 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
#include "GS32Config.h"
GS32Config::GS32Config() : _ns("gs32_cfg") {}
bool GS32Config::begin(const char* nameSpace) {
_ns = nameSpace;
return _prefs.begin(_ns, false);
}
void GS32Config::addBool(const char* key, bool* ptr, bool defaultVal) {
ConfigEntry entry = {String(key), (void*)ptr, ConfigEntry::TYPE_BOOL, 0, defaultVal ? 1 : 0, ""};
*ptr = defaultVal;
_entries.push_back(entry);
}
void GS32Config::addInt(const char* key, int* ptr, int defaultVal) {
ConfigEntry entry = {String(key), (void*)ptr, ConfigEntry::TYPE_INT, 0, defaultVal, ""};
*ptr = defaultVal;
_entries.push_back(entry);
}
void GS32Config::addInt8(const char* key, int8_t* ptr, int8_t defaultVal) {
ConfigEntry entry = {String(key), (void*)ptr, ConfigEntry::TYPE_INT8, 0, defaultVal, ""};
*ptr = defaultVal;
_entries.push_back(entry);
}
void GS32Config::addUInt8(const char* key, uint8_t* ptr, uint8_t defaultVal) {
ConfigEntry entry = {String(key), (void*)ptr, ConfigEntry::TYPE_UINT8, 0, defaultVal, ""};
*ptr = defaultVal;
_entries.push_back(entry);
}
void GS32Config::addText(const char* key, char* ptr, size_t maxLen, const char* defaultVal) {
ConfigEntry entry = {String(key), (void*)ptr, ConfigEntry::TYPE_STRING, maxLen, 0, String(defaultVal)};
memset(ptr, 0, maxLen);
strncpy(ptr, defaultVal, maxLen - 1);
_entries.push_back(entry);
}
void GS32Config::load() {
for (const auto& entry : _entries) {
switch (entry.type) {
case ConfigEntry::TYPE_BOOL: {
bool* p = (bool*)entry.ptr;
if (_prefs.isKey(entry.key.c_str())) *p = _prefs.getBool(entry.key.c_str(), entry.defaultInt);
else _prefs.putBool(entry.key.c_str(), *p);
break;
}
case ConfigEntry::TYPE_INT: {
int* p = (int*)entry.ptr;
if (_prefs.isKey(entry.key.c_str())) *p = _prefs.getInt(entry.key.c_str(), entry.defaultInt);
else _prefs.putInt(entry.key.c_str(), *p);
break;
}
case ConfigEntry::TYPE_INT8: {
int8_t* p = (int8_t*)entry.ptr;
if (_prefs.isKey(entry.key.c_str())) *p = (int8_t)_prefs.getChar(entry.key.c_str(), entry.defaultInt);
else _prefs.putChar(entry.key.c_str(), *p);
break;
}
case ConfigEntry::TYPE_UINT8: {
uint8_t* p = (uint8_t*)entry.ptr;
if (_prefs.isKey(entry.key.c_str())) *p = (uint8_t)_prefs.getUChar(entry.key.c_str(), entry.defaultInt);
else _prefs.putUChar(entry.key.c_str(), *p);
break;
}
case ConfigEntry::TYPE_STRING: {
char* p = (char*)entry.ptr;
if (_prefs.isKey(entry.key.c_str())) {
String val = _prefs.getString(entry.key.c_str(), entry.defaultStr.c_str());
strncpy(p, val.c_str(), entry.maxLen - 1);
} else {
_prefs.putString(entry.key.c_str(), p);
}
break;
}
}
}
}
void GS32Config::save() {
Serial.println("--- Saving Config ---");
for (const auto& entry : _entries) {
Serial.print("Saving key: "); Serial.print(entry.key);
Serial.print(" | Value: ");
switch (entry.type) {
case ConfigEntry::TYPE_BOOL:
Serial.println(*((bool*)entry.ptr));
_prefs.putBool(entry.key.c_str(), *((bool*)entry.ptr));
break;
case ConfigEntry::TYPE_INT:
Serial.println(*((int*)entry.ptr));
_prefs.putInt(entry.key.c_str(), *((int*)entry.ptr));
break;
case ConfigEntry::TYPE_INT8:
Serial.println(*((int8_t*)entry.ptr));
_prefs.putChar(entry.key.c_str(), *((int8_t*)entry.ptr));
break;
case ConfigEntry::TYPE_UINT8:
Serial.println(*((uint8_t*)entry.ptr));
_prefs.putUChar(entry.key.c_str(), *((uint8_t*)entry.ptr));
break;
case ConfigEntry::TYPE_STRING:
Serial.println((char*)entry.ptr);
_prefs.putString(entry.key.c_str(), (char*)entry.ptr);
break;
}
}
}
void GS32Config::factoryReset() {
for (const auto& entry : _entries) {
switch (entry.type) {
case ConfigEntry::TYPE_BOOL: {
bool* p = (bool*)entry.ptr;
*p = entry.defaultInt;
_prefs.putBool(entry.key.c_str(), *p);
break;
}
case ConfigEntry::TYPE_INT: {
int* p = (int*)entry.ptr;
*p = entry.defaultInt;
_prefs.putInt(entry.key.c_str(), *p);
break;
}
case ConfigEntry::TYPE_INT8: {
int8_t* p = (int8_t*)entry.ptr;
*p = (int8_t)entry.defaultInt;
_prefs.putChar(entry.key.c_str(), *p);
break;
}
case ConfigEntry::TYPE_UINT8: {
uint8_t* p = (uint8_t*)entry.ptr;
*p = (uint8_t)entry.defaultInt;
_prefs.putUChar(entry.key.c_str(), *p);
break;
}
case ConfigEntry::TYPE_STRING: {
char* p = (char*)entry.ptr;
memset(p, 0, entry.maxLen);
strncpy(p, entry.defaultStr.c_str(), entry.maxLen - 1);
_prefs.putString(entry.key.c_str(), p);
break;
}
}
}
}