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,8 +49,9 @@ void setup() {
if(mode == SETUP && preferences::getFormat()) { if(mode == SETUP && preferences::getFormat()) {
led::setColor(255,255,255); led::setColor(255,255,255);
format::start(preferences::getDriveName().c_str()); format::start(preferences::getDriveName().c_str());
preferences::save();
} }
// ========== Setup Mode ========== // // ========== Setup Mode ========== //
if (mode == SETUP) { if (mode == SETUP) {
led::setColor(preferences::getSetupColor()); // Set LED to blue led::setColor(preferences::getSetupColor()); // Set LED to blue

View File

@ -70,7 +70,7 @@ namespace msc {
flash.begin(); flash.begin();
fatfs.begin(&flash); fatfs.begin(&flash);
} }
void setID(const char* vid, const char* pid, const char* rev) { void setID(const char* vid, const char* pid, const char* rev) {
usb_msc.setID(vid, pid, rev); // Max. 8, 16, 4 characters usb_msc.setID(vid, pid, rev); // Max. 8, 16, 4 characters
} }
@ -89,7 +89,7 @@ namespace msc {
return tmp; return tmp;
} }
bool open(const char* path) { bool open(const char* path, bool write) {
debug("Open new file: "); debug("Open new file: ");
debugln(path); debugln(path);
@ -111,22 +111,14 @@ namespace msc {
file_stack.push(file_element); file_stack.push(file_element);
// Open file and return whether it was successful // 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() { bool openNextFile() {
debug("Opening next file: "); debug("Opening next file: ");
// Close current file and remove it from stack (it's not needed anymore) // Close current file and remove it from stack (it's not needed anymore)
debug("Stack (before file close): "); 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());
// If stack is now empty, we're done // If stack is now empty, we're done
if (file_stack.empty()) { if (file_stack.empty()) {
@ -153,6 +145,19 @@ namespace msc {
return false; 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() { uint32_t getPosition() {
return file.curPosition(); return file.curPosition();
} }
@ -204,4 +209,10 @@ namespace msc {
bool getInLine() { bool getInLine() {
return in_line; 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,11 +6,16 @@
namespace msc { namespace msc {
void init(); void init();
void setID(const char* vid, const char* pid, const char* rev); void setID(const char* vid, const char* pid, const char* rev);
void enableDrive(); void enableDrive();
bool changed(); bool changed();
bool open(const char* path);
bool open(const char* path, bool write = false);
bool openNextFile(); bool openNextFile();
void close();
uint32_t getPosition(); uint32_t getPosition();
void gotoPosition(uint32_t pos); void gotoPosition(uint32_t pos);
@ -18,4 +23,6 @@ namespace msc {
size_t read(char* buffer, size_t len); size_t read(char* buffer, size_t len);
size_t readLine(char* buffer, size_t len); size_t readLine(char* buffer, size_t len);
bool getInLine(); bool getInLine();
size_t write(const char* buffer, size_t len);
} }

View File

@ -33,6 +33,39 @@ namespace preferences {
bool format { false }; bool format { false };
std::string drive_name { "USB Nova" }; 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 ======== // // ======== PUBLIC ======== //
void load() { void load() {
// Read config file // Read config file
@ -83,7 +116,7 @@ namespace preferences {
arr.add(setup_color[1]); arr.add(setup_color[1]);
arr.add(setup_color[2]); arr.add(setup_color[2]);
} }
if (!config_doc.containsKey("idle_color")) { if (!config_doc.containsKey("idle_color")) {
JsonArray arr = config_doc.createNestedArray("idle_color"); JsonArray arr = config_doc.createNestedArray("idle_color");
arr.add(idle_color[0]); arr.add(idle_color[0]);
@ -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() { bool mscEnabled() {
return enable_msc; return enable_msc;
} }

View File

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