If flash init failed, format it automatically & save preferences.json
This commit is contained in:
parent
69a0e36bd8
commit
5f4543ca0e
26
USBNova.ino
26
USBNova.ino
|
@ -24,15 +24,29 @@ Mode mode;
|
||||||
void setup() {
|
void setup() {
|
||||||
// Initialize all the things
|
// Initialize all the things
|
||||||
debug_init();
|
debug_init();
|
||||||
msc::init();
|
|
||||||
selector::init();
|
selector::init();
|
||||||
led::init();
|
led::init();
|
||||||
preferences::load();
|
|
||||||
|
// Initialize memory and ceck for problems
|
||||||
|
if (!msc::init()) {
|
||||||
|
format::start(); // Format the drive
|
||||||
|
|
||||||
|
// If it still fails, blink red LED
|
||||||
|
if (!msc::init()) {
|
||||||
|
while (true) {
|
||||||
|
led::setColor(255, 0, 0);
|
||||||
|
delay(500);
|
||||||
|
led::setColor(0, 0, 0);
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Read the mode from the toggle switch position
|
// Read the mode from the toggle switch position
|
||||||
mode = selector::read() ? SETUP : ATTACK;
|
mode = selector::read() ? SETUP : ATTACK;
|
||||||
|
|
||||||
// Preferences
|
// Preferences
|
||||||
|
preferences::load();
|
||||||
led::setEnable(preferences::ledEnabled());
|
led::setEnable(preferences::ledEnabled());
|
||||||
keyboard::setLocale(locale::get(preferences::getDefaultLayout().c_str()));
|
keyboard::setLocale(locale::get(preferences::getDefaultLayout().c_str()));
|
||||||
keyboard::setID(preferences::getHidVid(), preferences::getHidPid(), preferences::getHidRev());
|
keyboard::setID(preferences::getHidVid(), preferences::getHidPid(), preferences::getHidRev());
|
||||||
|
@ -46,9 +60,13 @@ void setup() {
|
||||||
if (preferences::mscEnabled() || (mode == SETUP)) msc::enableDrive();
|
if (preferences::mscEnabled() || (mode == SETUP)) msc::enableDrive();
|
||||||
|
|
||||||
// Format Flash
|
// Format Flash
|
||||||
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create preferences file if it doesn't exist yet
|
||||||
|
if (!msc::exists(PREFERENCES_PATH)) {
|
||||||
preferences::save();
|
preferences::save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
3
config.h
3
config.h
|
@ -21,3 +21,6 @@
|
||||||
// ===== Parser Settings ===== //
|
// ===== Parser Settings ===== //
|
||||||
#define CASE_SENSETIVE false
|
#define CASE_SENSETIVE false
|
||||||
#define DEFAULT_SLEEP 5
|
#define DEFAULT_SLEEP 5
|
||||||
|
|
||||||
|
// ===== Other Stuff ====== //
|
||||||
|
#define PREFERENCES_PATH "preferences.json"
|
|
@ -4,5 +4,5 @@
|
||||||
|
|
||||||
|
|
||||||
namespace format {
|
namespace format {
|
||||||
bool start(const char* drive_name);
|
bool start(const char* drive_name = "USB Nova");
|
||||||
}
|
}
|
|
@ -66,9 +66,18 @@ namespace msc {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== PUBLIC ===== //
|
// ===== PUBLIC ===== //
|
||||||
void init() {
|
bool init() {
|
||||||
flash.begin();
|
if(!flash.begin()) {
|
||||||
fatfs.begin(&flash);
|
debugln("Couldn't find flash chip!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!fatfs.begin(&flash)) {
|
||||||
|
debugln("Couldn't mount flash!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setID(const char* vid, const char* pid, const char* rev) {
|
void setID(const char* vid, const char* pid, const char* rev) {
|
||||||
|
@ -89,7 +98,11 @@ namespace msc {
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool open(const char* path, bool write) {
|
bool exists(const char* filename) {
|
||||||
|
return fatfs.exists(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool open(const char* path) {
|
||||||
debug("Open new file: ");
|
debug("Open new file: ");
|
||||||
debugln(path);
|
debugln(path);
|
||||||
|
|
||||||
|
@ -111,7 +124,7 @@ 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, write ? (O_RDWR | O_CREAT) : O_RDONLY);
|
return file.open(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool openNextFile() {
|
bool openNextFile() {
|
||||||
|
@ -210,9 +223,17 @@ namespace msc {
|
||||||
return in_line;
|
return in_line;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t write(const char* buffer, size_t len) {
|
size_t write(const char* path, const char* buffer, size_t len) {
|
||||||
if (!file.isOpen()) return 0;
|
FatFile wfile;
|
||||||
|
wfile.open(path, (O_RDWR | O_CREAT));
|
||||||
|
if (!wfile.isOpen()) return 0;
|
||||||
|
|
||||||
return file.write(buffer, len);
|
size_t written = wfile.write(buffer, len);
|
||||||
|
wfile.close();
|
||||||
|
|
||||||
|
debug("Wrote ");
|
||||||
|
debugln(written);
|
||||||
|
|
||||||
|
return written;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,14 +5,15 @@
|
||||||
#include "SdFat.h"
|
#include "SdFat.h"
|
||||||
|
|
||||||
namespace msc {
|
namespace msc {
|
||||||
void init();
|
bool 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 exists(const char* filename);
|
||||||
|
|
||||||
bool open(const char* path, bool write = false);
|
bool open(const char* path);
|
||||||
bool openNextFile();
|
bool openNextFile();
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
@ -24,5 +25,5 @@ namespace msc {
|
||||||
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);
|
size_t write(const char* path, const char* buffer, size_t len);
|
||||||
}
|
}
|
|
@ -8,6 +8,8 @@
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include "../msc/msc.h"
|
#include "../msc/msc.h"
|
||||||
|
|
||||||
|
#define JSON_SIZE 1536
|
||||||
|
|
||||||
namespace preferences {
|
namespace preferences {
|
||||||
// ========== PRIVATE ========= //
|
// ========== PRIVATE ========= //
|
||||||
bool enable_msc { false };
|
bool enable_msc { false };
|
||||||
|
@ -69,15 +71,16 @@ namespace preferences {
|
||||||
// ======== PUBLIC ======== //
|
// ======== PUBLIC ======== //
|
||||||
void load() {
|
void load() {
|
||||||
// Read config file
|
// Read config file
|
||||||
char buffer[1024];
|
char* buffer = (char*)malloc(JSON_SIZE);
|
||||||
StaticJsonDocument<1024> config_doc;
|
DynamicJsonDocument config_doc(JSON_SIZE);
|
||||||
|
|
||||||
// Open the file and read it into a buffer
|
// Open the file and read it into a buffer
|
||||||
if (!msc::open("preferences.json")) return;
|
if (!msc::open(PREFERENCES_PATH)) return;
|
||||||
msc::read(buffer, sizeof(buffer));
|
msc::read(buffer, JSON_SIZE);
|
||||||
|
|
||||||
// Deserialize the JSON document
|
// Deserialize the JSON document
|
||||||
DeserializationError error = deserializeJson(config_doc, buffer);
|
DeserializationError error = deserializeJson(config_doc, buffer);
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
// Test if parsing succeeds.
|
// Test if parsing succeeds.
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -171,7 +174,7 @@ namespace preferences {
|
||||||
|
|
||||||
void save() {
|
void save() {
|
||||||
// Create a new JSON document (and string buffer)
|
// Create a new JSON document (and string buffer)
|
||||||
StaticJsonDocument<1024> json_doc;
|
DynamicJsonDocument json_doc(JSON_SIZE);
|
||||||
// JsonObject json_obj = json_doc.as<JsonObject>();
|
// JsonObject json_obj = json_doc.as<JsonObject>();
|
||||||
std::string json_str = { "" };
|
std::string json_str = { "" };
|
||||||
|
|
||||||
|
@ -180,16 +183,14 @@ namespace preferences {
|
||||||
|
|
||||||
// Serialize JSON to buffer
|
// Serialize JSON to buffer
|
||||||
serializeJsonPretty(json_doc, json_str);
|
serializeJsonPretty(json_doc, json_str);
|
||||||
|
json_doc.clear();
|
||||||
|
|
||||||
// Write the buffer to file
|
// Write the buffer to file (and print results)
|
||||||
msc::open("preferences.json", true);
|
debugln(json_str.c_str());
|
||||||
debugln(json_str.length());
|
msc::write(PREFERENCES_PATH, json_str.c_str(), json_str.length());
|
||||||
debugln(msc::write(json_str.c_str(), json_str.length()));
|
|
||||||
msc::close();
|
|
||||||
|
|
||||||
//debugln(json_str.length());
|
debug("Saved ");
|
||||||
//debugln(json_str.c_str());
|
debugln(PREFERENCES_PATH);
|
||||||
debugln("Saved preferences.json");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mscEnabled() {
|
bool mscEnabled() {
|
||||||
|
|
Loading…
Reference in New Issue