Separate reading G-code files and writing to a file

- extract common strings
- cleanup openFileWrite and openFileReadFilteredGcode formatting a bit
Alltogether - code size 400B down
This commit is contained in:
D.R.racer 2021-01-28 09:37:58 +01:00
parent c05b625b1c
commit 7279de7403
4 changed files with 80 additions and 108 deletions

View File

@ -4005,7 +4005,7 @@ void process_commands()
} else if (code_seen_P(PSTR("M28"))) { // PRUSA M28 } else if (code_seen_P(PSTR("M28"))) { // PRUSA M28
trace(); trace();
prusa_sd_card_upload = true; prusa_sd_card_upload = true;
card.openFile(strchr_pointer+4,false); card.openFileWrite(strchr_pointer+4);
} else if (code_seen_P(PSTR("SN"))) { // PRUSA SN } else if (code_seen_P(PSTR("SN"))) { // PRUSA SN
gcode_PRUSA_SN(); gcode_PRUSA_SN();
@ -5769,7 +5769,7 @@ if(eSoundMode!=e_SOUND_MODE_SILENT)
starpos = (strchr(strchr_pointer + 4,'*')); starpos = (strchr(strchr_pointer + 4,'*'));
if(starpos!=NULL) if(starpos!=NULL)
*(starpos)='\0'; *(starpos)='\0';
card.openFileFilteredGcode(strchr_pointer + 4); card.openFileReadFilteredGcode(strchr_pointer + 4);
break; break;
/*! /*!
@ -5833,7 +5833,7 @@ if(eSoundMode!=e_SOUND_MODE_SILENT)
strchr_pointer = strchr(npos,' ') + 1; strchr_pointer = strchr(npos,' ') + 1;
*(starpos) = '\0'; *(starpos) = '\0';
} }
card.openFile(strchr_pointer+4,false); card.openFileWrite(strchr_pointer+4);
break; break;
/*! ### M29 - Stop SD write <a href="https://reprap.org/wiki/G-code#M29:_Stop_writing_to_SD_card">M29: Stop writing to SD card</a> /*! ### M29 - Stop SD write <a href="https://reprap.org/wiki/G-code#M29:_Stop_writing_to_SD_card">M29: Stop writing to SD card</a>
@ -5894,7 +5894,7 @@ if(eSoundMode!=e_SOUND_MODE_SILENT)
if( card.cardOK ) if( card.cardOK )
{ {
card.openFile(namestartpos,true,!call_procedure); card.openFileReadFilteredGcode(namestartpos,!call_procedure);
if(code_seen('S')) if(code_seen('S'))
if(strchr_pointer<namestartpos) //only if "S" is occuring _before_ the filename if(strchr_pointer<namestartpos) //only if "S" is occuring _before_ the filename
card.setIndex(code_value_long()); card.setIndex(code_value_long());

View File

@ -277,7 +277,7 @@ void CardReader::startFileprint()
void CardReader::openLogFile(const char* name) void CardReader::openLogFile(const char* name)
{ {
logging = true; logging = true;
openFile(name, false); openFileWrite(name);
} }
void CardReader::getDirName(char* name, uint8_t level) void CardReader::getDirName(char* name, uint8_t level)
@ -375,8 +375,19 @@ void CardReader::diveSubfolder (const char *fileName, SdFile& dir)
} }
} }
// @@TODO merge with openFile, too much duplicated code and texts static const char ofKill[] PROGMEM = "trying to call sub-gcode files with too many levels.";
void CardReader::openFileFilteredGcode(const char* name, bool replace_current/* = false*/){ static const char ofSubroutineCallTgt[] PROGMEM = "SUBROUTINE CALL target:\"";
static const char ofParent[] PROGMEM = "\" parent:\"";
static const char ofPos[] PROGMEM = "\" pos";
static const char ofNowDoingFile[] PROGMEM = "Now doing file: ";
static const char ofNowFreshFile[] PROGMEM = "Now fresh file: ";
static const char ofFileOpened[] PROGMEM = "File opened: ";
static const char ofSize[] PROGMEM = " Size: ";
static const char ofFileSelected[] PROGMEM = "File selected";
static const char ofSDPrinting[] PROGMEM = "SD-PRINTING ";
static const char ofWritingToFile[] PROGMEM = "Writing to file: ";
void CardReader::openFileReadFilteredGcode(const char* name, bool replace_current/* = false*/){
if(!cardOK) if(!cardOK)
return; return;
@ -386,33 +397,33 @@ void CardReader::openFileFilteredGcode(const char* name, bool replace_current/*
// SERIAL_ERROR_START; // SERIAL_ERROR_START;
// SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:"); // SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
// SERIAL_ERRORLN(SD_PROCEDURE_DEPTH); // SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
kill(_n("trying to call sub-gcode files with too many levels."), 1); kill(ofKill, 1);
return; return;
} }
SERIAL_ECHO_START; SERIAL_ECHO_START;
SERIAL_ECHOPGM("SUBROUTINE CALL target:\""); SERIAL_ECHORPGM(ofSubroutineCallTgt);
SERIAL_ECHO(name); SERIAL_ECHO(name);
SERIAL_ECHOPGM("\" parent:\""); SERIAL_ECHORPGM(ofParent);
//store current filename and position //store current filename and position
getAbsFilename(filenames[file_subcall_ctr]); getAbsFilename(filenames[file_subcall_ctr]);
SERIAL_ECHO(filenames[file_subcall_ctr]); SERIAL_ECHO(filenames[file_subcall_ctr]);
SERIAL_ECHOPGM("\" pos"); SERIAL_ECHORPGM(ofPos);
SERIAL_ECHOLN(sdpos); SERIAL_ECHOLN(sdpos);
filespos[file_subcall_ctr]=sdpos; filespos[file_subcall_ctr]=sdpos;
file_subcall_ctr++; file_subcall_ctr++;
} else { } else {
SERIAL_ECHO_START; SERIAL_ECHO_START;
SERIAL_ECHOPGM("Now doing file: "); SERIAL_ECHORPGM(ofNowDoingFile);
SERIAL_ECHOLN(name); SERIAL_ECHOLN(name);
} }
file.close(); file.close();
} else { //opening fresh file } else { //opening fresh file
file_subcall_ctr=0; //resetting procedure depth in case user cancels print while in procedure file_subcall_ctr=0; //resetting procedure depth in case user cancels print while in procedure
SERIAL_ECHO_START; SERIAL_ECHO_START;
SERIAL_ECHOPGM("Now fresh file: "); SERIAL_ECHORPGM(ofNowFreshFile);
SERIAL_ECHOLN(name); SERIAL_ECHOLN(name);
} }
sdprinting = false; sdprinting = false;
@ -423,16 +434,16 @@ void CardReader::openFileFilteredGcode(const char* name, bool replace_current/*
if (file.openFilteredGcode(curDir, fname)) { if (file.openFilteredGcode(curDir, fname)) {
filesize = file.fileSize(); filesize = file.fileSize();
SERIAL_PROTOCOLRPGM(_N("File opened: "));////MSG_SD_FILE_OPENED SERIAL_PROTOCOLRPGM(ofFileOpened);////MSG_SD_FILE_OPENED
SERIAL_PROTOCOL(fname); SERIAL_PROTOCOL(fname);
SERIAL_PROTOCOLRPGM(_n(" Size: "));////MSG_SD_SIZE SERIAL_PROTOCOLRPGM(ofSize);////MSG_SD_SIZE
SERIAL_PROTOCOLLN(filesize); SERIAL_PROTOCOLLN(filesize);
sdpos = 0; sdpos = 0;
SERIAL_PROTOCOLLNRPGM(_N("File selected"));////MSG_SD_FILE_SELECTED SERIAL_PROTOCOLLNRPGM(ofFileSelected);////MSG_SD_FILE_SELECTED
getfilename(0, fname); getfilename(0, fname);
lcd_setstatus(longFilename[0] ? longFilename : fname); lcd_setstatus(longFilename[0] ? longFilename : fname);
lcd_setstatus("SD-PRINTING "); lcd_setstatuspgm(ofSDPrinting);
} else { } else {
SERIAL_PROTOCOLRPGM(MSG_SD_OPEN_FILE_FAIL); SERIAL_PROTOCOLRPGM(MSG_SD_OPEN_FILE_FAIL);
SERIAL_PROTOCOL(fname); SERIAL_PROTOCOL(fname);
@ -440,98 +451,59 @@ void CardReader::openFileFilteredGcode(const char* name, bool replace_current/*
} }
} }
void CardReader::openFile(const char* name,bool read, bool replace_current/*=true*/) void CardReader::openFileWrite(const char* name)
{ {
if(!cardOK) if(!cardOK)
return; 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((int)file_subcall_ctr>(int)SD_PROCEDURE_DEPTH-1)
{
// SERIAL_ERROR_START;
// SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
// SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
kill(_n("trying to call sub-gcode files with too many levels."), 1);
return;
}
SERIAL_ECHO_START;
SERIAL_ECHOPGM("SUBROUTINE CALL target:\"");
SERIAL_ECHO(name);
SERIAL_ECHOPGM("\" parent:\"");
//store current filename and position
getAbsFilename(filenames[file_subcall_ctr]);
SERIAL_ECHO(filenames[file_subcall_ctr]);
SERIAL_ECHOPGM("\" pos");
SERIAL_ECHOLN(sdpos);
filespos[file_subcall_ctr]=sdpos;
file_subcall_ctr++;
}
else
{
SERIAL_ECHO_START;
SERIAL_ECHOPGM("Now doing file: ");
SERIAL_ECHOLN(name);
}
file.close();
}
else //opening fresh file
{
file_subcall_ctr=0; //resetting procedure depth in case user cancels print while in procedure
SERIAL_ECHO_START;
SERIAL_ECHOPGM("Now fresh file: ");
SERIAL_ECHOLN(name);
}
sdprinting = false;
SdFile myDir; // @@TODO I doubt this is necessary for file saving:
const char *fname=name;
diveSubfolder(fname,myDir);
if(read) if((int)file_subcall_ctr>(int)SD_PROCEDURE_DEPTH-1){
{ // SERIAL_ERROR_START;
if (file.open(curDir, fname, O_READ)) // SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
{ // SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
filesize = file.fileSize(); kill(ofKill, 1);
SERIAL_PROTOCOLRPGM(_N("File opened: "));////MSG_SD_FILE_OPENED return;
SERIAL_PROTOCOL(fname); }
SERIAL_PROTOCOLRPGM(_n(" Size: "));////MSG_SD_SIZE
SERIAL_PROTOCOLLN(filesize); SERIAL_ECHO_START;
sdpos = 0; SERIAL_ECHORPGM(ofSubroutineCallTgt);
SERIAL_ECHO(name);
SERIAL_PROTOCOLLNRPGM(_N("File selected"));////MSG_SD_FILE_SELECTED SERIAL_ECHORPGM(ofParent);
getfilename(0, fname);
lcd_setstatus(longFilename[0] ? longFilename : fname); //store current filename and position
lcd_setstatuspgm(PSTR("SD-PRINTING")); getAbsFilename(filenames[file_subcall_ctr]);
SERIAL_ECHO(filenames[file_subcall_ctr]);
SERIAL_ECHORPGM(ofPos);
SERIAL_ECHOLN(sdpos);
filespos[file_subcall_ctr]=sdpos;
file_subcall_ctr++;
file.close();
} else { //opening fresh file
file_subcall_ctr=0; //resetting procedure depth in case user cancels print while in procedure
SERIAL_ECHO_START;
SERIAL_ECHORPGM(ofNowFreshFile);
SERIAL_ECHOLN(name);
} }
else sdprinting = false;
{
SERIAL_PROTOCOLRPGM(MSG_SD_OPEN_FILE_FAIL); SdFile myDir;
SERIAL_PROTOCOL(fname); const char *fname=name;
SERIAL_PROTOCOLLN('.'); diveSubfolder(fname,myDir);
//write
if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)){
SERIAL_PROTOCOLRPGM(MSG_SD_OPEN_FILE_FAIL);
SERIAL_PROTOCOL(fname);
SERIAL_PROTOCOLLN('.');
} else {
saving = true;
SERIAL_PROTOCOLRPGM(ofWritingToFile);////MSG_SD_WRITE_TO_FILE
SERIAL_PROTOCOLLN(name);
lcd_setstatus(fname);
} }
}
else
{ //write
if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
{
SERIAL_PROTOCOLRPGM(MSG_SD_OPEN_FILE_FAIL);
SERIAL_PROTOCOL(fname);
SERIAL_PROTOCOLLN('.');
}
else
{
saving = true;
SERIAL_PROTOCOLRPGM(_N("Writing to file: "));////MSG_SD_WRITE_TO_FILE
SERIAL_PROTOCOLLN(name);
lcd_setstatus(fname);
}
}
} }
void CardReader::removeFile(const char* name) void CardReader::removeFile(const char* name)
@ -1069,7 +1041,7 @@ void CardReader::printingHasFinished()
{ {
file.close(); file.close();
file_subcall_ctr--; file_subcall_ctr--;
openFile(filenames[file_subcall_ctr],true,true); openFileReadFilteredGcode(filenames[file_subcall_ctr],true);
setIndex(filespos[file_subcall_ctr]); setIndex(filespos[file_subcall_ctr]);
startFileprint(); startFileprint();
} }

View File

@ -21,8 +21,8 @@ public:
//this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset //this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset
void checkautostart(bool x); void checkautostart(bool x);
void openFile(const char* name,bool read,bool replace_current=true); void openFileWrite(const char* name);
void openFileFilteredGcode(const char* name, bool replace_current = false); void openFileReadFilteredGcode(const char* name, bool replace_current = false);
void openLogFile(const char* name); void openLogFile(const char* name);
void removeFile(const char* name); void removeFile(const char* name);
void closefile(bool store_location=false); void closefile(bool store_location=false);

View File

@ -8474,7 +8474,7 @@ static void lcd_selftest_screen_step(int _row, int _col, int _state, const char
static bool check_file(const char* filename) { static bool check_file(const char* filename) {
if (farm_mode) return true; if (farm_mode) return true;
card.openFileFilteredGcode((char*)filename, true); card.openFileReadFilteredGcode(filename, true);
bool result = false; bool result = false;
const uint32_t filesize = card.getFileSize(); const uint32_t filesize = card.getFileSize();
uint32_t startPos = 0; uint32_t startPos = 0;