Browse Source

Clean up and consolidate SD-related code (#10831)

Scott Lahteine 7 years ago
parent
commit
8b44745bc7
No account linked to committer's email address

+ 1
- 5
Marlin/src/sd/SdBaseFile.cpp View File

1055
   if (!isDir() || (0x1F & curPosition_)) return -1;
1055
   if (!isDir() || (0x1F & curPosition_)) return -1;
1056
 
1056
 
1057
   // If we have a longFilename buffer, mark it as invalid.
1057
   // If we have a longFilename buffer, mark it as invalid.
1058
-  // If long filename is found it will be filled automatically.
1058
+  // If a long filename is found it will be filled automatically.
1059
   if (longFilename) longFilename[0] = '\0';
1059
   if (longFilename) longFilename[0] = '\0';
1060
 
1060
 
1061
   while (1) {
1061
   while (1) {
1725
   return -1;
1725
   return -1;
1726
 }
1726
 }
1727
 
1727
 
1728
-#if ALLOW_DEPRECATED_FUNCTIONS
1729
-  void (*SdBaseFile::oldDateTime_)(uint16_t &date, uint16_t &time) = 0;
1730
-#endif
1731
-
1732
 #endif // SDSUPPORT
1728
 #endif // SDSUPPORT

+ 0
- 113
Marlin/src/sd/SdBaseFile.h View File

386
   bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
386
   bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
387
   bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
387
   bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
388
   dir_t* readDirCache();
388
   dir_t* readDirCache();
389
-
390
-// Deprecated functions
391
-#if ALLOW_DEPRECATED_FUNCTIONS
392
- public:
393
-
394
-  /**
395
-   * \deprecated Use:
396
-   * bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
397
-   * \param[out] bgnBlock the first block address for the file.
398
-   * \param[out] endBlock the last  block address for the file.
399
-   * \return true for success or false for failure.
400
-   */
401
-  bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) {
402
-    return contiguousRange(&bgnBlock, &endBlock);
403
-  }
404
-
405
-  /**
406
-   * \deprecated Use:
407
-   * bool createContiguous(SdBaseFile* dirFile, const char* path, uint32_t size)
408
-   * \param[in] dirFile The directory where the file will be created.
409
-   * \param[in] path A path with a valid DOS 8.3 file name.
410
-   * \param[in] size The desired file size.
411
-   * \return true for success or false for failure.
412
-   */
413
-  bool createContiguous(SdBaseFile& dirFile, const char* path, uint32_t size) {
414
-    return createContiguous(&dirFile, path, size);
415
-  }
416
-
417
-  /**
418
-   * \deprecated Use:
419
-   * static void dateTimeCallback(
420
-   *   void (*dateTime)(uint16_t* date, uint16_t* time));
421
-   * \param[in] dateTime The user's call back function.
422
-   */
423
-  static void dateTimeCallback(
424
-    void (*dateTime)(uint16_t &date, uint16_t &time)) {
425
-    oldDateTime_ = dateTime;
426
-    dateTime_ = dateTime ? oldToNew : 0;
427
-  }
428
-
429
-  /**
430
-   * \deprecated Use:
431
-   * bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
432
-   * \param[in] dirFile An open SdFat instance for the directory containing the
433
-   * file to be opened.
434
-   * \param[in] path A path with a valid 8.3 DOS name for the file.
435
-   * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
436
-   * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
437
-   * \return true for success or false for failure.
438
-   */
439
-  bool open(SdBaseFile& dirFile, const char* path, uint8_t oflag) {
440
-    return open(&dirFile, path, oflag);
441
-  }
442
-
443
-  /**
444
-   * \deprecated  Do not use in new apps
445
-   * \param[in] dirFile An open SdFat instance for the directory containing the
446
-   * file to be opened.
447
-   * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
448
-   * \return true for success or false for failure.
449
-   */
450
-  bool open(SdBaseFile& dirFile, const char* path) {
451
-    return open(dirFile, path, O_RDWR);
452
-  }
453
-
454
-  /**
455
-   * \deprecated Use:
456
-   * bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
457
-   * \param[in] dirFile An open SdFat instance for the directory.
458
-   * \param[in] index The \a index of the directory entry for the file to be
459
-   * opened.  The value for \a index is (directory file position)/32.
460
-   * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
461
-   * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
462
-   * \return true for success or false for failure.
463
-   */
464
-  bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) {
465
-    return open(&dirFile, index, oflag);
466
-  }
467
-
468
-  /**
469
-   * \deprecated Use: bool openRoot(SdVolume* vol);
470
-   * \param[in] vol The FAT volume containing the root directory to be opened.
471
-   * \return true for success or false for failure.
472
-   */
473
-  bool openRoot(SdVolume& vol) { return openRoot(&vol); }
474
-
475
-  /**
476
-   * \deprecated Use: int8_t readDir(dir_t* dir);
477
-   * \param[out] dir The dir_t struct that will receive the data.
478
-   * \return bytes read for success zero for eof or -1 for failure.
479
-   */
480
-  int8_t readDir(dir_t& dir, char* longFilename) {
481
-    return readDir(&dir, longFilename);
482
-  }
483
-
484
-  /**
485
-   * \deprecated Use:
486
-   * static uint8_t remove(SdBaseFile* dirFile, const char* path);
487
-   * \param[in] dirFile The directory that contains the file.
488
-   * \param[in] path The name of the file to be removed.
489
-   * \return true for success or false for failure.
490
-   */
491
-  static bool remove(SdBaseFile& dirFile, const char* path) { return remove(&dirFile, path); }
492
-
493
- private:
494
-  static void (*oldDateTime_)(uint16_t &date, uint16_t &time);
495
-  static void oldToNew(uint16_t * const date, uint16_t * const time) {
496
-    uint16_t d, t;
497
-    oldDateTime_(d, t);
498
-    *date = d;
499
-    *time = t;
500
-  }
501
-#endif  // ALLOW_DEPRECATED_FUNCTIONS
502
 };
