From 08c5557026e3cb008955181e7d07b04a8cae2a35 Mon Sep 17 00:00:00 2001 From: Victor Oliveira Date: Thu, 19 Nov 2020 02:32:33 -0300 Subject: [PATCH] Abort print on media removal (#20200) --- Marlin/src/sd/cardreader.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index 1be5f4f2f6..4416f4e907 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -454,8 +454,17 @@ void CardReader::manage_media() { DEBUG_ECHOLNPGM("SD: No UI Detected."); } +/** + * "Release" the media by clearing the 'mounted' flag. + * Used by M22, "Release Media", manage_media. + */ void CardReader::release() { - endFilePrint(); + // Card removed while printing? Abort! + if (IS_SD_PRINTING()) + card.flag.abort_sd_printing = true; + else + endFilePrint(); + flag.mounted = false; flag.workDirIsRoot = true; #if ALL(SDCARD_SORT_ALPHA, SDSORT_USES_RAM, SDSORT_CACHE_NAMES) @@ -463,6 +472,10 @@ void CardReader::release() { #endif } +/** + * Open a G-code file and set Marlin to start processing it. + * Enqueues M23 and M24 commands to initiate a media print. + */ void CardReader::openAndPrintFile(const char *name) { char cmd[4 + strlen(name) + 1]; // Room for "M23 ", filename, and null extern const char M23_STR[]; @@ -472,6 +485,12 @@ void CardReader::openAndPrintFile(const char *name) { queue.enqueue_now_P(M24_STR); } +/** + * Start or resume a media print by setting the sdprinting flag. + * The file browser pre-sort is also purged to free up memory, + * since you cannot browse files during active printing. + * Used by M24 and anywhere Start / Resume applies. + */ void CardReader::startFileprint() { if (isMounted()) { flag.sdprinting = true; @@ -479,6 +498,9 @@ void CardReader::startFileprint() { } } +// +// Run tasks upon finishing or aborting a file print. +// void CardReader::endFilePrint(TERN_(SD_RESORT, const bool re_sort/*=false*/)) { TERN_(ADVANCED_PAUSE_FEATURE, did_pause_print = 0); TERN_(DWIN_CREALITY_LCD, HMI_flag.print_finish = flag.sdprinting);