cardreader.h 4.8 KB

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