mirror of
https://github.com/bdring/Grbl_Esp32.git
synced 2025-09-02 10:53:01 +02:00
Improved config file handling and messaging
Instead of using stdio calls like fopen(), the config file reading code now uses the same Arduino FS class methods that the rest of the code uses. This was done simply for consistency. One might argue that it would be better for everything to use stdio; the problem is that there are some parts of WebUI that depend on Arduino file class semantics. I also commented out some config debug messages that are no longer helpful.
This commit is contained in:
@@ -22,6 +22,7 @@
|
|||||||
#include "MachineConfig.h"
|
#include "MachineConfig.h"
|
||||||
|
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include <SPIFFS.h>
|
||||||
|
|
||||||
void grbl_init() {
|
void grbl_init() {
|
||||||
try {
|
try {
|
||||||
@@ -43,6 +44,10 @@ void grbl_init() {
|
|||||||
report_machine_type(CLIENT_SERIAL);
|
report_machine_type(CLIENT_SERIAL);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (!SPIFFS.begin(true)) {
|
||||||
|
log_error("Cannot mount the local filesystem");
|
||||||
|
}
|
||||||
|
|
||||||
// Load Grbl settings from non-volatile storage
|
// Load Grbl settings from non-volatile storage
|
||||||
debug_serial("Initializing settings...");
|
debug_serial("Initializing settings...");
|
||||||
settings_init();
|
settings_init();
|
||||||
@@ -64,7 +69,6 @@ void grbl_init() {
|
|||||||
config->_axes->read_settings();
|
config->_axes->read_settings();
|
||||||
config->_axes->init();
|
config->_axes->init();
|
||||||
|
|
||||||
info_serial("Initializing system...");
|
|
||||||
config->_control->init();
|
config->_control->init();
|
||||||
init_output_pins(); // Configure pinout pins and pin-change interrupt (Renamed due to conflict with esp32 files)
|
init_output_pins(); // Configure pinout pins and pin-change interrupt (Renamed due to conflict with esp32 files)
|
||||||
memset(sys_position, 0, sizeof(sys_position)); // Clear machine position.
|
memset(sys_position, 0, sizeof(sys_position)); // Clear machine position.
|
||||||
|
@@ -476,36 +476,29 @@ void MachineConfig::afterParse() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t MachineConfig::readFile(const char* filename, char*& buffer) {
|
size_t MachineConfig::readFile(const char* filename, char*& buffer) {
|
||||||
if (!SPIFFS.begin(true)) {
|
String path = filename;
|
||||||
log_info("Cannot mount the local filesystem");
|
if ((path.length() > 0) && (path[0] != '/')) {
|
||||||
return 0;
|
path = "/" + path;
|
||||||
}
|
}
|
||||||
|
File file = SPIFFS.open(path, FILE_READ);
|
||||||
FILE* file = fopen(filename, "rb");
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
log_info("Cannot open the config file " << filename);
|
log_info("Cannot open the config file " << path);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
auto filesize = file.size();
|
||||||
// Let's just read the entire file in one chunk for now. If we get
|
// log_debug("Configuration file has " << int(filesize) << " bytes");
|
||||||
// in trouble with this, we can cut it in pieces and read it per chunk.
|
|
||||||
fseek(file, 0, SEEK_END);
|
|
||||||
auto filesize = ftell(file);
|
|
||||||
log_debug("Configuration file has " << int(filesize) << " bytes");
|
|
||||||
|
|
||||||
fseek(file, 0, SEEK_SET);
|
|
||||||
buffer = new char[filesize + 1];
|
buffer = new char[filesize + 1];
|
||||||
|
|
||||||
long pos = 0;
|
long pos = 0;
|
||||||
while (pos < filesize) {
|
while (pos < filesize) {
|
||||||
auto read = fread(buffer + pos, 1, filesize - pos, file);
|
auto read = file.read((uint8_t*)(buffer + pos), filesize - pos);
|
||||||
if (read == 0) {
|
if (read == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
pos += read;
|
pos += read;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file);
|
file.close();
|
||||||
buffer[filesize] = 0;
|
buffer[filesize] = 0;
|
||||||
|
|
||||||
log_debug("Read config file:\r\n" << buffer);
|
log_debug("Read config file:\r\n" << buffer);
|
||||||
@@ -522,9 +515,7 @@ size_t MachineConfig::readFile(const char* filename, char*& buffer) {
|
|||||||
char defaultConfig[] = "name: Default\nboard: None\n";
|
char defaultConfig[] = "name: Default\nboard: None\n";
|
||||||
|
|
||||||
bool MachineConfig::load(const char* filename) {
|
bool MachineConfig::load(const char* filename) {
|
||||||
// Regardless of what we do next, we _always_ want a MachineConfig instance.
|
// log_info("Heap size before load config is " << uint32_t(xPortGetFreeHeapSize()));
|
||||||
|
|
||||||
log_info("Heap size before load config is " << uint32_t(xPortGetFreeHeapSize()));
|
|
||||||
|
|
||||||
// If the system crashes we skip the config file and use the default
|
// If the system crashes we skip the config file and use the default
|
||||||
// builtin config. This helps prevent reset loops on bad config files.
|
// builtin config. This helps prevent reset loops on bad config files.
|
||||||
@@ -548,7 +539,7 @@ bool MachineConfig::load(const char* filename) {
|
|||||||
// Process file:
|
// Process file:
|
||||||
bool successful = false;
|
bool successful = false;
|
||||||
try {
|
try {
|
||||||
log_info("Heap size before parsing is " << uint32_t(xPortGetFreeHeapSize()));
|
// log_info("Heap size before parsing is " << uint32_t(xPortGetFreeHeapSize()));
|
||||||
|
|
||||||
Configuration::Parser parser(input->begin(), input->end());
|
Configuration::Parser parser(input->begin(), input->end());
|
||||||
Configuration::ParserHandler handler(parser);
|
Configuration::ParserHandler handler(parser);
|
||||||
@@ -568,7 +559,7 @@ bool MachineConfig::load(const char* filename) {
|
|||||||
|
|
||||||
log_info("Parsed configuration. Running after-parse tasks");
|
log_info("Parsed configuration. Running after-parse tasks");
|
||||||
|
|
||||||
log_info("Heap size before after-parse is " << uint32_t(xPortGetFreeHeapSize()));
|
// log_info("Heap size before after-parse is " << uint32_t(xPortGetFreeHeapSize()));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Configuration::AfterParse afterParse;
|
Configuration::AfterParse afterParse;
|
||||||
@@ -578,7 +569,7 @@ bool MachineConfig::load(const char* filename) {
|
|||||||
|
|
||||||
log_info("Validating configuration");
|
log_info("Validating configuration");
|
||||||
|
|
||||||
log_info("Heap size before validation is " << uint32_t(xPortGetFreeHeapSize()));
|
// log_info("Heap size before validation is " << uint32_t(xPortGetFreeHeapSize()));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Configuration::Validator validator;
|
Configuration::Validator validator;
|
||||||
@@ -588,14 +579,10 @@ bool MachineConfig::load(const char* filename) {
|
|||||||
|
|
||||||
log_info("Validated configuration");
|
log_info("Validated configuration");
|
||||||
|
|
||||||
log_info("Heap size after configuation load is " << uint32_t(xPortGetFreeHeapSize()));
|
// log_info("Heap size after configuation load is " << uint32_t(xPortGetFreeHeapSize()));
|
||||||
|
|
||||||
successful = true;
|
successful = true;
|
||||||
|
|
||||||
// TODO FIXME: If we get here, we want to make the settings live by saving them as
|
|
||||||
// '/spiffs/config.yaml.new', storing it, and then deleting the .yaml and renaming it.
|
|
||||||
// That way, we can always check if the yaml is there, and if it's not, load the yaml.new.
|
|
||||||
|
|
||||||
} catch (const Configuration::ParseException& ex) {
|
} catch (const Configuration::ParseException& ex) {
|
||||||
sys.state = State::ConfigAlarm;
|
sys.state = State::ConfigAlarm;
|
||||||
auto startNear = ex.Near();
|
auto startNear = ex.Near();
|
||||||
|
@@ -59,8 +59,6 @@ namespace WebUI {
|
|||||||
|
|
||||||
String& h = config->_comms->_hostname;
|
String& h = config->_comms->_hostname;
|
||||||
|
|
||||||
//Start SPIFFS
|
|
||||||
SPIFFS.begin(true);
|
|
||||||
# ifdef ENABLE_OTA
|
# ifdef ENABLE_OTA
|
||||||
ArduinoOTA
|
ArduinoOTA
|
||||||
.onStart([]() {
|
.onStart([]() {
|
||||||
@@ -137,8 +135,6 @@ namespace WebUI {
|
|||||||
# ifdef ENABLE_OTA
|
# ifdef ENABLE_OTA
|
||||||
ArduinoOTA.end();
|
ArduinoOTA.end();
|
||||||
# endif
|
# endif
|
||||||
//Stop SPIFFS
|
|
||||||
SPIFFS.end();
|
|
||||||
# ifdef ENABLE_MDNS
|
# ifdef ENABLE_MDNS
|
||||||
//Stop mDNS
|
//Stop mDNS
|
||||||
MDNS.end();
|
MDNS.end();
|
||||||
|
Reference in New Issue
Block a user