389
 };
503
 
390
 
504
 #endif // _SDBASEFILE_H_
391
 #endif // _SDBASEFILE_H_

+ 0
- 5
Marlin/src/sd/SdFatConfig.h View File

63
 #define ENDL_CALLS_FLUSH 0
63
 #define ENDL_CALLS_FLUSH 0
64
 
64
 
65
 /**
65
 /**
66
- * Allow use of deprecated functions if ALLOW_DEPRECATED_FUNCTIONS is nonzero
67
- */
68
-#define ALLOW_DEPRECATED_FUNCTIONS 1
69
-
70
-/**
71
  * Allow FAT12 volumes if FAT12_SUPPORT is nonzero.
66
  * Allow FAT12 volumes if FAT12_SUPPORT is nonzero.
72
  * FAT12 has not been well tested.
67
  * FAT12 has not been well tested.
73
  */
68
  */

+ 0
- 18
Marlin/src/sd/SdVolume.h View File

190
   }
190
   }
191
   bool readBlock(uint32_t block, uint8_t* dst) { return sdCard_->readBlock(block, dst); }
191
   bool readBlock(uint32_t block, uint8_t* dst) { return sdCard_->readBlock(block, dst); }
192
   bool writeBlock(uint32_t block, const uint8_t* dst) { return sdCard_->writeBlock(block, dst); }
192
   bool writeBlock(uint32_t block, const uint8_t* dst) { return sdCard_->writeBlock(block, dst); }
193
-
194
-  // Deprecated functions
195
-  #if ALLOW_DEPRECATED_FUNCTIONS
196
-    public:
197
-      /**
198
-       * \deprecated Use: bool SdVolume::init(Sd2Card* dev);
199
-       * \param[in] dev The SD card where the volume is located.
200
-       * \return true for success or false for failure.
201
-       */
202
-      bool init(Sd2Card& dev) { return init(&dev); }
203
-      /**
204
-       * \deprecated Use: bool SdVolume::init(Sd2Card* dev, uint8_t vol);
205
-       * \param[in] dev The SD card where the volume is located.
206
-       * \param[in] part The partition to be used.
207
-       * \return true for success or false for failure.
208
-       */
209
-      bool init(Sd2Card& dev, uint8_t part) { return init(&dev, part); }
210
-  #endif  // ALLOW_DEPRECATED_FUNCTIONS
211
 };
193
 };
212
 
194
 
213
 #endif // _SDVOLUME_H_
195
 #endif // _SDVOLUME_H_

+ 85
- 121
Marlin/src/sd/cardreader.cpp View File

