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.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #pragma once
  23. #include "../inc/MarlinConfig.h"
  24. #if ENABLED(SDSUPPORT)
  25. extern const char M23_STR[], M24_STR[];
  26. #if ENABLED(SDCARD_SORT_ALPHA)
  27. #if ENABLED(SDSORT_DYNAMIC_RAM)
  28. #define SD_RESORT 1
  29. #endif
  30. #if FOLDER_SORTING || ENABLED(SDSORT_GCODE)
  31. #define HAS_FOLDER_SORTING 1
  32. #endif
  33. #endif
  34. #if ENABLED(SDCARD_RATHERRECENTFIRST) && DISABLED(SDCARD_SORT_ALPHA)
  35. #define SD_ORDER(N,C) ((C) - 1 - (N))
  36. #else
  37. #define SD_ORDER(N,C) N
  38. #endif
  39. #define MAX_DIR_DEPTH 10 // Maximum folder depth
  40. #define MAXDIRNAMELENGTH 8 // DOS folder name size
  41. #define MAXPATHNAMELENGTH (1 + (MAXDIRNAMELENGTH + 1) * (MAX_DIR_DEPTH) + 1 + FILENAME_LENGTH) // "/" + N * ("ADIRNAME/") + "filename.ext"
  42. #include "SdFile.h"
  43. #include "disk_io_driver.h"
  44. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  45. #include "usb_flashdrive/Sd2Card_FlashDrive.h"
  46. #endif
  47. #if NEED_SD2CARD_SDIO
  48. #include "Sd2Card_sdio.h"
  49. #elif NEED_SD2CARD_SPI
  50. #include "Sd2Card.h"
  51. #endif
  52. #if ENABLED(MULTI_VOLUME)
  53. #define SV_SD_ONBOARD 1
  54. #define SV_USB_FLASH_DRIVE 2
  55. #define _VOLUME_ID(N) _CAT(SV_, N)
  56. #define SHARED_VOLUME_IS(N) (DEFAULT_SHARED_VOLUME == _VOLUME_ID(N))
  57. #if !SHARED_VOLUME_IS(SD_ONBOARD) && !SHARED_VOLUME_IS(USB_FLASH_DRIVE)
  58. #error "DEFAULT_SHARED_VOLUME must be either SD_ONBOARD or USB_FLASH_DRIVE."
  59. #endif
  60. #else
  61. #define SHARED_VOLUME_IS(...) 0
  62. #endif
  63. typedef struct {
  64. bool saving:1,
  65. logging:1,
  66. sdprinting:1,
  67. sdprintdone:1,
  68. mounted:1,
  69. filenameIsDir:1,
  70. workDirIsRoot:1,
  71. abort_sd_printing:1
  72. #if DO_LIST_BIN_FILES
  73. , filenameIsBin:1
  74. #endif
  75. #if ENABLED(BINARY_FILE_TRANSFER)
  76. , binary_mode:1
  77. #endif
  78. ;
  79. } card_flags_t;
  80. enum ListingFlags : uint8_t { LS_LONG_FILENAME, LS_ONLY_BIN, LS_TIMESTAMP };
  81. #if ENABLED(AUTO_REPORT_SD_STATUS)
  82. #include "../libs/autoreport.h"
  83. #endif
  84. class CardReader {
  85. public:
  86. static card_flags_t flag; // Flags (above)
  87. static char filename[FILENAME_LENGTH], // DOS 8.3 filename of the selected item
  88. longFilename[LONG_FILENAME_LENGTH]; // Long name of the selected item
  89. // Fast! binary file transfer
  90. #if ENABLED(BINARY_FILE_TRANSFER)
  91. #if HAS_MULTI_SERIAL
  92. static serial_index_t transfer_port_index;
  93. #else
  94. static constexpr serial_index_t transfer_port_index = 0;
  95. #endif
  96. #endif
  97. // // // Methods // // //
  98. CardReader();
  99. static void changeMedia(DiskIODriver *_driver) { driver = _driver; }
  100. static SdFile getroot() { return root; }
  101. static void mount();
  102. static void release();
  103. static bool isMounted() { return flag.mounted; }
  104. // Handle media insert/remove
  105. static void manage_media();
  106. // SD Card Logging
  107. static void openLogFile(const char * const path);
  108. static void write_command(char * const buf);
  109. #if DISABLED(NO_SD_AUTOSTART) // Auto-Start auto#.g file handling
  110. static uint8_t autofile_index; // Next auto#.g index to run, plus one. Ignored by autofile_check when zero.
  111. static void autofile_begin(); // Begin check. Called automatically after boot-up.
  112. static bool autofile_check(); // Check for the next auto-start file and run it.
  113. static void autofile_cancel() { autofile_index = 0; }
  114. #endif
  115. // Basic file ops
  116. static void openFileRead(const char * const path, const uint8_t subcall=0);
  117. static void openFileWrite(const char * const path);
  118. static void closefile(const bool store_location=false);
  119. static bool fileExists(const char * const name);
  120. static void removeFile(const char * const name);
  121. static char* longest_filename() { return longFilename[0] ? longFilename : filename; }
  122. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  123. static void printLongPath(char * const path); // Used by M33
  124. #endif
  125. // Working Directory for SD card menu
  126. static void cdroot();
  127. static void cd(const char *relpath);
  128. static int8_t cdup();
  129. static uint16_t countFilesInWorkDir();
  130. static uint16_t get_num_Files();
  131. // Select a file
  132. static void selectFileByIndex(const uint16_t nr);
  133. static void selectFileByName(const char * const match); // (working directory only)
  134. // Print job
  135. static void report_status();
  136. static void getAbsFilenameInCWD(char *dst);
  137. static void printSelectedFilename();
  138. static void openAndPrintFile(const char *name); // (working directory or full path)
  139. static void startOrResumeFilePrinting();
  140. static void endFilePrintNow(TERN_(SD_RESORT, const bool re_sort=false));
  141. static void abortFilePrintNow(TERN_(SD_RESORT, const bool re_sort=false));
  142. static void fileHasFinished();
  143. static void abortFilePrintSoon() { flag.abort_sd_printing = isFileOpen(); }
  144. static void pauseSDPrint() { flag.sdprinting = false; }
  145. static bool isPrinting() { return flag.sdprinting; }
  146. static bool isPaused() { return isFileOpen() && !isPrinting(); }
  147. #if HAS_PRINT_PROGRESS_PERMYRIAD
  148. static uint16_t permyriadDone() {
  149. if (flag.sdprintdone) return 10000;
  150. if (isFileOpen() && filesize) return sdpos / ((filesize + 9999) / 10000);
  151. return 0;
  152. }
  153. #endif
  154. static uint8_t percentDone() {
  155. if (flag.sdprintdone) return 100;
  156. if (isFileOpen() && filesize) return sdpos / ((filesize + 99) / 100);
  157. return 0;
  158. }
  159. /**
  160. * Dive down to a relative or absolute path.
  161. * Relative paths apply to the workDir.
  162. *
  163. * update_cwd: Pass 'true' to update the workDir on success.
  164. * inDirPtr: On exit your pointer points to the target SdFile.
  165. * A nullptr indicates failure.
  166. * path: Start with '/' for abs path. End with '/' to get a folder ref.
  167. * echo: Set 'true' to print the path throughout the loop.
  168. */
  169. static const char* diveToFile(const bool update_cwd, SdFile* &inDirPtr, const char * const path, const bool echo=false);
  170. #if ENABLED(SDCARD_SORT_ALPHA)
  171. static void presort();
  172. static void getfilename_sorted(const uint16_t nr);
  173. #if ENABLED(SDSORT_GCODE)
  174. FORCE_INLINE static void setSortOn(bool b) { sort_alpha = b; presort(); }
  175. FORCE_INLINE static void setSortFolders(int i) { sort_folders = i; presort(); }
  176. //FORCE_INLINE static void setSortReverse(bool b) { sort_reverse = b; }
  177. #endif
  178. #else
  179. FORCE_INLINE static void getfilename_sorted(const uint16_t nr) { selectFileByIndex(nr); }
  180. #endif
  181. static void ls(const uint8_t lsflags);
  182. #if ENABLED(POWER_LOSS_RECOVERY)
  183. static bool jobRecoverFileExists();
  184. static void openJobRecoveryFile(const bool read);
  185. static void removeJobRecoveryFile();
  186. #endif
  187. // Binary flag for the current file
  188. static bool fileIsBinary() { return TERN0(DO_LIST_BIN_FILES, flag.filenameIsBin); }
  189. static void setBinFlag(const bool bin) { TERN(DO_LIST_BIN_FILES, flag.filenameIsBin = bin, UNUSED(bin)); }
  190. // Current Working Dir - Set by cd, cdup, cdroot, and diveToFile(true, ...)
  191. static char* getWorkDirName() { workDir.getDosName(filename); return filename; }
  192. static SdFile& getWorkDir() { return workDir.isOpen() ? workDir : root; }
  193. // Print File stats
  194. static uint32_t getFileSize() { return filesize; }
  195. static uint32_t getIndex() { return sdpos; }
  196. static bool isFileOpen() { return isMounted() && file.isOpen(); }
  197. static bool eof() { return getIndex() >= getFileSize(); }
  198. // File data operations
  199. static int16_t get() { int16_t out = (int16_t)file.read(); sdpos = file.curPosition(); return out; }
  200. static int16_t read(void *buf, uint16_t nbyte) { return file.isOpen() ? file.read(buf, nbyte) : -1; }
  201. static int16_t write(void *buf, uint16_t nbyte) { return file.isOpen() ? file.write(buf, nbyte) : -1; }
  202. static void setIndex(const uint32_t index) { file.seekSet((sdpos = index)); }
  203. // TODO: rename to diskIODriver()
  204. static DiskIODriver* diskIODriver() { return driver; }
  205. #if ENABLED(AUTO_REPORT_SD_STATUS)
  206. //
  207. // SD Auto Reporting
  208. //
  209. struct AutoReportSD { static void report() { report_status(); } };
  210. static AutoReporter<AutoReportSD> auto_reporter;
  211. #endif
  212. #if SHARED_VOLUME_IS(USB_FLASH_DRIVE) || ENABLED(USB_FLASH_DRIVE_SUPPORT)
  213. #define HAS_USB_FLASH_DRIVE 1
  214. static DiskIODriver_USBFlash media_driver_usbFlash;
  215. #endif
  216. #if NEED_SD2CARD_SDIO || NEED_SD2CARD_SPI
  217. typedef TERN(NEED_SD2CARD_SDIO, DiskIODriver_SDIO, DiskIODriver_SPI_SD) sdcard_driver_t;
  218. static sdcard_driver_t media_driver_sdcard;
  219. #endif
  220. private:
  221. //
  222. // Working directory and parents
  223. //
  224. static SdFile root, workDir, workDirParents[MAX_DIR_DEPTH];
  225. static uint8_t workDirDepth;
  226. //
  227. // Alphabetical file and folder sorting
  228. //
  229. #if ENABLED(SDCARD_SORT_ALPHA)
  230. static uint16_t sort_count; // Count of sorted items in the current directory
  231. #if ENABLED(SDSORT_GCODE)
  232. static bool sort_alpha; // Flag to enable / disable the feature
  233. static int sort_folders; // Folder sorting before/none/after
  234. //static bool sort_reverse; // Flag to enable / disable reverse sorting
  235. #endif
  236. // By default the sort index is static
  237. #if ENABLED(SDSORT_DYNAMIC_RAM)
  238. static uint8_t *sort_order;
  239. #else
  240. static uint8_t sort_order[SDSORT_LIMIT];
  241. #endif
  242. #if BOTH(SDSORT_USES_RAM, SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
  243. #define SORTED_LONGNAME_MAXLEN (SDSORT_CACHE_VFATS) * (FILENAME_LENGTH)
  244. #define SORTED_LONGNAME_STORAGE (SORTED_LONGNAME_MAXLEN + 1)
  245. #else
  246. #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH
  247. #define SORTED_LONGNAME_STORAGE SORTED_LONGNAME_MAXLEN
  248. #endif
  249. // Cache filenames to speed up SD menus.
  250. #if ENABLED(SDSORT_USES_RAM)
  251. // If using dynamic ram for names, allocate on the heap.
  252. #if ENABLED(SDSORT_CACHE_NAMES)
  253. static uint16_t nrFiles; // Cache the total count
  254. #if ENABLED(SDSORT_DYNAMIC_RAM)
  255. static char **sortshort, **sortnames;
  256. #else
  257. static char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
  258. #endif
  259. #endif
  260. #if (ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)) || NONE(SDSORT_CACHE_NAMES, SDSORT_USES_STACK)
  261. static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
  262. #endif
  263. // Folder sorting uses an isDir array when caching items.
  264. #if HAS_FOLDER_SORTING
  265. #if ENABLED(SDSORT_DYNAMIC_RAM)
  266. static uint8_t *isDir;
  267. #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
  268. static uint8_t isDir[(SDSORT_LIMIT + 7) >> 3];
  269. #endif
  270. #endif
  271. #endif // SDSORT_USES_RAM
  272. #endif // SDCARD_SORT_ALPHA
  273. static DiskIODriver *driver;
  274. static SdVolume volume;
  275. static SdFile file;
  276. static uint32_t filesize, // Total size of the current file, in bytes
  277. sdpos; // Index most recently read (one behind file.getPos)
  278. //
  279. // Procedure calls to other files
  280. //
  281. #if HAS_MEDIA_SUBCALLS
  282. static uint8_t file_subcall_ctr;
  283. static uint32_t filespos[SD_PROCEDURE_DEPTH];
  284. static char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  285. #endif
  286. //
  287. // Directory items
  288. //
  289. static bool is_visible_entity(const dir_t &p OPTARG(CUSTOM_FIRMWARE_UPLOAD, const bool onlyBin=false));
  290. static int countItems(SdFile dir);
  291. static void selectByIndex(SdFile dir, const uint8_t index);
  292. static void selectByName(SdFile dir, const char * const match);
  293. static void printListing(SdFile parent, const char * const prepend, const uint8_t lsflags
  294. OPTARG(LONG_FILENAME_HOST_SUPPORT, const char * const prependLong=nullptr)
  295. );
  296. #if ENABLED(SDCARD_SORT_ALPHA)
  297. static void flush_presort();
  298. #endif
  299. };
  300. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  301. #define IS_SD_INSERTED() DiskIODriver_USBFlash::isInserted()
  302. #elif HAS_SD_DETECT
  303. #define IS_SD_INSERTED() (READ(SD_DETECT_PIN) == SD_DETECT_STATE)
  304. #else
  305. // No card detect line? Assume the card is inserted.
  306. #define IS_SD_INSERTED() true
  307. #endif
  308. #define IS_SD_PRINTING() (card.flag.sdprinting && !card.flag.abort_sd_printing)
  309. #define IS_SD_FETCHING() (!card.flag.sdprintdone && IS_SD_PRINTING())
  310. #define IS_SD_PAUSED() card.isPaused()
  311. #define IS_SD_FILE_OPEN() card.isFileOpen()
  312. extern CardReader card;
  313. #else // !SDSUPPORT
  314. #define IS_SD_PRINTING() false
  315. #define IS_SD_FETCHING() false
  316. #define IS_SD_PAUSED() false
  317. #define IS_SD_FILE_OPEN() false
  318. #define LONG_FILENAME_LENGTH 0
  319. #endif // !SDSUPPORT