My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cardreader.cpp 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfig.h"
  23. #if ENABLED(SDSUPPORT)
  24. //#define DEBUG_CARDREADER
  25. #include "cardreader.h"
  26. #include "../MarlinCore.h"
  27. #include "../lcd/ultralcd.h"
  28. #if ENABLED(DWIN_CREALITY_LCD)
  29. #include "../lcd/dwin/e3v2/dwin.h"
  30. #endif
  31. #include "../module/planner.h" // for synchronize
  32. #include "../module/printcounter.h"
  33. #include "../gcode/queue.h"
  34. #include "../module/settings.h"
  35. #include "../module/stepper/indirection.h"
  36. #if ENABLED(EMERGENCY_PARSER)
  37. #include "../feature/e_parser.h"
  38. #endif
  39. #if ENABLED(POWER_LOSS_RECOVERY)
  40. #include "../feature/powerloss.h"
  41. #endif
  42. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  43. #include "../feature/pause.h"
  44. #endif
  45. #define DEBUG_OUT EITHER(DEBUG_CARDREADER, MARLIN_DEV_MODE)
  46. #include "../core/debug_out.h"
  47. #include "../libs/hex_print.h"
  48. // public:
  49. card_flags_t CardReader::flag;
  50. char CardReader::filename[FILENAME_LENGTH], CardReader::longFilename[LONG_FILENAME_LENGTH];
  51. int8_t CardReader::autostart_index;
  52. #if BOTH(HAS_MULTI_SERIAL, BINARY_FILE_TRANSFER)
  53. int8_t CardReader::transfer_port_index;
  54. #endif
  55. // private:
  56. SdFile CardReader::root, CardReader::workDir, CardReader::workDirParents[MAX_DIR_DEPTH];
  57. uint8_t CardReader::workDirDepth;
  58. #if ENABLED(SDCARD_SORT_ALPHA)
  59. uint16_t CardReader::sort_count;
  60. #if ENABLED(SDSORT_GCODE)
  61. bool CardReader::sort_alpha;
  62. int CardReader::sort_folders;
  63. //bool CardReader::sort_reverse;
  64. #endif
  65. #if ENABLED(SDSORT_DYNAMIC_RAM)
  66. uint8_t *CardReader::sort_order;
  67. #else
  68. uint8_t CardReader::sort_order[SDSORT_LIMIT];
  69. #endif
  70. #if ENABLED(SDSORT_USES_RAM)
  71. #if ENABLED(SDSORT_CACHE_NAMES)
  72. uint16_t CardReader::nrFiles; // Cached total file count
  73. #if ENABLED(SDSORT_DYNAMIC_RAM)
  74. char **CardReader::sortshort, **CardReader::sortnames;
  75. #else
  76. char CardReader::sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
  77. char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
  78. #endif
  79. #elif DISABLED(SDSORT_USES_STACK)
  80. char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
  81. #endif
  82. #if HAS_FOLDER_SORTING
  83. #if ENABLED(SDSORT_DYNAMIC_RAM)
  84. uint8_t *CardReader::isDir;
  85. #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
  86. uint8_t CardReader::isDir[(SDSORT_LIMIT+7)>>3];
  87. #endif
  88. #define IS_DIR(n) TEST(isDir[(n) >> 3], (n) & 0x07)
  89. #endif
  90. #endif // SDSORT_USES_RAM
  91. #endif // SDCARD_SORT_ALPHA
  92. Sd2Card CardReader::sd2card;
  93. SdVolume CardReader::volume;
  94. SdFile CardReader::file;
  95. uint8_t CardReader::file_subcall_ctr;
  96. uint32_t CardReader::filespos[SD_PROCEDURE_DEPTH];
  97. char CardReader::proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  98. uint32_t CardReader::filesize, CardReader::sdpos;
  99. CardReader::CardReader() {
  100. #if ENABLED(SDCARD_SORT_ALPHA)
  101. sort_count = 0;
  102. #if ENABLED(SDSORT_GCODE)
  103. sort_alpha = true;
  104. sort_folders = FOLDER_SORTING;
  105. //sort_reverse = false;
  106. #endif
  107. #endif
  108. flag.sdprinting = flag.mounted = flag.saving = flag.logging = false;
  109. filesize = sdpos = 0;
  110. file_subcall_ctr = 0;
  111. workDirDepth = 0;
  112. ZERO(workDirParents);
  113. // Disable autostart until card is initialized
  114. autostart_index = -1;
  115. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  116. SET_INPUT_PULLUP(SD_DETECT_PIN);
  117. #endif
  118. #if PIN_EXISTS(SDPOWER)
  119. OUT_WRITE(SDPOWER_PIN, HIGH); // Power the SD reader
  120. #endif
  121. }
  122. //
  123. // Get a DOS 8.3 filename in its useful form
  124. //
  125. char *createFilename(char * const buffer, const dir_t &p) {
  126. char *pos = buffer;
  127. LOOP_L_N(i, 11) {
  128. if (p.name[i] == ' ') continue;
  129. if (i == 8) *pos++ = '.';
  130. *pos++ = p.name[i];
  131. }
  132. *pos++ = 0;
  133. return buffer;
  134. }
  135. //
  136. // Return 'true' if the item is a folder or G-code file
  137. //
  138. bool CardReader::is_dir_or_gcode(const dir_t &p) {
  139. //uint8_t pn0 = p.name[0];
  140. if ( (p.attributes & DIR_ATT_HIDDEN) // Hidden by attribute
  141. // When readDir() > 0 these must be false:
  142. //|| pn0 == DIR_NAME_FREE || pn0 == DIR_NAME_DELETED // Clear or Deleted entry
  143. //|| pn0 == '.' || longFilename[0] == '.' // Hidden file
  144. //|| !DIR_IS_FILE_OR_SUBDIR(&p) // Not a File or Directory
  145. ) return false;
  146. flag.filenameIsDir = DIR_IS_SUBDIR(&p); // We know it's a File or Folder
  147. return (
  148. flag.filenameIsDir // All Directories are ok
  149. || (p.name[8] == 'G' && p.name[9] != '~') // Non-backup *.G* files are accepted
  150. );
  151. }
  152. //
  153. // Get the number of (compliant) items in the folder
  154. //
  155. int CardReader::countItems(SdFile dir) {
  156. dir_t p;
  157. int c = 0;
  158. while (dir.readDir(&p, longFilename) > 0)
  159. c += is_dir_or_gcode(p);
  160. #if ALL(SDCARD_SORT_ALPHA, SDSORT_USES_RAM, SDSORT_CACHE_NAMES)
  161. nrFiles = c;
  162. #endif
  163. return c;
  164. }
  165. //
  166. // Get file/folder info for an item by index
  167. //
  168. void CardReader::selectByIndex(SdFile dir, const uint8_t index) {
  169. dir_t p;
  170. for (uint8_t cnt = 0; dir.readDir(&p, longFilename) > 0;) {
  171. if (is_dir_or_gcode(p)) {
  172. if (cnt == index) {
  173. createFilename(filename, p);
  174. return; // 0 based index
  175. }
  176. cnt++;
  177. }
  178. }
  179. }
  180. //
  181. // Get file/folder info for an item by name
  182. //
  183. void CardReader::selectByName(SdFile dir, const char * const match) {
  184. dir_t p;
  185. for (uint8_t cnt = 0; dir.readDir(&p, longFilename) > 0; cnt++) {
  186. if (is_dir_or_gcode(p)) {
  187. createFilename(filename, p);
  188. if (strcasecmp(match, filename) == 0) return;
  189. }
  190. }
  191. }
  192. //
  193. // Recursive method to list all files within a folder
  194. //
  195. void CardReader::printListing(SdFile parent, const char * const prepend/*=nullptr*/) {
  196. dir_t p;
  197. while (parent.readDir(&p, longFilename) > 0) {
  198. if (DIR_IS_SUBDIR(&p)) {
  199. // Get the short name for the item, which we know is a folder
  200. char dosFilename[FILENAME_LENGTH];
  201. createFilename(dosFilename, p);
  202. // Allocate enough stack space for the full path to a folder, trailing slash, and nul
  203. const bool prepend_is_empty = (!prepend || prepend[0] == '\0');
  204. const int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(dosFilename) + 1 + 1;
  205. char path[len];
  206. // Append the FOLDERNAME12/ to the passed string.
  207. // It contains the full path to the "parent" argument.
  208. // We now have the full path to the item in this folder.
  209. strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty
  210. strcat(path, dosFilename); // FILENAME_LENGTH characters maximum
  211. strcat(path, "/"); // 1 character
  212. // Serial.print(path);
  213. // Get a new directory object using the full path
  214. // and dive recursively into it.
  215. SdFile child;
  216. if (!child.open(&parent, dosFilename, O_READ)) {
  217. SERIAL_ECHO_START();
  218. SERIAL_ECHOLNPAIR(STR_SD_CANT_OPEN_SUBDIR, dosFilename);
  219. }
  220. printListing(child, path);
  221. // close() is done automatically by destructor of SdFile
  222. }
  223. else if (is_dir_or_gcode(p)) {
  224. createFilename(filename, p);
  225. if (prepend) SERIAL_ECHO(prepend);
  226. SERIAL_ECHO(filename);
  227. SERIAL_CHAR(' ');
  228. SERIAL_ECHOLN(p.fileSize);
  229. }
  230. }
  231. }
  232. //
  233. // List all files on the SD card
  234. //
  235. void CardReader::ls() {
  236. if (flag.mounted) {
  237. root.rewind();
  238. printListing(root);
  239. }
  240. }
  241. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  242. //
  243. // Get a long pretty path based on a DOS 8.3 path
  244. //
  245. void CardReader::printLongPath(char * const path) {
  246. int i, pathLen = strlen(path);
  247. // SERIAL_ECHOPGM("Full Path: "); SERIAL_ECHOLN(path);
  248. // Zero out slashes to make segments
  249. for (i = 0; i < pathLen; i++) if (path[i] == '/') path[i] = '\0';
  250. SdFile diveDir = root; // start from the root for segment 1
  251. for (i = 0; i < pathLen;) {
  252. if (path[i] == '\0') i++; // move past a single nul
  253. char *segment = &path[i]; // The segment after most slashes
  254. // If a segment is empty (extra-slash) then exit
  255. if (!*segment) break;
  256. // Go to the next segment
  257. while (path[++i]) { }
  258. // SERIAL_ECHOPGM("Looking for segment: "); SERIAL_ECHOLN(segment);
  259. // Find the item, setting the long filename
  260. diveDir.rewind();
  261. selectByName(diveDir, segment);
  262. // Print /LongNamePart to serial output
  263. SERIAL_CHAR('/');
  264. SERIAL_ECHO(longFilename[0] ? longFilename : "???");
  265. // If the filename was printed then that's it
  266. if (!flag.filenameIsDir) break;
  267. // SERIAL_ECHOPGM("Opening dir: "); SERIAL_ECHOLN(segment);
  268. // Open the sub-item as the new dive parent
  269. SdFile dir;
  270. if (!dir.open(&diveDir, segment, O_READ)) {
  271. SERIAL_EOL();
  272. SERIAL_ECHO_START();
  273. SERIAL_ECHOPAIR(STR_SD_CANT_OPEN_SUBDIR, segment);
  274. break;
  275. }
  276. diveDir.close();
  277. diveDir = dir;
  278. } // while i<pathLen
  279. SERIAL_EOL();
  280. }
  281. #endif // LONG_FILENAME_HOST_SUPPORT
  282. //
  283. // Echo the DOS 8.3 filename (and long filename, if any)
  284. //
  285. void CardReader::printFilename() {
  286. if (file.isOpen()) {
  287. char dosFilename[FILENAME_LENGTH];
  288. file.getDosName(dosFilename);
  289. SERIAL_ECHO(dosFilename);
  290. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  291. selectFileByName(dosFilename);
  292. if (longFilename[0]) {
  293. SERIAL_ECHO(' ');
  294. SERIAL_ECHO(longFilename);
  295. }
  296. #endif
  297. }
  298. else
  299. SERIAL_ECHOPGM("(no file)");
  300. SERIAL_EOL();
  301. }
  302. void CardReader::mount() {
  303. flag.mounted = false;
  304. if (root.isOpen()) root.close();
  305. if (!sd2card.init(SPI_SPEED, SDSS)
  306. #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
  307. && !sd2card.init(SPI_SPEED, LCD_SDSS)
  308. #endif
  309. ) SERIAL_ECHO_MSG(STR_SD_INIT_FAIL);
  310. else if (!volume.init(&sd2card))
  311. SERIAL_ERROR_MSG(STR_SD_VOL_INIT_FAIL);
  312. else if (!root.openRoot(&volume))
  313. SERIAL_ERROR_MSG(STR_SD_OPENROOT_FAIL);
  314. else {
  315. flag.mounted = true;
  316. SERIAL_ECHO_MSG(STR_SD_CARD_OK);
  317. }
  318. if (flag.mounted)
  319. cdroot();
  320. #if ENABLED(USB_FLASH_DRIVE_SUPPORT) || PIN_EXISTS(SD_DETECT)
  321. else if (marlin_state != MF_INITIALIZING)
  322. ui.set_status_P(GET_TEXT(MSG_SD_INIT_FAIL), -1);
  323. #endif
  324. ui.refresh();
  325. }
  326. /**
  327. * Handle SD card events
  328. */
  329. #if MB(FYSETC_CHEETAH, FYSETC_AIO_II)
  330. #include "../module/stepper.h"
  331. #endif
  332. void CardReader::manage_media() {
  333. static uint8_t prev_stat = 2; // First call, no prior state
  334. uint8_t stat = uint8_t(IS_SD_INSERTED());
  335. if (stat == prev_stat) return;
  336. DEBUG_ECHOLNPAIR("SD: Status changed from ", prev_stat, " to ", stat);
  337. flag.workDirIsRoot = true; // Return to root on mount/release
  338. if (ui.detected()) {
  339. uint8_t old_stat = prev_stat;
  340. prev_stat = stat; // Change now to prevent re-entry
  341. if (stat) { // Media Inserted
  342. safe_delay(500); // Some boards need a delay to get settled
  343. mount(); // Try to mount the media
  344. #if MB(FYSETC_CHEETAH, FYSETC_CHEETAH_V12, FYSETC_AIO_II)
  345. reset_stepper_drivers(); // Workaround for Cheetah bug
  346. #endif
  347. if (!isMounted()) stat = 0; // Not mounted?
  348. }
  349. else {
  350. #if PIN_EXISTS(SD_DETECT)
  351. release(); // Card is released
  352. #endif
  353. }
  354. ui.media_changed(old_stat, stat); // Update the UI
  355. if (stat) {
  356. TERN_(SDCARD_EEPROM_EMULATION, settings.first_load());
  357. if (old_stat == 2) // First mount?
  358. DEBUG_ECHOLNPGM("First mount.");
  359. TERN(POWER_LOSS_RECOVERY,
  360. recovery.check(), // Check for PLR file. (If not there it will beginautostart)
  361. beginautostart() // Look for auto0.g on the next loop
  362. );
  363. }
  364. }
  365. else
  366. DEBUG_ECHOLNPGM("SD: No UI Detected.");
  367. }
  368. void CardReader::release() {
  369. endFilePrint();
  370. flag.mounted = false;
  371. flag.workDirIsRoot = true;
  372. #if ALL(SDCARD_SORT_ALPHA, SDSORT_USES_RAM, SDSORT_CACHE_NAMES)
  373. nrFiles = 0;
  374. #endif
  375. }
  376. void CardReader::openAndPrintFile(const char *name) {
  377. char cmd[4 + strlen(name) + 1]; // Room for "M23 ", filename, and null
  378. extern const char M23_STR[];
  379. sprintf_P(cmd, M23_STR, name);
  380. for (char *c = &cmd[4]; *c; c++) *c = tolower(*c);
  381. queue.enqueue_one_now(cmd);
  382. queue.enqueue_now_P(M24_STR);
  383. }
  384. void CardReader::startFileprint() {
  385. if (isMounted()) {
  386. flag.sdprinting = true;
  387. TERN_(SD_RESORT, flush_presort());
  388. }
  389. }
  390. void CardReader::endFilePrint(TERN_(SD_RESORT, const bool re_sort/*=false*/)) {
  391. TERN_(ADVANCED_PAUSE_FEATURE, did_pause_print = 0);
  392. TERN_(DWIN_CREALITY_LCD, HMI_flag.print_finish = flag.sdprinting);
  393. flag.sdprinting = flag.abort_sd_printing = false;
  394. if (isFileOpen()) file.close();
  395. TERN_(SD_RESORT, if (re_sort) presort());
  396. }
  397. void CardReader::openLogFile(char * const path) {
  398. flag.logging = DISABLED(SDCARD_READONLY);
  399. TERN(SDCARD_READONLY,,openFileWrite(path));
  400. }
  401. //
  402. // Get the root-relative DOS path of the selected file
  403. //
  404. void CardReader::getAbsFilename(char *dst) {
  405. *dst++ = '/';
  406. uint8_t cnt = 1;
  407. auto appendAtom = [&](SdFile &file) {
  408. file.getDosName(dst);
  409. while (*dst && cnt < MAXPATHNAMELENGTH) { dst++; cnt++; }
  410. if (cnt < MAXPATHNAMELENGTH) { *dst = '/'; dst++; cnt++; }
  411. };
  412. LOOP_L_N(i, workDirDepth) // Loop down to current work dir
  413. appendAtom(workDirParents[i]);
  414. if (cnt < MAXPATHNAMELENGTH - (FILENAME_LENGTH) - 1) { // Leave room for filename and nul
  415. appendAtom(file);
  416. --dst;
  417. }
  418. *dst = '\0';
  419. }
  420. void openFailed(const char * const fname) {
  421. SERIAL_ECHOLNPAIR(STR_SD_OPEN_FILE_FAIL, fname, ".");
  422. }
  423. void announceOpen(const uint8_t doing, const char * const path) {
  424. if (doing) {
  425. PORT_REDIRECT(SERIAL_BOTH);
  426. SERIAL_ECHO_START();
  427. SERIAL_ECHOPGM("Now ");
  428. serialprintPGM(doing == 1 ? PSTR("doing") : PSTR("fresh"));
  429. SERIAL_ECHOLNPAIR(" file: ", path);
  430. PORT_RESTORE();
  431. }
  432. }
  433. //
  434. // Open a file by DOS path for read
  435. // The 'subcall_type' flag indicates...
  436. // - 0 : Standard open from host or user interface.
  437. // - 1 : (file open) Opening a new sub-procedure.
  438. // - 1 : (no file open) Opening a macro (M98).
  439. // - 2 : Resuming from a sub-procedure
  440. //
  441. void CardReader::openFileRead(char * const path, const uint8_t subcall_type/*=0*/) {
  442. if (!isMounted()) return;
  443. switch (subcall_type) {
  444. case 0: // Starting a new print. "Now fresh file: ..."
  445. announceOpen(2, path);
  446. file_subcall_ctr = 0;
  447. break;
  448. case 1: // Starting a sub-procedure
  449. // With no file is open it's a simple macro. "Now doing file: ..."
  450. if (!isFileOpen()) { announceOpen(1, path); break; }
  451. // Too deep? The firmware has to bail.
  452. if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
  453. SERIAL_ERROR_MSG("Exceeded max SUBROUTINE depth:" STRINGIFY(SD_PROCEDURE_DEPTH));
  454. kill(GET_TEXT(MSG_KILL_SUBCALL_OVERFLOW));
  455. return;
  456. }
  457. // Store current filename (based on workDirParents) and position
  458. getAbsFilename(proc_filenames[file_subcall_ctr]);
  459. filespos[file_subcall_ctr] = sdpos;
  460. // For sub-procedures say 'SUBROUTINE CALL target: "..." parent: "..." pos12345'
  461. SERIAL_ECHO_START();
  462. SERIAL_ECHOLNPAIR("SUBROUTINE CALL target:\"", path, "\" parent:\"", proc_filenames[file_subcall_ctr], "\" pos", sdpos);
  463. file_subcall_ctr++;
  464. break;
  465. case 2: // Resuming previous file after sub-procedure
  466. SERIAL_ECHO_MSG("END SUBROUTINE");
  467. break;
  468. }
  469. endFilePrint();
  470. SdFile *diveDir;
  471. const char * const fname = diveToFile(true, diveDir, path);
  472. if (!fname) return;
  473. if (file.open(diveDir, fname, O_READ)) {
  474. filesize = file.fileSize();
  475. sdpos = 0;
  476. PORT_REDIRECT(SERIAL_BOTH);
  477. SERIAL_ECHOLNPAIR(STR_SD_FILE_OPENED, fname, STR_SD_SIZE, filesize);
  478. SERIAL_ECHOLNPGM(STR_SD_FILE_SELECTED);
  479. PORT_RESTORE();
  480. selectFileByName(fname);
  481. ui.set_status(longFilename[0] ? longFilename : fname);
  482. }
  483. else
  484. openFailed(fname);
  485. }
  486. inline void echo_write_to_file(const char * const fname) {
  487. SERIAL_ECHOLNPAIR(STR_SD_WRITE_TO_FILE, fname);
  488. }
  489. //
  490. // Open a file by DOS path for write
  491. //
  492. void CardReader::openFileWrite(char * const path) {
  493. if (!isMounted()) return;
  494. announceOpen(2, path);
  495. file_subcall_ctr = 0;
  496. endFilePrint();
  497. SdFile *diveDir;
  498. const char * const fname = diveToFile(false, diveDir, path);
  499. if (!fname) return;
  500. #if ENABLED(SDCARD_READONLY)
  501. openFailed(fname);
  502. #else
  503. if (file.open(diveDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
  504. flag.saving = true;
  505. selectFileByName(fname);
  506. TERN_(EMERGENCY_PARSER, emergency_parser.disable());
  507. echo_write_to_file(fname);
  508. ui.set_status(fname);
  509. }
  510. else
  511. openFailed(fname);
  512. #endif
  513. }
  514. //
  515. // Check if a file exists by absolute or workDir-relative path
  516. //
  517. bool CardReader::fileExists(const char * const path) {
  518. if (!isMounted()) return false;
  519. SdFile *diveDir = nullptr;
  520. const char * const fname = diveToFile(false, diveDir, path);
  521. return fname != nullptr;
  522. }
  523. //
  524. // Delete a file by name in the working directory
  525. //
  526. void CardReader::removeFile(const char * const name) {
  527. if (!isMounted()) return;
  528. //endFilePrint();
  529. SdFile *curDir;
  530. const char * const fname = diveToFile(false, curDir, name);
  531. if (!fname) return;
  532. #if ENABLED(SDCARD_READONLY)
  533. SERIAL_ECHOLNPAIR("Deletion failed (read-only), File: ", fname, ".");
  534. #else
  535. if (file.remove(curDir, fname)) {
  536. SERIAL_ECHOLNPAIR("File deleted:", fname);
  537. sdpos = 0;
  538. TERN_(SDCARD_SORT_ALPHA, presort());
  539. }
  540. else
  541. SERIAL_ECHOLNPAIR("Deletion failed, File: ", fname, ".");
  542. #endif
  543. }
  544. void CardReader::report_status() {
  545. if (isPrinting()) {
  546. SERIAL_ECHOPGM(STR_SD_PRINTING_BYTE);
  547. SERIAL_ECHO(sdpos);
  548. SERIAL_CHAR('/');
  549. SERIAL_ECHOLN(filesize);
  550. }
  551. else
  552. SERIAL_ECHOLNPGM(STR_SD_NOT_PRINTING);
  553. }
  554. void CardReader::write_command(char * const buf) {
  555. char* begin = buf;
  556. char* npos = nullptr;
  557. char* end = buf + strlen(buf) - 1;
  558. file.writeError = false;
  559. if ((npos = strchr(buf, 'N')) != nullptr) {
  560. begin = strchr(npos, ' ') + 1;
  561. end = strchr(npos, '*') - 1;
  562. }
  563. end[1] = '\r';
  564. end[2] = '\n';
  565. end[3] = '\0';
  566. file.write(begin);
  567. if (file.writeError) SERIAL_ERROR_MSG(STR_SD_ERR_WRITE_TO_FILE);
  568. }
  569. //
  570. // Run the next autostart file. Called:
  571. // - On boot after successful card init
  572. // - After finishing the previous autostart file
  573. // - From the LCD command to run the autostart file
  574. //
  575. void CardReader::checkautostart() {
  576. if (autostart_index < 0 || flag.sdprinting) return;
  577. if (!isMounted()) mount();
  578. TERN_(SDCARD_EEPROM_EMULATION, else settings.first_load());
  579. // Don't run auto#.g when a PLR file exists
  580. if (isMounted() && TERN1(POWER_LOSS_RECOVERY, !recovery.valid())) {
  581. char autoname[8];
  582. sprintf_P(autoname, PSTR("auto%c.g"), autostart_index + '0');
  583. dir_t p;
  584. root.rewind();
  585. while (root.readDir(&p, nullptr) > 0) {
  586. for (int8_t i = (int8_t)strlen((char*)p.name); i--;) p.name[i] = tolower(p.name[i]);
  587. if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
  588. openAndPrintFile(autoname);
  589. autostart_index++;
  590. return;
  591. }
  592. }
  593. }
  594. autostart_index = -1;
  595. }
  596. void CardReader::beginautostart() {
  597. autostart_index = 0;
  598. cdroot();
  599. }
  600. void CardReader::closefile(const bool store_location/*=false*/) {
  601. file.sync();
  602. file.close();
  603. flag.saving = flag.logging = false;
  604. sdpos = 0;
  605. TERN_(EMERGENCY_PARSER, emergency_parser.enable());
  606. if (store_location) {
  607. //future: store printer state, filename and position for continuing a stopped print
  608. // so one can unplug the printer and continue printing the next day.
  609. }
  610. }
  611. //
  612. // Get info for a file in the working directory by index
  613. //
  614. void CardReader::selectFileByIndex(const uint16_t nr) {
  615. #if ENABLED(SDSORT_CACHE_NAMES)
  616. if (nr < sort_count) {
  617. strcpy(filename, sortshort[nr]);
  618. strcpy(longFilename, sortnames[nr]);
  619. flag.filenameIsDir = IS_DIR(nr);
  620. return;
  621. }
  622. #endif
  623. workDir.rewind();
  624. selectByIndex(workDir, nr);
  625. }
  626. //
  627. // Get info for a file in the working directory by DOS name
  628. //
  629. void CardReader::selectFileByName(const char * const match) {
  630. #if ENABLED(SDSORT_CACHE_NAMES)
  631. for (uint16_t nr = 0; nr < sort_count; nr++)
  632. if (strcasecmp(match, sortshort[nr]) == 0) {
  633. strcpy(filename, sortshort[nr]);
  634. strcpy(longFilename, sortnames[nr]);
  635. flag.filenameIsDir = IS_DIR(nr);
  636. return;
  637. }
  638. #endif
  639. workDir.rewind();
  640. selectByName(workDir, match);
  641. }
  642. uint16_t CardReader::countFilesInWorkDir() {
  643. workDir.rewind();
  644. return countItems(workDir);
  645. }
  646. /**
  647. * Dive to the given DOS 8.3 file path, with optional echo of the dive paths.
  648. *
  649. * On exit:
  650. * - Your curDir pointer contains an SdFile reference to the file's directory.
  651. * - If update_cwd was 'true' the workDir now points to the file's directory.
  652. *
  653. * Returns a pointer to the last segment (filename) of the given DOS 8.3 path.
  654. *
  655. * A nullptr result indicates an unrecoverable error.
  656. */
  657. const char* CardReader::diveToFile(const bool update_cwd, SdFile*& diveDir, const char * const path, const bool echo/*=false*/) {
  658. // Track both parent and subfolder
  659. static SdFile newDir1, newDir2;
  660. SdFile *sub = &newDir1, *startDir;
  661. // Parsing the path string
  662. const char *item_name_adr = path;
  663. DEBUG_ECHOLNPAIR("diveToFile: path = '", path, "'");
  664. if (path[0] == '/') { // Starting at the root directory?
  665. diveDir = &root;
  666. item_name_adr++;
  667. DEBUG_ECHOLNPAIR("diveToFile: CWD to root: ", hex_address((void*)diveDir));
  668. if (update_cwd) workDirDepth = 0; // The cwd can be updated for the benefit of sub-programs
  669. }
  670. else
  671. diveDir = &workDir; // Dive from workDir (as set by the UI)
  672. startDir = diveDir;
  673. DEBUG_ECHOLNPAIR("diveToFile: startDir = ", hex_address((void*)startDir));
  674. while (item_name_adr) {
  675. // Find next subdirectory delimiter
  676. char * const name_end = strchr(item_name_adr, '/');
  677. // Last atom in the path? Item found.
  678. if (name_end <= item_name_adr) break;
  679. // Set subDirName
  680. const uint8_t len = name_end - item_name_adr;
  681. char dosSubdirname[len + 1];
  682. strncpy(dosSubdirname, item_name_adr, len);
  683. dosSubdirname[len] = 0;
  684. if (echo) SERIAL_ECHOLN(dosSubdirname);
  685. DEBUG_ECHOLNPAIR("diveToFile: sub = ", hex_address((void*)sub));
  686. // Open diveDir (closing first)
  687. sub->close();
  688. if (!sub->open(diveDir, dosSubdirname, O_READ)) {
  689. openFailed(dosSubdirname);
  690. item_name_adr = nullptr;
  691. break;
  692. }
  693. // Close diveDir if not at starting-point
  694. if (diveDir != startDir) {
  695. DEBUG_ECHOLNPAIR("diveToFile: closing diveDir: ", hex_address((void*)diveDir));
  696. diveDir->close();
  697. }
  698. // diveDir now subDir
  699. diveDir = sub;
  700. DEBUG_ECHOLNPAIR("diveToFile: diveDir = sub: ", hex_address((void*)diveDir));
  701. // Update workDirParents and workDirDepth
  702. if (update_cwd) {
  703. DEBUG_ECHOLNPAIR("diveToFile: update_cwd");
  704. if (workDirDepth < MAX_DIR_DEPTH)
  705. workDirParents[workDirDepth++] = *diveDir;
  706. }
  707. // Point sub at the other scratch object
  708. sub = (diveDir != &newDir1) ? &newDir1 : &newDir2;
  709. DEBUG_ECHOLNPAIR("diveToFile: swapping sub = ", hex_address((void*)sub));
  710. // Next path atom address
  711. item_name_adr = name_end + 1;
  712. }
  713. if (update_cwd) {
  714. workDir = *diveDir;
  715. DEBUG_ECHOLNPAIR("diveToFile: final workDir = ", hex_address((void*)diveDir));
  716. flag.workDirIsRoot = (workDirDepth == 0);
  717. TERN_(SDCARD_SORT_ALPHA, presort());
  718. }
  719. return item_name_adr;
  720. }
  721. void CardReader::cd(const char * relpath) {
  722. SdFile newDir;
  723. SdFile *parent = workDir.isOpen() ? &workDir : &root;
  724. if (newDir.open(parent, relpath, O_READ)) {
  725. workDir = newDir;
  726. flag.workDirIsRoot = false;
  727. if (workDirDepth < MAX_DIR_DEPTH)
  728. workDirParents[workDirDepth++] = workDir;
  729. TERN_(SDCARD_SORT_ALPHA, presort());
  730. }
  731. else {
  732. SERIAL_ECHO_START();
  733. SERIAL_ECHOLNPAIR(STR_SD_CANT_ENTER_SUBDIR, relpath);
  734. }
  735. }
  736. int8_t CardReader::cdup() {
  737. if (workDirDepth > 0) { // At least 1 dir has been saved
  738. workDir = --workDirDepth ? workDirParents[workDirDepth - 1] : root; // Use parent, or root if none
  739. TERN_(SDCARD_SORT_ALPHA, presort());
  740. }
  741. if (!workDirDepth) flag.workDirIsRoot = true;
  742. return workDirDepth;
  743. }
  744. void CardReader::cdroot() {
  745. workDir = root;
  746. flag.workDirIsRoot = true;
  747. TERN_(SDCARD_SORT_ALPHA, presort());
  748. }
  749. #if ENABLED(SDCARD_SORT_ALPHA)
  750. /**
  751. * Get the name of a file in the working directory by sort-index
  752. */
  753. void CardReader::getfilename_sorted(const uint16_t nr) {
  754. selectFileByIndex(TERN1(SDSORT_GCODE, sort_alpha) && (nr < sort_count)
  755. ? sort_order[nr] : nr);
  756. }
  757. #if ENABLED(SDSORT_USES_RAM)
  758. #if ENABLED(SDSORT_DYNAMIC_RAM)
  759. // Use dynamic method to copy long filename
  760. #define SET_SORTNAME(I) (sortnames[I] = strdup(longest_filename()))
  761. #if ENABLED(SDSORT_CACHE_NAMES)
  762. // When caching also store the short name, since
  763. // we're replacing the selectFileByIndex() behavior.
  764. #define SET_SORTSHORT(I) (sortshort[I] = strdup(filename))
  765. #else
  766. #define SET_SORTSHORT(I) NOOP
  767. #endif
  768. #else
  769. // Copy filenames into the static array
  770. #define _SET_SORTNAME(I) strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN)
  771. #if SORTED_LONGNAME_MAXLEN == LONG_FILENAME_LENGTH
  772. // Short name sorting always use LONG_FILENAME_LENGTH with no trailing nul
  773. #define SET_SORTNAME(I) _SET_SORTNAME(I)
  774. #else
  775. // Copy multiple name blocks. Add a nul for the longest case.
  776. #define SET_SORTNAME(I) do{ _SET_SORTNAME(I); sortnames[I][SORTED_LONGNAME_MAXLEN] = '\0'; }while(0)
  777. #endif
  778. #if ENABLED(SDSORT_CACHE_NAMES)
  779. #define SET_SORTSHORT(I) strcpy(sortshort[I], filename)
  780. #else
  781. #define SET_SORTSHORT(I) NOOP
  782. #endif
  783. #endif
  784. #endif
  785. /**
  786. * Read all the files and produce a sort key
  787. *
  788. * We can do this in 3 ways...
  789. * - Minimal RAM: Read two filenames at a time sorting along...
  790. * - Some RAM: Buffer the directory just for this sort
  791. * - Most RAM: Buffer the directory and return filenames from RAM
  792. */
  793. void CardReader::presort() {
  794. // Throw away old sort index
  795. flush_presort();
  796. // Sorting may be turned off
  797. if (TERN0(SDSORT_GCODE, !sort_alpha)) return;
  798. // If there are files, sort up to the limit
  799. uint16_t fileCnt = countFilesInWorkDir();
  800. if (fileCnt > 0) {
  801. // Never sort more than the max allowed
  802. // If you use folders to organize, 20 may be enough
  803. NOMORE(fileCnt, uint16_t(SDSORT_LIMIT));
  804. // Sort order is always needed. May be static or dynamic.
  805. TERN_(SDSORT_DYNAMIC_RAM, sort_order = new uint8_t[fileCnt]);
  806. // Use RAM to store the entire directory during pre-sort.
  807. // SDSORT_LIMIT should be set to prevent over-allocation.
  808. #if ENABLED(SDSORT_USES_RAM)
  809. // If using dynamic ram for names, allocate on the heap.
  810. #if ENABLED(SDSORT_CACHE_NAMES)
  811. #if ENABLED(SDSORT_DYNAMIC_RAM)
  812. sortshort = new char*[fileCnt];
  813. sortnames = new char*[fileCnt];
  814. #endif
  815. #elif ENABLED(SDSORT_USES_STACK)
  816. char sortnames[fileCnt][SORTED_LONGNAME_STORAGE];
  817. #endif
  818. // Folder sorting needs 1 bit per entry for flags.
  819. #if HAS_FOLDER_SORTING
  820. #if ENABLED(SDSORT_DYNAMIC_RAM)
  821. isDir = new uint8_t[(fileCnt + 7) >> 3];
  822. #elif ENABLED(SDSORT_USES_STACK)
  823. uint8_t isDir[(fileCnt + 7) >> 3];
  824. #endif
  825. #endif
  826. #else // !SDSORT_USES_RAM
  827. // By default re-read the names from SD for every compare
  828. // retaining only two filenames at a time. This is very
  829. // slow but is safest and uses minimal RAM.
  830. char name1[LONG_FILENAME_LENGTH];
  831. #endif
  832. if (fileCnt > 1) {
  833. // Init sort order.
  834. for (uint16_t i = 0; i < fileCnt; i++) {
  835. sort_order[i] = i;
  836. // If using RAM then read all filenames now.
  837. #if ENABLED(SDSORT_USES_RAM)
  838. selectFileByIndex(i);
  839. SET_SORTNAME(i);
  840. SET_SORTSHORT(i);
  841. // char out[30];
  842. // sprintf_P(out, PSTR("---- %i %s %s"), i, flag.filenameIsDir ? "D" : " ", sortnames[i]);
  843. // SERIAL_ECHOLN(out);
  844. #if HAS_FOLDER_SORTING
  845. const uint16_t bit = i & 0x07, ind = i >> 3;
  846. if (bit == 0) isDir[ind] = 0x00;
  847. if (flag.filenameIsDir) SBI(isDir[ind], bit);
  848. #endif
  849. #endif
  850. }
  851. // Bubble Sort
  852. for (uint16_t i = fileCnt; --i;) {
  853. bool didSwap = false;
  854. uint8_t o1 = sort_order[0];
  855. #if DISABLED(SDSORT_USES_RAM)
  856. selectFileByIndex(o1); // Pre-fetch the first entry and save it
  857. strcpy(name1, longest_filename()); // so the loop only needs one fetch
  858. TERN_(HAS_FOLDER_SORTING, bool dir1 = flag.filenameIsDir);
  859. #endif
  860. for (uint16_t j = 0; j < i; ++j) {
  861. const uint16_t o2 = sort_order[j + 1];
  862. // Compare names from the array or just the two buffered names
  863. #if ENABLED(SDSORT_USES_RAM)
  864. #define _SORT_CMP_NODIR() (strcasecmp(sortnames[o1], sortnames[o2]) > 0)
  865. #else
  866. #define _SORT_CMP_NODIR() (strcasecmp(name1, name2) > 0)
  867. #endif
  868. #if HAS_FOLDER_SORTING
  869. #if ENABLED(SDSORT_USES_RAM)
  870. // Folder sorting needs an index and bit to test for folder-ness.
  871. #define _SORT_CMP_DIR(fs) (IS_DIR(o1) == IS_DIR(o2) ? _SORT_CMP_NODIR() : IS_DIR(fs > 0 ? o1 : o2))
  872. #else
  873. #define _SORT_CMP_DIR(fs) ((dir1 == flag.filenameIsDir) ? _SORT_CMP_NODIR() : (fs > 0 ? dir1 : !dir1))
  874. #endif
  875. #endif
  876. // The most economical method reads names as-needed
  877. // throughout the loop. Slow if there are many.
  878. #if DISABLED(SDSORT_USES_RAM)
  879. selectFileByIndex(o2);
  880. const bool dir2 = flag.filenameIsDir;
  881. char * const name2 = longest_filename(); // use the string in-place
  882. #endif // !SDSORT_USES_RAM
  883. // Sort the current pair according to settings.
  884. if (
  885. #if HAS_FOLDER_SORTING
  886. #if ENABLED(SDSORT_GCODE)
  887. sort_folders ? _SORT_CMP_DIR(sort_folders) : _SORT_CMP_NODIR()
  888. #else
  889. _SORT_CMP_DIR(FOLDER_SORTING)
  890. #endif
  891. #else
  892. _SORT_CMP_NODIR()
  893. #endif
  894. ) {
  895. // Reorder the index, indicate that sorting happened
  896. // Note that the next o1 will be the current o1. No new fetch needed.
  897. sort_order[j] = o2;
  898. sort_order[j + 1] = o1;
  899. didSwap = true;
  900. }
  901. else {
  902. // The next o1 is the current o2. No new fetch needed.
  903. o1 = o2;
  904. #if DISABLED(SDSORT_USES_RAM)
  905. TERN_(HAS_FOLDER_SORTING, dir1 = dir2);
  906. strcpy(name1, name2);
  907. #endif
  908. }
  909. }
  910. if (!didSwap) break;
  911. }
  912. // Using RAM but not keeping names around
  913. #if ENABLED(SDSORT_USES_RAM) && DISABLED(SDSORT_CACHE_NAMES)
  914. #if ENABLED(SDSORT_DYNAMIC_RAM)
  915. for (uint16_t i = 0; i < fileCnt; ++i) free(sortnames[i]);
  916. TERN_(HAS_FOLDER_SORTING, free(isDir));
  917. #endif
  918. #endif
  919. }
  920. else {
  921. sort_order[0] = 0;
  922. #if BOTH(SDSORT_USES_RAM, SDSORT_CACHE_NAMES)
  923. #if ENABLED(SDSORT_DYNAMIC_RAM)
  924. sortnames = new char*[1];
  925. sortshort = new char*[1];
  926. isDir = new uint8_t[1];
  927. #endif
  928. selectFileByIndex(0);
  929. SET_SORTNAME(0);
  930. SET_SORTSHORT(0);
  931. isDir[0] = flag.filenameIsDir;
  932. #endif
  933. }
  934. sort_count = fileCnt;
  935. }
  936. }
  937. void CardReader::flush_presort() {
  938. if (sort_count > 0) {
  939. #if ENABLED(SDSORT_DYNAMIC_RAM)
  940. delete sort_order;
  941. #if ENABLED(SDSORT_CACHE_NAMES)
  942. LOOP_L_N(i, sort_count) {
  943. free(sortshort[i]); // strdup
  944. free(sortnames[i]); // strdup
  945. }
  946. delete sortshort;
  947. delete sortnames;
  948. #endif
  949. #endif
  950. sort_count = 0;
  951. }
  952. }
  953. #endif // SDCARD_SORT_ALPHA
  954. uint16_t CardReader::get_num_Files() {
  955. if (!isMounted()) return 0;
  956. return (
  957. #if ALL(SDCARD_SORT_ALPHA, SDSORT_USES_RAM, SDSORT_CACHE_NAMES)
  958. nrFiles // no need to access the SD card for filenames
  959. #else
  960. countFilesInWorkDir()
  961. #endif
  962. );
  963. }
  964. //
  965. // Return from procedure or close out the Print Job
  966. //
  967. void CardReader::fileHasFinished() {
  968. planner.synchronize();
  969. file.close();
  970. if (file_subcall_ctr > 0) { // Resume calling file after closing procedure
  971. file_subcall_ctr--;
  972. openFileRead(proc_filenames[file_subcall_ctr], 2); // 2 = Returning from sub-procedure
  973. setIndex(filespos[file_subcall_ctr]);
  974. startFileprint();
  975. }
  976. else {
  977. endFilePrint(TERN_(SD_RESORT, true));
  978. marlin_state = MF_SD_COMPLETE;
  979. }
  980. }
  981. #if ENABLED(AUTO_REPORT_SD_STATUS)
  982. uint8_t CardReader::auto_report_sd_interval = 0;
  983. millis_t CardReader::next_sd_report_ms;
  984. #if HAS_MULTI_SERIAL
  985. int8_t CardReader::auto_report_port;
  986. #endif
  987. void CardReader::auto_report_sd_status() {
  988. millis_t current_ms = millis();
  989. if (auto_report_sd_interval && ELAPSED(current_ms, next_sd_report_ms)) {
  990. next_sd_report_ms = current_ms + 1000UL * auto_report_sd_interval;
  991. PORT_REDIRECT(auto_report_port);
  992. report_status();
  993. }
  994. }
  995. #endif // AUTO_REPORT_SD_STATUS
  996. #if ENABLED(POWER_LOSS_RECOVERY)
  997. bool CardReader::jobRecoverFileExists() {
  998. const bool exists = recovery.file.open(&root, recovery.filename, O_READ);
  999. if (exists) recovery.file.close();
  1000. return exists;
  1001. }
  1002. void CardReader::openJobRecoveryFile(const bool read) {
  1003. if (!isMounted()) return;
  1004. if (recovery.file.isOpen()) return;
  1005. if (!recovery.file.open(&root, recovery.filename, read ? O_READ : O_CREAT | O_WRITE | O_TRUNC | O_SYNC))
  1006. SERIAL_ECHOLNPAIR(STR_SD_OPEN_FILE_FAIL, recovery.filename, ".");
  1007. else if (!read)
  1008. echo_write_to_file(recovery.filename);
  1009. }
  1010. // Removing the job recovery file currently requires closing
  1011. // the file being printed, so during SD printing the file should
  1012. // be zeroed and written instead of deleted.
  1013. void CardReader::removeJobRecoveryFile() {
  1014. if (jobRecoverFileExists()) {
  1015. recovery.init();
  1016. removeFile(recovery.filename);
  1017. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  1018. SERIAL_ECHOPGM("Power-loss file delete");
  1019. serialprintPGM(jobRecoverFileExists() ? PSTR(" failed.\n") : PSTR("d.\n"));
  1020. #endif
  1021. }
  1022. }
  1023. #endif // POWER_LOSS_RECOVERY
  1024. #endif // SDSUPPORT