Переглянути джерело

Cleanup of cardreader.*

- Apply standards to cardreader.*
- Fix minor issues with cardreader.cpp
- Apply standards to some other stale regions
Scott Lahteine 9 роки тому
джерело
коміт
f171656f8e
6 змінених файлів з 325 додано та 464 видалено
  1. 1
    8
      Marlin/BlinkM.cpp
  2. 3
    4
      Marlin/BlinkM.h
  3. 3
    3
      Marlin/ConfigurationStore.h
  4. 250
    376
      Marlin/cardreader.cpp
  5. 30
    34
      Marlin/cardreader.h
  6. 38
    39
      Marlin/digipot_mcp4451.cpp

+ 1
- 8
Marlin/BlinkM.cpp Переглянути файл

@@ -5,16 +5,9 @@
5 5
 #include "Marlin.h"
6 6
 #ifdef BLINKM
7 7
 
8
-#if (ARDUINO >= 100)
9
-  # include "Arduino.h"
10
-#else
11
-  # include "WProgram.h"
12
-#endif
13
-
14 8
 #include "BlinkM.h"
15 9
 
16
-void SendColors(byte red, byte grn, byte blu)
17
-{
10
+void SendColors(byte red, byte grn, byte blu) {
18 11
   Wire.begin(); 
19 12
   Wire.beginTransmission(0x09);
20 13
   Wire.write('o');                    //to disable ongoing script, only needs to be used once

+ 3
- 4
Marlin/BlinkM.h Переглянути файл

@@ -2,13 +2,12 @@
2 2
   BlinkM.h
3 3
   Library header file for BlinkM library
4 4
  */
5
-#if (ARDUINO >= 100)
6
-  # include "Arduino.h"
5
+#if ARDUINO >= 100
6
+  #include "Arduino.h"
7 7
 #else
8
-  # include "WProgram.h"
8
+  #include "WProgram.h"
9 9
 #endif
10 10
 
11 11
 #include "Wire.h"
12 12
 
13 13
 void SendColors(byte red, byte grn, byte blu);
14
-

+ 3
- 3
Marlin/ConfigurationStore.h Переглянути файл

@@ -1,5 +1,5 @@
1
-#ifndef CONFIG_STORE_H
2
-#define CONFIG_STORE_H
1
+#ifndef CONFIGURATIONSTORE_H
2
+#define CONFIGURATIONSTORE_H
3 3
 
4 4
 #include "Configuration.h"
5 5
 
@@ -19,4 +19,4 @@ void Config_ResetDefault();
19 19
   FORCE_INLINE void Config_RetrieveSettings() { Config_ResetDefault(); Config_PrintSettings(); }
20 20
 #endif
21 21
 
22
-#endif // __CONFIG_STORE_H
22
+#endif //CONFIGURATIONSTORE_H

+ 250
- 376
Marlin/cardreader.cpp Переглянути файл

@@ -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

+ 30
- 34
Marlin/cardreader.h Переглянути файл

@@ -3,21 +3,21 @@
3 3
 
4 4
 #ifdef SDSUPPORT
5 5
 
6
-#define MAX_DIR_DEPTH 10
6
+#define MAX_DIR_DEPTH 10          // Maximum folder depth
7 7
 
8 8
 #include "SdFile.h"
9
-enum LsAction {LS_SerialPrint,LS_Count,LS_GetFilename};
10
-class CardReader
11
-{
9
+enum LsAction { LS_SerialPrint, LS_Count, LS_GetFilename };
10
+
11
+class CardReader {
12 12
 public:
13 13
   CardReader();
14
-  
14
+
15 15
   void initsd();
16 16
   void write_command(char *buf);
17 17
   //files auto[0-9].g on the sd card are performed in a row
18 18
   //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
19 19
 
20
-  void checkautostart(bool x); 
20
+  void checkautostart(bool x);
21 21
   void openFile(char* name,bool read,bool replace_current=true);
22 22
   void openLogFile(char* name);
23 23
   void removeFile(char* name);
@@ -30,9 +30,8 @@ public:
30 30
 
31 31
   void getfilename(uint16_t nr, const char* const match=NULL);
32 32
   uint16_t getnrfilenames();
33
-  
33
+
34 34
   void getAbsFilename(char *t);
35
-  
36 35
 
37 36
   void ls();
38 37
   void chdir(const char * relpath);
@@ -41,56 +40,52 @@ public:
41 40
 
42 41
 
43 42
   FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
44
-  FORCE_INLINE bool eof() { return sdpos>=filesize ;};
45
-  FORCE_INLINE int16_t get() {  sdpos = file.curPosition();return (int16_t)file.read();};
46
-  FORCE_INLINE void setIndex(long index) {sdpos = index;file.seekSet(index);};
47
-  FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;};
48
-  FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;};
43
+  FORCE_INLINE bool eof() { return sdpos >= filesize; }
44
+  FORCE_INLINE int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
45
+  FORCE_INLINE void setIndex(long index) { sdpos = index; file.seekSet(index); }
46
+  FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
47
+  FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
49 48
 
50 49
 public:
51
-  bool saving;
52
-  bool logging;
53
-  bool sdprinting;  
54
-  bool cardOK;
55
-  char filename[FILENAME_LENGTH];
56
-  char longFilename[LONG_FILENAME_LENGTH];
57
-  bool filenameIsDir;
50
+  bool saving, logging, sdprinting, cardOK, filenameIsDir;
51
+  char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
58 52
   int autostart_index;
59 53
 private:
60
-  SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
54
+  SdFile root, *curDir, workDir, workDirParents[MAX_DIR_DEPTH];
61 55
   uint16_t workDirDepth;
62 56
   Sd2Card card;
63 57
   SdVolume volume;
64 58
   SdFile file;
65 59
   #define SD_PROCEDURE_DEPTH 1
66
-  #define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH+MAX_DIR_DEPTH+1)
60
+  #define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH + MAX_DIR_DEPTH + 1)
67 61
   uint8_t file_subcall_ctr;
68 62
   uint32_t filespos[SD_PROCEDURE_DEPTH];
69 63
   char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
70 64
   uint32_t filesize;
71
-  //int16_t n;
72 65
   unsigned long autostart_atmillis;
73
-  uint32_t sdpos ;
66
+  uint32_t sdpos;
74 67
 
75 68
   bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
76
-  
69
+
77 70
   LsAction lsAction; //stored for recursion.
78
-  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.
71
+  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.
79 72
   char* diveDirName;
80 73
   void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
81 74
 };
75
+
82 76
 extern CardReader card;
77
+
83 78
 #define IS_SD_PRINTING (card.sdprinting)
84 79
 
85 80
 #if (SDCARDDETECT > -1)
86
-# ifdef SDCARDDETECTINVERTED 
87
-#  define IS_SD_INSERTED (READ(SDCARDDETECT)!=0)
88
-# else
89
-#  define IS_SD_INSERTED (READ(SDCARDDETECT)==0)
90
-# endif //SDCARDTETECTINVERTED
81
+  #ifdef SDCARDDETECTINVERTED
82
+    #define IS_SD_INSERTED (READ(SDCARDDETECT) != 0)
83
+  #else
84
+    #define IS_SD_INSERTED (READ(SDCARDDETECT) == 0)
85
+  #endif
91 86
 #else
92
-//If we don't have a card detect line, aways asume the card is inserted
93
-# define IS_SD_INSERTED true
87
+  //No card detect line? Assume the card is inserted.
88
+  #define IS_SD_INSERTED true
94 89
 #endif
95 90
 
96 91
 #else
@@ -98,4 +93,5 @@ extern CardReader card;
98 93
 #define IS_SD_PRINTING (false)
99 94
 
100 95
 #endif //SDSUPPORT
101
-#endif
96
+
97
+#endif //__CARDREADER_H

+ 38
- 39
Marlin/digipot_mcp4451.cpp Переглянути файл

@@ -1,59 +1,58 @@
1 1
 #include "Configuration.h"
2 2
 
3 3
 #ifdef DIGIPOT_I2C
4
+
4 5
 #include "Stream.h"
5 6
 #include "utility/twi.h"
6 7
 #include "Wire.h"
7 8
 
8 9
 // Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
9 10
 #if MB(5DPRINT)
10
-#define DIGIPOT_I2C_FACTOR 117.96
11
-#define DIGIPOT_I2C_MAX_CURRENT 1.736
11
+  #define DIGIPOT_I2C_FACTOR 117.96
12
+  #define DIGIPOT_I2C_MAX_CURRENT 1.736
12 13
 #else
13
-#define DIGIPOT_I2C_FACTOR 106.7
14
-#define DIGIPOT_I2C_MAX_CURRENT 2.5
14
+  #define DIGIPOT_I2C_FACTOR 106.7
15
+  #define DIGIPOT_I2C_MAX_CURRENT 2.5
15 16
 #endif
16 17
 
17
-static byte current_to_wiper( float current ){
18
-    return byte(ceil(float((DIGIPOT_I2C_FACTOR*current))));
18
+static byte current_to_wiper(float current) {
19
+  return byte(ceil(float((DIGIPOT_I2C_FACTOR*current))));
19 20
 }
20 21
 
21
-static void i2c_send(byte addr, byte a, byte b)
22
-{
23
-	Wire.beginTransmission(addr);
24
-    Wire.write(a);
25
-    Wire.write(b);
26
-    Wire.endTransmission();
22
+static void i2c_send(byte addr, byte a, byte b) {
23
+  Wire.beginTransmission(addr);
24
+  Wire.write(a);
25
+  Wire.write(b);
26
+  Wire.endTransmission();
27 27
 }
28 28
 
29 29
 // This is for the MCP4451 I2C based digipot
30
-void digipot_i2c_set_current( int channel, float current )
31
-{
32
-    current = min( (float) max( current, 0.0f ), DIGIPOT_I2C_MAX_CURRENT);
33
-    // these addresses are specific to Azteeg X3 Pro, can be set to others,
34
-    // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
35
-    byte addr= 0x2C; // channel 0-3
36
-    if(channel >= 4) {
37
-    	addr= 0x2E; // channel 4-7
38
-    	channel-= 4;
39
-    }
40
-
41
-    // Initial setup
42
-    i2c_send( addr, 0x40, 0xff );
43
-    i2c_send( addr, 0xA0, 0xff );
44
-
45
-    // Set actual wiper value
46
-    byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
47
-    i2c_send( addr, addresses[channel], current_to_wiper(current) );
30
+void digipot_i2c_set_current(int channel, float current) {
31
+  current = min( (float) max( current, 0.0f ), DIGIPOT_I2C_MAX_CURRENT);
32
+  // these addresses are specific to Azteeg X3 Pro, can be set to others,
33
+  // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
34
+  byte addr = 0x2C; // channel 0-3
35
+  if (channel >= 4) {
36
+  	addr = 0x2E; // channel 4-7
37
+  	channel -= 4;
38
+  }
39
+
40
+  // Initial setup
41
+  i2c_send(addr, 0x40, 0xff);
42
+  i2c_send(addr, 0xA0, 0xff);
43
+
44
+  // Set actual wiper value
45
+  byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
46
+  i2c_send(addr, addresses[channel], current_to_wiper(current));
48 47
 }
49 48
 
50
-void digipot_i2c_init()
51
-{
52
-    const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
53
-    Wire.begin();
54
-    // setup initial currents as defined in Configuration_adv.h
55
-    for(int i=0;i<=sizeof(digipot_motor_current)/sizeof(float);i++) {
56
-        digipot_i2c_set_current(i, digipot_motor_current[i]);
57
-    }
49
+void digipot_i2c_init() {
50
+  const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
51
+  Wire.begin();
52
+  // setup initial currents as defined in Configuration_adv.h
53
+  for(int i = 0; i <= sizeof(digipot_motor_current) / sizeof(float); i++) {
54
+    digipot_i2c_set_current(i, digipot_motor_current[i]);
55
+  }
58 56
 }
59
-#endif
57
+
58
+#endif //DIGIPOT_I2C

Завантаження…
Відмінити
Зберегти