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.

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