diff --git a/USBNova.ino b/USBNova.ino index 137c8f9..b0476e8 100644 --- a/USBNova.ino +++ b/USBNova.ino @@ -49,8 +49,9 @@ void setup() { if(mode == SETUP && preferences::getFormat()) { led::setColor(255,255,255); format::start(preferences::getDriveName().c_str()); + preferences::save(); } - + // ========== Setup Mode ========== // if (mode == SETUP) { led::setColor(preferences::getSetupColor()); // Set LED to blue diff --git a/src/msc/msc.cpp b/src/msc/msc.cpp index 0adf4b4..bf5e15e 100644 --- a/src/msc/msc.cpp +++ b/src/msc/msc.cpp @@ -70,7 +70,7 @@ namespace msc { flash.begin(); fatfs.begin(&flash); } - + void setID(const char* vid, const char* pid, const char* rev) { usb_msc.setID(vid, pid, rev); // Max. 8, 16, 4 characters } @@ -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); + } } \ No newline at end of file diff --git a/src/msc/msc.h b/src/msc/msc.h index 80db6ce..e8b761d 100644 --- a/src/msc/msc.h +++ b/src/msc/msc.h @@ -6,11 +6,16 @@ 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); @@ -18,4 +23,6 @@ namespace msc { 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); } \ No newline at end of file diff --git a/src/preferences/preferences.cpp b/src/preferences/preferences.cpp index d16f5bd..3284702 100644 --- a/src/preferences/preferences.cpp +++ b/src/preferences/preferences.cpp @@ -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 @@ -83,7 +116,7 @@ namespace preferences { arr.add(setup_color[1]); arr.add(setup_color[2]); } - + if (!config_doc.containsKey("idle_color")) { JsonArray arr = config_doc.createNestedArray("idle_color"); 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(); + 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; } diff --git a/src/preferences/preferences.h b/src/preferences/preferences.h index 3f5d7f0..2ae0b8a 100644 --- a/src/preferences/preferences.h +++ b/src/preferences/preferences.h @@ -6,6 +6,7 @@ namespace preferences { void load(); + void save(); bool mscEnabled(); bool ledEnabled();