cardreader.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 MAX_DIR_DEPTH 10 // Maximum folder depth
  27. #include "SdFile.h"
  28. #include "types.h"
  29. #include "enum.h"
  30. class CardReader {
  31. public:
  32. CardReader();
  33. void initsd();
  34. void write_command(char *buf);
  35. //files auto[0-9].g on the sd card are performed in a row
  36. //this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset
  37. void checkautostart(bool x);
  38. void openFile(char* name, bool read, bool push_current=false);
  39. void openLogFile(char* name);
  40. #if ENABLED(POWEROFF_SAVE_SD_FILE)
  41. void openPowerOffFile(char* name, uint8_t oflag);
  42. void closePowerOffFile();
  43. bool existPowerOffFile(char* name);
  44. int16_t savePowerOffInfo(const void* data, uint16_t size);
  45. int16_t getPowerOffInfo(void* data, uint16_t size);
  46. void removePowerOffFile();
  47. #endif
  48. void removeFile(char* name);
  49. void closefile(bool store_location=false);
  50. void release();
  51. void openAndPrintFile(const char *name);
  52. void startFileprint();
  53. void stopSDPrint();
  54. void getStatus();
  55. void printingHasFinished();
  56. #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
  57. void printLongPath(char *path);
  58. #endif
  59. void getfilename(uint16_t nr, const char* const match=NULL);
  60. uint16_t getnrfilenames();
  61. void getAbsFilename(char *t);
  62. void ls();
  63. void chdir(const char *relpath);
  64. void updir();
  65. void setroot();
  66. #if ENABLED(SDCARD_SORT_ALPHA)
  67. void presort();
  68. void getfilename_sorted(const uint16_t nr);
  69. #if ENABLED(SDSORT_GCODE)
  70. FORCE_INLINE void setSortOn(bool b) { sort_alpha = b; presort(); }
  71. FORCE_INLINE void setSortFolders(int i) { sort_folders = i; presort(); }
  72. //FORCE_INLINE void setSortReverse(bool b) { sort_reverse = b; }
  73. #endif
  74. #endif
  75. FORCE_INLINE void pauseSDPrint() { sdprinting = false; }
  76. FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
  77. FORCE_INLINE bool eof() { return sdpos >= filesize; }
  78. FORCE_INLINE int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
  79. FORCE_INLINE void setIndex(long index) { sdpos = index; file.seekSet(index); }
  80. FORCE_INLINE uint32_t getIndex() { return sdpos; }
  81. // FORCE_INLINE char* getCurrentPrintFileName() { return filenames[file_subcall_ctr]; }
  82. FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
  83. FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
  84. public:
  85. bool saving, logging, sdprinting, cardOK, filenameIsDir;
  86. char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
  87. int autostart_index;
  88. private:
  89. SdFile root, *curDir, workDir, workDirParents[MAX_DIR_DEPTH];
  90. uint8_t workDirDepth;
  91. // Sort files and folders alphabetically.
  92. #if ENABLED(SDCARD_SORT_ALPHA)
  93. uint16_t sort_count; // Count of sorted items in the current directory
  94. #if ENABLED(SDSORT_GCODE)
  95. bool sort_alpha; // Flag to enable / disable the feature
  96. int sort_folders; // Flag to enable / disable folder sorting
  97. //bool sort_reverse; // Flag to enable / disable reverse sorting
  98. #endif
  99. // By default the sort index is static
  100. #if ENABLED(SDSORT_DYNAMIC_RAM)
  101. uint8_t *sort_order;
  102. #else
  103. uint8_t sort_order[SDSORT_LIMIT];
  104. #endif
  105. // Cache filenames to speed up SD menus.
  106. #if ENABLED(SDSORT_USES_RAM)
  107. // If using dynamic ram for names, allocate on the heap.
  108. #if ENABLED(SDSORT_CACHE_NAMES)
  109. #if ENABLED(SDSORT_DYNAMIC_RAM)
  110. char **sortshort, **sortnames;
  111. #else
  112. char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
  113. char sortnames[SDSORT_LIMIT][LONG_FILENAME_LENGTH];
  114. #endif
  115. #elif DISABLED(SDSORT_USES_STACK)
  116. char sortnames[SDSORT_LIMIT][LONG_FILENAME_LENGTH];
  117. #endif
  118. // Folder sorting uses an isDir array when caching items.
  119. #if HAS_FOLDER_SORTING
  120. #if ENABLED(SDSORT_DYNAMIC_RAM)
  121. uint8_t *isDir;
  122. #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
  123. uint8_t isDir[(SDSORT_LIMIT+7)>>3];
  124. #endif
  125. #endif
  126. #endif // SDSORT_USES_RAM
  127. #endif // SDCARD_SORT_ALPHA
  128. Sd2Card card;
  129. SdVolume volume;
  130. SdFile file;
  131. #if ENABLED(POWEROFF_SAVE_SD_FILE)
  132. SdFile powerOffFile;
  133. #endif
  134. #define SD_PROCEDURE_DEPTH 1
  135. #define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH + MAX_DIR_DEPTH + 1)
  136. uint8_t file_subcall_ctr;
  137. uint32_t filespos[SD_PROCEDURE_DEPTH];
  138. char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  139. uint32_t filesize;
  140. uint32_t sdpos;
  141. millis_t next_autostart_ms;
  142. bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
  143. LsAction lsAction; //stored for recursion.
  144. 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.
  145. char* diveDirName;
  146. void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
  147. #if ENABLED(SDCARD_SORT_ALPHA)
  148. void flush_presort();
  149. #endif
  150. };
  151. extern CardReader card;
  152. #if ENABLED(POWEROFF_SAVE_SD_FILE)
  153. struct power_off_info_t
  154. {
  155. /* header (1B + 7B = 8B) */
  156. uint8_t valid_head;
  157. // uint8_t reserved1[8-1];
  158. /* Gcode related information. (44B + 20B = 64B) */
  159. float current_position[NUM_AXIS];
  160. float feedrate;
  161. float saved_z;
  162. int target_temperature[HOTENDS];
  163. int target_temperature_bed;
  164. // uint8_t reserved2[64-44];
  165. /* print queue related information. (396B + 116B = 512B) */
  166. int cmd_queue_index_r;
  167. int cmd_queue_index_w;
  168. int commands_in_queue;
  169. char command_queue[BUFSIZE][MAX_CMD_SIZE];
  170. // uint8_t reserved3[512-396];
  171. /* SD card related information. (165B + 91B = 256B)*/
  172. uint32_t sdpos;
  173. millis_t print_job_start_ms;
  174. char sd_filename[MAXPATHNAMELENGTH];
  175. char power_off_filename[16];
  176. // uint8_t reserved4[256-166];
  177. uint8_t valid_foot;
  178. };
  179. extern struct power_off_info_t power_off_info;
  180. extern int power_off_commands_count;
  181. extern int power_off_type_yes;
  182. #endif
  183. #define IS_SD_PRINTING (card.sdprinting)
  184. #define IS_SD_FILE_OPEN (card.isFileOpen())
  185. #if PIN_EXISTS(SD_DETECT)
  186. #if ENABLED(SD_DETECT_INVERTED)
  187. #define IS_SD_INSERTED (READ(SD_DETECT_PIN) != 0)
  188. #else
  189. #define IS_SD_INSERTED (READ(SD_DETECT_PIN) == 0)
  190. #endif
  191. #else
  192. //No card detect line? Assume the card is inserted.
  193. #define IS_SD_INSERTED true
  194. #endif
  195. #else
  196. #define IS_SD_PRINTING (false)
  197. #define IS_SD_FILE_OPEN (false)
  198. #endif // SDSUPPORT
  199. #endif // __CARDREADER_H