From 61932b859efb472f7075c390b12a7b41c6e17230 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 12 Aug 2016 03:21:10 -0700 Subject: [PATCH 1/2] Fix bug in CardReader::stopSDPrint If the SD print is paused, it cannot be stopped --- Marlin/cardreader.cpp | 20 ++++++-------------- Marlin/cardreader.h | 2 +- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/Marlin/cardreader.cpp b/Marlin/cardreader.cpp index a0cdadc970..1eeab46437 100644 --- a/Marlin/cardreader.cpp +++ b/Marlin/cardreader.cpp @@ -276,19 +276,12 @@ void CardReader::openAndPrintFile(const char *name) { } void CardReader::startFileprint() { - if (cardOK) - sdprinting = true; -} - -void CardReader::pauseSDPrint() { - if (sdprinting) sdprinting = false; + if (cardOK) sdprinting = true; } void CardReader::stopSDPrint() { - if (sdprinting) { - sdprinting = false; - file.close(); - } + sdprinting = false; + if (isFileOpen()) file.close(); } void CardReader::openLogFile(char* name) { @@ -340,7 +333,6 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) { SERIAL_ECHOPGM("Now doing file: "); SERIAL_ECHOLN(name); } - file.close(); } else { //opening fresh file file_subcall_ctr = 0; //resetting procedure depth in case user cancels print while in procedure @@ -348,7 +340,8 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) { SERIAL_ECHOPGM("Now fresh file: "); SERIAL_ECHOLN(name); } - sdprinting = false; + + stopSDPrint(); SdFile myDir; curDir = &root; @@ -425,8 +418,7 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) { void CardReader::removeFile(char* name) { if (!cardOK) return; - file.close(); - sdprinting = false; + stopSDPrint(); SdFile myDir; curDir = &root; diff --git a/Marlin/cardreader.h b/Marlin/cardreader.h index 8c22e581c8..b0ed92b89d 100644 --- a/Marlin/cardreader.h +++ b/Marlin/cardreader.h @@ -51,7 +51,6 @@ public: void release(); void openAndPrintFile(const char *name); void startFileprint(); - void pauseSDPrint(); void stopSDPrint(); void getStatus(); void printingHasFinished(); @@ -70,6 +69,7 @@ public: void updir(); void setroot(); + FORCE_INLINE void pauseSDPrint() { sdprinting = false; } FORCE_INLINE bool isFileOpen() { return file.isOpen(); } FORCE_INLINE bool eof() { return sdpos >= filesize; } FORCE_INLINE int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); } From 192ac2dfd759da68c8333559de8568b8f0416aba Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 12 Aug 2016 03:22:02 -0700 Subject: [PATCH 2/2] Squish some CardReader code --- Marlin/Marlin.h | 49 +++++++++++++++++++++---------------------- Marlin/cardreader.cpp | 43 +++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/Marlin/Marlin.h b/Marlin/Marlin.h index 9579d3daa7..28c3250a30 100644 --- a/Marlin/Marlin.h +++ b/Marlin/Marlin.h @@ -60,35 +60,34 @@ #include "stopwatch.h" #endif -#define SERIAL_CHAR(x) MYSERIAL.write(x) +extern const char echomagic[] PROGMEM; +extern const char errormagic[] PROGMEM; + +#define SERIAL_CHAR(x) (MYSERIAL.write(x)) #define SERIAL_EOL SERIAL_CHAR('\n') -#define SERIAL_PROTOCOLCHAR(x) SERIAL_CHAR(x) -#define SERIAL_PROTOCOL(x) MYSERIAL.print(x) -#define SERIAL_PROTOCOL_F(x,y) MYSERIAL.print(x,y) -#define SERIAL_PROTOCOLPGM(x) serialprintPGM(PSTR(x)) -#define SERIAL_PROTOCOLLN(x) do{ MYSERIAL.print(x); SERIAL_EOL; }while(0) -#define SERIAL_PROTOCOLLNPGM(x) do{ serialprintPGM(PSTR(x "\n")); }while(0) +#define SERIAL_PROTOCOLCHAR(x) SERIAL_CHAR(x) +#define SERIAL_PROTOCOL(x) (MYSERIAL.print(x)) +#define SERIAL_PROTOCOL_F(x,y) (MYSERIAL.print(x,y)) +#define SERIAL_PROTOCOLPGM(x) (serialprintPGM(PSTR(x))) +#define SERIAL_PROTOCOLLN(x) do{ MYSERIAL.print(x); SERIAL_EOL; }while(0) +#define SERIAL_PROTOCOLLNPGM(x) (serialprintPGM(PSTR(x "\n"))) +#define SERIAL_PROTOCOLPAIR(name, value) (serial_echopair_P(PSTR(name),(value))) +#define SERIAL_PROTOCOLLNPAIR(name, value) do{ SERIAL_PROTOCOLPAIR(name, value); SERIAL_EOL; }while(0) -#define SERIAL_PROTOCOLPAIR(name, value) SERIAL_ECHOPAIR(name, value) -#define SERIAL_PROTOCOLLNPAIR(name, value) do{ SERIAL_ECHOPAIR(name, value); SERIAL_EOL; }while(0) +#define SERIAL_ECHO_START (serialprintPGM(echomagic)) +#define SERIAL_ECHO(x) SERIAL_PROTOCOL(x) +#define SERIAL_ECHOPGM(x) SERIAL_PROTOCOLPGM(x) +#define SERIAL_ECHOLN(x) SERIAL_PROTOCOLLN(x) +#define SERIAL_ECHOLNPGM(x) SERIAL_PROTOCOLLNPGM(x) +#define SERIAL_ECHOPAIR(name,value) SERIAL_PROTOCOLPAIR(name, value) +#define SERIAL_ECHOLNPAIR(name, value) SERIAL_PROTOCOLLNPAIR(name, value) -extern const char errormagic[] PROGMEM; -extern const char echomagic[] PROGMEM; - -#define SERIAL_ERROR_START serialprintPGM(errormagic) -#define SERIAL_ERROR(x) SERIAL_PROTOCOL(x) -#define SERIAL_ERRORPGM(x) SERIAL_PROTOCOLPGM(x) -#define SERIAL_ERRORLN(x) SERIAL_PROTOCOLLN(x) -#define SERIAL_ERRORLNPGM(x) SERIAL_PROTOCOLLNPGM(x) - -#define SERIAL_ECHO_START serialprintPGM(echomagic) -#define SERIAL_ECHO(x) SERIAL_PROTOCOL(x) -#define SERIAL_ECHOPGM(x) SERIAL_PROTOCOLPGM(x) -#define SERIAL_ECHOLN(x) SERIAL_PROTOCOLLN(x) -#define SERIAL_ECHOLNPGM(x) SERIAL_PROTOCOLLNPGM(x) - -#define SERIAL_ECHOPAIR(name,value) (serial_echopair_P(PSTR(name),(value))) +#define SERIAL_ERROR_START (serialprintPGM(errormagic)) +#define SERIAL_ERROR(x) SERIAL_PROTOCOL(x) +#define SERIAL_ERRORPGM(x) SERIAL_PROTOCOLPGM(x) +#define SERIAL_ERRORLN(x) SERIAL_PROTOCOLLN(x) +#define SERIAL_ERRORLNPGM(x) SERIAL_PROTOCOLLNPGM(x) void serial_echopair_P(const char* s_P, const char *v); void serial_echopair_P(const char* s_P, char v); diff --git a/Marlin/cardreader.cpp b/Marlin/cardreader.cpp index 1eeab46437..319665aa65 100644 --- a/Marlin/cardreader.cpp +++ b/Marlin/cardreader.cpp @@ -32,12 +32,9 @@ #if ENABLED(SDSUPPORT) CardReader::CardReader() { + sdprinting = cardOK = saving = logging = false; filesize = 0; sdpos = 0; - sdprinting = false; - cardOK = false; - saving = false; - logging = false; workDirDepth = 0; file_subcall_ctr = 0; memset(workDirParents, 0, sizeof(workDirParents)); @@ -303,8 +300,11 @@ void CardReader::getAbsFilename(char *t) { } void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) { + if (!cardOK) return; - if (file.isOpen()) { //replacing current file by new file, or subfile call + + uint8_t doing = 0; + if (isFileOpen()) { //replacing current file by new file, or subfile call if (push_current) { if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) { SERIAL_ERROR_START; @@ -314,31 +314,30 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) { return; } - SERIAL_ECHO_START; - SERIAL_ECHOPGM("SUBROUTINE CALL target:\""); - SERIAL_ECHO(name); - SERIAL_ECHOPGM("\" parent:\""); - - //store current filename and position + // Store current filename and position getAbsFilename(proc_filenames[file_subcall_ctr]); - SERIAL_ECHO(proc_filenames[file_subcall_ctr]); - SERIAL_ECHOPGM("\" pos"); - SERIAL_ECHOLN(sdpos); + SERIAL_ECHO_START; + SERIAL_ECHOPAIR("SUBROUTINE CALL target:\"", name); + SERIAL_ECHOPAIR("\" parent:\"", proc_filenames[file_subcall_ctr]); + SERIAL_ECHOLNPAIR("\" pos", sdpos); filespos[file_subcall_ctr] = sdpos; file_subcall_ctr++; } else { - SERIAL_ECHO_START; - SERIAL_ECHOPGM("Now doing file: "); - SERIAL_ECHOLN(name); + doing = 1; } } - else { //opening fresh file - file_subcall_ctr = 0; //resetting procedure depth in case user cancels print while in procedure + else { // Opening fresh file + doing = 2; + file_subcall_ctr = 0; // Reset procedure depth in case user cancels print while in procedure + } + + if (doing) { SERIAL_ECHO_START; - SERIAL_ECHOPGM("Now fresh file: "); - SERIAL_ECHOLN(name); + SERIAL_ECHOPGM("Now "); + SERIAL_ECHO(doing == 1 ? "doing" : "fresh"); + SERIAL_ECHOLNPAIR(" file: ", name); } stopSDPrint(); @@ -346,8 +345,8 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) { SdFile myDir; curDir = &root; char *fname = name; - char *dirname_start, *dirname_end; + if (name[0] == '/') { dirname_start = &name[1]; while (dirname_start != NULL) {