-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESP_Configuration.cpp
More file actions
195 lines (171 loc) · 5.46 KB
/
ESP_Configuration.cpp
File metadata and controls
195 lines (171 loc) · 5.46 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
#include "ESP_Configuration.h"
#include "ConfigurationItem.h"
#include <FS.h>
#include <SPIFFS.h>
int min(int a, int b){
if(a < b) return a;
return b;
}
void Configuration::init(){
// Here is where we handle loadingSavedConfiguration or saving Default
SPIFFS.begin();
resetPointer();
if(!loadConfiguration()){ // this will return false if there is no config in the FS, or if there is but with less data points
Serial.println("COULD NOT FIND THINGO");
saveConfiguration(); // This should save the default parameters into the FS
} else {
Serial.println("LOADED THINGS");
}
Serial.println(stuff);
}
ConfigurationItem* Configuration::findConfigWithKey(const char* key){
resetPointer();
do {
if(strcmp(currentConfig->key, key) == 0){
return currentConfig;
}
} while (nextValue());
return 0;
}
bool Configuration::checkForTwenty(){
// checks if we are running on a 20 char limit
bool twenty = false;
if(!SPIFFS.exists("/config.txt")){
return false;
}
File f = SPIFFS.open("/config.txt", "r");
if(!f)
return false;
char buf[21];
if(f.size() > 20){
f.readBytes(buf, 20);
buf[21] = 0x0;
if(strcmp(buf, "Hello") == 0){
twenty = true;
}
}
f.close();
return twenty;
}
bool Configuration::loadForTwenty(){
// Connect to FS, try and load config.txt, if no file return false
// Read until 0x00, is length is different to TOTAL_CONFIG_LENGTH, we load what we can into setVal(so we overwrite default), then return false to save the rest
bool intact = true;
if(!SPIFFS.exists("/config.txt")){
return false;
}
File f = SPIFFS.open("/config.txt", "r");
if(!f)
return false;
resetPointer();
char buf[21];
bool reachedEnd = false;
Serial.printf("f.size is %d, f.position is %d, currentConfig->getLength() is %d, min is %d\n", f.size(), f.position(), currentConfig->getLength(), min(currentConfig->getLength(), 20));
do {
if(f.size() - f.position() < min(currentConfig->getLength(), 20) || reachedEnd){
// Config file is not big enough, lets move on.
Serial.println("bad one found! ");
intact = false;
currentConfig->setDefault(); // this will make sure, when we save, we have loaded the default values for the values we didn't have
reachedEnd = true;
continue;
} else {
// good
Serial.println("Good one found! ");
}
f.readBytes(buf, min(currentConfig->getLength(), 20));
buf[min(currentConfig->getLength(), 20)] = 0x0;
currentConfig->set(buf);
Serial.println(currentConfig->toString());
memset(buf, 0, 21); //cleanup BUF
} while (nextValue());
f.close();
return intact;
}
void* Configuration::getConfiguration(const char* key){
ConfigurationItem* item = findConfigWithKey(key);
if(item){
return item->get();
} else {
return ␣
}
}
bool Configuration::setConfiguration(const char* key, void* value){
if(findConfigWithKey(key)->setValue(value)){
return saveConfiguration();
}
return false;
}
bool Configuration::resetPointer(){
pointer = 0;
currentConfig = configList[pointer];
}
bool Configuration::nextValue(){
if(pointer < numConfigs - 1){
pointer++;
currentConfig = configList[pointer];
return true;
}
currentConfig = configList[pointer];
pointer = 0;
return false;
}
bool Configuration::setDefaults(){
// Serial.println("I AM BEING CALLED");
resetPointer();
do {
currentConfig->setDefault();
} while (nextValue());
saveConfiguration();
}
bool Configuration::loadConfiguration(){
// Connect to FS, try and load config.txt, if no file return false
// Read until 0x00, is length is different to TOTAL_CONFIG_LENGTH, we load what we can into setVal(so we overwrite default), then return false to save the rest
bool intact = true;
if(!SPIFFS.exists("/config.txt")){
return false;
}
File f = SPIFFS.open("/config.txt", "r");
if(!f)
return false;
resetPointer();
char buf[LARGEST_CONF_VALUE];
do {
if(f.size() - f.position() < currentConfig->getLength()){
// Config file is not big enough, lets move on.
intact = false;
currentConfig->setDefault(); // this will make sure, when we save, we have loaded the default values for the values we didn't have
continue;
}
f.readBytes(buf, currentConfig->getLength());
buf[currentConfig->getLength()] = 0x0;
currentConfig->set(buf);
memset(buf, 0, LARGEST_CONF_VALUE); //cleanup BUF
} while (nextValue());
f.close();
return intact;
}
bool Configuration::saveConfiguration(){
// Connect to FS, loop through configs and concat getPrintable into strings
File f = SPIFFS.open("/config.txt", "w+");
if(!f){
return false;
}
resetPointer();
unsigned char buf[SAVE_BUFFER];
unsigned char* pos = &buf[0];
int length = 0;
do {
length += currentConfig->getPrintable((char*)pos);
pos = &buf[0] + length;
} while(nextValue());
buf[length+1] = 0x00;
f.write(buf, length+1);
f.close();
return true;
}
bool Configuration::keyExists(char* key){
if(findConfigWithKey(key))
return true;
return false;
}