Cardreader read/write open methods

This commit is contained in:
Scott Lahteine 2019-12-12 18:46:49 -06:00
parent 98382fcea5
commit f3d64b7115
6 changed files with 86 additions and 60 deletions

View File

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

View File

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

View File

@ -54,11 +54,11 @@ void GcodeSuite::M28() {
#endif #endif
} }
else else
card.openFile(p, false); card.openFileWrite(p);
#else #else
card.openFile(parser.string_arg, false); card.openFileWrite(parser.string_arg);
#endif #endif
} }

View File

@ -26,8 +26,7 @@
#include "../gcode.h" #include "../gcode.h"
#include "../../sd/cardreader.h" #include "../../sd/cardreader.h"
#include "../../module/printcounter.h" #include "../../module/planner.h" // for synchronize()
#include "../../module/planner.h"
#include "../../Marlin.h" // for startOrResumeJob #include "../../Marlin.h" // for startOrResumeJob
@ -45,9 +44,9 @@ void GcodeSuite::M32() {
if (IS_SD_PRINTING()) planner.synchronize(); if (IS_SD_PRINTING()) planner.synchronize();
if (card.isMounted()) { if (card.isMounted()) {
const bool call_procedure = parser.boolval('P'); const uint8_t call_procedure = parser.boolval('P');
card.openFile(parser.string_arg, true, call_procedure); card.openFileRead(parser.string_arg, call_procedure);
if (parser.seenval('S')) card.setIndex(parser.value_long()); if (parser.seenval('S')) card.setIndex(parser.value_long());

View File

@ -418,7 +418,7 @@ void CardReader::stopSDPrint(
void CardReader::openLogFile(char * const path) { void CardReader::openLogFile(char * const path) {
flag.logging = true; flag.logging = true;
openFile(path, false); openFileWrite(path);
} }
// //
@ -444,16 +444,42 @@ void CardReader::getAbsFilename(char *dst) {
*dst = '\0'; *dst = '\0';
} }
// void openFailed(const char * const fname) {
// Open a file by DOS path - for read or write SERIAL_ECHOLNPAIR(MSG_SD_OPEN_FILE_FAIL, fname, ".");
// }
void CardReader::openFile(char * const path, const bool read, const bool subcall/*=false*/) {
void announceOpen(const uint8_t doing, const char * const path) {
if (doing) {
SERIAL_ECHO_START();
SERIAL_ECHOPGM("Now ");
serialprintPGM(doing == 1 ? PSTR("doing") : PSTR("fresh"));
SERIAL_ECHOLNPAIR(" file: ", path);
}
}
//
// Open a file by DOS path for read
// The 'subcall_type' flag indicates...
// - 0 : Standard open from host or user interface.
// - 1 : (file open) Opening a new sub-procedure.
// - 1 : (no file open) Opening a macro (M98).
// - 2 : Resuming from a sub-procedure
//
void CardReader::openFileRead(char * const path, const uint8_t subcall_type/*=0*/) {
if (!isMounted()) return; if (!isMounted()) return;
uint8_t doing = 0; switch (subcall_type) {
if (isFileOpen()) { // Replacing current file or doing a subroutine case 0: // Starting a new print. "Now fresh file: ..."
if (subcall) { announceOpen(2, path);
file_subcall_ctr = 0;
break;
case 1: // Starting a sub-procedure
// With no file is open it's a simple macro. "Now doing file: ..."
if (!isFileOpen()) { announceOpen(1, path); break; }
// Too deep? The firmware has to bail.
if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) { if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
SERIAL_ERROR_MSG("trying to call sub-gcode files with too many levels. MAX level is:" STRINGIFY(SD_PROCEDURE_DEPTH)); SERIAL_ERROR_MSG("trying to call sub-gcode files with too many levels. MAX level is:" STRINGIFY(SD_PROCEDURE_DEPTH));
kill(); kill();
@ -464,25 +490,15 @@ void CardReader::openFile(char * const path, const bool read, const bool subcall
getAbsFilename(proc_filenames[file_subcall_ctr]); getAbsFilename(proc_filenames[file_subcall_ctr]);
filespos[file_subcall_ctr] = sdpos; filespos[file_subcall_ctr] = sdpos;
// For sub-procedures say 'SUBROUTINE CALL target: "..." parent: "..." pos12345'
SERIAL_ECHO_START(); SERIAL_ECHO_START();
SERIAL_ECHOLNPAIR("SUBROUTINE CALL target:\"", path, "\" parent:\"", proc_filenames[file_subcall_ctr], "\" pos", sdpos); SERIAL_ECHOLNPAIR("SUBROUTINE CALL target:\"", path, "\" parent:\"", proc_filenames[file_subcall_ctr], "\" pos", sdpos);
file_subcall_ctr++; file_subcall_ctr++;
} break;
else
doing = 1;
}
else if (subcall) // Returning from a subcall?
SERIAL_ECHO_MSG("END SUBROUTINE");
else { // Opening fresh file
doing = 2;
file_subcall_ctr = 0; // Reset procedure depth in case user cancels print while in procedure
}
if (doing) { case 2: // Resuming previous file after sub-procedure
SERIAL_ECHO_START(); SERIAL_ECHO_MSG("END SUBROUTINE");
SERIAL_ECHOPGM("Now "); break;
serialprintPGM(doing == 1 ? PSTR("doing") : PSTR("fresh"));
SERIAL_ECHOLNPAIR(" file: ", path);
} }
stopSDPrint(); stopSDPrint();
@ -491,35 +507,45 @@ void CardReader::openFile(char * const path, const bool read, const bool subcall
const char * const fname = diveToFile(curDir, path); const char * const fname = diveToFile(curDir, path);
if (!fname) return; if (!fname) return;
if (read) { if (file.open(curDir, fname, O_READ)) {
if (file.open(curDir, fname, O_READ)) { filesize = file.fileSize();
filesize = file.fileSize(); sdpos = 0;
sdpos = 0; SERIAL_ECHOLNPAIR(MSG_SD_FILE_OPENED, fname, MSG_SD_SIZE, filesize);
SERIAL_ECHOLNPAIR(MSG_SD_FILE_OPENED, fname, MSG_SD_SIZE, filesize); SERIAL_ECHOLNPGM(MSG_SD_FILE_SELECTED);
SERIAL_ECHOLNPGM(MSG_SD_FILE_SELECTED);
selectFileByName(fname); selectFileByName(fname);
ui.set_status(longFilename[0] ? longFilename : fname); ui.set_status(longFilename[0] ? longFilename : fname);
//if (longFilename[0]) {
// SERIAL_ECHOPAIR(MSG_SD_FILE_LONG_NAME, longFilename);
//}
}
else
SERIAL_ECHOLNPAIR(MSG_SD_OPEN_FILE_FAIL, fname, ".");
} }
else { //write else
if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) openFailed(fname);
SERIAL_ECHOLNPAIR(MSG_SD_OPEN_FILE_FAIL, fname, "."); }
else {
flag.saving = true; //
selectFileByName(fname); // Open a file by DOS path for write
#if ENABLED(EMERGENCY_PARSER) //
emergency_parser.disable(); void CardReader::openFileWrite(char * const path) {
#endif if (!isMounted()) return;
SERIAL_ECHOLNPAIR(MSG_SD_WRITE_TO_FILE, fname);
ui.set_status(fname); announceOpen(2, path);
} file_subcall_ctr = 0;
stopSDPrint();
SdFile *curDir;
const char * const fname = diveToFile(curDir, path);
if (!fname) return;
if (file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
flag.saving = true;
selectFileByName(fname);
#if ENABLED(EMERGENCY_PARSER)
emergency_parser.disable();
#endif
SERIAL_ECHOLNPAIR(MSG_SD_WRITE_TO_FILE, fname);
ui.set_status(fname);
} }
else
openFailed(fname);
} }
// //
@ -1035,9 +1061,9 @@ uint16_t CardReader::get_num_Files() {
void CardReader::printingHasFinished() { void CardReader::printingHasFinished() {
planner.synchronize(); planner.synchronize();
file.close(); file.close();
if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure. if (file_subcall_ctr > 0) { // Resume calling file after closing procedure
file_subcall_ctr--; file_subcall_ctr--;
openFile(proc_filenames[file_subcall_ctr], true, true); openFileRead(proc_filenames[file_subcall_ctr], 2); // 2 = Returning from sub-procedure
setIndex(filespos[file_subcall_ctr]); setIndex(filespos[file_subcall_ctr]);
startFileprint(); startFileprint();
} }

View File

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