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