cardreader.h 5.4 KB

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