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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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 ENABLED(SDCARD_SORT_ALPHA) && ENABLED(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. enum LsAction : uint8_t { LS_SerialPrint, LS_Count, LS_GetFilename };
  31. typedef struct {
  32. bool saving:1,
  33. logging:1,
  34. sdprinting:1,
  35. detected:1,
  36. filenameIsDir:1,
  37. abort_sd_printing:1
  38. #if ENABLED(FAST_FILE_TRANSFER)
  39. , binary_mode:1
  40. #endif
  41. ;
  42. } card_flags_t;
  43. class CardReader {
  44. public:
  45. CardReader();
  46. static void initsd();
  47. static void write_command(char *buf);
  48. static void beginautostart();
  49. static void checkautostart();
  50. static void openFile(char * const path, const bool read, const bool subcall=false);
  51. static void openLogFile(char * const path);
  52. static void removeFile(const char * const name);
  53. static void closefile(const bool store_location=false);
  54. static void release();
  55. static void openAndPrintFile(const char *name);
  56. static void startFileprint();
  57. static void stopSDPrint(
  58. #if SD_RESORT
  59. const bool re_sort=false
  60. #endif
  61. );
  62. static void report_status();
  63. static void printingHasFinished();
  64. static void printFilename();
  65. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  66. static void printLongPath(char *path);
  67. #endif
  68. static void getfilename(uint16_t nr, const char* const match=NULL);
  69. static uint16_t getnrfilenames();
  70. static void getAbsFilename(char *t);
  71. static void ls();
  72. static void chdir(const char *relpath);
  73. static int8_t updir();
  74. static void setroot();
  75. static const char* diveToFile(SdFile*& curDir, const char * const path, const bool echo);
  76. static uint16_t get_num_Files();
  77. #if ENABLED(SDCARD_SORT_ALPHA)
  78. static void presort();
  79. static void getfilename_sorted(const uint16_t nr);
  80. #if ENABLED(SDSORT_GCODE)
  81. FORCE_INLINE static void setSortOn(bool b) { sort_alpha = b; presort(); }
  82. FORCE_INLINE static void setSortFolders(int i) { sort_folders = i; presort(); }
  83. //FORCE_INLINE static void setSortReverse(bool b) { sort_reverse = b; }
  84. #endif
  85. #else
  86. FORCE_INLINE static void getfilename_sorted(const uint16_t nr) { getfilename(nr); }
  87. #endif
  88. #if ENABLED(POWER_LOSS_RECOVERY)
  89. static bool jobRecoverFileExists();
  90. static void openJobRecoveryFile(const bool read);
  91. static void removeJobRecoveryFile();
  92. #endif
  93. static inline void pauseSDPrint() { flag.sdprinting = false; }
  94. static inline bool isDetected() { return flag.detected; }
  95. static inline bool isFileOpen() { return isDetected() && file.isOpen(); }
  96. static inline bool isPaused() { return isFileOpen() && !flag.sdprinting; }
  97. static inline bool isPrinting() { return flag.sdprinting; }
  98. static inline bool eof() { return sdpos >= filesize; }
  99. static inline int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
  100. static inline void setIndex(const uint32_t index) { sdpos = index; file.seekSet(index); }
  101. static inline uint32_t getIndex() { return sdpos; }
  102. static inline uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
  103. static inline char* getWorkDirName() { workDir.getFilename(filename); return filename; }
  104. static inline int16_t read(void* buf, uint16_t nbyte) { return file.isOpen() ? file.read(buf, nbyte) : -1; }
  105. static inline int16_t write(void* buf, uint16_t nbyte) { return file.isOpen() ? file.write(buf, nbyte) : -1; }
  106. static Sd2Card& getSd2Card() { return sd2card; }
  107. #if ENABLED(AUTO_REPORT_SD_STATUS)
  108. static void auto_report_sd_status(void);
  109. static inline void set_auto_report_interval(const uint8_t v) {
  110. #if NUM_SERIAL > 1
  111. auto_report_port = serial_port_index;
  112. #endif
  113. NOMORE(v, 60);
  114. auto_report_sd_interval = v;
  115. next_sd_report_ms = millis() + 1000UL * v;
  116. }
  117. #endif
  118. static inline char* longest_filename() { return longFilename[0] ? longFilename : filename; }
  119. public:
  120. static card_flags_t flag;
  121. static char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
  122. static int8_t autostart_index;
  123. #if ENABLED(FAST_FILE_TRANSFER)
  124. #if NUM_SERIAL > 1
  125. static int8_t transfer_port;
  126. #else
  127. static constexpr int8_t transfer_port = SERIAL_PORT;
  128. #endif
  129. #endif
  130. private:
  131. static SdFile root, workDir, workDirParents[MAX_DIR_DEPTH];
  132. static uint8_t workDirDepth;
  133. // Sort files and folders alphabetically.
  134. #if ENABLED(SDCARD_SORT_ALPHA)
  135. static uint16_t sort_count; // Count of sorted items in the current directory
  136. #if ENABLED(SDSORT_GCODE)
  137. static bool sort_alpha; // Flag to enable / disable the feature
  138. static int sort_folders; // Folder sorting before/none/after
  139. //static bool sort_reverse; // Flag to enable / disable reverse sorting
  140. #endif
  141. // By default the sort index is static
  142. #if ENABLED(SDSORT_DYNAMIC_RAM)
  143. static uint8_t *sort_order;
  144. #else
  145. static uint8_t sort_order[SDSORT_LIMIT];
  146. #endif
  147. #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
  148. #define SORTED_LONGNAME_MAXLEN ((SDSORT_CACHE_VFATS) * (FILENAME_LENGTH) + 1)
  149. #else
  150. #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH
  151. #endif
  152. // Cache filenames to speed up SD menus.
  153. #if ENABLED(SDSORT_USES_RAM)
  154. // If using dynamic ram for names, allocate on the heap.
  155. #if ENABLED(SDSORT_CACHE_NAMES)
  156. #if ENABLED(SDSORT_DYNAMIC_RAM)
  157. static char **sortshort, **sortnames;
  158. #else
  159. static char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
  160. static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
  161. #endif
  162. #elif DISABLED(SDSORT_USES_STACK)
  163. static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
  164. #endif
  165. // Folder sorting uses an isDir array when caching items.
  166. #if HAS_FOLDER_SORTING
  167. #if ENABLED(SDSORT_DYNAMIC_RAM)
  168. static uint8_t *isDir;
  169. #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
  170. static uint8_t isDir[(SDSORT_LIMIT+7)>>3];
  171. #endif
  172. #endif
  173. #endif // SDSORT_USES_RAM
  174. #endif // SDCARD_SORT_ALPHA
  175. static Sd2Card sd2card;
  176. static SdVolume volume;
  177. static SdFile file;
  178. #ifndef SD_PROCEDURE_DEPTH
  179. #define SD_PROCEDURE_DEPTH 1
  180. #endif
  181. static uint8_t file_subcall_ctr;
  182. static uint32_t filespos[SD_PROCEDURE_DEPTH];
  183. static char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  184. static uint32_t filesize, sdpos;
  185. static LsAction lsAction; //stored for recursion.
  186. static uint16_t nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory.
  187. static char *diveDirName;
  188. static void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
  189. #if ENABLED(SDCARD_SORT_ALPHA)
  190. static void flush_presort();
  191. #endif
  192. #if ENABLED(AUTO_REPORT_SD_STATUS)
  193. static uint8_t auto_report_sd_interval;
  194. static millis_t next_sd_report_ms;
  195. #if NUM_SERIAL > 1
  196. static int8_t auto_report_port;
  197. #endif
  198. #endif
  199. };
  200. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  201. #define IS_SD_INSERTED() Sd2Card::isInserted()
  202. #elif PIN_EXISTS(SD_DETECT)
  203. #if ENABLED(SD_DETECT_INVERTED)
  204. #define IS_SD_INSERTED() READ(SD_DETECT_PIN)
  205. #else
  206. #define IS_SD_INSERTED() !READ(SD_DETECT_PIN)
  207. #endif
  208. #else
  209. // No card detect line? Assume the card is inserted.
  210. #define IS_SD_INSERTED() true
  211. #endif
  212. #define IS_SD_PRINTING() card.flag.sdprinting
  213. #define IS_SD_FILE_OPEN() card.isFileOpen()
  214. extern CardReader card;
  215. #else // !SDSUPPORT
  216. #define IS_SD_PRINTING() false
  217. #define IS_SD_FILE_OPEN() false
  218. #endif // !SDSUPPORT