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

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