Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 1
/**
2
 * Marlin 3D Printer Firmware
3
 * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
 *
5
 * Based on Sprinter and grbl.
6
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
 
23
#ifndef _CARDREADER_H_
24
#define _CARDREADER_H_
25
 
26
#include "MarlinConfig.h"
27
 
28
#if ENABLED(SDSUPPORT)
29
 
30
#define SD_RESORT ENABLED(SDCARD_SORT_ALPHA) && ENABLED(SDSORT_DYNAMIC_RAM)
31
 
32
#define MAX_DIR_DEPTH 10          // Maximum folder depth
33
 
34
#include "SdFile.h"
35
 
36
class CardReader {
37
public:
38
  CardReader();
39
 
40
  void initsd();
41
  void write_command(char *buf);
42
 
43
  void beginautostart();
44
  void checkautostart();
45
 
46
  void openFile(char * const path, const bool read, const bool subcall=false);
47
  void openLogFile(char * const path);
48
  void removeFile(const char * const name);
49
  void closefile(const bool store_location=false);
50
  void release();
51
  void openAndPrintFile(const char *name);
52
  void startFileprint();
53
  void stopSDPrint(
54
    #if SD_RESORT
55
      const bool re_sort=false
56
    #endif
57
  );
58
  void getStatus();
59
  void printingHasFinished();
60
  void printFilename();
61
 
62
  #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
63
    void printLongPath(char *path);
64
  #endif
65
 
66
  void getfilename(uint16_t nr, const char* const match=NULL);
67
  uint16_t getnrfilenames();
68
 
69
  void getAbsFilename(char *t);
70
 
71
  void ls();
72
  void chdir(const char *relpath);
73
  int8_t updir();
74
  void setroot();
75
 
76
  const char* diveToFile(SdFile*& curDir, const char * const path, const bool echo);
77
 
78
  uint16_t get_num_Files();
79
 
80
  #if ENABLED(SDCARD_SORT_ALPHA)
81
    void presort();
82
    void getfilename_sorted(const uint16_t nr);
83
    #if ENABLED(SDSORT_GCODE)
84
      FORCE_INLINE void setSortOn(bool b) { sort_alpha = b; presort(); }
85
      FORCE_INLINE void setSortFolders(int i) { sort_folders = i; presort(); }
86
      //FORCE_INLINE void setSortReverse(bool b) { sort_reverse = b; }
87
    #endif
88
  #endif
89
 
90
  #if ENABLED(POWER_LOSS_RECOVERY)
91
    void openJobRecoveryFile(const bool read);
92
    void closeJobRecoveryFile();
93
    bool jobRecoverFileExists();
94
    int16_t saveJobRecoveryInfo();
95
    int16_t loadJobRecoveryInfo();
96
    void removeJobRecoveryFile();
97
  #endif
98
 
99
  FORCE_INLINE void pauseSDPrint() { sdprinting = false; }
100
  FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
101
  FORCE_INLINE bool eof() { return sdpos >= filesize; }
102
  FORCE_INLINE int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
103
  FORCE_INLINE void setIndex(const uint32_t index) { sdpos = index; file.seekSet(index); }
104
  FORCE_INLINE uint32_t getIndex() { return sdpos; }
105
  FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
106
  FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
107
 
108
  #if ENABLED(AUTO_REPORT_SD_STATUS)
109
    void auto_report_sd_status(void);
110
    FORCE_INLINE void set_auto_report_interval(uint8_t v) {
111
      NOMORE(v, 60);
112
      auto_report_sd_interval = v;
113
      next_sd_report_ms = millis() + 1000UL * v;
114
    }
115
  #endif
116
 
117
  FORCE_INLINE char* longest_filename() { return longFilename[0] ? longFilename : filename; }
118
 
119
public:
120
  bool saving, logging, sdprinting, cardOK, filenameIsDir, abort_sd_printing;
121
  char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
122
  int8_t autostart_index;
123
private:
124
  SdFile root, workDir, workDirParents[MAX_DIR_DEPTH];
125
  uint8_t workDirDepth;
126
 
127
  // Sort files and folders alphabetically.
128
  #if ENABLED(SDCARD_SORT_ALPHA)
129
    uint16_t sort_count;        // Count of sorted items in the current directory
130
    #if ENABLED(SDSORT_GCODE)
131
      bool sort_alpha;          // Flag to enable / disable the feature
132
      int sort_folders;         // Flag to enable / disable folder sorting
133
      //bool sort_reverse;      // Flag to enable / disable reverse sorting
134
    #endif
135
 
136
    // By default the sort index is static
137
    #if ENABLED(SDSORT_DYNAMIC_RAM)
138
      uint8_t *sort_order;
139
    #else
140
      uint8_t sort_order[SDSORT_LIMIT];
141
    #endif
142
 
143
    #if ENABLED(SDSORT_USES_RAM) && ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
144
      #define SORTED_LONGNAME_MAXLEN ((SDSORT_CACHE_VFATS) * (FILENAME_LENGTH) + 1)
145
    #else
146
      #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH
147
    #endif
148
 
149
    // Cache filenames to speed up SD menus.
150
    #if ENABLED(SDSORT_USES_RAM)
151
 
152
      // If using dynamic ram for names, allocate on the heap.
153
      #if ENABLED(SDSORT_CACHE_NAMES)
154
        #if ENABLED(SDSORT_DYNAMIC_RAM)
155
          char **sortshort, **sortnames;
156
        #else
157
          char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
158
          char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
159
        #endif
160
      #elif DISABLED(SDSORT_USES_STACK)
161
        char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
162
      #endif
163
 
164
      // Folder sorting uses an isDir array when caching items.
165
      #if HAS_FOLDER_SORTING
166
        #if ENABLED(SDSORT_DYNAMIC_RAM)
167
          uint8_t *isDir;
168
        #elif ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)
169
          uint8_t isDir[(SDSORT_LIMIT+7)>>3];
170
        #endif
171
      #endif
172
 
173
    #endif // SDSORT_USES_RAM
174
 
175
  #endif // SDCARD_SORT_ALPHA
176
 
177
  Sd2Card sd2card;
178
  SdVolume volume;
179
  SdFile file;
180
 
181
  #if ENABLED(POWER_LOSS_RECOVERY)
182
    SdFile jobRecoveryFile;
183
  #endif
184
 
185
  #define SD_PROCEDURE_DEPTH 1
186
  #define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH + MAX_DIR_DEPTH + 1)
187
  uint8_t file_subcall_ctr;
188
  uint32_t filespos[SD_PROCEDURE_DEPTH];
189
  char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
190
  uint32_t filesize, sdpos;
191
 
192
  LsAction lsAction; //stored for recursion.
193
  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.
194
  char* diveDirName;
195
  void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
196
 
197
  #if ENABLED(SDCARD_SORT_ALPHA)
198
    void flush_presort();
199
  #endif
200
 
201
  #if ENABLED(AUTO_REPORT_SD_STATUS)
202
    static uint8_t auto_report_sd_interval;
203
    static millis_t next_sd_report_ms;
204
  #endif
205
};
206
 
207
#if PIN_EXISTS(SD_DETECT)
208
  #if ENABLED(SD_DETECT_INVERTED)
209
    #define IS_SD_INSERTED()  READ(SD_DETECT_PIN)
210
  #else
211
    #define IS_SD_INSERTED() !READ(SD_DETECT_PIN)
212
  #endif
213
#else
214
  // No card detect line? Assume the card is inserted.
215
  #define IS_SD_INSERTED() true
216
#endif
217
 
218
extern CardReader card;
219
 
220
#endif // SDSUPPORT
221
 
222
#if ENABLED(SDSUPPORT)
223
  #define IS_SD_PRINTING()  card.sdprinting
224
  #define IS_SD_FILE_OPEN() card.isFileOpen()
225
#else
226
  #define IS_SD_PRINTING()  false
227
  #define IS_SD_FILE_OPEN() false
228
#endif
229
 
230
#endif // _CARDREADER_H_