My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cardreader.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 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. 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(BINARY_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=nullptr);
  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=false);
  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(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. static SdFile getroot() { return root; }
  124. #if ENABLED(BINARY_FILE_TRANSFER)
  125. #if NUM_SERIAL > 1
  126. static int8_t transfer_port_index;
  127. #else
  128. static constexpr int8_t transfer_port_index = 0;
  129. #endif
  130. #endif
  131. private:
  132. static SdFile root, workDir, workDirParents[MAX_DIR_DEPTH];
  133. static uint8_t workDirDepth;
  134. // Sort files and folders alphabetically.
  135. #if ENABLED(SDCARD_SORT_ALPHA)
  136. static uint16_t sort_count; // Count of sorted items in the current directory
  137. #if ENABLED(SDSORT_GCODE)
  138. static bool sort_alpha; // Flag to enable / disable the feature
  139. static int sort_folders; // Folder sorting before/none/after
  140. //static bool sort_reverse; // Flag to enable / disable reverse sorting
  141. #endif
  142. // By default the sort index is static
  143. #if ENABLED(SDSORT_DYNAMIC_RAM)
  144. static uint8_t *sort_order;
  145. #else
  146. static uint8_t sort_order[SDSORT_LIMIT];
  147. #endif
  148. #if BOTH(SDSORT_USES_RAM, SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
  149. #define SORTED_LONGNAME_MAXLEN (SDSORT_CACHE_VFATS) * (FILENAME_LENGTH)
  150. #define SORTED_LONGNAME_STORAGE (SORTED_LONGNAME_MAXLEN + 1)
  151. #else
  152. #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH
  153. #define SORTED_LONGNAME_STORAGE SORTED_LONGNAME_MAXLEN
  154. #endif
  155. // Cache filenames to speed up SD menus.
  156. #if ENABLED(SDSORT_USES_RAM)
  157. // If using dynamic ram for names, allocate on the heap.
  158. #if ENABLED(SDSORT_CACHE_NAMES)
  159. #if ENABLED(SDSORT_DYNAMIC_RAM)
  160. static char **sortshort, **sortnames;
  161. #else
  162. static char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
  163. #endif
  164. #endif
  165. #if (ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)) || NONE(SDSORT_CACHE_NAMES, SDSORT_USES_STACK)
  166. static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
  167. #endif
  168. // Folder sorting uses an isDir array when caching items.
  169. #if HAS_FOLDER_SORTING
  170. #if ENABLED(SDSORT_DYNAMIC_RAM)
  171. static uint8_t *isDir;
  172. #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
  173. static uint8_t isDir[(SDSORT_LIMIT+7)>>3];
  174. #endif
  175. #endif
  176. #endif // SDSORT_USES_RAM
  177. #endif // SDCARD_SORT_ALPHA
  178. static Sd2Card sd2card;
  179. static SdVolume volume;
  180. static SdFile file;
  181. #ifndef SD_PROCEDURE_DEPTH
  182. #define SD_PROCEDURE_DEPTH 1
  183. #endif
  184. static uint8_t file_subcall_ctr;
  185. static uint32_t filespos[SD_PROCEDURE_DEPTH];
  186. static char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  187. static uint32_t filesize, sdpos;
  188. static LsAction lsAction; //stored for recursion.
  189. 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.
  190. static char *diveDirName;
  191. static void lsDive(const char *prepend, SdFile parent, const char * const match=nullptr);
  192. #if ENABLED(SDCARD_SORT_ALPHA)
  193. static void flush_presort();
  194. #endif
  195. #if ENABLED(AUTO_REPORT_SD_STATUS)
  196. static uint8_t auto_report_sd_interval;
  197. static millis_t next_sd_report_ms;
  198. #if NUM_SERIAL > 1
  199. static int8_t auto_report_port;
  200. #endif
  201. #endif
  202. };
  203. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  204. #define IS_SD_INSERTED() Sd2Card::isInserted()
  205. #elif PIN_EXISTS(SD_DETECT)
  206. #if ENABLED(SD_DETECT_INVERTED)
  207. #define IS_SD_INSERTED() READ(SD_DETECT_PIN)
  208. #else
  209. #define IS_SD_INSERTED() !READ(SD_DETECT_PIN)
  210. #endif
  211. #else
  212. // No card detect line? Assume the card is inserted.
  213. #define IS_SD_INSERTED() true
  214. #endif
  215. #define IS_SD_PRINTING() card.flag.sdprinting
  216. #define IS_SD_FILE_OPEN() card.isFileOpen()
  217. extern CardReader card;
  218. #else // !SDSUPPORT
  219. #define IS_SD_PRINTING() false
  220. #define IS_SD_FILE_OPEN() false
  221. #endif // !SDSUPPORT