From fb54d06582aaea77b86721b39e6f07317ecf12a4 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 17 Apr 2022 21:18:39 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Flatten=20manage=5Fmedia=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Marlin/src/sd/cardreader.cpp | 73 +++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index 652a44e52651..bc210ca5556c 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -472,50 +472,53 @@ void CardReader::mount() { #endif void CardReader::manage_media() { - static uint8_t prev_stat = 2; // First call, no prior state + static uint8_t prev_stat = 2; // First call, no prior state uint8_t stat = uint8_t(IS_SD_INSERTED()); if (stat == prev_stat) return; - DEBUG_ECHOLNPGM("SD: Status changed from ", prev_stat, " to ", stat); + DEBUG_SECTION(mm, "CardReader::manage_media", true); + DEBUG_ECHOLNPGM("SD Status ", prev_stat, " -> ", stat); - flag.workDirIsRoot = true; // Return to root on mount/release + flag.workDirIsRoot = true; // Return to root on mount/release - if (ui.detected()) { + if (!ui.detected()) { + DEBUG_ECHOLNPGM("SD: No UI Detected."); + return; + } - uint8_t old_stat = prev_stat; - prev_stat = stat; // Change now to prevent re-entry + uint8_t old_stat = prev_stat; + prev_stat = stat; // Change now to prevent re-entry - if (stat) { // Media Inserted - safe_delay(500); // Some boards need a delay to get settled - if (TERN1(SD_IGNORE_AT_STARTUP, old_stat != 2)) - mount(); // Try to mount the media - #if MB(FYSETC_CHEETAH, FYSETC_CHEETAH_V12, FYSETC_AIO_II) - reset_stepper_drivers(); // Workaround for Cheetah bug - #endif - if (!isMounted()) stat = 0; // Not mounted? - } - else { - #if PIN_EXISTS(SD_DETECT) - release(); // Card is released - #endif - } + if (stat) { // Media Inserted + safe_delay(500); // Some boards need a delay to get settled + if (TERN1(SD_IGNORE_AT_STARTUP, old_stat != 2)) + mount(); // Try to mount the media + #if MB(FYSETC_CHEETAH, FYSETC_CHEETAH_V12, FYSETC_AIO_II) + reset_stepper_drivers(); // Workaround for Cheetah bug + #endif + if (!isMounted()) stat = 0; // Not mounted? + } + else { + #if PIN_EXISTS(SD_DETECT) + release(); // Card is released + #endif + } - ui.media_changed(old_stat, stat); // Update the UI + ui.media_changed(old_stat, stat); // Update the UI - if (stat) { - TERN_(SDCARD_EEPROM_EMULATION, settings.first_load()); - if (old_stat == 2) { // First mount? - DEBUG_ECHOLNPGM("First mount."); - #if ENABLED(POWER_LOSS_RECOVERY) - recovery.check(); // Check for PLR file. (If not there then call autofile_begin) - #elif DISABLED(NO_SD_AUTOSTART) - autofile_begin(); // Look for auto0.g on the next loop - #endif - } - } - } - else - DEBUG_ECHOLNPGM("SD: No UI Detected."); + if (!stat) return; // Exit if no media is present + + TERN_(SDCARD_EEPROM_EMULATION, settings.first_load()); + + if (old_stat != 2) return; // First mount? + + DEBUG_ECHOLNPGM("First mount."); + + #if ENABLED(POWER_LOSS_RECOVERY) + recovery.check(); // Check for PLR file. (If not there then call autofile_begin) + #elif DISABLED(NO_SD_AUTOSTART) + autofile_begin(); // Look for auto0.g on the next loop + #endif } /**