cardreader.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef CARDREADER_H
  2. #define CARDREADER_H
  3. #define SDSUPPORT
  4. #ifdef SDSUPPORT
  5. #define MAX_DIR_DEPTH 6
  6. #include "SdFile.h"
  7. class CardReader
  8. {
  9. public:
  10. CardReader();
  11. enum LsAction : uint8_t
  12. {
  13. LS_SerialPrint,
  14. LS_Count,
  15. LS_GetFilename,
  16. };
  17. struct ls_param
  18. {
  19. bool LFN : 1;
  20. bool timestamp : 1;
  21. } __attribute__((packed));
  22. void initsd();
  23. void write_command(char *buf);
  24. void write_command_no_newline(char *buf);
  25. //files auto[0-9].g on the sd card are performed in a row
  26. //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
  27. void checkautostart(bool x);
  28. void openFileWrite(const char* name);
  29. void openFileReadFilteredGcode(const char* name, bool replace_current = false);
  30. void openLogFile(const char* name);
  31. void removeFile(const char* name);
  32. void closefile(bool store_location=false);
  33. void release();
  34. void startFileprint();
  35. uint32_t getFileSize();
  36. void getStatus(bool arg_P);
  37. void printingHasFinished();
  38. void getfilename(uint16_t nr, const char* const match=NULL);
  39. void getfilename_simple(uint32_t position, const char * const match = NULL);
  40. void getfilename_next(uint32_t position, const char * const match = NULL);
  41. uint16_t getnrfilenames();
  42. void getAbsFilename(char *t);
  43. void printAbsFilenameFast();
  44. void getDirName(char* name, uint8_t level);
  45. uint16_t getWorkDirDepth();
  46. void ls(struct ls_param params);
  47. bool chdir(const char * relpath, bool doPresort);
  48. void updir();
  49. void setroot(bool doPresort);
  50. #ifdef SDCARD_SORT_ALPHA
  51. void presort();
  52. #ifdef SDSORT_QUICKSORT
  53. void swap(uint8_t left, uint8_t right);
  54. void quicksort(uint8_t left, uint8_t right);
  55. #endif //SDSORT_QUICKSORT
  56. void getfilename_sorted(const uint16_t nr, uint8_t sdSort);
  57. #endif
  58. FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
  59. bool eof() { return sdpos>=filesize; }
  60. FORCE_INLINE int16_t getFilteredGcodeChar()
  61. {
  62. int16_t c = (int16_t)file.readFilteredGcode();
  63. sdpos = file.curPosition();
  64. return c;
  65. };
  66. void setIndex(long index) {sdpos = index;file.seekSetFilteredGcode(index);};
  67. FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;};
  68. FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;};
  69. FORCE_INLINE uint32_t get_sdpos() { if (!isFileOpen()) return 0; else return(sdpos); };
  70. bool ToshibaFlashAir_isEnabled() const { return card.getFlashAirCompatible(); }
  71. void ToshibaFlashAir_enable(bool enable) { card.setFlashAirCompatible(enable); }
  72. bool ToshibaFlashAir_GetIP(uint8_t *ip);
  73. public:
  74. bool saving;
  75. bool logging;
  76. bool sdprinting ;
  77. bool cardOK ;
  78. char filename[13];
  79. // There are scenarios when simple modification time is not enough (on MS Windows)
  80. // Therefore these timestamps hold the most recent one of creation/modification date/times
  81. uint16_t crmodTime, crmodDate;
  82. uint32_t /* cluster, */ position;
  83. char longFilename[LONG_FILENAME_LENGTH];
  84. bool filenameIsDir;
  85. int lastnr; //last number of the autostart;
  86. #ifdef SDCARD_SORT_ALPHA
  87. bool presort_flag;
  88. char dir_names[MAX_DIR_DEPTH][9];
  89. #endif // SDCARD_SORT_ALPHA
  90. private:
  91. SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
  92. uint16_t workDirDepth;
  93. // Sort files and folders alphabetically.
  94. #ifdef SDCARD_SORT_ALPHA
  95. uint16_t sort_count; // Count of sorted items in the current directory
  96. uint32_t sort_positions[SDSORT_LIMIT];
  97. #endif // SDCARD_SORT_ALPHA
  98. #ifdef DEBUG_SD_SPEED_TEST
  99. public:
  100. #endif //DEBUG_SD_SPEED_TEST
  101. Sd2Card card;
  102. private:
  103. SdVolume volume;
  104. SdFile file;
  105. #define SD_PROCEDURE_DEPTH 1
  106. #define MAXPATHNAMELENGTH (13*MAX_DIR_DEPTH+MAX_DIR_DEPTH+1)
  107. uint8_t file_subcall_ctr;
  108. uint32_t filespos[SD_PROCEDURE_DEPTH];
  109. char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  110. uint32_t filesize;
  111. //int16_t n;
  112. unsigned long autostart_atmillis;
  113. uint32_t sdpos ;
  114. bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
  115. 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.
  116. char* diveDirName;
  117. bool diveSubfolder (const char *&fileName);
  118. void lsDive(const char *prepend, SdFile parent, const char * const match=NULL, LsAction lsAction = LS_GetFilename, struct ls_param *lsParams = NULL);
  119. #ifdef SDCARD_SORT_ALPHA
  120. void flush_presort();
  121. #endif
  122. };
  123. extern bool Stopped;
  124. extern CardReader card;
  125. #define IS_SD_PRINTING (card.sdprinting)
  126. #if (SDCARDDETECT > -1)
  127. # ifdef SDCARDDETECTINVERTED
  128. # define IS_SD_INSERTED (READ(SDCARDDETECT)!=0)
  129. # else
  130. # define IS_SD_INSERTED (READ(SDCARDDETECT)==0)
  131. # endif //SDCARDTETECTINVERTED
  132. #else
  133. //If we don't have a card detect line, aways asume the card is inserted
  134. # define IS_SD_INSERTED true
  135. #endif
  136. #else
  137. #define IS_SD_PRINTING (false)
  138. #endif //SDSUPPORT
  139. #endif