Browse Source

Cardreader read/write open methods

Scott Lahteine 5 years ago
parent
commit
f3d64b7115

+ 1
- 1
Marlin/src/feature/binary_protocol.h View File

@@ -77,7 +77,7 @@ private:
77 77
   static bool file_open(char* filename) {
78 78
     if (!dummy_transfer) {
79 79
       card.mount();
80
-      card.openFile(filename, false);
80
+      card.openFileWrite(filename);
81 81
       if (!card.isFileOpen()) return false;
82 82
     }
83 83
     transfer_active = true;

+ 1
- 1
Marlin/src/gcode/sdcard/M23.cpp View File

@@ -36,7 +36,7 @@
36 36
 void GcodeSuite::M23() {
37 37
   // Simplify3D includes the size, so zero out all spaces (#7227)
38 38
   for (char *fn = parser.string_arg; *fn; ++fn) if (*fn == ' ') *fn = '\0';
39
-  card.openFile(parser.string_arg, true);
39
+  card.openFileRead(parser.string_arg);
40 40
 
41 41
   #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
42 42
     ui.set_progress(0);

+ 2
- 2
Marlin/src/gcode/sdcard/M28_M29.cpp View File

@@ -54,11 +54,11 @@ void GcodeSuite::M28() {
54 54
       #endif
55 55
     }
56 56
     else
57
-      card.openFile(p, false);
57
+      card.openFileWrite(p);
58 58
 
59 59
   #else
60 60
 
61
-    card.openFile(parser.string_arg, false);
61
+    card.openFileWrite(parser.string_arg);
62 62
 
63 63
   #endif
64 64
 }

+ 3
- 4
Marlin/src/gcode/sdcard/M32.cpp View File

@@ -26,8 +26,7 @@
26 26
 
27 27
 #include "../gcode.h"
28 28
 #include "../../sd/cardreader.h"
29
-#include "../../module/printcounter.h"
30
-#include "../../module/planner.h"
29
+#include "../../module/planner.h" // for synchronize()
31 30
 
32 31
 #include "../../Marlin.h" // for startOrResumeJob
33 32
 
@@ -45,9 +44,9 @@ void GcodeSuite::M32() {
45 44
   if (IS_SD_PRINTING()) planner.synchronize();
46 45
 
47 46
   if (card.isMounted()) {
48
-    const bool call_procedure = parser.boolval('P');
47
+    const uint8_t call_procedure = parser.boolval('P');
49 48
 
50
-    card.openFile(parser.string_arg, true, call_procedure);
49
+    card.openFileRead(parser.string_arg, call_procedure);
51 50
 
52 51
     if (parser.seenval('S')) card.setIndex(parser.value_long());
53 52
 

+ 77
- 51
Marlin/src/sd/cardreader.cpp View File

@@ -418,7 +418,7 @@ void CardReader::stopSDPrint(
418 418
 
419 419
 void CardReader::openLogFile(char * const path) {
420 420
   flag.logging = true;
421
-  openFile(path, false);
421
+  openFileWrite(path);
422 422
 }
423 423
 
424 424
 //
@@ -444,16 +444,42 @@ void CardReader::getAbsFilename(char *dst) {
444 444
   *dst = '\0';
445 445
 }
446 446
 
447
+void openFailed(const char * const fname) {
448
+  SERIAL_ECHOLNPAIR(MSG_SD_OPEN_FILE_FAIL, fname, ".");
449
+}
450
+
451
+void announceOpen(const uint8_t doing, const char * const path) {
452
+  if (doing) {
453
+    SERIAL_ECHO_START();
454
+    SERIAL_ECHOPGM("Now ");
455
+    serialprintPGM(doing == 1 ? PSTR("doing") : PSTR("fresh"));
456
+    SERIAL_ECHOLNPAIR(" file: ", path);
457
+  }
458
+}
459
+
447 460
 //
448
-// Open a file by DOS path - for read or write
461
+// Open a file by DOS path for read
462
+// The 'subcall_type' flag indicates...
463
+//   - 0 : Standard open from host or user interface.
464
+//   - 1 : (file open) Opening a new sub-procedure.
465
+//   - 1 : (no file open) Opening a macro (M98).
466
+//   - 2 : Resuming from a sub-procedure
449 467
 //
450
-void CardReader::openFile(char * const path, const bool read, const bool subcall/*=false*/) {
451
-
468
+void CardReader::openFileRead(char * const path, const uint8_t subcall_type/*=0*/) {
452 469
   if (!isMounted()) return;
453 470
 
454
-  uint8_t doing = 0;
455
-  if (isFileOpen()) {                     // Replacing current file or doing a subroutine
456
-    if (subcall) {
471
+  switch (subcall_type) {
472
+    case 0:      // Starting a new print. "Now fresh file: ..."
473
+      announceOpen(2, path);
474
+      file_subcall_ctr = 0;
475
+      break;
476
+
477
+    case 1:      // Starting a sub-procedure
478
+
479
+      // With no file is open it's a simple macro. "Now doing file: ..."
480
+      if (!isFileOpen()) { announceOpen(1, path); break; }
481
+
482
+      // Too deep? The firmware has to bail.
457 483
       if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
458 484
         SERIAL_ERROR_MSG("trying to call sub-gcode files with too many levels. MAX level is:" STRINGIFY(SD_PROCEDURE_DEPTH));
459 485
         kill();
@@ -464,25 +490,15 @@ void CardReader::openFile(char * const path, const bool read, const bool subcall
464 490
       getAbsFilename(proc_filenames[file_subcall_ctr]);
465 491
       filespos[file_subcall_ctr] = sdpos;
466 492
 
493
+      // For sub-procedures say 'SUBROUTINE CALL target: "..." parent: "..." pos12345'
467 494
       SERIAL_ECHO_START();
468 495
       SERIAL_ECHOLNPAIR("SUBROUTINE CALL target:\"", path, "\" parent:\"", proc_filenames[file_subcall_ctr], "\" pos", sdpos);
469 496
       file_subcall_ctr++;
470
-    }
471
-    else
472
-      doing = 1;
473
-  }
474
-  else if (subcall)       // Returning from a subcall?
475
-    SERIAL_ECHO_MSG("END SUBROUTINE");
476
-  else {                  // Opening fresh file
477
-    doing = 2;
478
-    file_subcall_ctr = 0; // Reset procedure depth in case user cancels print while in procedure
479
-  }
497
+      break;
480 498
 
481
-  if (doing) {
482
-    SERIAL_ECHO_START();
483
-    SERIAL_ECHOPGM("Now ");
484
-    serialprintPGM(doing == 1 ? PSTR("doing") : PSTR("fresh"));
485
-    SERIAL_ECHOLNPAIR(" file: ", path);
499
+    case 2:      // Resuming previous file after sub-procedure
500
+      SERIAL_ECHO_MSG("END SUBROUTINE");
501
+      break;
486 502
   }
487 503
 
488 504
   stopSDPrint();
@@ -491,35 +507,45 @@ void CardReader::openFile(char * const path, const bool read, const bool subcall
491 507
   const char * const fname = diveToFile(curDir, path);
492 508
   if (!fname) return;
493 509
 
494
-  if (read) {
495
-    if (file.open(curDir, fname, O_READ)) {
496
-      filesize = file.fileSize();
497
-      sdpos = 0;
498
-      SERIAL_ECHOLNPAIR(MSG_SD_FILE_OPENED, fname, MSG_SD_SIZE, filesize);
499
-      SERIAL_ECHOLNPGM(MSG_SD_FILE_SELECTED);
500
-
501
-      selectFileByName(fname);
502
-      ui.set_status(longFilename[0] ? longFilename : fname);
503
-      //if (longFilename[0]) {
504
-      //  SERIAL_ECHOPAIR(MSG_SD_FILE_LONG_NAME, longFilename);
505
-      //}
506
-    }
507
-    else
508
-      SERIAL_ECHOLNPAIR(MSG_SD_OPEN_FILE_FAIL, fname, ".");
510
+  if (file.open(curDir, fname, O_READ)) {
511
+    filesize = file.fileSize();
512
+    sdpos = 0;
513
+    SERIAL_ECHOLNPAIR(MSG_SD_FILE_OPENED, fname, MSG_SD_SIZE, filesize);
514
+    SERIAL_ECHOLNPGM(MSG_SD_FILE_SELECTED);
515
+
516
+    selectFileByName(fname);
517
+    ui.set_status(longFilename[0] ? longFilename : fname);
509 518
   }
510
-  else { //write
511
-    if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
512
-      SERIAL_ECHOLNPAIR(MSG_SD_OPEN_FILE_FAIL, fname, ".");
513
-    else {
514
-      flag.saving = true;
515
-      selectFileByName(fname);
516
-      #if ENABLED(EMERGENCY_PARSER)
517
-        emergency_parser.disable();
518
-      #endif
519
-      SERIAL_ECHOLNPAIR(MSG_SD_WRITE_TO_FILE, fname);
520
-      ui.set_status(fname);
521
-    }
519
+  else
520
+    openFailed(fname);
521
+}
522
+
523
+//
524
+// Open a file by DOS path for write
525
+//
526
+void CardReader::openFileWrite(char * const path) {
527
+  if (!isMounted()) return;
528
+
529
+  announceOpen(2, path);
530
+  file_subcall_ctr = 0;
531
+
532
+  stopSDPrint();
533
+
534
+  SdFile *curDir;
535
+  const char * const fname = diveToFile(curDir, path);
536
+  if (!fname) return;
537
+
538
+  if (file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
539
+    flag.saving = true;
540
+    selectFileByName(fname);
541
+    #if ENABLED(EMERGENCY_PARSER)
542
+      emergency_parser.disable();
543
+    #endif
544
+    SERIAL_ECHOLNPAIR(MSG_SD_WRITE_TO_FILE, fname);
545
+    ui.set_status(fname);
522 546
   }
547
+  else
548
+    openFailed(fname);
523 549
 }
524 550
 
525 551
 //
@@ -1035,9 +1061,9 @@ uint16_t CardReader::get_num_Files() {
1035 1061
 void CardReader::printingHasFinished() {
1036 1062
   planner.synchronize();
1037 1063
   file.close();
1038
-  if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure.
1064
+  if (file_subcall_ctr > 0) { // Resume calling file after closing procedure
1039 1065
     file_subcall_ctr--;
1040
-    openFile(proc_filenames[file_subcall_ctr], true, true);
1066
+    openFileRead(proc_filenames[file_subcall_ctr], 2); // 2 = Returning from sub-procedure
1041 1067
     setIndex(filespos[file_subcall_ctr]);
1042 1068
     startFileprint();
1043 1069
   }

+ 2
- 1
Marlin/src/sd/cardreader.h View File

@@ -83,7 +83,8 @@ public:
83 83
   static void checkautostart();
84 84
 
85 85
   // Basic file ops
86
-  static void openFile(char * const path, const bool read, const bool subcall=false);
86
+  static void openFileRead(char * const path, const uint8_t subcall=0);
87
+  static void openFileWrite(char * const path);
87 88
   static void closefile(const bool store_location=false);
88 89
   static void removeFile(const char * const name);
89 90
 

Loading…
Cancel
Save