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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #define IFSD(A,B) TERN(SDSUPPORT,A,B)
  25. #if ENABLED(SDSUPPORT)
  26. extern const char M23_STR[], M24_STR[];
  27. #if BOTH(SDCARD_SORT_ALPHA, SDSORT_DYNAMIC_RAM)
  28. #define SD_RESORT 1
  29. #endif
  30. #if ENABLED(SDCARD_RATHERRECENTFIRST) && DISABLED(SDCARD_SORT_ALPHA)
  31. #define SD_ORDER(N,C) ((C) - 1 - (N))
  32. #else
  33. #define SD_ORDER(N,C) N
  34. #endif
  35. #define MAX_DIR_DEPTH 10 // Maximum folder depth
  36. #define MAXDIRNAMELENGTH 8 // DOS folder name size
  37. #define MAXPATHNAMELENGTH (1 + (MAXDIRNAMELENGTH + 1) * (MAX_DIR_DEPTH) + 1 + FILENAME_LENGTH) // "/" + N * ("ADIRNAME/") + "filename.ext"
  38. #include "SdFile.h"
  39. typedef struct {
  40. bool saving:1,
  41. logging:1,
  42. sdprinting:1,
  43. mounted:1,
  44. filenameIsDir:1,
  45. workDirIsRoot:1,
  46. abort_sd_printing:1
  47. #if ENABLED(BINARY_FILE_TRANSFER)
  48. , binary_mode:1
  49. #endif
  50. ;
  51. } card_flags_t;
  52. class CardReader {
  53. public:
  54. static card_flags_t flag; // Flags (above)
  55. static char filename[FILENAME_LENGTH], // DOS 8.3 filename of the selected item
  56. longFilename[LONG_FILENAME_LENGTH]; // Long name of the selected item
  57. // Fast! binary file transfer
  58. #if ENABLED(BINARY_FILE_TRANSFER)
  59. #if HAS_MULTI_SERIAL
  60. static int8_t transfer_port_index;
  61. #else
  62. static constexpr int8_t transfer_port_index = 0;
  63. #endif
  64. #endif
  65. // // // Methods // // //
  66. CardReader();
  67. static SdFile getroot() { return root; }
  68. static void mount();
  69. static void release();
  70. static inline bool isMounted() { return flag.mounted; }
  71. static void ls();
  72. // Handle media insert/remove
  73. static void manage_media();
  74. // SD Card Logging
  75. static void openLogFile(char * const path);
  76. static void write_command(char * const buf);
  77. #if DISABLED(NO_SD_AUTOSTART) // Auto-Start auto#.g file handling
  78. static uint8_t autofile_index; // Next auto#.g index to run, plus one. Ignored by autofile_check when zero.
  79. static void autofile_begin(); // Begin check. Called automatically after boot-up.
  80. static bool autofile_check(); // Check for the next auto-start file and run it.
  81. static inline void autofile_cancel() { autofile_index = 0; }
  82. #endif
  83. // Basic file ops
  84. static void openFileRead(char * const path, const uint8_t subcall=0);
  85. static void openFileWrite(char * const path);
  86. static void closefile(const bool store_location=false);
  87. static bool fileExists(const char * const name);
  88. static void removeFile(const char * const name);
  89. static inline char* longest_filename() { return longFilename[0] ? longFilename : filename; }
  90. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  91. static void printLongPath(char * const path); // Used by M33
  92. #endif
  93. // Working Directory for SD card menu
  94. static void cdroot();
  95. static void cd(const char *relpath);
  96. static int8_t cdup();
  97. static uint16_t countFilesInWorkDir();
  98. static uint16_t get_num_Files();
  99. // Select a file
  100. static void selectFileByIndex(const uint16_t nr);
  101. static void selectFileByName(const char* const match);
  102. // Print job
  103. static void openAndPrintFile(const char *name); // (working directory)
  104. static void fileHasFinished();
  105. static void getAbsFilename(char *dst);
  106. static void printFilename();
  107. static void startFileprint();
  108. static void endFilePrint(TERN_(SD_RESORT, const bool re_sort=false));
  109. static void report_status();
  110. static inline void pauseSDPrint() { flag.sdprinting = false; }
  111. static inline bool isPaused() { return isFileOpen() && !flag.sdprinting; }
  112. static inline bool isPrinting() { return flag.sdprinting; }
  113. #if HAS_PRINT_PROGRESS_PERMYRIAD
  114. static inline uint16_t permyriadDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 9999) / 10000) : 0; }
  115. #endif
  116. static inline uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
  117. // Helper for open and remove
  118. static const char* diveToFile(const bool update_cwd, SdFile*& curDir, const char * const path, const bool echo=false);
  119. #if ENABLED(SDCARD_SORT_ALPHA)
  120. static void presort();
  121. static void getfilename_sorted(const uint16_t nr);
  122. #if ENABLED(SDSORT_GCODE)
  123. FORCE_INLINE static void setSortOn(bool b) { sort_alpha = b; presort(); }
  124. FORCE_INLINE static void setSortFolders(int i) { sort_folders = i; presort(); }
  125. //FORCE_INLINE static void setSortReverse(bool b) { sort_reverse = b; }
  126. #endif
  127. #else
  128. FORCE_INLINE static void getfilename_sorted(const uint16_t nr) { selectFileByIndex(nr); }
  129. #endif
  130. #if ENABLED(POWER_LOSS_RECOVERY)
  131. static bool jobRecoverFileExists();
  132. static void openJobRecoveryFile(const bool read);
  133. static void removeJobRecoveryFile();
  134. #endif
  135. static inline bool isFileOpen() { return isMounted() && file.isOpen(); }
  136. static inline uint32_t getIndex() { return sdpos; }
  137. static inline uint32_t getFileSize() { return filesize; }
  138. static inline bool eof() { return sdpos >= filesize; }
  139. static inline void setIndex(const uint32_t index) { file.seekSet((sdpos = index)); }
  140. static inline char* getWorkDirName() { workDir.getDosName(filename); return filename; }
  141. static inline int16_t get() { int16_t out = (int16_t)file.read(); sdpos = file.curPosition(); return out; }
  142. static inline int16_t read(void* buf, uint16_t nbyte) { return file.isOpen() ? file.read(buf, nbyte) : -1; }
  143. static inline int16_t write(void* buf, uint16_t nbyte) { return file.isOpen() ? file.write(buf, nbyte) : -1; }
  144. static Sd2Card& getSd2Card() { return sd2card; }
  145. #if ENABLED(AUTO_REPORT_SD_STATUS)
  146. static void auto_report_sd_status();
  147. static inline void set_auto_report_interval(uint8_t v) {
  148. TERN_(HAS_MULTI_SERIAL, auto_report_port = serialHook.portMask);
  149. NOMORE(v, 60);
  150. auto_report_sd_interval = v;
  151. next_sd_report_ms = millis() + 1000UL * v;
  152. }
  153. #endif
  154. private:
  155. //
  156. // Working directory and parents
  157. //
  158. static SdFile root, workDir, workDirParents[MAX_DIR_DEPTH];
  159. static uint8_t workDirDepth;
  160. //
  161. // Alphabetical file and folder sorting
  162. //
  163. #if ENABLED(SDCARD_SORT_ALPHA)
  164. static uint16_t sort_count; // Count of sorted items in the current directory
  165. #if ENABLED(SDSORT_GCODE)
  166. static bool sort_alpha; // Flag to enable / disable the feature
  167. static int sort_folders; // Folder sorting before/none/after
  168. //static bool sort_reverse; // Flag to enable / disable reverse sorting
  169. #endif
  170. // By default the sort index is static
  171. #if ENABLED(SDSORT_DYNAMIC_RAM)
  172. static uint8_t *sort_order;
  173. #else
  174. static uint8_t sort_order[SDSORT_LIMIT];
  175. #endif
  176. #if BOTH(SDSORT_USES_RAM, SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
  177. #define SORTED_LONGNAME_MAXLEN (SDSORT_CACHE_VFATS) * (FILENAME_LENGTH)
  178. #define SORTED_LONGNAME_STORAGE (SORTED_LONGNAME_MAXLEN + 1)
  179. #else
  180. #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH
  181. #define SORTED_LONGNAME_STORAGE SORTED_LONGNAME_MAXLEN
  182. #endif
  183. // Cache filenames to speed up SD menus.
  184. #if ENABLED(SDSORT_USES_RAM)
  185. // If using dynamic ram for names, allocate on the heap.
  186. #if ENABLED(SDSORT_CACHE_NAMES)
  187. static uint16_t nrFiles; // Cache the total count
  188. #if ENABLED(SDSORT_DYNAMIC_RAM)
  189. static char **sortshort, **sortnames;
  190. #else
  191. static char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
  192. #endif
  193. #endif
  194. #if (ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)) || NONE(SDSORT_CACHE_NAMES, SDSORT_USES_STACK)
  195. static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
  196. #endif
  197. // Folder sorting uses an isDir array when caching items.
  198. #if HAS_FOLDER_SORTING
  199. #if ENABLED(SDSORT_DYNAMIC_RAM)
  200. static uint8_t *isDir;
  201. #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
  202. static uint8_t isDir[(SDSORT_LIMIT+7)>>3];
  203. #endif
  204. #endif
  205. #endif // SDSORT_USES_RAM
  206. #endif // SDCARD_SORT_ALPHA
  207. static Sd2Card sd2card;
  208. static SdVolume volume;
  209. static SdFile file;
  210. static uint32_t filesize, // Total size of the current file, in bytes
  211. sdpos; // Index most recently read (one behind file.getPos)
  212. //
  213. // Procedure calls to other files
  214. //
  215. #if HAS_MEDIA_SUBCALLS
  216. static uint8_t file_subcall_ctr;
  217. static uint32_t filespos[SD_PROCEDURE_DEPTH];
  218. static char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  219. #endif
  220. //
  221. // SD Auto Reporting
  222. //
  223. #if ENABLED(AUTO_REPORT_SD_STATUS)
  224. static uint8_t auto_report_sd_interval;
  225. static millis_t next_sd_report_ms;
  226. #if HAS_MULTI_SERIAL
  227. static serial_index_t auto_report_port;
  228. #endif
  229. #endif
  230. //
  231. // Directory items
  232. //
  233. static bool is_dir_or_gcode(const dir_t &p);
  234. static int countItems(SdFile dir);
  235. static void selectByIndex(SdFile dir, const uint8_t index);
  236. static void selectByName(SdFile dir, const char * const match);
  237. static void printListing(SdFile parent, const char * const prepend=nullptr);
  238. #if ENABLED(SDCARD_SORT_ALPHA)
  239. static void flush_presort();
  240. #endif
  241. };
  242. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  243. #define IS_SD_INSERTED() Sd2Card::isInserted()
  244. #elif PIN_EXISTS(SD_DETECT)
  245. #define IS_SD_INSERTED() (READ(SD_DETECT_PIN) == SD_DETECT_STATE)
  246. #else
  247. // No card detect line? Assume the card is inserted.
  248. #define IS_SD_INSERTED() true
  249. #endif
  250. #define IS_SD_PRINTING() card.flag.sdprinting
  251. #define IS_SD_PAUSED() card.isPaused()
  252. #define IS_SD_FILE_OPEN() card.isFileOpen()
  253. extern CardReader card;
  254. #else // !SDSUPPORT
  255. #define IS_SD_PRINTING() false
  256. #define IS_SD_PAUSED() false
  257. #define IS_SD_FILE_OPEN() false
  258. #define LONG_FILENAME_LENGTH 0
  259. #endif // !SDSUPPORT