100
   uint8_t cnt = 0;
100
   uint8_t cnt = 0;
101
 
101
 
102
   // Read the next entry from a directory
102
   // Read the next entry from a directory
103
-  while (parent.readDir(p, longFilename) > 0) {
103
+  while (parent.readDir(&p, longFilename) > 0) {
104
 
104
 
105
     // If the entry is a directory and the action is LS_SerialPrint
105
     // If the entry is a directory and the action is LS_SerialPrint
106
     if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) {
106
     if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) {
107
 
107
 
108
       // Get the short name for the item, which we know is a folder
108
       // Get the short name for the item, which we know is a folder
109
-      char lfilename[FILENAME_LENGTH];
110
-      createFilename(lfilename, p);
109
+      char dosFilename[FILENAME_LENGTH];
110
+      createFilename(dosFilename, p);
111
 
111
 
112
       // Allocate enough stack space for the full path to a folder, trailing slash, and nul
112
       // Allocate enough stack space for the full path to a folder, trailing slash, and nul
113
       bool prepend_is_empty = (prepend[0] == '\0');
113
       bool prepend_is_empty = (prepend[0] == '\0');
114
-      int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(lfilename) + 1 + 1;
114
+      int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(dosFilename) + 1 + 1;
115
       char path[len];
115
       char path[len];
116
 
116
 
117
       // Append the FOLDERNAME12/ to the passed string.
117
       // Append the FOLDERNAME12/ to the passed string.
118
       // It contains the full path to the "parent" argument.
118
       // It contains the full path to the "parent" argument.
119
       // We now have the full path to the item in this folder.
119
       // We now have the full path to the item in this folder.
120
       strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty
120
       strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty
121
-      strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum
121
+      strcat(path, dosFilename); // FILENAME_LENGTH-1 characters maximum
122
       strcat(path, "/");       // 1 character
122
       strcat(path, "/");       // 1 character
123
 
123
 
124
       // Serial.print(path);
124
       // Serial.print(path);
126
       // Get a new directory object using the full path
126
       // Get a new directory object using the full path
127
       // and dive recursively into it.
127
       // and dive recursively into it.
128
       SdFile dir;
128
       SdFile dir;
129
-      if (!dir.open(parent, lfilename, O_READ)) {
129
+      if (!dir.open(&parent, dosFilename, O_READ)) {
130
         if (lsAction == LS_SerialPrint) {
130
         if (lsAction == LS_SerialPrint) {
131
           SERIAL_ECHO_START_P(port);
131
           SERIAL_ECHO_START_P(port);
132
           SERIAL_ECHOPGM_P(port, MSG_SD_CANT_OPEN_SUBDIR);
132
           SERIAL_ECHOPGM_P(port, MSG_SD_CANT_OPEN_SUBDIR);
133
-          SERIAL_ECHOLN_P(port, lfilename);
133
+          SERIAL_ECHOLN_P(port, dosFilename);
134
         }
134
         }
135
       }
135
       }
136
       lsDive(path, dir
136
       lsDive(path, dir
246
 
246
 
247
       // Open the sub-item as the new dive parent
247
       // Open the sub-item as the new dive parent
248
       SdFile dir;
248
       SdFile dir;
249
-      if (!dir.open(diveDir, segment, O_READ)) {
249
+      if (!dir.open(&diveDir, segment, O_READ)) {
250
         SERIAL_EOL_P(port);
250
         SERIAL_EOL_P(port);
251
         SERIAL_ECHO_START_P(port);
251
         SERIAL_ECHO_START_P(port);
252
         SERIAL_ECHOPGM_P(port, MSG_SD_CANT_OPEN_SUBDIR);
252
         SERIAL_ECHOPGM_P(port, MSG_SD_CANT_OPEN_SUBDIR);
273
   #endif
273
   #endif
274
 ) {
274
 ) {
275
   if (file.isOpen()) {
275
   if (file.isOpen()) {
276
-    char lfilename[FILENAME_LENGTH];
277
-    file.getFilename(lfilename);
278
-    SERIAL_ECHO_P(port, lfilename);
276
+    char dosFilename[FILENAME_LENGTH];
277
+    file.getFilename(dosFilename);
278
+    SERIAL_ECHO_P(port, dosFilename);
279
     #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
279
     #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
280
-      getfilename(0, lfilename);
280
+      getfilename(0, dosFilename);
281
       if (longFilename[0]) {
281
       if (longFilename[0]) {
282
         SERIAL_ECHO_P(port, ' ');
282
         SERIAL_ECHO_P(port, ' ');
283
         SERIAL_ECHO_P(port, longFilename);
283
         SERIAL_ECHO_P(port, longFilename);
298
     #define SPI_SPEED SPI_FULL_SPEED
298
     #define SPI_SPEED SPI_FULL_SPEED
299
   #endif
299
   #endif
300
 
300
 
301
-  if (!card.init(SPI_SPEED, SDSS)
301
+  if (!sd2card.init(SPI_SPEED, SDSS)
302
     #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
302
     #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
303
-      && !card.init(SPI_SPEED, LCD_SDSS)
303
+      && !sd2card.init(SPI_SPEED, LCD_SDSS)
304
     #endif
304
     #endif
305
   ) {
305
   ) {
306
-    //if (!card.init(SPI_HALF_SPEED,SDSS))
306
+    //if (!sd2card.init(SPI_HALF_SPEED,SDSS))
307
     SERIAL_ECHO_START();
307
     SERIAL_ECHO_START();
308
     SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
308
     SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
309
   }
309
   }
310
-  else if (!volume.init(&card)) {
310
+  else if (!volume.init(&sd2card)) {
311
     SERIAL_ERROR_START();
311
     SERIAL_ERROR_START();
312
     SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
312
     SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
313
   }
313
   }
323
   setroot();
323
   setroot();
324
 }
324
 }
325
 
325
 
326
-void CardReader::setroot() {
327
-  /*if (!workDir.openRoot(&volume)) {
328
-    SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
329
-  }*/
330
-  workDir = root;
331
-  curDir = &workDir;
332
-  #if ENABLED(SDCARD_SORT_ALPHA)
333
-    presort();
334
-  #endif
335
-}
336
-
337
 void CardReader::release() {
326
 void CardReader::release() {
338
   sdprinting = false;
327
   sdprinting = false;
339
   cardOK = false;
328
   cardOK = false;
371
   #endif
360
   #endif
372
 }
361
 }
373
 
362
 
374
-void CardReader::openLogFile(char* name) {
363
+void CardReader::openLogFile(char * const path) {
375
   logging = true;
364
   logging = true;
376
-  openFile(name, false);
365
+  openFile(path, false);
377
 }
366
 }
378
 
367
 
379
 void appendAtom(SdFile &file, char *& dst, uint8_t &cnt) {
368
 void appendAtom(SdFile &file, char *& dst, uint8_t &cnt) {
396
   *t = '\0';
385
   *t = '\0';
397
 }
386
 }
398
 
387
 
399
-void CardReader::openFile(char* name, const bool read, const bool subcall/*=false*/) {
388
+void CardReader::openFile(char * const path, const bool read, const bool subcall/*=false*/) {
400
 
389
 
401
   if (!cardOK) return;
390
   if (!cardOK) return;
402
 
391
 
416
       filespos[file_subcall_ctr] = sdpos;
405
       filespos[file_subcall_ctr] = sdpos;
417
 
406
 
418
       SERIAL_ECHO_START();
407
       SERIAL_ECHO_START();
419
-      SERIAL_ECHOPAIR("SUBROUTINE CALL target:\"", name);
408
+      SERIAL_ECHOPAIR("SUBROUTINE CALL target:\"", path);
420
       SERIAL_ECHOPAIR("\" parent:\"", proc_filenames[file_subcall_ctr]);
409
       SERIAL_ECHOPAIR("\" parent:\"", proc_filenames[file_subcall_ctr]);
421
       SERIAL_ECHOLNPAIR("\" pos", sdpos);
410
       SERIAL_ECHOLNPAIR("\" pos", sdpos);
422
       file_subcall_ctr++;
411
       file_subcall_ctr++;
437
     SERIAL_ECHO_START();
426
     SERIAL_ECHO_START();
438
     SERIAL_ECHOPGM("Now ");
427
     SERIAL_ECHOPGM("Now ");
439
     serialprintPGM(doing == 1 ? PSTR("doing") : PSTR("fresh"));
428
     serialprintPGM(doing == 1 ? PSTR("doing") : PSTR("fresh"));
440
-    SERIAL_ECHOLNPAIR(" file: ", name);
429
+    SERIAL_ECHOLNPAIR(" file: ", path);
441
   }
430
   }
442
 
431
 
443
   stopSDPrint();
432
   stopSDPrint();
444
 
433
 
445
-  SdFile myDir;
446
-  curDir = &root;
447
-  char *fname = name;
448
-  char *dirname_start, *dirname_end;
449
-
450
-  if (name[0] == '/') {
451
-    dirname_start = &name[1];
452
-    while (dirname_start != NULL) {
453
-      dirname_end = strchr(dirname_start, '/');
454
-      //SERIAL_ECHOPGM("start:");SERIAL_ECHOLN((int)(dirname_start - name));
455
-      //SERIAL_ECHOPGM("end  :");SERIAL_ECHOLN((int)(dirname_end - name));
456
-      if (dirname_end != NULL && dirname_end > dirname_start) {
457
-        char subdirname[FILENAME_LENGTH];
458
-        strncpy(subdirname, dirname_start, dirname_end - dirname_start);
459
-        subdirname[dirname_end - dirname_start] = '\0';
460
-        if (!myDir.open(curDir, subdirname, O_READ)) {
461
-          SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
462
-          SERIAL_PROTOCOL(subdirname);
463
-          SERIAL_PROTOCOLCHAR('.');
464
-          return;
465
-        }
466
-        else {
467
-          //SERIAL_ECHOLNPGM("dive ok");
468
-        }
469
-
470
-        curDir = &myDir;
471
-        dirname_start = dirname_end + 1;
472
-      }
473
-      else { // the remainder after all /fsa/fdsa/ is the filename
474
-        fname = dirname_start;
475
-        //SERIAL_ECHOLNPGM("remainder");
476
-        //SERIAL_ECHOLN(fname);
477
-        break;
478
-      }
479
-    }
480
-  }
481
-  else
482
-    curDir = &workDir; // Relative paths start in current directory
434
+  SdFile *curDir;
435
+  const char * const fname = diveToFile(curDir, path, false);
436
+  if (!fname) return;
483
 
437
 
484
   if (read) {
438
   if (read) {
485
     if (file.open(curDir, fname, O_READ)) {
439
     if (file.open(curDir, fname, O_READ)) {
509
     }
463
     }
510
     else {
464
     else {
511
       saving = true;
465
       saving = true;
512
-      SERIAL_PROTOCOLLNPAIR(MSG_SD_WRITE_TO_FILE, name);
466
+      SERIAL_PROTOCOLLNPAIR(MSG_SD_WRITE_TO_FILE, path);
513
       lcd_setstatus(fname);
467
       lcd_setstatus(fname);
514
     }
468
     }
515
   }
469
   }
520
 
474
 
521
   stopSDPrint();
475
   stopSDPrint();
522
 
476
 
523
-  SdFile myDir;
524
-  curDir = &root;
525
-  const char *fname = name;
526
-
527
-  char *dirname_start, *dirname_end;
528
-  if (name[0] == '/') {
529
-    dirname_start = strchr(name, '/') + 1;
530
-    while (dirname_start != NULL) {
531
-      dirname_end = strchr(dirname_start, '/');
532
-      //SERIAL_ECHOPGM("start:");SERIAL_ECHOLN((int)(dirname_start - name));
533
-      //SERIAL_ECHOPGM("end  :");SERIAL_ECHOLN((int)(dirname_end - name));
534
-      if (dirname_end != NULL && dirname_end > dirname_start) {
535
-        char subdirname[FILENAME_LENGTH];
536
-        strncpy(subdirname, dirname_start, dirname_end - dirname_start);
537
-        subdirname[dirname_end - dirname_start] = 0;
538
-        SERIAL_ECHOLN(subdirname);
539
-        if (!myDir.open(curDir, subdirname, O_READ)) {
540
-          SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, subdirname);
541
-          SERIAL_PROTOCOLCHAR('.');
542
-          SERIAL_EOL();
543
-          return;
544
-        }
545
-
546
-        curDir = &myDir;
547
-        dirname_start = dirname_end + 1;
548
-      }
549
-      else {
550
-        fname = dirname_start;
551
-        break;
552
-      }
553
-    }
554
-  }
555
-  else // Relative paths are rooted in the current directory
556
-    curDir = &workDir;
477
+  SdFile *curDir;
478
+  const char * const fname = diveToFile(curDir, name, false);
479
+  if (!fname) return;
557
 
480
 
558
   if (file.remove(curDir, fname)) {
481
   if (file.remove(curDir, fname)) {
559
     SERIAL_PROTOCOLPGM("File deleted:");
482
     SERIAL_PROTOCOLPGM("File deleted:");
623
     sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
546
     sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
624
     dir_t p;
547
     dir_t p;
625
     root.rewind();
548
     root.rewind();
626
-    while (root.readDir(p, NULL) > 0) {
549
+    while (root.readDir(&p, NULL) > 0) {
627
       for (int8_t i = (int8_t)strlen((char*)p.name); i--;) p.name[i] = tolower(p.name[i]);
550
       for (int8_t i = (int8_t)strlen((char*)p.name); i--;) p.name[i] = tolower(p.name[i]);
628
       if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
551
       if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
629
         openAndPrintFile(autoname);
552
         openAndPrintFile(autoname);
653
 
576
 
654
 /**
577
 /**
655
  * Get the name of a file in the current directory by index
578
  * Get the name of a file in the current directory by index
579
+ * with optional name to match.
656
  */
580
  */
657
 void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
581
 void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
658
   #if ENABLED(SDSORT_CACHE_NAMES)
582
   #if ENABLED(SDSORT_CACHE_NAMES)
669
       return;
593
       return;
670
     }
594
     }
671
   #endif // SDSORT_CACHE_NAMES
595
   #endif // SDSORT_CACHE_NAMES
672
-  curDir = &workDir;
673
   lsAction = LS_GetFilename;
596
   lsAction = LS_GetFilename;
674
   nrFile_index = nr;
597
   nrFile_index = nr;
675
-  curDir->rewind();
676
-  lsDive(NULL, *curDir, match);
598
+  workDir.rewind();
599
+  lsDive(NULL, workDir, match);
677
 }
600
 }
678
 
601
 
679
 uint16_t CardReader::getnrfilenames() {
602
 uint16_t CardReader::getnrfilenames() {
680
-  curDir = &workDir;
681
   lsAction = LS_Count;
603
   lsAction = LS_Count;
682
   nrFiles = 0;
604
   nrFiles = 0;
683
-  curDir->rewind();
684
-  lsDive(NULL, *curDir);
605
+  workDir.rewind();
606
+  lsDive(NULL, workDir);
685
   //SERIAL_ECHOLN(nrFiles);
607
   //SERIAL_ECHOLN(nrFiles);
686
   return nrFiles;
608
   return nrFiles;
687
 }
609
 }
688
 
610
 
689
-void CardReader::chdir(const char * relpath) {
690
-  SdFile newDir;
691
-  SdFile *parent = &root;
611
+/**
612
+ * Dive to the given file path, with optional echo.
613
+ * On exit set curDir and return the name part of the path.
614
+ * A NULL result indicates an unrecoverable error.
615
+ */
616
+const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, const bool echo) {
617
+  SdFile myDir;
618
+  if (path[0] != '/') { curDir = &workDir; return path; }
692
 
619
 
693
-  if (workDir.isOpen()) parent = &workDir;
620
+  curDir = &root;
621
+  const char *dirname_start = &path[1];
622
+  while (dirname_start) {
623
+    char * const dirname_end = strchr(dirname_start, '/');
624
+    if (dirname_end <= dirname_start) break;
694
 
625
 
695
-  if (!newDir.open(*parent, relpath, O_READ)) {
696
-    SERIAL_ECHO_START();
697
-    SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
698
-    SERIAL_ECHOLN(relpath);
626
+    char dosSubdirname[FILENAME_LENGTH];
627
+    const uint8_t len = dirname_end - dirname_start;
628
+    strncpy(dosSubdirname, dirname_start, len);
629
+    dosSubdirname[len] = 0;
630
+
631
+    if (echo) SERIAL_ECHOLN(dosSubdirname);
632
+
633
+    if (!myDir.open(curDir, dosSubdirname, O_READ)) {
634
+      SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, dosSubdirname);
635
+      SERIAL_PROTOCOLCHAR('.');
636
+      SERIAL_EOL();
637
+      return NULL;
638
+    }
639
+    curDir = &myDir;
640
+    dirname_start = dirname_end + 1;
699
   }
641
   }
700
-  else {
642
+  return dirname_start;
643
+}
644
+
645
+void CardReader::chdir(const char * relpath) {
646
+  SdFile newDir;
647
+  SdFile *parent = workDir.isOpen() ? &workDir : &root;
648
+
649
+  if (newDir.open(parent, relpath, O_READ)) {
701
     workDir = newDir;
650
     workDir = newDir;
702
     if (workDirDepth < MAX_DIR_DEPTH)
651
     if (workDirDepth < MAX_DIR_DEPTH)
703
       workDirParents[workDirDepth++] = workDir;
652
       workDirParents[workDirDepth++] = workDir;
705
       presort();
654
       presort();
706
     #endif
655
     #endif
707
   }
656
   }
657
+  else {
658
+    SERIAL_ECHO_START();
659
+    SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
660
+    SERIAL_ECHOLN(relpath);
661
+  }
708
 }
662
 }
709
 
663
 
710
 int8_t CardReader::updir() {
664
 int8_t CardReader::updir() {
717
   return workDirDepth;
671
   return workDirDepth;
718
 }
672
 }
719
 
673
 
674
+void CardReader::setroot() {
675
+  /*if (!workDir.openRoot(&volume)) {
676
+    SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
677
+  }*/
678
+  workDir = root;
679
+  #if ENABLED(SDCARD_SORT_ALPHA)
680
+    presort();
681
+  #endif
682
+}
683
+
720
 #if ENABLED(SDCARD_SORT_ALPHA)
684
 #if ENABLED(SDCARD_SORT_ALPHA)
721
 
685
 
722
   /**
686
   /**

+ 7
- 5
Marlin/src/sd/cardreader.h View File

43
   void beginautostart();
43
   void beginautostart();
44
   void checkautostart();
44
   void checkautostart();
45
 
45
 
46
-  void openFile(char* name, const bool read, const bool subcall=false);
47
-  void openLogFile(char* name);
46
+  void openFile(char * const path, const bool read, const bool subcall=false);
47
+  void openLogFile(char * const path);
48
   void removeFile(const char * const name);
48
   void removeFile(const char * const name);
49
   void closefile(const bool store_location=false);
49
   void closefile(const bool store_location=false);
50
   void release();
50
   void release();
89
   int8_t updir();
89
   int8_t updir();
90
   void setroot();
90
   void setroot();
91
 
91
 
92
+  const char* diveToFile(SdFile*& curDir, const char * const path, const bool echo);
93
+
92
   uint16_t get_num_Files();
94
   uint16_t get_num_Files();
93
 
95
 
94
   #if ENABLED(SDCARD_SORT_ALPHA)
96
   #if ENABLED(SDCARD_SORT_ALPHA)
119
   FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
121
   FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
120
   FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
122
   FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
121
 
123
 
122
-  Sd2Card& getSd2Card() { return card; }
124
+  Sd2Card& getSd2Card() { return sd2card; }
123
 
125
 
124
   #if ENABLED(AUTO_REPORT_SD_STATUS)
126
   #if ENABLED(AUTO_REPORT_SD_STATUS)
125
     void auto_report_sd_status(void);
127
     void auto_report_sd_status(void);
142
   char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
144
   char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
143
   int autostart_index;
145
   int autostart_index;
144
 private:
146
 private:
145
-  SdFile root, *curDir, workDir, workDirParents[MAX_DIR_DEPTH];
147
+  SdFile root, workDir, workDirParents[MAX_DIR_DEPTH];
146
   uint8_t workDirDepth;
148
   uint8_t workDirDepth;
147
 
149
 
148
   // Sort files and folders alphabetically.
150
   // Sort files and folders alphabetically.
195
 
197
 
196
   #endif // SDCARD_SORT_ALPHA
198
   #endif // SDCARD_SORT_ALPHA
197
 
199
 
198
-  Sd2Card card;
200
+  Sd2Card sd2card;
199
   SdVolume volume;
201
   SdVolume volume;
200
   SdFile file;
202
   SdFile file;
201
 
203
 

Loading…
Cancel
Save