cardreader.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. //files auto[0-9].g on the sd card are performed in a row
  14. //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
  15. void checkautostart(bool x);
  16. void openFile(char* name,bool read,bool replace_current=true);
  17. void openLogFile(char* name);
  18. #if defined(SDSUPPORT) && defined(POWEROFF_SAVE_SD_FILE)
  19. void openPowerOffFile(char* name, uint8_t oflag);
  20. void closePowerOffFile();
  21. bool existPowerOffFile(char* name);
  22. int16_t savePowerOffInfo(const void* data, uint16_t size);
  23. int16_t getPowerOffInfo(void* data, uint16_t size);
  24. void removePowerOffFile();
  25. #endif
  26. void removeFile(char* name);
  27. void closefile(bool store_location=false);
  28. void release();
  29. void startFileprint();
  30. void pauseSDPrint();
  31. void getStatus();
  32. void printingHasFinished();
  33. void getfilename(const uint8_t nr);
  34. uint16_t getnrfilenames();
  35. void getAbsFilename(char *t);
  36. void ls();
  37. void chdir(const char * relpath);
  38. void updir();
  39. void setroot();
  40. FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
  41. FORCE_INLINE bool eof() { return sdpos>=filesize ;};
  42. FORCE_INLINE int16_t get() { sdpos = file.curPosition();return (int16_t)file.read();};
  43. FORCE_INLINE void setIndex(long index) {sdpos = index;file.seekSet(index);};
  44. FORCE_INLINE uint32_t getIndex() { return sdpos; }
  45. FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;};
  46. FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;};
  47. public:
  48. bool saving;
  49. bool logging;
  50. bool sdprinting ;
  51. bool cardOK ;
  52. char filename[13];
  53. char longFilename[LONG_FILENAME_LENGTH];
  54. bool filenameIsDir;
  55. int lastnr; //last number of the autostart;
  56. private:
  57. SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
  58. uint16_t workDirDepth;
  59. Sd2Card card;
  60. SdVolume volume;
  61. SdFile file;
  62. #if defined(SDSUPPORT) && defined(POWEROFF_SAVE_SD_FILE)
  63. SdFile powerOffFile;
  64. #endif
  65. #define SD_PROCEDURE_DEPTH 1
  66. #define MAXPATHNAMELENGTH (13*MAX_DIR_DEPTH+MAX_DIR_DEPTH+1)
  67. uint8_t file_subcall_ctr;
  68. uint32_t filespos[SD_PROCEDURE_DEPTH];
  69. char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
  70. uint32_t filesize;
  71. //int16_t n;
  72. unsigned long autostart_atmillis;
  73. uint32_t sdpos ;
  74. bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
  75. LsAction lsAction; //stored for recursion.
  76. 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.
  77. char* diveDirName;
  78. void lsDive(const char *prepend,SdFile parent);
  79. };
  80. extern CardReader card;
  81. #if defined(SDSUPPORT) && defined(POWEROFF_SAVE_SD_FILE)
  82. struct power_off_info_t {
  83. /* header (1B + 7B = 8B) */
  84. uint8_t valid_head;
  85. //uint8_t reserved1[8-1];
  86. /* Gcode related information. (44B + 20B = 64B) */
  87. float current_position[NUM_AXIS];
  88. float feedrate;
  89. float saved_z;
  90. int target_temperature[4];
  91. int target_temperature_bed;
  92. //uint8_t reserved2[64-44];
  93. /* print queue related information. (396B + 116B = 512B) */
  94. int cmd_queue_index_r;
  95. int cmd_queue_index_w;
  96. int commands_in_queue;
  97. char command_queue[BUFSIZE][MAX_CMD_SIZE];
  98. //uint8_t reserved3[512-396];
  99. /* SD card related information. (165B + 91B = 256B)*/
  100. uint32_t sdpos;
  101. millis_t print_job_start_ms;
  102. char sd_filename[MAXPATHNAMELENGTH];
  103. char power_off_filename[16];
  104. //uint8_t reserved4[256-166];
  105. uint8_t valid_foot;
  106. };
  107. extern struct power_off_info_t power_off_info;
  108. extern int power_off_commands_count;
  109. extern int power_off_type_yes;
  110. #endif
  111. #define IS_SD_PRINTING (card.sdprinting)
  112. #if (SDCARDDETECT > -1)
  113. # ifdef SDCARDDETECTINVERTED
  114. # define IS_SD_INSERTED (READ(SDCARDDETECT)!=0)
  115. # else
  116. # define IS_SD_INSERTED (READ(SDCARDDETECT)==0)
  117. # endif //SDCARDTETECTINVERTED
  118. #else
  119. //If we don't have a card detect line, aways asume the card is inserted
  120. # define IS_SD_INSERTED true
  121. #endif
  122. #else
  123. #define IS_SD_PRINTING (false)
  124. #endif //SDSUPPORT
  125. #endif