Save preferences.json after formatting

This commit is contained in:
Spacehuhn 2022-07-13 22:58:16 +02:00
parent 407125694b
commit 69a0e36bd8
5 changed files with 91 additions and 15 deletions

View File

@ -49,6 +49,7 @@ void setup() {
if(mode == SETUP && preferences::getFormat()) {
led::setColor(255,255,255);
format::start(preferences::getDriveName().c_str());
preferences::save();
}
// ========== Setup Mode ========== //

View File

@ -89,7 +89,7 @@ namespace msc {
return tmp;
}
bool open(const char* path) {
bool open(const char* path, bool write) {
debug("Open new file: ");
debugln(path);
@ -111,22 +111,14 @@ namespace msc {
file_stack.push(file_element);
// Open file and return whether it was successful
return file.open(path);
return file.open(path, write ? (O_RDWR | O_CREAT) : O_RDONLY);
}
bool openNextFile() {
debug("Opening next file: ");
// Close current file and remove it from stack (it's not needed anymore)
debug("Stack (before file close): ");
debugln(file_stack.size());
debugln(file_stack.top().path.c_str());
file.close();
file_stack.pop();
debug("Stack (after file close): ");
debugln(file_stack.size());
close();
// If stack is now empty, we're done
if (file_stack.empty()) {
@ -153,6 +145,19 @@ namespace msc {
return false;
}
void close() {
// Close current file and remove it from stack (it's not needed anymore)
debug("Stack (before file close): ");
debugln(file_stack.size());
debugln(file_stack.top().path.c_str());
file.close();
file_stack.pop();
debug("Stack (after file close): ");
debugln(file_stack.size());
}
uint32_t getPosition() {
return file.curPosition();
}
@ -204,4 +209,10 @@ namespace msc {
bool getInLine() {
return in_line;
}
size_t write(const char* buffer, size_t len) {
if (!file.isOpen()) return 0;
return file.write(buffer, len);
}
}

View File

@ -6,16 +6,23 @@
namespace msc {
void init();
void setID(const char* vid, const char* pid, const char* rev);
void enableDrive();
bool changed();
bool open(const char* path);
bool open(const char* path, bool write = false);
bool openNextFile();
void close();
uint32_t getPosition();
void gotoPosition(uint32_t pos);
size_t read(char* buffer, size_t len);
size_t readLine(char* buffer, size_t len);
bool getInLine();
size_t write(const char* buffer, size_t len);
}

View File

@ -33,6 +33,39 @@ namespace preferences {
bool format { false };
std::string drive_name { "USB Nova" };
void toJson(JsonDocument& root) {
root["enable_msc"] = enable_msc;
root["enable_led"] = enable_led;
root["hid_vid"] = hid_vid;
root["hid_pid"] = hid_pid;
root["hid_rev"] = hid_rev;
root["msc_vid"] = msc_vid;
root["msc_pid"] = msc_pid;
root["msc_rev"] = msc_rev;
root["default_layout"] = default_layout;
root["default_delay"] = default_delay;
root["main_script"] = main_script;
JsonArray attack_color_arr = root.createNestedArray("attack_color");
attack_color_arr.add(attack_color[0]);
attack_color_arr.add(attack_color[1]);
attack_color_arr.add(attack_color[2]);
JsonArray setup_color_arr = root.createNestedArray("setup_color");
setup_color_arr.add(setup_color[0]);
setup_color_arr.add(setup_color[1]);
setup_color_arr.add(setup_color[2]);
JsonArray idle_color_arr = root.createNestedArray("idle_color");
idle_color_arr.add(idle_color[0]);
idle_color_arr.add(idle_color[1]);
idle_color_arr.add(idle_color[2]);
}
// ======== PUBLIC ======== //
void load() {
// Read config file
@ -136,6 +169,29 @@ namespace preferences {
}
}
void save() {
// Create a new JSON document (and string buffer)
StaticJsonDocument<1024> json_doc;
// JsonObject json_obj = json_doc.as<JsonObject>();
std::string json_str = { "" };
// Add values
toJson(json_doc);
// Serialize JSON to buffer
serializeJsonPretty(json_doc, json_str);
// Write the buffer to file
msc::open("preferences.json", true);
debugln(json_str.length());
debugln(msc::write(json_str.c_str(), json_str.length()));
msc::close();
//debugln(json_str.length());
//debugln(json_str.c_str());
debugln("Saved preferences.json");
}
bool mscEnabled() {
return enable_msc;
}

View File

@ -6,6 +6,7 @@
namespace preferences {
void load();
void save();
bool mscEnabled();
bool ledEnabled();