cardreader.h 5.9 KB

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