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

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