cardreader.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef CARDREADER_H
  2. #define CARDREADER_H
  3. #define SDSUPPORT
  4. #ifdef SDSUPPORT
  5. #define MAX_DIR_DEPTH 10
  6. #include "SdFile.h"
  7. enum LsAction {LS_SerialPrint,LS_SerialPrint_LFN,LS_Count,LS_GetFilename};
  8. class CardReader
  9. {
  10. public:
  11. CardReader();
  12. void initsd();
  13. void write_command(char *buf);
  14. void write_command_no_newline(char *buf);
  15. //files auto[0-9].g on the sd card are performed in a row
  16. //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
  17. void checkautostart(bool x);
  18. void openFileWrite(const char* name);
  19. void openFileReadFilteredGcode(const char* name, bool replace_current = false);
  20. void openLogFile(const char* name);
  21. void removeFile(const char* name);
  22. void closefile(bool store_location=false);
  23. void release();
  24. void startFileprint();
  25. uint32_t getFileSize();
  26. void getStatus();
  27. void printingHasFinished();
  28. void getfilename(uint16_t nr, const char* const match=NULL);
  29. void getfilename_simple(uint32_t position, const char * const match = NULL);
  30. uint16_t getnrfilenames();
  31. void getAbsFilename(char *t);
  32. void getDirName(char* name, uint8_t level);
  33. uint16_t getWorkDirDepth();
  34. void ls(bool printLFN);
  35. void chdir(const char * relpath);
  36. void updir();
  37. void setroot();
  38. #ifdef SDCARD_SORT_ALPHA
  39. void presort();
  40. #ifdef SDSORT_QUICKSORT
  41. void swap(uint8_t left, uint8_t right);
  42. void quicksort(uint8_t left, uint8_t right);
  43. #endif //SDSORT_QUICKSORT
  44. void getfilename_sorted(const uint16_t nr);
  45. #if SDSORT_GCODE
  46. FORCE_INLINE void setSortOn(bool b) { sort_alpha = b; presort(); }
  47. FORCE_INLINE void setSortFolders(int i) { sort_folders = i; presort(); }
  48. //FORCE_INLINE void setSortReverse(bool b) { sort_reverse = b; }
  49. #endif
  50. #endif
  51. FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
  52. FORCE_INLINE bool eof() { return sdpos>=filesize ;};
  53. // FORCE_INLINE int16_t getX() { sdpos = file.curPosition();return (int16_t)file.read();};
  54. //@@TODO potential performance problem - when the comment reading fails, sdpos points to the last correctly read character.
  55. // However, repeated reading (e.g. after power panic) the comment will be read again - it should survive correctly, it will just take a few moments to skip
  56. FORCE_INLINE int16_t getFilteredGcodeChar() { sdpos = file.curPosition();return (int16_t)file.readFilteredGcode();};
  57. /*FORCE_INLINE*/ void setIndex(long index) {sdpos = index;file.seekSetFilteredGcode(index);};
  58. FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;};
  59. FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;};
  60. FORCE_INLINE uint32_t get_sdpos() { if (!isFileOpen()) return 0; else return(sdpos); };
  61. bool ToshibaFlashAir_isEnabled() const { return card.getFlashAirCompatible(); }
  62. void ToshibaFlashAir_enable(bool enable) { card.setFlashAirCompatible(enable); }
  63. bool ToshibaFlashAir_GetIP(uint8_t *ip);
  64. public:
  65. bool saving;
  66. bool logging;
  67. bool sdprinting ;
  68. bool cardOK ;
  69. char filename[13];
  70. // There are scenarios when simple modification time is not enough (on MS Windows)
  71. // Therefore these timestamps hold the most recent one of creation/modification date/times
  72. uint16_t crmodTime, crmodDate;
  73. uint32_t cluster, position;
  74. char longFilename[LONG_FILENAME_LENGTH];
  75. bool filenameIsDir;
  76. int lastnr; //last number of the autostart;
  77. private:
  78. SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
  79. uint16_t workDirDepth;
  80. // Sort files and folders alphabetically.
  81. #ifdef SDCARD_SORT_ALPHA
  82. uint16_t sort_count; // Count of sorted items in the current directory
  83. #if SDSORT_GCODE
  84. bool sort_alpha; // Flag to enable / disable the feature
  85. int sort_folders; // Flag to enable / disable folder sorting
  86. //bool sort_reverse; // Flag to enable / disable reverse sorting
  87. #endif
  88. // By default the sort index is static
  89. #if SDSORT_DYNAMIC_RAM
  90. uint8_t *sort_order;
  91. #else
  92. uint8_t sort_order[SDSORT_LIMIT];
  93. #endif
  94. // Cache filenames to speed up SD menus.
  95. #if SDSORT_USES_RAM
  96. // If using dynamic ram for names, allocate on the heap.
  97. #if SDSORT_CACHE_NAMES
  98. #if SDSORT_DYNAMIC_RAM
  99. char **sortshort, **sortnames;
  100. #else
  101. char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
  102. char sortnames[SDSORT_LIMIT][FILENAME_LENGTH];
  103. #endif
  104. #elif !SDSORT_USES_STACK
  105. char sortnames[SDSORT_LIMIT][FILENAME_LENGTH];
  106. uint16_t modification_time[SDSORT_LIMIT];
  107. uint16_t modification_date[SDSORT_LIMIT];
  108. #endif
  109. // Folder sorting uses an isDir array when caching items.
  110. #if HAS_FOLDER_SORTING
  111. #if SDSORT_DYNAMIC_RAM
  112. uint8_t *isDir;
  113. #elif (SDSORT_CACHE_NAMES) || !(SDSORT_USES_STACK)
  114. uint8_t isDir[(SDSORT_LIMIT + 7) >> 3];
  115. #endif
  116. #endif
  117. #endif // SDSORT_USES_RAM
  118. #endif // SDCARD_SORT_ALPHA
  119. #ifdef DEBUG_SD_SPEED_TEST
  120. public:
  121. #endif //DEBUG_SD_SPEED_TEST
  122. Sd2Card card;
  123. private:
  124. SdVolume volume;
  125. SdFile file;
  126. #define SD_PROCEDURE_DEPTH 1
  127. #define MAXPATHNAMELENGTH (13*MAX_DIR_DEPTH+MAX_DIR_DEPTH+1)
  128. uint8_t file_subcall_ctr;
  129. uint32_t filespos[SD_PROCEDURE_DEPTH];
  130. char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  131. uint32_t filesize;
  132. //int16_t n;
  133. unsigned long autostart_atmillis;
  134. uint32_t sdpos ;
  135. bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
  136. LsAction lsAction; //stored for recursion.
  137. int16_t nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory.
  138. char* diveDirName;
  139. void diveSubfolder (const char *fileName, SdFile& dir);
  140. void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
  141. #ifdef SDCARD_SORT_ALPHA
  142. void flush_presort();
  143. #endif
  144. };
  145. extern bool Stopped;
  146. extern CardReader card;
  147. #define IS_SD_PRINTING (card.sdprinting)
  148. #if (SDCARDDETECT > -1)
  149. # ifdef SDCARDDETECTINVERTED
  150. # define IS_SD_INSERTED (READ(SDCARDDETECT)!=0)
  151. # else
  152. # define IS_SD_INSERTED (READ(SDCARDDETECT)==0)
  153. # endif //SDCARDTETECTINVERTED
  154. #else
  155. //If we don't have a card detect line, aways asume the card is inserted
  156. # define IS_SD_INSERTED true
  157. #endif
  158. #else
  159. #define IS_SD_PRINTING (false)
  160. #endif //SDSUPPORT
  161. #endif