cardreader.h 4.8 KB

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