cardreader.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "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 * 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. void printingHasFinished();
  50. void printFilename();
  51. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  52. void printLongPath(char *path);
  53. #endif
  54. void getfilename(uint16_t nr, const char* const match=NULL);
  55. uint16_t getnrfilenames();
  56. void getAbsFilename(char *t);
  57. void ls();
  58. void chdir(const char *relpath);
  59. int8_t updir();
  60. void setroot();
  61. const char* diveToFile(SdFile*& curDir, const char * const path, const bool echo);
  62. uint16_t get_num_Files();
  63. #if ENABLED(SDCARD_SORT_ALPHA)
  64. void presort();
  65. void getfilename_sorted(const uint16_t nr);
  66. #if ENABLED(SDSORT_GCODE)
  67. FORCE_INLINE void setSortOn(bool b) { sort_alpha = b; presort(); }
  68. FORCE_INLINE void setSortFolders(int i) { sort_folders = i; presort(); }
  69. //FORCE_INLINE void setSortReverse(bool b) { sort_reverse = b; }
  70. #endif
  71. #endif
  72. #if ENABLED(POWER_LOSS_RECOVERY)
  73. void openJobRecoveryFile(const bool read);
  74. void closeJobRecoveryFile();
  75. bool jobRecoverFileExists();
  76. int16_t saveJobRecoveryInfo();
  77. int16_t loadJobRecoveryInfo();
  78. void removeJobRecoveryFile();
  79. #endif
  80. FORCE_INLINE void pauseSDPrint() { sdprinting = false; }
  81. FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
  82. FORCE_INLINE bool eof() { return sdpos >= filesize; }
  83. FORCE_INLINE int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
  84. FORCE_INLINE void setIndex(const uint32_t index) { sdpos = index; file.seekSet(index); }
  85. FORCE_INLINE uint32_t getIndex() { return sdpos; }
  86. FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
  87. FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
  88. #if ENABLED(AUTO_REPORT_SD_STATUS)
  89. void auto_report_sd_status(void);
  90. FORCE_INLINE void set_auto_report_interval(uint8_t v) {
  91. NOMORE(v, 60);
  92. auto_report_sd_interval = v;
  93. next_sd_report_ms = millis() + 1000UL * v;
  94. }
  95. #endif
  96. FORCE_INLINE char* longest_filename() { return longFilename[0] ? longFilename : filename; }
  97. public:
  98. bool saving, logging, sdprinting, cardOK, filenameIsDir;
  99. char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
  100. int8_t autostart_index;
  101. private:
  102. SdFile root, workDir, workDirParents[MAX_DIR_DEPTH];
  103. uint8_t workDirDepth;
  104. // Sort files and folders alphabetically.
  105. #if ENABLED(SDCARD_SORT_ALPHA)
  106. uint16_t sort_count; // Count of sorted items in the current directory
  107. #if ENABLED(SDSORT_GCODE)
  108. bool sort_alpha; // Flag to enable / disable the feature
  109. int sort_folders; // Flag to enable / disable folder sorting
  110. //bool sort_reverse; // Flag to enable / disable reverse sorting
  111. #endif
  112. // By default the sort index is static
  113. #if ENABLED(SDSORT_DYNAMIC_RAM)
  114. uint8_t *sort_order;
  115. #else
  116. uint8_t sort_order[SDSORT_LIMIT];
  117. #endif
  118. #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
  119. #define SORTED_LONGNAME_MAXLEN ((SDSORT_CACHE_VFATS) * (FILENAME_LENGTH) + 1)
  120. #else
  121. #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH
  122. #endif
  123. // Cache filenames to speed up SD menus.
  124. #if ENABLED(SDSORT_USES_RAM)
  125. // If using dynamic ram for names, allocate on the heap.
  126. #if ENABLED(SDSORT_CACHE_NAMES)
  127. #if ENABLED(SDSORT_DYNAMIC_RAM)
  128. char **sortshort, **sortnames;
  129. #else
  130. char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
  131. char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
  132. #endif
  133. #elif DISABLED(SDSORT_USES_STACK)
  134. char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
  135. #endif
  136. // Folder sorting uses an isDir array when caching items.
  137. #if HAS_FOLDER_SORTING
  138. #if ENABLED(SDSORT_DYNAMIC_RAM)
  139. uint8_t *isDir;
  140. #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
  141. uint8_t isDir[(SDSORT_LIMIT+7)>>3];
  142. #endif
  143. #endif
  144. #endif // SDSORT_USES_RAM
  145. #endif // SDCARD_SORT_ALPHA
  146. Sd2Card sd2card;
  147. SdVolume volume;
  148. SdFile file;
  149. #if ENABLED(POWER_LOSS_RECOVERY)
  150. SdFile jobRecoveryFile;
  151. #endif
  152. #define SD_PROCEDURE_DEPTH 1
  153. #define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH + MAX_DIR_DEPTH + 1)
  154. uint8_t file_subcall_ctr;
  155. uint32_t filespos[SD_PROCEDURE_DEPTH];
  156. char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  157. uint32_t filesize, sdpos;
  158. LsAction lsAction; //stored for recursion.
  159. 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.
  160. char* diveDirName;
  161. void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
  162. #if ENABLED(SDCARD_SORT_ALPHA)
  163. void flush_presort();
  164. #endif
  165. #if ENABLED(AUTO_REPORT_SD_STATUS)
  166. static uint8_t auto_report_sd_interval;
  167. static millis_t next_sd_report_ms;
  168. #endif
  169. };
  170. #if PIN_EXISTS(SD_DETECT)
  171. #if ENABLED(SD_DETECT_INVERTED)
  172. #define IS_SD_INSERTED (READ(SD_DETECT_PIN) == HIGH)
  173. #else
  174. #define IS_SD_INSERTED (READ(SD_DETECT_PIN) == LOW)
  175. #endif
  176. #else
  177. // No card detect line? Assume the card is inserted.
  178. #define IS_SD_INSERTED true
  179. #endif
  180. extern CardReader card;
  181. #endif // SDSUPPORT
  182. #if ENABLED(SDSUPPORT)
  183. #define IS_SD_PRINTING (card.sdprinting)
  184. #define IS_SD_FILE_OPEN (card.isFileOpen())
  185. #else
  186. #define IS_SD_PRINTING (false)
  187. #define IS_SD_FILE_OPEN (false)
  188. #endif
  189. #endif // _CARDREADER_H_