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

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