My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cardreader.h 13KB

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