1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-02 02:42:36 +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:
Mitch Bradley
2021-06-11 12:44:30 -10:00
parent b5ac5eb166
commit 6858f9957c
3 changed files with 19 additions and 32 deletions

View File

@@ -22,6 +22,7 @@
#include "MachineConfig.h"
#include <WiFi.h>
#include <SPIFFS.h>
void grbl_init() {
try {
@@ -43,6 +44,10 @@ void grbl_init() {
report_machine_type(CLIENT_SERIAL);
#endif
if (!SPIFFS.begin(true)) {
log_error("Cannot mount the local filesystem");
}
// Load Grbl settings from non-volatile storage
debug_serial("Initializing settings...");
settings_init();
@@ -64,7 +69,6 @@ void grbl_init() {
config->_axes->read_settings();
config->_axes->init();
info_serial("Initializing system...");
config->_control->init();
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.

View File

@@ -476,36 +476,29 @@ void MachineConfig::afterParse() {
}
size_t MachineConfig::readFile(const char* filename, char*& buffer) {
if (!SPIFFS.begin(true)) {
log_info("Cannot mount the local filesystem");
return 0;
String path = filename;
if ((path.length() > 0) && (path[0] != '/')) {
path = "/" + path;
}
FILE* file = fopen(filename, "rb");
File file = SPIFFS.open(path, FILE_READ);
if (!file) {
log_info("Cannot open the config file " << filename);
log_info("Cannot open the config file " << path);
return 0;
}
// Let's just read the entire file in one chunk for now. If we get
// 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);
auto filesize = file.size();
// log_debug("Configuration file has " << int(filesize) << " bytes");
buffer = new char[filesize + 1];
long pos = 0;
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) {
break;
}
pos += read;
}
fclose(file);
file.close();
buffer[filesize] = 0;
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";
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
// builtin config. This helps prevent reset loops on bad config files.
@@ -548,7 +539,7 @@ bool MachineConfig::load(const char* filename) {
// Process file:
bool successful = false;
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::ParserHandler handler(parser);
@@ -568,7 +559,7 @@ bool MachineConfig::load(const char* filename) {
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 {
Configuration::AfterParse afterParse;
@@ -578,7 +569,7 @@ bool MachineConfig::load(const char* filename) {
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 {
Configuration::Validator validator;
@@ -588,14 +579,10 @@ bool MachineConfig::load(const char* filename) {
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;
// 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) {
sys.state = State::ConfigAlarm;
auto startNear = ex.Near();

View File

@@ -59,8 +59,6 @@ namespace WebUI {
String& h = config->_comms->_hostname;
//Start SPIFFS
SPIFFS.begin(true);
# ifdef ENABLE_OTA
ArduinoOTA
.onStart([]() {
@@ -137,8 +135,6 @@ namespace WebUI {
# ifdef ENABLE_OTA
ArduinoOTA.end();
# endif
//Stop SPIFFS
SPIFFS.end();
# ifdef ENABLE_MDNS
//Stop mDNS
MDNS.end();