Commit 53352c38 authored by Simone Primarosa's avatar Simone Primarosa

Added ability to write configuration on SD

parent 7e5cfed9
...@@ -189,7 +189,7 @@ void CardReader::getAbsFilename(char *t) { ...@@ -189,7 +189,7 @@ void CardReader::getAbsFilename(char *t) {
t[0] = 0; t[0] = 0;
} }
void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/) { void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/, bool lcd_status/*=true*/) {
if (!cardOK) return; if (!cardOK) return;
if (file.isOpen()) { //replacing current file by new file, or subfile call if (file.isOpen()) { //replacing current file by new file, or subfile call
if (!replace_current) { if (!replace_current) {
...@@ -200,7 +200,6 @@ void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/) ...@@ -200,7 +200,6 @@ void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/)
kill(); kill();
return; return;
} }
SERIAL_ECHO_START; SERIAL_ECHO_START;
SERIAL_ECHOPGM("SUBROUTINE CALL target:\""); SERIAL_ECHOPGM("SUBROUTINE CALL target:\"");
SERIAL_ECHO(name); SERIAL_ECHO(name);
...@@ -282,7 +281,7 @@ void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/) ...@@ -282,7 +281,7 @@ void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/)
SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED); SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
getfilename(0, fname); getfilename(0, fname);
lcd_setstatus(longFilename[0] ? longFilename : fname); if(lcd_status) lcd_setstatus(longFilename[0] ? longFilename : fname);
} }
else { else {
SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL); SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
...@@ -291,16 +290,16 @@ void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/) ...@@ -291,16 +290,16 @@ void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/)
} }
} }
else { //write else { //write
if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) { if (file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
SERIAL_PROTOCOL(fname);
SERIAL_PROTOCOLCHAR('.');
}
else {
saving = true; saving = true;
SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE); SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE);
SERIAL_PROTOCOLLN(name); SERIAL_PROTOCOLLN(name);
lcd_setstatus(fname); if(lcd_status) lcd_setstatus(fname);
}
else {
SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
SERIAL_PROTOCOL(fname);
SERIAL_PROTOCOLCHAR('.');
} }
} }
} }
...@@ -432,7 +431,7 @@ void CardReader::checkautostart(bool force) { ...@@ -432,7 +431,7 @@ void CardReader::checkautostart(bool force) {
autostart_index++; autostart_index++;
} }
void CardReader::closefile(bool store_location) { void CardReader::closeFile(bool store_location) {
file.sync(); file.sync();
file.close(); file.close();
saving = logging = false; saving = logging = false;
...@@ -443,6 +442,94 @@ void CardReader::closefile(bool store_location) { ...@@ -443,6 +442,94 @@ void CardReader::closefile(bool store_location) {
} }
} }
void CardReader::parseKeyLine(char *key, char *value, int &len_k, int &len_v) {
if (!cardOK || !isFileOpen()) return;
int ln_buf = 0;
char ln_char;
bool ln_space = false, ln_ignore = false, key_found = false;
while(!eof()) { //READ KEY
ln_char = (char)get();
if(ln_char == '\n') {
ln_buf = 0;
ln_ignore = false; //We've reached a new line try to find a key again
continue;
}
if(ln_ignore) continue;
if(ln_char == ' ') {
ln_space = true;
continue;
}
if(ln_char == '=') {
key[ln_buf] = '\0';
len_k = ln_buf;
key_found = true;
break; //key finded and buffered
}
if(ln_char == ';' || ln_buf+1 >= len_k || ln_space && ln_buf > 0) { //comments on key is not allowd. Also key len can't be longer than len_k or contain spaces. Stop buffering and try the next line
ln_ignore = true;
continue;
}
ln_space = false;
key[ln_buf] = ln_char;
ln_buf++;
}
if(!key_found) { //definitly there isn't no more key that can be readed in the file
key[0] = '\0';
value[0] = '\0';
len_k = 0;
len_v = 0;
return;
}
ln_buf = 0;
ln_ignore = false;
while(!eof()) { //READ VALUE
ln_char = (char)get();
if(ln_char == '\n') {
value[ln_buf] = '\0';
len_v = ln_buf;
break; //new line reached, we can stop
}
if(ln_ignore|| ln_char == ' ' && ln_buf == 0) continue; //ignore also initial spaces of the value
if(ln_char == ';' || ln_buf+1 >= len_v) { //comments reached or value len longer than len_v. Stop buffering and go to the next line.
ln_ignore = true;
continue;
}
value[ln_buf] = ln_char;
ln_buf++;
}
}
void CardReader::unparseKeyLine(const char *key, char *value) {
if (!cardOK || !isFileOpen()) return;
file.writeError = false;
file.write(key);
if (file.writeError) {
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
return;
}
file.writeError = false;
file.write("=");
if (file.writeError) {
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
return;
}
file.writeError = false;
file.write(value);
if (file.writeError) {
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
return;
}
file.writeError = false;
file.write("\n");
if (file.writeError) {
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
}
}
/** /**
* Get the name of a file in the current directory by index * Get the name of a file in the current directory by index
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment