-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileSystem.ino
More file actions
141 lines (121 loc) · 4.55 KB
/
fileSystem.ino
File metadata and controls
141 lines (121 loc) · 4.55 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
void spiffsInit() {
Serial.printf("[SPIFFS] Mounting file system...");
if (SPIFFS.begin()) {
// Dir dir = SPIFFS.openDir("/");
// while (dir.next()) {
// String fileName = dir.fileName();
// size_t fileSize = dir.fileSize();
// Serial.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
// }
// Serial.printf("\n");
}
else {
Serial.printf("[SPIFFS] Failed to mount file system");
return;
}
Serial.println(" mounted.");
if (!loadPresets()) {
//savePresets();
savePresetFlag = true; // moved savePresets() to loop because it was causing some exceptions in websocket callback (receive buffer was filling up)
}
else {
Serial.println("[load] Presets loaded!");
}
}
bool loadPresets() {
File presetFile = SPIFFS.open("/presets.json", "r");
if (!presetFile) {
if (SERIAL_DEBUG) Serial.printf("[load] Failed to open presets file\n");
return false;
}
size_t size = presetFile.size();
if (size > 1024) {
if (SERIAL_DEBUG) Serial.printf("[load] Presets file size is too large\n");
return false;
}
char buf[size];
presetFile.readBytes(buf, size);
//if (SERIAL_DEBUG) Serial.printf("[load] SIZE: %d\n", size);
StaticJsonBuffer<512> jsonBuffer; // (or just because I have the space..). (http://arduinojson.org/assistant/)
JsonArray& array = jsonBuffer.parseArray(buf);
//if (SERIAL_DEBUG) Serial.printf("[load] SIZEBUFF: %d\n", jsonBuffer.size());
if (!array.success()) {
if (SERIAL_DEBUG) Serial.printf("[load] Load Presets (parseArray()) failed\n");
return false;
}
else {
if (SERIAL_DEBUG) Serial.printf("[load] JSON Array test values: ");
for (int i = 0; i < NUM_PRESETS; i++) {
presets[i].speed = array[i]["speed"];
presets[i].delay = array[i]["delay"];
presets[i].hue = array[i]["hue"];
uint8_t _mode = array[i]["mode"];
presets[i].mode = (Mode) _mode;
presets[i].hue_cycle = array[i]["hue_cycle"];
presets[i].brightness = array[i]["brightness"];
presets[i].white = array[i]["white"];
presets[i].preset = array[i]["preset"];
presets[i].num_devices = array[i]["num_devices"];
//if (SERIAL_DEBUG) Serial.printf("[load] SIZE2: %d ", jsonBuffer2.size());
//if (SERIAL_DEBUG) Serial.printf("P%u.preset=%u ", i, presets[i].preset);
if (SERIAL_DEBUG){
Serial.printf("[load] hue[%u]:%u\n", i, presets[i].hue);
Serial.printf("[load] array[%u] hue:%u\n", i, (uint8_t)array[i]["hue"]);
}
}
if (SERIAL_DEBUG) Serial.printf("\n");
}
if (SERIAL_DEBUG) {
Serial.printf("[load] ");
array.prettyPrintTo(Serial);
}
presetFile.close();
return array.success();
}
bool savePresets() {
if (SERIAL_DEBUG) Serial.printf("[save] Save Presets Start\n");
SPIFFS.remove("/presets.json"); // delete so we don't append
File presetFile = SPIFFS.open("/presets.json", "w");
if (!presetFile) {
if (SERIAL_DEBUG) Serial.printf("[save] Failed to open presets file for writing\n");
return false;
}
if (SERIAL_DEBUG) Serial.printf("[save] preset file loaded for saving\n");
DynamicJsonBuffer jsonBuffer; // couldn't get static buffer to work in the for loop.
JsonArray& json = jsonBuffer.createArray();
for (uint8_t i = 0; i < NUM_PRESETS; i++) {
JsonObject& root = jsonBuffer.createObject();
root["speed"] = presets[i].speed;
root["delay"] = presets[i].delay;
root["hue"] = presets[i].hue;
root["mode"] = presets[i].mode;
root["hue_cycle"] = presets[i].hue_cycle;
root["brightness"] = presets[i].brightness;
root["white"] = presets[i].white;
root["preset"] = presets[i].preset;
root["num_devices"] = presets[i].num_devices;
json.add(root);
if (SERIAL_DEBUG) {
Serial.printf("[save] hue[%u]:%u\n", i, presets[i].hue);
Serial.printf("[save] array[%u] hue:%u\n", i, (uint8_t)json[i]["hue"]);
}
}
if (SERIAL_DEBUG) Serial.printf("[save] buffersize: %d\n", jsonBuffer.size());
json.printTo(Serial);
json.printTo(presetFile);
presetFile.close();
if (SERIAL_DEBUG) Serial.printf("[save] Presets saved!\n");
return true;
}
/* format bytes helper function */
//String formatBytes(size_t bytes) {
// if (bytes < 1024) {
// return String(bytes) + "B";
// } else if (bytes < (1024 * 1024)) {
// return String(bytes / 1024.0) + "KB";
// } else if (bytes < (1024 * 1024 * 1024)) {
// return String(bytes / 1024.0 / 1024.0) + "MB";
// } else {
// return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
// }
//}