Browse Source

Fix bug: diveToFile breaks M23 (#13865)

Robby Candra 5 years ago
parent
commit
c369477cb0

+ 0
- 1
Marlin/src/feature/power_loss_recovery.cpp View File

@@ -392,7 +392,6 @@ void PrintJobRecovery::resume() {
392 392
 
393 393
   // Resume the SD file from the last position
394 394
   char *fn = info.sd_filename;
395
-  while (*fn == '/') fn++;
396 395
   sprintf_P(cmd, PSTR("M23 %s"), fn);
397 396
   gcode.process_subcommands_now(cmd);
398 397
   sprintf_P(cmd, PSTR("M24 S%ld T%ld"), info.sdpos, info.print_job_elapsed);

+ 2
- 0
Marlin/src/gcode/sdcard/M23.cpp View File

@@ -29,6 +29,8 @@
29 29
 
30 30
 /**
31 31
  * M23: Open a file
32
+ *
33
+ * The path is relative to the root directory
32 34
  */
33 35
 void GcodeSuite::M23() {
34 36
   // Simplify3D includes the size, so zero out all spaces (#7227)

+ 39
- 9
Marlin/src/sd/cardreader.cpp View File

@@ -461,7 +461,7 @@ void CardReader::openFile(char * const path, const bool read, const bool subcall
461 461
   stopSDPrint();
462 462
 
463 463
   SdFile *curDir;
464
-  const char * const fname = diveToFile(curDir, path, false);
464
+  const char * const fname = diveToFile(curDir, path);
465 465
   if (!fname) return;
466 466
 
467 467
   if (read) {
@@ -501,7 +501,7 @@ void CardReader::removeFile(const char * const name) {
501 501
   //stopSDPrint();
502 502
 
503 503
   SdFile *curDir;
504
-  const char * const fname = diveToFile(curDir, name, false);
504
+  const char * const fname = diveToFile(curDir, name);
505 505
   if (!fname) return;
506 506
 
507 507
   if (file.remove(curDir, fname)) {
@@ -641,15 +641,31 @@ uint16_t CardReader::getnrfilenames() {
641 641
  *
642 642
  * A NULL result indicates an unrecoverable error.
643 643
  */
644
-const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, const bool echo) {
645
-  SdFile myDir;
646
-  if (path[0] != '/') { curDir = &workDir; return path; }
644
+const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, const bool echo/*=false*/) {
645
+  // Track both parent and subfolder
646
+  static SdFile newDir1, newDir2;
647
+  SdFile *sub = &newDir1, *startDir;
648
+
649
+  const char *dirname_start = path;
650
+  char echo_fn[105];
651
+  
652
+  if (path[0] == '/') { 
653
+    curDir = &root;
654
+    workDirDepth = 0;
655
+    dirname_start++;
656
+  }
657
+  else
658
+    curDir = &workDir; 
659
+
660
+  startDir = curDir;
647 661
 
648
-  curDir = &root;
649
-  const char *dirname_start = &path[1];
662
+  // Start dive
650 663
   while (dirname_start) {
664
+    // Find next sub
651 665
     char * const dirname_end = strchr(dirname_start, '/');
652 666
     if (dirname_end <= dirname_start) break;
667
+
668
+    // Set subDirName
653 669
     const uint8_t len = dirname_end - dirname_start;
654 670
     char dosSubdirname[len + 1];
655 671
     strncpy(dosSubdirname, dirname_start, len);
@@ -657,11 +673,25 @@ const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, con
657 673
 
658 674
     if (echo) SERIAL_ECHOLN(dosSubdirname);
659 675
 
660
-    if (!myDir.open(curDir, dosSubdirname, O_READ)) {
676
+    // Open curDir
677
+    if (!sub->open(curDir, dosSubdirname, O_READ)) {
661 678
       SERIAL_ECHOLNPAIR(MSG_SD_OPEN_FILE_FAIL, dosSubdirname, ".");
662 679
       return NULL;
663 680
     }
664
-    curDir = &myDir;
681
+
682
+    // Close curDir if not at starting-point
683
+    if (curDir != startDir) curDir->close();
684
+
685
+    // curDir now subDir
686
+    curDir = sub;
687
+
688
+    // Update workDirParents and workDirDepth
689
+    if (workDirDepth < MAX_DIR_DEPTH) workDirParents[workDirDepth++] = *curDir;
690
+
691
+    // Point sub pointer to unused newDir
692
+    sub = (curDir != &newDir1) ? &newDir1 : &newDir2;
693
+
694
+    // dirname_start point to next sub
665 695
     dirname_start = dirname_end + 1;
666 696
   }
667 697
   return dirname_start;

+ 1
- 1
Marlin/src/sd/cardreader.h View File

@@ -88,7 +88,7 @@ public:
88 88
   static int8_t updir();
89 89
   static void setroot();
90 90
 
91
-  static const char* diveToFile(SdFile*& curDir, const char * const path, const bool echo);
91
+  static const char* diveToFile(SdFile*& curDir, const char * const path, const bool echo=false);
92 92
 
93 93
   static uint16_t get_num_Files();
94 94
 

Loading…
Cancel
Save