cardreader.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifdef SDSORT_QUICKSORT
  55. void swap(uint8_t left, uint8_t right);
  56. void quicksort(uint8_t left, uint8_t right);
  57. #endif //SDSORT_QUICKSORT
  58. void getfilename_sorted(const uint16_t nr, uint8_t sdSort);
  59. #endif
  60. FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
  61. bool eof() { return sdpos>=filesize; }
  62. FORCE_INLINE int16_t getFilteredGcodeChar()
  63. {
  64. int16_t c = (int16_t)file.readFilteredGcode();
  65. sdpos = file.curPosition();
  66. return c;
  67. };
  68. void setIndex(long index) {sdpos = index;file.seekSetFilteredGcode(index);};
  69. FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;};
  70. FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;};
  71. FORCE_INLINE uint32_t get_sdpos() { if (!isFileOpen()) return 0; else return(sdpos); };
  72. bool ToshibaFlashAir_isEnabled() const { return card.getFlashAirCompatible(); }
  73. void ToshibaFlashAir_enable(bool enable) { card.setFlashAirCompatible(enable); }
  74. bool ToshibaFlashAir_GetIP(uint8_t *ip);
  75. public:
  76. bool saving;
  77. bool logging;
  78. bool sdprinting ;
  79. bool cardOK ;
  80. char filename[13];
  81. // There are scenarios when simple modification time is not enough (on MS Windows)
  82. // Therefore these timestamps hold the most recent one of creation/modification date/times
  83. uint16_t crmodTime, crmodDate;
  84. uint32_t /* cluster, */ position;
  85. char longFilename[LONG_FILENAME_LENGTH];
  86. bool filenameIsDir;
  87. int lastnr; //last number of the autostart;
  88. #ifdef SDCARD_SORT_ALPHA
  89. bool presort_flag;
  90. char dir_names[MAX_DIR_DEPTH][9];
  91. #endif // SDCARD_SORT_ALPHA
  92. private:
  93. SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
  94. uint16_t workDirDepth;
  95. // Sort files and folders alphabetically.
  96. #ifdef SDCARD_SORT_ALPHA
  97. uint16_t sort_count; // Count of sorted items in the current directory
  98. uint16_t sort_entries[SDSORT_LIMIT];
  99. #endif // SDCARD_SORT_ALPHA
  100. #ifdef DEBUG_SD_SPEED_TEST
  101. public:
  102. #endif //DEBUG_SD_SPEED_TEST
  103. Sd2Card card;
  104. private:
  105. SdVolume volume;
  106. SdFile file;
  107. #define SD_PROCEDURE_DEPTH 1
  108. #define MAXPATHNAMELENGTH (13*MAX_DIR_DEPTH+MAX_DIR_DEPTH+1)
  109. uint8_t file_subcall_ctr;
  110. uint32_t filespos[SD_PROCEDURE_DEPTH];
  111. char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  112. uint32_t filesize;
  113. //int16_t n;
  114. ShortTimer autostart_atmillis;
  115. uint32_t sdpos ;
  116. 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.
  117. bool diveSubfolder (const char *&fileName);
  118. void lsDive(const char *prepend, SdFile parent, const char * const match=NULL, LsAction lsAction = LS_GetFilename, ls_param lsParams = ls_param());
  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