Selaa lähdekoodia

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
D.R.racer 3 vuotta sitten
vanhempi
commit
7279de7403
4 muutettua tiedostoa jossa 80 lisäystä ja 108 poistoa
  1. 4 4
      Firmware/Marlin_main.cpp
  2. 73 101
      Firmware/cardreader.cpp
  3. 2 2
      Firmware/cardreader.h
  4. 1 1
      Firmware/ultralcd.cpp

+ 4 - 4
Firmware/Marlin_main.cpp

@@ -4005,7 +4005,7 @@ void process_commands()
     } else if (code_seen_P(PSTR("M28"))) { // PRUSA M28
         trace();
         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
         gcode_PRUSA_SN();
@@ -5769,7 +5769,7 @@ if(eSoundMode!=e_SOUND_MODE_SILENT)
       starpos = (strchr(strchr_pointer + 4,'*'));
 	  if(starpos!=NULL)
         *(starpos)='\0';
-      card.openFileFilteredGcode(strchr_pointer + 4);
+      card.openFileReadFilteredGcode(strchr_pointer + 4);
       break;
 
     /*!
@@ -5833,7 +5833,7 @@ if(eSoundMode!=e_SOUND_MODE_SILENT)
         strchr_pointer = strchr(npos,' ') + 1;
         *(starpos) = '\0';
       }
-      card.openFile(strchr_pointer+4,false);
+      card.openFileWrite(strchr_pointer+4);
       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>
@@ -5894,7 +5894,7 @@ if(eSoundMode!=e_SOUND_MODE_SILENT)
 
       if( card.cardOK )
       {
-        card.openFile(namestartpos,true,!call_procedure);
+        card.openFileReadFilteredGcode(namestartpos,!call_procedure);
         if(code_seen('S'))
           if(strchr_pointer<namestartpos) //only if "S" is occuring _before_ the filename
             card.setIndex(code_value_long());

+ 73 - 101
Firmware/cardreader.cpp

@@ -277,7 +277,7 @@ void CardReader::startFileprint()
 void CardReader::openLogFile(const char* name)
 {
   logging = true;
-  openFile(name, false);
+  openFileWrite(name);
 }
 
 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
-void CardReader::openFileFilteredGcode(const char* name, bool replace_current/* = false*/){
+static const char ofKill[] PROGMEM = "trying to call sub-gcode files with too many levels.";
+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)
         return;
     
