Prusa3d Marlin fork
cardreader.h
1 #ifndef CARDREADER_H
2 #define CARDREADER_H
3 
4 #define SDSUPPORT
5 
6 #ifdef SDSUPPORT
7 
8 #define MAX_DIR_DEPTH 6
9 
10 #include "SdFile.h"
12 {
13 public:
14  CardReader();
15 
16  enum LsAction : uint8_t
17  {
18  LS_SerialPrint,
19  LS_Count,
20  LS_GetFilename,
21  };
22  struct ls_param
23  {
24  bool LFN : 1;
25  bool timestamp : 1;
26  inline ls_param():LFN(0), timestamp(0) { }
27  inline ls_param(bool LFN, bool timestamp):LFN(LFN), timestamp(timestamp) { }
28  } __attribute__((packed));
29 
30  void mount(bool doPresort = true);
31  void write_command(char *buf);
32  void write_command_no_newline(char *buf);
33  //files auto[0-9].g on the sd card are performed in a row
34  //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
35 
36  void checkautostart(bool x);
37  void openFileWrite(const char* name);
38  void openFileReadFilteredGcode(const char* name, bool replace_current = false);
39  void openLogFile(const char* name);
40  void removeFile(const char* name);
41  void closefile(bool store_location=false);
42  void release();
43  void startFileprint();
44  uint32_t getFileSize();
45  void getStatus(bool arg_P);
46  void printingHasFinished();
47 
48  void getfilename(uint16_t nr, const char* const match=NULL);
49  void getfilename_simple(uint16_t entry, const char * const match = NULL);
50  void getfilename_next(uint32_t position, const char * const match = NULL);
51  uint16_t getnrfilenames();
52 
53  void getAbsFilename(char *t);
54  void printAbsFilenameFast();
55  void getDirName(char* name, uint8_t level);
56  uint8_t getWorkDirDepth();
57 
58 
59  void ls(ls_param params);
60  bool chdir(const char * relpath, bool doPresort);
61  void updir();
62  void cdroot(bool doPresort);
63 
64  #ifdef SDCARD_SORT_ALPHA
65  void presort();
66  void getfilename_sorted(const uint16_t nr, uint8_t sdSort);
67  void getfilename_afterMaxSorting(uint16_t entry, const char * const match = NULL);
68  #endif
69 
70  FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
71  bool eof() { return sdpos>=filesize; }
72  FORCE_INLINE int16_t getFilteredGcodeChar()
73  {
74  int16_t c = (int16_t)file.readFilteredGcode();
75  sdpos = file.curPosition();
76  return c;
77  };
78  void setIndex(long index) {sdpos = index;file.seekSetFilteredGcode(index);};
79  FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;};
80  FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;};
81  FORCE_INLINE uint32_t get_sdpos() { if (!isFileOpen()) return 0; else return(sdpos); };
82 
83  bool ToshibaFlashAir_isEnabled() const { return card.getFlashAirCompatible(); }
84  void ToshibaFlashAir_enable(bool enable) { card.setFlashAirCompatible(enable); }
85  bool ToshibaFlashAir_GetIP(uint8_t *ip);
86 
87  //Reprint
88  bool FileExists(const char* filename);
89 
90 public:
91  bool saving;
92  bool logging;
93  bool sdprinting ;
94  bool mounted;
95  char filename[FILENAME_LENGTH];
96  // There are scenarios when simple modification time is not enough (on MS Windows)
97  // Therefore these timestamps hold the most recent one of creation/modification date/times
98  uint16_t crmodTime, crmodDate;
99  uint32_t /* cluster, */ position;
100  char longFilename[LONG_FILENAME_LENGTH];
101  bool filenameIsDir;
102  int lastnr; //last number of the autostart;
103 #ifdef SDCARD_SORT_ALPHA
104  bool presort_flag;
105 #endif // SDCARD_SORT_ALPHA
106  char dir_names[MAX_DIR_DEPTH][9];
107 private:
108  SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
109  uint8_t workDirDepth;
110 
111  // Sort files and folders alphabetically.
112 #ifdef SDCARD_SORT_ALPHA
113  uint16_t sort_count; // Count of sorted items in the current directory
114  uint16_t sort_entries[SDSORT_LIMIT];
115  uint16_t lastSortedFilePosition;
116 
117 #endif // SDCARD_SORT_ALPHA
118 
119 #ifdef DEBUG_SD_SPEED_TEST
120 public:
121 #endif //DEBUG_SD_SPEED_TEST
122  Sd2Card card;
123 
124 private:
125  SdVolume volume;
126  SdFile file;
127  #define SD_PROCEDURE_DEPTH 1
128  #define MAXPATHNAMELENGTH (13*MAX_DIR_DEPTH+MAX_DIR_DEPTH+1)
129  uint8_t file_subcall_ctr;
130  uint32_t filespos[SD_PROCEDURE_DEPTH];
131  char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
132  uint32_t filesize;
133  //int16_t n;
134  ShortTimer autostart_atmillis;
135  uint32_t sdpos ;
136 
137  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.
138 
139  bool diveSubfolder (const char *&fileName);
140  void lsDive(const char *prepend, SdFile parent, const char * const match=NULL, LsAction lsAction = LS_GetFilename, ls_param lsParams = ls_param());
141 #ifdef SDCARD_SORT_ALPHA
142  void flush_presort();
143 #endif
144 };
145 extern bool Stopped;
146 extern CardReader card;
147 #define IS_SD_PRINTING (card.sdprinting)
148 
149 #if (SDCARDDETECT > -1)
150 # ifdef SDCARDDETECTINVERTED
151 # define IS_SD_INSERTED (READ(SDCARDDETECT)!=0)
152 # else
153 # define IS_SD_INSERTED (READ(SDCARDDETECT)==0)
154 # endif //SDCARDTETECTINVERTED
155 #else
156 //If we don't have a card detect line, aways asume the card is inserted
157 # define IS_SD_INSERTED true
158 #endif
159 
160 #else
161 
162 #define IS_SD_PRINTING (false)
163 
164 #endif //SDSUPPORT
165 #endif
SdFile class.
Definition: cardreader.h:12
void presort()
Definition: cardreader.cpp:789
void getfilename_sorted(const uint16_t nr, uint8_t sdSort)
Definition: cardreader.cpp:766
Raw access to SD and SDHC flash memory cards.
Definition: Sd2Card.h:148
bool getFilename(char *name)
Definition: SdBaseFile.cpp:280
bool isOpen() const
Definition: SdBaseFile.h:262
uint32_t curPosition() const
Definition: SdBaseFile.h:212
SdBaseFile with Print.
Definition: SdFile.h:36
Access FAT16 and FAT32 volumes on SD and SDHC cards.
Definition: SdVolume.h:60
Definition: cardreader.h:23