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.2KB

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