cardreader.h 4.8 KB

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