@@ -386,33 +397,33 @@ void CardReader::openFileFilteredGcode(const char* name, bool replace_current/*
                 // 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);
+                kill(ofKill, 1);
                 return;
             }
             
             SERIAL_ECHO_START;
-            SERIAL_ECHOPGM("SUBROUTINE CALL target:\"");
+            SERIAL_ECHORPGM(ofSubroutineCallTgt);
             SERIAL_ECHO(name);
-            SERIAL_ECHOPGM("\" parent:\"");
+            SERIAL_ECHORPGM(ofParent);
             
             //store current filename and position
             getAbsFilename(filenames[file_subcall_ctr]);
             
             SERIAL_ECHO(filenames[file_subcall_ctr]);
-            SERIAL_ECHOPGM("\" pos");
+            SERIAL_ECHORPGM(ofPos);
             SERIAL_ECHOLN(sdpos);
             filespos[file_subcall_ctr]=sdpos;
             file_subcall_ctr++;
         } else {
             SERIAL_ECHO_START;
-            SERIAL_ECHOPGM("Now doing file: ");
+            SERIAL_ECHORPGM(ofNowDoingFile);
             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_ECHORPGM(ofNowFreshFile);
         SERIAL_ECHOLN(name);
     }
     sdprinting = false;
@@ -423,16 +434,16 @@ void CardReader::openFileFilteredGcode(const char* name, bool replace_current/*
   
     if (file.openFilteredGcode(curDir, fname)) {
         filesize = file.fileSize();
-        SERIAL_PROTOCOLRPGM(_N("File opened: "));////MSG_SD_FILE_OPENED
+        SERIAL_PROTOCOLRPGM(ofFileOpened);////MSG_SD_FILE_OPENED
         SERIAL_PROTOCOL(fname);
-        SERIAL_PROTOCOLRPGM(_n(" Size: "));////MSG_SD_SIZE
+        SERIAL_PROTOCOLRPGM(ofSize);////MSG_SD_SIZE
         SERIAL_PROTOCOLLN(filesize);
         sdpos = 0;
         
-        SERIAL_PROTOCOLLNRPGM(_N("File selected"));////MSG_SD_FILE_SELECTED
+        SERIAL_PROTOCOLLNRPGM(ofFileSelected);////MSG_SD_FILE_SELECTED
         getfilename(0, fname);
         lcd_setstatus(longFilename[0] ? longFilename : fname);
-        lcd_setstatus("SD-PRINTING         ");
+        lcd_setstatuspgm(ofSDPrinting);
       } else {
         SERIAL_PROTOCOLRPGM(MSG_SD_OPEN_FILE_FAIL);
         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)
-    return;
-  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;
+    if(!cardOK)
+        return;
+    if(file.isOpen()){  //replacing current file by new file, or subfile call
 
-  SdFile myDir;
-  const char *fname=name;
-  diveSubfolder(fname,myDir);
+        // @@TODO I doubt this is necessary for file saving:
 
-  if(read)
-  {
-    if (file.open(curDir, fname, O_READ)) 
-    {
-      filesize = file.fileSize();
-      SERIAL_PROTOCOLRPGM(_N("File opened: "));////MSG_SD_FILE_OPENED
-      SERIAL_PROTOCOL(fname);
-      SERIAL_PROTOCOLRPGM(_n(" Size: "));////MSG_SD_SIZE
-      SERIAL_PROTOCOLLN(filesize);
-      sdpos = 0;
-      
-      SERIAL_PROTOCOLLNRPGM(_N("File selected"));////MSG_SD_FILE_SELECTED
-      getfilename(0, fname);
-      lcd_setstatus(longFilename[0] ? longFilename : fname);
-      lcd_setstatuspgm(PSTR("SD-PRINTING"));
-    }
-    else
-    {
-      SERIAL_PROTOCOLRPGM(MSG_SD_OPEN_FILE_FAIL);
-      SERIAL_PROTOCOL(fname);
-      SERIAL_PROTOCOLLN('.');
-    }
-  }
-  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('.');
+        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(ofKill, 1);
+            return;
+        }
+        
+        SERIAL_ECHO_START;
+        SERIAL_ECHORPGM(ofSubroutineCallTgt);
+        SERIAL_ECHO(name);
+        SERIAL_ECHORPGM(ofParent);
+        
+        //store current filename and position
+        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
-    {
-      saving = true;
-      SERIAL_PROTOCOLRPGM(_N("Writing to file: "));////MSG_SD_WRITE_TO_FILE
-      SERIAL_PROTOCOLLN(name);
-      lcd_setstatus(fname);
+    sdprinting = false;
+    
+    SdFile myDir;
+    const char *fname=name;
+    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);
     }
-  }
-  
 }
 
 void CardReader::removeFile(const char* name)
@@ -1069,7 +1041,7 @@ void CardReader::printingHasFinished()
     {
       file.close();
       file_subcall_ctr--;
-      openFile(filenames[file_subcall_ctr],true,true);
+      openFileReadFilteredGcode(filenames[file_subcall_ctr],true);
       setIndex(filespos[file_subcall_ctr]);
       startFileprint();
     }

+ 2 - 2
Firmware/cardreader.h

@@ -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
 
   void checkautostart(bool x); 
-  void openFile(const char* name,bool read,bool replace_current=true);
-  void openFileFilteredGcode(const char* name, bool replace_current = false);
+  void openFileWrite(const char* name);
+  void openFileReadFilteredGcode(const char* name, bool replace_current = false);
   void openLogFile(const char* name);
   void removeFile(const char* name);
   void closefile(bool store_location=false);

+ 1 - 1
Firmware/ultralcd.cpp

@@ -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) {
 	if (farm_mode) return true;
-	card.openFileFilteredGcode((char*)filename, true);
+	card.openFileReadFilteredGcode(filename, true);
 	bool result = false;
 	const uint32_t filesize = card.getFileSize();
 	uint32_t startPos = 0;