cardreader.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef CARDREADER_H
  2. #define CARDREADER_H
  3. #ifdef SDSUPPORT
  4. #define MAX_DIR_DEPTH 10
  5. #include "SdFile.h"
  6. enum LsAction {LS_SerialPrint,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(char* name,bool read,bool replace_current=true);
  18. void openLogFile(char* name);
  19. void removeFile(char* name);
  20. void closefile(bool store_location=false);
  21. void release();
  22. void startFileprint();
  23. void pauseSDPrint();
  24. uint32_t getFileSize();
  25. void getStatus();
  26. void printingHasFinished();
  27. void getfilename(uint16_t nr, const char* const match=NULL);
  28. uint16_t getnrfilenames();
  29. void getAbsFilename(char *t);
  30. void getDirName(char* name, uint8_t level);
  31. uint16_t getWorkDirDepth();
  32. void ls();
  33. void chdir(const char * relpath);
  34. void updir();
  35. void setroot();
  36. FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
  37. FORCE_INLINE bool eof() { return sdpos>=filesize ;};
  38. FORCE_INLINE int16_t get() { sdpos = file.curPosition();return (int16_t)file.read();};
  39. FORCE_INLINE void setIndex(long index) {sdpos = index;file.seekSet(index);};
  40. FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;};
  41. FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;};
  42. FORCE_INLINE uint32_t get_sdpos() { if (!isFileOpen()) return 0; else return(sdpos); };
  43. bool ToshibaFlashAir_isEnabled() const { return card.getFlashAirCompatible(); }
  44. void ToshibaFlashAir_enable(bool enable) { card.setFlashAirCompatible(enable); }
  45. bool ToshibaFlashAir_GetIP(uint8_t *ip);
  46. public:
  47. bool saving;
  48. bool logging;
  49. bool sdprinting ;
  50. bool cardOK ;
  51. char filename[13];
  52. char longFilename[LONG_FILENAME_LENGTH];
  53. bool filenameIsDir;
  54. int lastnr; //last number of the autostart;
  55. private:
  56. SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
  57. uint16_t workDirDepth;
  58. Sd2Card card;
  59. SdVolume volume;
  60. SdFile file;
  61. #define SD_PROCEDURE_DEPTH 1
  62. #define MAXPATHNAMELENGTH (13*MAX_DIR_DEPTH+MAX_DIR_DEPTH+1)
  63. uint8_t file_subcall_ctr;
  64. uint32_t filespos[SD_PROCEDURE_DEPTH];
  65. char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  66. uint32_t filesize;
  67. //int16_t n;
  68. unsigned long autostart_atmillis;
  69. uint32_t sdpos ;
  70. bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
  71. LsAction lsAction; //stored for recursion.
  72. 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.
  73. char* diveDirName;
  74. void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
  75. };
  76. extern CardReader card;
  77. #define IS_SD_PRINTING (card.sdprinting)
  78. #if (SDCARDDETECT > -1)
  79. # ifdef SDCARDDETECTINVERTED
  80. # define IS_SD_INSERTED (READ(SDCARDDETECT)!=0)
  81. # else
  82. # define IS_SD_INSERTED (READ(SDCARDDETECT)==0)
  83. # endif //SDCARDTETECTINVERTED
  84. #else
  85. //If we don't have a card detect line, aways asume the card is inserted
  86. # define IS_SD_INSERTED true
  87. #endif
  88. #else
  89. #define IS_SD_PRINTING (false)
  90. #endif //SDSUPPORT
  91. #endif