|
@@ -7,91 +7,69 @@
|
7
|
7
|
|
8
|
8
|
#ifdef SDSUPPORT
|
9
|
9
|
|
|
10
|
+CardReader::CardReader() {
|
|
11
|
+ filesize = 0;
|
|
12
|
+ sdpos = 0;
|
|
13
|
+ sdprinting = false;
|
|
14
|
+ cardOK = false;
|
|
15
|
+ saving = false;
|
|
16
|
+ logging = false;
|
|
17
|
+ workDirDepth = 0;
|
|
18
|
+ file_subcall_ctr = 0;
|
|
19
|
+ memset(workDirParents, 0, sizeof(workDirParents));
|
10
|
20
|
|
11
|
|
-
|
12
|
|
-CardReader::CardReader()
|
13
|
|
-{
|
14
|
|
- filesize = 0;
|
15
|
|
- sdpos = 0;
|
16
|
|
- sdprinting = false;
|
17
|
|
- cardOK = false;
|
18
|
|
- saving = false;
|
19
|
|
- logging = false;
|
20
|
|
- autostart_atmillis=0;
|
21
|
|
- workDirDepth = 0;
|
22
|
|
- file_subcall_ctr=0;
|
23
|
|
- memset(workDirParents, 0, sizeof(workDirParents));
|
24
|
|
-
|
25
|
|
- autostart_stilltocheck=true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
|
26
|
|
- autostart_index=0;
|
|
21
|
+ autostart_stilltocheck = true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
|
|
22
|
+ autostart_index = 0;
|
27
|
23
|
//power to SD reader
|
28
|
24
|
#if SDPOWER > -1
|
29
|
|
- SET_OUTPUT(SDPOWER);
|
30
|
|
- WRITE(SDPOWER,HIGH);
|
|
25
|
+ SET_OUTPUT(SDPOWER);
|
|
26
|
+ WRITE(SDPOWER, HIGH);
|
31
|
27
|
#endif //SDPOWER
|
32
|
|
-
|
33
|
|
- autostart_atmillis=millis()+5000;
|
|
28
|
+
|
|
29
|
+ autostart_atmillis = millis() + 5000;
|
34
|
30
|
}
|
35
|
31
|
|
36
|
|
-char *createFilename(char *buffer,const dir_t &p) //buffer>12characters
|
37
|
|
-{
|
38
|
|
- char *pos=buffer;
|
39
|
|
- for (uint8_t i = 0; i < 11; i++)
|
40
|
|
- {
|
41
|
|
- if (p.name[i] == ' ')continue;
|
42
|
|
- if (i == 8)
|
43
|
|
- {
|
44
|
|
- *pos++='.';
|
45
|
|
- }
|
46
|
|
- *pos++=p.name[i];
|
|
32
|
+char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters
|
|
33
|
+ char *pos = buffer;
|
|
34
|
+ for (uint8_t i = 0; i < 11; i++) {
|
|
35
|
+ if (p.name[i] == ' ') continue;
|
|
36
|
+ if (i == 8) *pos++ = '.';
|
|
37
|
+ *pos++ = p.name[i];
|
47
|
38
|
}
|
48
|
|
- *pos++=0;
|
|
39
|
+ *pos++ = 0;
|
49
|
40
|
return buffer;
|
50
|
41
|
}
|
51
|
42
|
|
52
|
|
-
|
53
|
|
-void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/)
|
54
|
|
-{
|
|
43
|
+void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
|
55
|
44
|
dir_t p;
|
56
|
|
- uint8_t cnt=0;
|
57
|
|
-
|
58
|
|
- while (parent.readDir(p, longFilename) > 0)
|
59
|
|
- {
|
60
|
|
- if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint
|
61
|
|
- {
|
|
45
|
+ uint8_t cnt = 0;
|
62
|
46
|
|
|
47
|
+ while (parent.readDir(p, longFilename) > 0) {
|
|
48
|
+ if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { // hence LS_SerialPrint
|
63
|
49
|
char path[FILENAME_LENGTH*2];
|
64
|
50
|
char lfilename[FILENAME_LENGTH];
|
65
|
|
- createFilename(lfilename,p);
|
66
|
|
-
|
67
|
|
- path[0]=0;
|
68
|
|
- if(prepend[0]==0) //avoid leading / if already in prepend
|
69
|
|
- {
|
70
|
|
- strcat(path,"/");
|
71
|
|
- }
|
72
|
|
- strcat(path,prepend);
|
73
|
|
- strcat(path,lfilename);
|
74
|
|
- strcat(path,"/");
|
75
|
|
-
|
|
51
|
+ createFilename(lfilename, p);
|
|
52
|
+
|
|
53
|
+ path[0] = 0;
|
|
54
|
+ if (prepend[0] == 0) strcat(path, "/"); //avoid leading / if already in prepend
|
|
55
|
+ strcat(path, prepend);
|
|
56
|
+ strcat(path, lfilename);
|
|
57
|
+ strcat(path, "/");
|
|
58
|
+
|
76
|
59
|
//Serial.print(path);
|
77
|
|
-
|
|
60
|
+
|
78
|
61
|
SdFile dir;
|
79
|
|
- if(!dir.open(parent,lfilename, O_READ))
|
80
|
|
- {
|
81
|
|
- if(lsAction==LS_SerialPrint)
|
82
|
|
- {
|
|
62
|
+ if (!dir.open(parent, lfilename, O_READ)) {
|
|
63
|
+ if (lsAction == LS_SerialPrint) {
|
83
|
64
|
SERIAL_ECHO_START;
|
84
|
65
|
SERIAL_ECHOLN(MSG_SD_CANT_OPEN_SUBDIR);
|
85
|
66
|
SERIAL_ECHOLN(lfilename);
|
86
|
67
|
}
|
87
|
68
|
}
|
88
|
|
- lsDive(path,dir);
|
|
69
|
+ lsDive(path, dir);
|
89
|
70
|
//close done automatically by destructor of SdFile
|
90
|
|
-
|
91
|
|
-
|
92
|
71
|
}
|
93
|
|
- else
|
94
|
|
- {
|
|
72
|
+ else {
|
95
|
73
|
char pn0 = p.name[0];
|
96
|
74
|
if (pn0 == DIR_NAME_FREE) break;
|
97
|
75
|
if (pn0 == DIR_NAME_DELETED || pn0 == '.' || pn0 == '_') continue;
|
|
@@ -99,384 +77,313 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
|
99
|
77
|
if (lf0 == '.' || lf0 == '_') continue;
|
100
|
78
|
|
101
|
79
|
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
|
102
|
|
- filenameIsDir=DIR_IS_SUBDIR(&p);
|
103
|
|
-
|
104
|
|
-
|
105
|
|
- if(!filenameIsDir)
|
106
|
|
- {
|
107
|
|
- if(p.name[8]!='G') continue;
|
108
|
|
- if(p.name[9]=='~') continue;
|
109
|
|
- }
|
110
|
|
- //if(cnt++!=nr) continue;
|
111
|
|
- createFilename(filename,p);
|
112
|
|
- if(lsAction==LS_SerialPrint)
|
113
|
|
- {
|
|
80
|
+
|
|
81
|
+ filenameIsDir = DIR_IS_SUBDIR(&p);
|
|
82
|
+
|
|
83
|
+ if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
|
|
84
|
+
|
|
85
|
+ //if (cnt++ != nr) continue;
|
|
86
|
+ createFilename(filename, p);
|
|
87
|
+ if (lsAction == LS_SerialPrint) {
|
114
|
88
|
SERIAL_PROTOCOL(prepend);
|
115
|
89
|
SERIAL_PROTOCOLLN(filename);
|
116
|
90
|
}
|
117
|
|
- else if(lsAction==LS_Count)
|
118
|
|
- {
|
|
91
|
+ else if (lsAction == LS_Count) {
|
119
|
92
|
nrFiles++;
|
120
|
|
- }
|
121
|
|
- else if(lsAction==LS_GetFilename)
|
122
|
|
- {
|
|
93
|
+ }
|
|
94
|
+ else if (lsAction == LS_GetFilename) {
|
123
|
95
|
if (match != NULL) {
|
124
|
96
|
if (strcasecmp(match, filename) == 0) return;
|
125
|
97
|
}
|
126
|
98
|
else if (cnt == nrFiles) return;
|
127
|
99
|
cnt++;
|
128
|
|
-
|
129
|
100
|
}
|
130
|
101
|
}
|
131
|
102
|
}
|
132
|
103
|
}
|
133
|
104
|
|
134
|
|
-void CardReader::ls()
|
135
|
|
-{
|
136
|
|
- lsAction=LS_SerialPrint;
|
137
|
|
- if(lsAction==LS_Count)
|
138
|
|
- nrFiles=0;
|
139
|
|
-
|
|
105
|
+void CardReader::ls() {
|
|
106
|
+ lsAction = LS_SerialPrint;
|
140
|
107
|
root.rewind();
|
141
|
|
- lsDive("",root);
|
|
108
|
+ lsDive("", root);
|
142
|
109
|
}
|
143
|
110
|
|
144
|
|
-
|
145
|
|
-void CardReader::initsd()
|
146
|
|
-{
|
|
111
|
+void CardReader::initsd() {
|
147
|
112
|
cardOK = false;
|
148
|
|
- if(root.isOpen())
|
149
|
|
- root.close();
|
150
|
|
-#ifdef SDSLOW
|
151
|
|
- if (!card.init(SPI_HALF_SPEED,SDSS)
|
152
|
|
- #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
|
153
|
|
- && !card.init(SPI_HALF_SPEED,LCD_SDSS)
|
154
|
|
- #endif
|
155
|
|
- )
|
156
|
|
-#else
|
157
|
|
- if (!card.init(SPI_FULL_SPEED,SDSS)
|
158
|
|
- #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
|
159
|
|
- && !card.init(SPI_FULL_SPEED,LCD_SDSS)
|
|
113
|
+ if (root.isOpen()) root.close();
|
|
114
|
+
|
|
115
|
+ #ifdef SDSLOW
|
|
116
|
+ #define SPI_SPEED SPI_HALF_SPEED
|
|
117
|
+ #else
|
|
118
|
+ #define SPI_SPEED SPI_FULL_SPEED
|
160
|
119
|
#endif
|
161
|
|
- )
|
162
|
|
-#endif
|
163
|
|
- {
|
|
120
|
+
|
|
121
|
+ if (!card.init(SPI_SPEED,SDSS)
|
|
122
|
+ #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
|
|
123
|
+ && !card.init(SPI_SPEED, LCD_SDSS)
|
|
124
|
+ #endif
|
|
125
|
+ ) {
|
164
|
126
|
//if (!card.init(SPI_HALF_SPEED,SDSS))
|
165
|
127
|
SERIAL_ECHO_START;
|
166
|
128
|
SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
|
167
|
129
|
}
|
168
|
|
- else if (!volume.init(&card))
|
169
|
|
- {
|
|
130
|
+ else if (!volume.init(&card)) {
|
170
|
131
|
SERIAL_ERROR_START;
|
171
|
132
|
SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
|
172
|
133
|
}
|
173
|
|
- else if (!root.openRoot(&volume))
|
174
|
|
- {
|
|
134
|
+ else if (!root.openRoot(&volume)) {
|
175
|
135
|
SERIAL_ERROR_START;
|
176
|
136
|
SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
|
177
|
137
|
}
|
178
|
|
- else
|
179
|
|
- {
|
|
138
|
+ else {
|
180
|
139
|
cardOK = true;
|
181
|
140
|
SERIAL_ECHO_START;
|
182
|
141
|
SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
|
183
|
142
|
}
|
184
|
|
- workDir=root;
|
185
|
|
- curDir=&root;
|
|
143
|
+ workDir = root;
|
|
144
|
+ curDir = &root;
|
186
|
145
|
/*
|
187
|
|
- if(!workDir.openRoot(&volume))
|
188
|
|
- {
|
|
146
|
+ if (!workDir.openRoot(&volume)) {
|
189
|
147
|
SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
|
190
|
148
|
}
|
191
|
149
|
*/
|
192
|
|
-
|
193
|
150
|
}
|
194
|
151
|
|
195
|
|
-void CardReader::setroot()
|
196
|
|
-{
|
197
|
|
- /*if(!workDir.openRoot(&volume))
|
198
|
|
- {
|
|
152
|
+void CardReader::setroot() {
|
|
153
|
+ /*if (!workDir.openRoot(&volume)) {
|
199
|
154
|
SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
|
200
|
155
|
}*/
|
201
|
|
- workDir=root;
|
202
|
|
-
|
203
|
|
- curDir=&workDir;
|
|
156
|
+ workDir = root;
|
|
157
|
+ curDir = &workDir;
|
204
|
158
|
}
|
205
|
|
-void CardReader::release()
|
206
|
|
-{
|
|
159
|
+
|
|
160
|
+void CardReader::release() {
|
207
|
161
|
sdprinting = false;
|
208
|
162
|
cardOK = false;
|
209
|
163
|
}
|
210
|
164
|
|
211
|
|
-void CardReader::startFileprint()
|
212
|
|
-{
|
213
|
|
- if(cardOK)
|
214
|
|
- {
|
|
165
|
+void CardReader::startFileprint() {
|
|
166
|
+ if (cardOK) {
|
215
|
167
|
sdprinting = true;
|
216
|
168
|
}
|
217
|
169
|
}
|
218
|
170
|
|
219
|
|
-void CardReader::pauseSDPrint()
|
220
|
|
-{
|
221
|
|
- if(sdprinting)
|
222
|
|
- {
|
223
|
|
- sdprinting = false;
|
224
|
|
- }
|
|
171
|
+void CardReader::pauseSDPrint() {
|
|
172
|
+ if (sdprinting) sdprinting = false;
|
225
|
173
|
}
|
226
|
174
|
|
227
|
|
-
|
228
|
|
-void CardReader::openLogFile(char* name)
|
229
|
|
-{
|
|
175
|
+void CardReader::openLogFile(char* name) {
|
230
|
176
|
logging = true;
|
231
|
177
|
openFile(name, false);
|
232
|
178
|
}
|
233
|
179
|
|
234
|
|
-void CardReader::getAbsFilename(char *t)
|
235
|
|
-{
|
236
|
|
- uint8_t cnt=0;
|
237
|
|
- *t='/';t++;cnt++;
|
238
|
|
- for(uint8_t i=0;i<workDirDepth;i++)
|
239
|
|
- {
|
|
180
|
+void CardReader::getAbsFilename(char *t) {
|
|
181
|
+ uint8_t cnt = 0;
|
|
182
|
+ *t = '/'; t++; cnt++;
|
|
183
|
+ for (uint8_t i = 0; i < workDirDepth; i++) {
|
240
|
184
|
workDirParents[i].getFilename(t); //SDBaseFile.getfilename!
|
241
|
|
- while(*t!=0 && cnt< MAXPATHNAMELENGTH)
|
242
|
|
- {t++;cnt++;} //crawl counter forward.
|
|
185
|
+ while(*t && cnt < MAXPATHNAMELENGTH) { t++; cnt++; } //crawl counter forward.
|
243
|
186
|
}
|
244
|
|
- if(cnt<MAXPATHNAMELENGTH-FILENAME_LENGTH)
|
|
187
|
+ if (cnt < MAXPATHNAMELENGTH - FILENAME_LENGTH)
|
245
|
188
|
file.getFilename(t);
|
246
|
189
|
else
|
247
|
|
- t[0]=0;
|
|
190
|
+ t[0] = 0;
|
248
|
191
|
}
|
249
|
192
|
|
250
|
|
-void CardReader::openFile(char* name,bool read, bool replace_current/*=true*/)
|
251
|
|
-{
|
252
|
|
- if(!cardOK)
|
253
|
|
- return;
|
254
|
|
- if(file.isOpen()) //replacing current file by new file, or subfile call
|
255
|
|
- {
|
256
|
|
- if(!replace_current)
|
257
|
|
- {
|
258
|
|
- if((int)file_subcall_ctr>(int)SD_PROCEDURE_DEPTH-1)
|
259
|
|
- {
|
|
193
|
+void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/) {
|
|
194
|
+ if (!cardOK) return;
|
|
195
|
+ if (file.isOpen()) { //replacing current file by new file, or subfile call
|
|
196
|
+ if (!replace_current) {
|
|
197
|
+ if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
|
260
|
198
|
SERIAL_ERROR_START;
|
261
|
199
|
SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
|
262
|
200
|
SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
|
263
|
201
|
kill();
|
264
|
202
|
return;
|
265
|
203
|
}
|
266
|
|
-
|
|
204
|
+
|
267
|
205
|
SERIAL_ECHO_START;
|
268
|
206
|
SERIAL_ECHOPGM("SUBROUTINE CALL target:\"");
|
269
|
207
|
SERIAL_ECHO(name);
|
270
|
208
|
SERIAL_ECHOPGM("\" parent:\"");
|
271
|
|
-
|
|
209
|
+
|
272
|
210
|
//store current filename and position
|
273
|
211
|
getAbsFilename(filenames[file_subcall_ctr]);
|
274
|
|
-
|
|
212
|
+
|
275
|
213
|
SERIAL_ECHO(filenames[file_subcall_ctr]);
|
276
|
214
|
SERIAL_ECHOPGM("\" pos");
|
277
|
215
|
SERIAL_ECHOLN(sdpos);
|
278
|
|
- filespos[file_subcall_ctr]=sdpos;
|
|
216
|
+ filespos[file_subcall_ctr] = sdpos;
|
279
|
217
|
file_subcall_ctr++;
|
280
|
218
|
}
|
281
|
|
- else
|
282
|
|
- {
|
|
219
|
+ else {
|
283
|
220
|
SERIAL_ECHO_START;
|
284
|
221
|
SERIAL_ECHOPGM("Now doing file: ");
|
285
|
222
|
SERIAL_ECHOLN(name);
|
286
|
223
|
}
|
287
|
224
|
file.close();
|
288
|
225
|
}
|
289
|
|
- else //opening fresh file
|
290
|
|
- {
|
291
|
|
- file_subcall_ctr=0; //resetting procedure depth in case user cancels print while in procedure
|
|
226
|
+ else { //opening fresh file
|
|
227
|
+ file_subcall_ctr = 0; //resetting procedure depth in case user cancels print while in procedure
|
292
|
228
|
SERIAL_ECHO_START;
|
293
|
229
|
SERIAL_ECHOPGM("Now fresh file: ");
|
294
|
230
|
SERIAL_ECHOLN(name);
|
295
|
231
|
}
|
296
|
232
|
sdprinting = false;
|
297
|
|
-
|
298
|
|
-
|
|
233
|
+
|
299
|
234
|
SdFile myDir;
|
300
|
|
- curDir=&root;
|
301
|
|
- char *fname=name;
|
302
|
|
-
|
303
|
|
- char *dirname_start,*dirname_end;
|
304
|
|
- if(name[0]=='/')
|
305
|
|
- {
|
306
|
|
- dirname_start=strchr(name,'/')+1;
|
307
|
|
- while(dirname_start>0)
|
308
|
|
- {
|
309
|
|
- dirname_end=strchr(dirname_start,'/');
|
310
|
|
- //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
|
311
|
|
- //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
|
312
|
|
- if(dirname_end>0 && dirname_end>dirname_start)
|
313
|
|
- {
|
|
235
|
+ curDir = &root;
|
|
236
|
+ char *fname = name;
|
|
237
|
+
|
|
238
|
+ char *dirname_start, *dirname_end;
|
|
239
|
+ if (name[0] == '/') {
|
|
240
|
+ dirname_start = &name[1];
|
|
241
|
+ while(dirname_start > 0) {
|
|
242
|
+ dirname_end = strchr(dirname_start, '/');
|
|
243
|
+ //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start - name));
|
|
244
|
+ //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end - name));
|
|
245
|
+ if (dirname_end > 0 && dirname_end > dirname_start) {
|
314
|
246
|
char subdirname[FILENAME_LENGTH];
|
315
|
|
- strncpy(subdirname, dirname_start, dirname_end-dirname_start);
|
316
|
|
- subdirname[dirname_end-dirname_start]=0;
|
|
247
|
+ strncpy(subdirname, dirname_start, dirname_end - dirname_start);
|
|
248
|
+ subdirname[dirname_end - dirname_start] = 0;
|
317
|
249
|
SERIAL_ECHOLN(subdirname);
|
318
|
|
- if(!myDir.open(curDir,subdirname,O_READ))
|
319
|
|
- {
|
|
250
|
+ if (!myDir.open(curDir, subdirname, O_READ)) {
|
320
|
251
|
SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
|
321
|
252
|
SERIAL_PROTOCOL(subdirname);
|
322
|
253
|
SERIAL_PROTOCOLLNPGM(".");
|
323
|
254
|
return;
|
324
|
255
|
}
|
325
|
|
- else
|
326
|
|
- {
|
|
256
|
+ else {
|
327
|
257
|
//SERIAL_ECHOLN("dive ok");
|
328
|
258
|
}
|
329
|
|
-
|
330
|
|
- curDir=&myDir;
|
331
|
|
- dirname_start=dirname_end+1;
|
|
259
|
+
|
|
260
|
+ curDir = &myDir;
|
|
261
|
+ dirname_start = dirname_end + 1;
|
332
|
262
|
}
|
333
|
|
- else // the reminder after all /fsa/fdsa/ is the filename
|
334
|
|
- {
|
335
|
|
- fname=dirname_start;
|
336
|
|
- //SERIAL_ECHOLN("remaider");
|
|
263
|
+ else { // the remainder after all /fsa/fdsa/ is the filename
|
|
264
|
+ fname = dirname_start;
|
|
265
|
+ //SERIAL_ECHOLN("remainder");
|
337
|
266
|
//SERIAL_ECHOLN(fname);
|
338
|
267
|
break;
|
339
|
268
|
}
|
340
|
|
-
|
341
|
269
|
}
|
342
|
270
|
}
|
343
|
|
- else //relative path
|
344
|
|
- {
|
345
|
|
- curDir=&workDir;
|
|
271
|
+ else { //relative path
|
|
272
|
+ curDir = &workDir;
|
346
|
273
|
}
|
347
|
|
- if(read)
|
348
|
|
- {
|
349
|
|
- if (file.open(curDir, fname, O_READ))
|
350
|
|
- {
|
|
274
|
+
|
|
275
|
+ if (read) {
|
|
276
|
+ if (file.open(curDir, fname, O_READ)) {
|
351
|
277
|
filesize = file.fileSize();
|
352
|
278
|
SERIAL_PROTOCOLPGM(MSG_SD_FILE_OPENED);
|
353
|
279
|
SERIAL_PROTOCOL(fname);
|
354
|
280
|
SERIAL_PROTOCOLPGM(MSG_SD_SIZE);
|
355
|
281
|
SERIAL_PROTOCOLLN(filesize);
|
356
|
282
|
sdpos = 0;
|
357
|
|
-
|
|
283
|
+
|
358
|
284
|
SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
|
359
|
285
|
getfilename(0, fname);
|
360
|
286
|
lcd_setstatus(longFilename[0] ? longFilename : fname);
|
361
|
287
|
}
|
362
|
|
- else
|
363
|
|
- {
|
|
288
|
+ else {
|
364
|
289
|
SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
|
365
|
290
|
SERIAL_PROTOCOL(fname);
|
366
|
291
|
SERIAL_PROTOCOLLNPGM(".");
|
367
|
292
|
}
|
368
|
293
|
}
|
369
|
|
- else
|
370
|
|
- { //write
|
371
|
|
- if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
|
372
|
|
- {
|
|
294
|
+ else { //write
|
|
295
|
+ if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
|
373
|
296
|
SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
|
374
|
297
|
SERIAL_PROTOCOL(fname);
|
375
|
298
|
SERIAL_PROTOCOLLNPGM(".");
|
376
|
299
|
}
|
377
|
|
- else
|
378
|
|
- {
|
|
300
|
+ else {
|
379
|
301
|
saving = true;
|
380
|
302
|
SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE);
|
381
|
303
|
SERIAL_PROTOCOLLN(name);
|
382
|
304
|
lcd_setstatus(fname);
|
383
|
305
|
}
|
384
|
306
|
}
|
385
|
|
-
|
386
|
307
|
}
|
387
|
308
|
|
388
|
|
-void CardReader::removeFile(char* name)
|
389
|
|
-{
|
390
|
|
- if(!cardOK)
|
391
|
|
- return;
|
|
309
|
+void CardReader::removeFile(char* name) {
|
|
310
|
+ if (!cardOK) return;
|
|
311
|
+
|
392
|
312
|
file.close();
|
393
|
313
|
sdprinting = false;
|
394
|
|
-
|
395
|
|
-
|
|
314
|
+
|
396
|
315
|
SdFile myDir;
|
397
|
|
- curDir=&root;
|
398
|
|
- char *fname=name;
|
399
|
|
-
|
400
|
|
- char *dirname_start,*dirname_end;
|
401
|
|
- if(name[0]=='/')
|
402
|
|
- {
|
403
|
|
- dirname_start=strchr(name,'/')+1;
|
404
|
|
- while(dirname_start>0)
|
405
|
|
- {
|
406
|
|
- dirname_end=strchr(dirname_start,'/');
|
407
|
|
- //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
|
408
|
|
- //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
|
409
|
|
- if(dirname_end>0 && dirname_end>dirname_start)
|
410
|
|
- {
|
|
316
|
+ curDir = &root;
|
|
317
|
+ char *fname = name;
|
|
318
|
+
|
|
319
|
+ char *dirname_start, *dirname_end;
|
|
320
|
+ if (name[0] == '/') {
|
|
321
|
+ dirname_start = strchr(name, '/') + 1;
|
|
322
|
+ while (dirname_start > 0) {
|
|
323
|
+ dirname_end = strchr(dirname_start, '/');
|
|
324
|
+ //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start - name));
|
|
325
|
+ //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end - name));
|
|
326
|
+ if (dirname_end > 0 && dirname_end > dirname_start) {
|
411
|
327
|
char subdirname[FILENAME_LENGTH];
|
412
|
|
- strncpy(subdirname, dirname_start, dirname_end-dirname_start);
|
413
|
|
- subdirname[dirname_end-dirname_start]=0;
|
|
328
|
+ strncpy(subdirname, dirname_start, dirname_end - dirname_start);
|
|
329
|
+ subdirname[dirname_end - dirname_start] = 0;
|
414
|
330
|
SERIAL_ECHOLN(subdirname);
|
415
|
|
- if(!myDir.open(curDir,subdirname,O_READ))
|
416
|
|
- {
|
|
331
|
+ if (!myDir.open(curDir, subdirname, O_READ)) {
|
417
|
332
|
SERIAL_PROTOCOLPGM("open failed, File: ");
|
418
|
333
|
SERIAL_PROTOCOL(subdirname);
|
419
|
334
|
SERIAL_PROTOCOLLNPGM(".");
|
420
|
335
|
return;
|
421
|
336
|
}
|
422
|
|
- else
|
423
|
|
- {
|
|
337
|
+ else {
|
424
|
338
|
//SERIAL_ECHOLN("dive ok");
|
425
|
339
|
}
|
426
|
|
-
|
427
|
|
- curDir=&myDir;
|
428
|
|
- dirname_start=dirname_end+1;
|
|
340
|
+
|
|
341
|
+ curDir = &myDir;
|
|
342
|
+ dirname_start = dirname_end + 1;
|
429
|
343
|
}
|
430
|
|
- else // the reminder after all /fsa/fdsa/ is the filename
|
431
|
|
- {
|
432
|
|
- fname=dirname_start;
|
433
|
|
- //SERIAL_ECHOLN("remaider");
|
|
344
|
+ else { // the remainder after all /fsa/fdsa/ is the filename
|
|
345
|
+ fname = dirname_start;
|
|
346
|
+ //SERIAL_ECHOLN("remainder");
|
434
|
347
|
//SERIAL_ECHOLN(fname);
|
435
|
348
|
break;
|
436
|
349
|
}
|
437
|
|
-
|
438
|
350
|
}
|
439
|
351
|
}
|
440
|
|
- else //relative path
|
441
|
|
- {
|
442
|
|
- curDir=&workDir;
|
|
352
|
+ else { // relative path
|
|
353
|
+ curDir = &workDir;
|
|
354
|
+ }
|
|
355
|
+
|
|
356
|
+ if (file.remove(curDir, fname)) {
|
|
357
|
+ SERIAL_PROTOCOLPGM("File deleted:");
|
|
358
|
+ SERIAL_PROTOCOLLN(fname);
|
|
359
|
+ sdpos = 0;
|
|
360
|
+ }
|
|
361
|
+ else {
|
|
362
|
+ SERIAL_PROTOCOLPGM("Deletion failed, File: ");
|
|
363
|
+ SERIAL_PROTOCOL(fname);
|
|
364
|
+ SERIAL_PROTOCOLLNPGM(".");
|
443
|
365
|
}
|
444
|
|
- if (file.remove(curDir, fname))
|
445
|
|
- {
|
446
|
|
- SERIAL_PROTOCOLPGM("File deleted:");
|
447
|
|
- SERIAL_PROTOCOLLN(fname);
|
448
|
|
- sdpos = 0;
|
449
|
|
- }
|
450
|
|
- else
|
451
|
|
- {
|
452
|
|
- SERIAL_PROTOCOLPGM("Deletion failed, File: ");
|
453
|
|
- SERIAL_PROTOCOL(fname);
|
454
|
|
- SERIAL_PROTOCOLLNPGM(".");
|
455
|
|
- }
|
456
|
|
-
|
457
|
366
|
}
|
458
|
367
|
|
459
|
|
-void CardReader::getStatus()
|
460
|
|
-{
|
461
|
|
- if(cardOK){
|
|
368
|
+void CardReader::getStatus() {
|
|
369
|
+ if (cardOK) {
|
462
|
370
|
SERIAL_PROTOCOLPGM(MSG_SD_PRINTING_BYTE);
|
463
|
371
|
SERIAL_PROTOCOL(sdpos);
|
464
|
372
|
SERIAL_PROTOCOLPGM("/");
|
465
|
373
|
SERIAL_PROTOCOLLN(filesize);
|
466
|
374
|
}
|
467
|
|
- else{
|
|
375
|
+ else {
|
468
|
376
|
SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
|
469
|
377
|
}
|
470
|
378
|
}
|
471
|
|
-void CardReader::write_command(char *buf)
|
472
|
|
-{
|
|
379
|
+
|
|
380
|
+void CardReader::write_command(char *buf) {
|
473
|
381
|
char* begin = buf;
|
474
|
382
|
char* npos = 0;
|
475
|
383
|
char* end = buf + strlen(buf) - 1;
|
476
|
384
|
|
477
|
385
|
file.writeError = false;
|
478
|
|
- if((npos = strchr(buf, 'N')) != NULL)
|
479
|
|
- {
|
|
386
|
+ if ((npos = strchr(buf, 'N')) != NULL) {
|
480
|
387
|
begin = strchr(npos, ' ') + 1;
|
481
|
388
|
end = strchr(npos, '*') - 1;
|
482
|
389
|
}
|
|
@@ -484,162 +391,129 @@ void CardReader::write_command(char *buf)
|
484
|
391
|
end[2] = '\n';
|
485
|
392
|
end[3] = '\0';
|
486
|
393
|
file.write(begin);
|
487
|
|
- if (file.writeError)
|
488
|
|
- {
|
|
394
|
+ if (file.writeError) {
|
489
|
395
|
SERIAL_ERROR_START;
|
490
|
396
|
SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
|
491
|
397
|
}
|
492
|
398
|
}
|
493
|
399
|
|
|
400
|
+void CardReader::checkautostart(bool force) {
|
|
401
|
+ if (!force && (!autostart_stilltocheck || autostart_atmillis < millis()))
|
|
402
|
+ return;
|
494
|
403
|
|
495
|
|
-void CardReader::checkautostart(bool force)
|
496
|
|
-{
|
497
|
|
- if(!force)
|
498
|
|
- {
|
499
|
|
- if(!autostart_stilltocheck)
|
500
|
|
- return;
|
501
|
|
- if(autostart_atmillis<millis())
|
502
|
|
- return;
|
503
|
|
- }
|
504
|
|
- autostart_stilltocheck=false;
|
505
|
|
- if(!cardOK)
|
506
|
|
- {
|
|
404
|
+ autostart_stilltocheck = false;
|
|
405
|
+
|
|
406
|
+ if (!cardOK) {
|
507
|
407
|
initsd();
|
508
|
|
- if(!cardOK) //fail
|
509
|
|
- return;
|
|
408
|
+ if (!cardOK) return; // fail
|
510
|
409
|
}
|
511
|
|
-
|
|
410
|
+
|
512
|
411
|
char autoname[30];
|
513
|
412
|
sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
|
514
|
|
- for(int8_t i=0;i<(int8_t)strlen(autoname);i++)
|
515
|
|
- autoname[i]=tolower(autoname[i]);
|
|
413
|
+ for (int8_t i = 0; i < (int8_t)strlen(autoname); i++) autoname[i] = tolower(autoname[i]);
|
|
414
|
+
|
516
|
415
|
dir_t p;
|
517
|
416
|
|
518
|
417
|
root.rewind();
|
519
|
|
-
|
520
|
|
- bool found=false;
|
521
|
|
- while (root.readDir(p, NULL) > 0)
|
522
|
|
- {
|
523
|
|
- for(int8_t i=0;i<(int8_t)strlen((char*)p.name);i++)
|
524
|
|
- p.name[i]=tolower(p.name[i]);
|
525
|
|
- //Serial.print((char*)p.name);
|
526
|
|
- //Serial.print(" ");
|
527
|
|
- //Serial.println(autoname);
|
528
|
|
- if(p.name[9]!='~') //skip safety copies
|
529
|
|
- if(strncmp((char*)p.name,autoname,5)==0)
|
530
|
|
- {
|
531
|
|
- char cmd[30];
|
532
|
418
|
|
|
419
|
+ bool found = false;
|
|
420
|
+ while (root.readDir(p, NULL) > 0) {
|
|
421
|
+ for (int8_t i = 0; i < (int8_t)strlen((char*)p.name); i++) p.name[i] = tolower(p.name[i]);
|
|
422
|
+ if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
|
|
423
|
+ char cmd[30];
|
533
|
424
|
sprintf_P(cmd, PSTR("M23 %s"), autoname);
|
534
|
425
|
enquecommand(cmd);
|
535
|
426
|
enquecommands_P(PSTR("M24"));
|
536
|
|
- found=true;
|
|
427
|
+ found = true;
|
537
|
428
|
}
|
538
|
429
|
}
|
539
|
|
- if(!found)
|
540
|
|
- autostart_index=-1;
|
|
430
|
+ if (!found)
|
|
431
|
+ autostart_index = -1;
|
541
|
432
|
else
|
542
|
433
|
autostart_index++;
|
543
|
434
|
}
|
544
|
435
|
|
545
|
|
-void CardReader::closefile(bool store_location)
|
546
|
|
-{
|
|
436
|
+void CardReader::closefile(bool store_location) {
|
547
|
437
|
file.sync();
|
548
|
438
|
file.close();
|
549
|
|
- saving = false;
|
550
|
|
- logging = false;
|
551
|
|
-
|
552
|
|
- if(store_location)
|
553
|
|
- {
|
|
439
|
+ saving = logging = false;
|
|
440
|
+
|
|
441
|
+ if (store_location) {
|
554
|
442
|
//future: store printer state, filename and position for continuing a stopped print
|
555
|
443
|
// so one can unplug the printer and continue printing the next day.
|
556
|
|
-
|
557
|
444
|
}
|
558
|
|
-
|
559
|
|
-
|
560
|
445
|
}
|
561
|
446
|
|
562
|
|
-void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/)
|
563
|
|
-{
|
564
|
|
- curDir=&workDir;
|
565
|
|
- lsAction=LS_GetFilename;
|
566
|
|
- nrFiles=nr;
|
|
447
|
+/**
|
|
448
|
+ * Get the name of a file in the current directory by index
|
|
449
|
+ */
|
|
450
|
+void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
|
|
451
|
+ curDir = &workDir;
|
|
452
|
+ lsAction = LS_GetFilename;
|
|
453
|
+ nrFiles = nr;
|
567
|
454
|
curDir->rewind();
|
568
|
|
- lsDive("",*curDir,match);
|
569
|
|
-
|
|
455
|
+ lsDive("", *curDir, match);
|
570
|
456
|
}
|
571
|
457
|
|
572
|
|
-uint16_t CardReader::getnrfilenames()
|
573
|
|
-{
|
574
|
|
- curDir=&workDir;
|
575
|
|
- lsAction=LS_Count;
|
576
|
|
- nrFiles=0;
|
|
458
|
+uint16_t CardReader::getnrfilenames() {
|
|
459
|
+ curDir = &workDir;
|
|
460
|
+ lsAction = LS_Count;
|
|
461
|
+ nrFiles = 0;
|
577
|
462
|
curDir->rewind();
|
578
|
|
- lsDive("",*curDir);
|
|
463
|
+ lsDive("", *curDir);
|
579
|
464
|
//SERIAL_ECHOLN(nrFiles);
|
580
|
465
|
return nrFiles;
|
581
|
466
|
}
|
582
|
467
|
|
583
|
|
-void CardReader::chdir(const char * relpath)
|
584
|
|
-{
|
|
468
|
+void CardReader::chdir(const char * relpath) {
|
585
|
469
|
SdFile newfile;
|
586
|
|
- SdFile *parent=&root;
|
587
|
|
-
|
588
|
|
- if(workDir.isOpen())
|
589
|
|
- parent=&workDir;
|
590
|
|
-
|
591
|
|
- if(!newfile.open(*parent,relpath, O_READ))
|
592
|
|
- {
|
593
|
|
- SERIAL_ECHO_START;
|
594
|
|
- SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
|
595
|
|
- SERIAL_ECHOLN(relpath);
|
|
470
|
+ SdFile *parent = &root;
|
|
471
|
+
|
|
472
|
+ if (workDir.isOpen()) parent = &workDir;
|
|
473
|
+
|
|
474
|
+ if (!newfile.open(*parent, relpath, O_READ)) {
|
|
475
|
+ SERIAL_ECHO_START;
|
|
476
|
+ SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
|
|
477
|
+ SERIAL_ECHOLN(relpath);
|
596
|
478
|
}
|
597
|
|
- else
|
598
|
|
- {
|
|
479
|
+ else {
|
599
|
480
|
if (workDirDepth < MAX_DIR_DEPTH) {
|
600
|
|
- for (int d = ++workDirDepth; d--;)
|
601
|
|
- workDirParents[d+1] = workDirParents[d];
|
602
|
|
- workDirParents[0]=*parent;
|
|
481
|
+ ++workDirDepth;
|
|
482
|
+ for (int d = workDirDepth; d--;) workDirParents[d + 1] = workDirParents[d];
|
|
483
|
+ workDirParents[0] = *parent;
|
603
|
484
|
}
|
604
|
|
- workDir=newfile;
|
|
485
|
+ workDir = newfile;
|
605
|
486
|
}
|
606
|
487
|
}
|
607
|
488
|
|
608
|
|
-void CardReader::updir()
|
609
|
|
-{
|
610
|
|
- if(workDirDepth > 0)
|
611
|
|
- {
|
|
489
|
+void CardReader::updir() {
|
|
490
|
+ if (workDirDepth > 0) {
|
612
|
491
|
--workDirDepth;
|
613
|
492
|
workDir = workDirParents[0];
|
614
|
|
- int d;
|
615
|
493
|
for (int d = 0; d < workDirDepth; d++)
|
616
|
494
|
workDirParents[d] = workDirParents[d+1];
|
617
|
495
|
}
|
618
|
496
|
}
|
619
|
497
|
|
620
|
|
-
|
621
|
|
-void CardReader::printingHasFinished()
|
622
|
|
-{
|
623
|
|
- st_synchronize();
|
624
|
|
- if(file_subcall_ctr>0) //heading up to a parent file that called current as a procedure.
|
625
|
|
- {
|
626
|
|
- file.close();
|
627
|
|
- file_subcall_ctr--;
|
628
|
|
- openFile(filenames[file_subcall_ctr],true,true);
|
629
|
|
- setIndex(filespos[file_subcall_ctr]);
|
630
|
|
- startFileprint();
|
631
|
|
- }
|
632
|
|
- else
|
633
|
|
- {
|
634
|
|
- quickStop();
|
635
|
|
- file.close();
|
636
|
|
- sdprinting = false;
|
637
|
|
- if(SD_FINISHED_STEPPERRELEASE)
|
638
|
|
- {
|
639
|
|
- //finishAndDisableSteppers();
|
640
|
|
- enquecommands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
|
641
|
|
- }
|
642
|
|
- autotempShutdown();
|
|
498
|
+void CardReader::printingHasFinished() {
|
|
499
|
+ st_synchronize();
|
|
500
|
+ if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure.
|
|
501
|
+ file.close();
|
|
502
|
+ file_subcall_ctr--;
|
|
503
|
+ openFile(filenames[file_subcall_ctr], true, true);
|
|
504
|
+ setIndex(filespos[file_subcall_ctr]);
|
|
505
|
+ startFileprint();
|
|
506
|
+ }
|
|
507
|
+ else {
|
|
508
|
+ quickStop();
|
|
509
|
+ file.close();
|
|
510
|
+ sdprinting = false;
|
|
511
|
+ if (SD_FINISHED_STEPPERRELEASE) {
|
|
512
|
+ //finishAndDisableSteppers();
|
|
513
|
+ enquecommands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
|
643
|
514
|
}
|
|
515
|
+ autotempShutdown();
|
|
516
|
+ }
|
644
|
517
|
}
|
|
518
|
+
|
645
|
519
|
#endif //SDSUPPORT
|