My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

cardreader.h 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #ifndef _CARDREADER_H_
  23. #define _CARDREADER_H_
  24. #include "../inc/MarlinConfig.h"
  25. #if ENABLED(SDSUPPORT)
  26. #define SD_RESORT ENABLED(SDCARD_SORT_ALPHA) && ENABLED(SDSORT_DYNAMIC_RAM)
  27. #define MAX_DIR_DEPTH 10 // Maximum folder depth
  28. #include "SdFile.h"
  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* name, const bool read, const bool subcall=false);
  37. void openLogFile(char* name);
  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. uint16_t get_num_Files();
  78. #if ENABLED(SDCARD_SORT_ALPHA)
  79. void presort();
  80. void getfilename_sorted(const uint16_t nr);
  81. #if ENABLED(SDSORT_GCODE)
  82. FORCE_INLINE void setSortOn(bool b) { sort_alpha = b; presort(); }
  83. FORCE_INLINE void setSortFolders(int i) { sort_folders = i; presort(); }
  84. //FORCE_INLINE void setSortReverse(bool b) { sort_reverse = b; }
  85. #endif
  86. #endif
  87. #if ENABLED(POWER_LOSS_RECOVERY)
  88. void openJobRecoveryFile(const bool read);
  89. void closeJobRecoveryFile();
  90. bool jobRecoverFileExists();
  91. int16_t saveJobRecoveryInfo();
  92. int16_t loadJobRecoveryInfo();
  93. void removeJobRecoveryFile();
  94. #endif
  95. FORCE_INLINE void pauseSDPrint() { sdprinting = false; }
  96. FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
  97. FORCE_INLINE bool eof() { return sdpos >= filesize; }
  98. FORCE_INLINE int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
  99. FORCE_INLINE void setIndex(const uint32_t index) { sdpos = index; file.seekSet(index); }
  100. FORCE_INLINE uint32_t getIndex() { return sdpos; }
  101. FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
  102. FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
  103. Sd2Card& getSd2Card() { return card; }
  104. #if ENABLED(AUTO_REPORT_SD_STATUS)
  105. void auto_report_sd_status(void);
  106. FORCE_INLINE void set_auto_report_interval(uint8_t v
  107. #if NUM_SERIAL > 1
  108. , int8_t port
  109. #endif
  110. ) {
  111. #if NUM_SERIAL > 1
  112. serialport = port;
  113. #endif
  114. NOMORE(v, 60);
  115. auto_report_sd_interval = v;
  116. next_sd_report_ms = millis() + 1000UL * v;
  117. }
  118. #endif
  119. public:
  120. bool saving, logging, sdprinting, cardOK, filenameIsDir;
  121. char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
  122. int autostart_index;
  123. private:
  124. SdFile root, *curDir, workDir, workDirParents[MAX_DIR_DEPTH];
  125. uint8_t workDirDepth;
  126. // Sort files and folders alphabetically.
  127. #if ENABLED(SDCARD_SORT_ALPHA)
  128. uint16_t sort_count; // Count of sorted items in the current directory
  129. #if ENABLED(SDSORT_GCODE)
  130. bool sort_alpha; // Flag to enable / disable the feature
  131. int sort_folders; // Flag to enable / disable folder sorting
  132. //bool sort_reverse; // Flag to enable / disable reverse sorting
  133. #endif
  134. // By default the sort index is static
  135. #if ENABLED(SDSORT_DYNAMIC_RAM)
  136. uint8_t *sort_order;
  137. #else
  138. uint8_t sort_order[SDSORT_LIMIT];
  139. #endif
  140. #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
  141. #define SORTED_LONGNAME_MAXLEN ((SDSORT_CACHE_VFATS) * (FILENAME_LENGTH) + 1)
  142. #else
  143. #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH
  144. #endif
  145. // Cache filenames to speed up SD menus.
  146. #if ENABLED(SDSORT_USES_RAM)
  147. // If using dynamic ram for names, allocate on the heap.
  148. #if ENABLED(SDSORT_CACHE_NAMES)
  149. #if ENABLED(SDSORT_DYNAMIC_RAM)
  150. char **sortshort, **sortnames;
  151. #else
  152. char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
  153. char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
  154. #endif
  155. #elif DISABLED(SDSORT_USES_STACK)
  156. char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
  157. #endif
  158. // Folder sorting uses an isDir array when caching items.
  159. #if HAS_FOLDER_SORTING
  160. #if ENABLED(SDSORT_DYNAMIC_RAM)
  161. uint8_t *isDir;
  162. #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
  163. uint8_t isDir[(SDSORT_LIMIT+7)>>3];
  164. #endif
  165. #endif
  166. #endif // SDSORT_USES_RAM
  167. #endif // SDCARD_SORT_ALPHA
  168. Sd2Card card;
  169. SdVolume volume;
  170. SdFile file;
  171. #if ENABLED(POWER_LOSS_RECOVERY)
  172. SdFile jobRecoveryFile;
  173. #endif
  174. #define SD_PROCEDURE_DEPTH 1
  175. #define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH + MAX_DIR_DEPTH + 1)
  176. uint8_t file_subcall_ctr;
  177. uint32_t filespos[SD_PROCEDURE_DEPTH];
  178. char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  179. uint32_t filesize, sdpos;
  180. LsAction lsAction; //stored for recursion.
  181. 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.
  182. char* diveDirName;
  183. void lsDive(const char *prepend, SdFile parent, const char * const match=NULL
  184. #if NUM_SERIAL > 1
  185. , const int8_t port = -1
  186. #endif
  187. );
  188. #if ENABLED(SDCARD_SORT_ALPHA)
  189. void flush_presort();
  190. #endif
  191. #if ENABLED(AUTO_REPORT_SD_STATUS)
  192. static uint8_t auto_report_sd_interval;
  193. static millis_t next_sd_report_ms;
  194. #if NUM_SERIAL > 1
  195. static int8_t serialport;
  196. #endif
  197. #endif
  198. };
  199. #if PIN_EXISTS(SD_DETECT)
  200. #if ENABLED(SD_DETECT_INVERTED)
  201. #define IS_SD_INSERTED (READ(SD_DETECT_PIN) == HIGH)
  202. #else
  203. #define IS_SD_INSERTED (READ(SD_DETECT_PIN) == LOW)
  204. #endif
  205. #else
  206. // No card detect line? Assume the card is inserted.
  207. #define IS_SD_INSERTED true
  208. #endif
  209. extern CardReader card;
  210. #endif // SDSUPPORT
  211. #if ENABLED(SDSUPPORT)
  212. #define IS_SD_PRINTING (card.sdprinting)
  213. #define IS_SD_FILE_OPEN (card.isFileOpen())
  214. #else
  215. #define IS_SD_PRINTING (false)
  216. #define IS_SD_FILE_OPEN (false)
  217. #endif
  218. #endif // _CARDREADER_H_