Skip to content

Commit

Permalink
the usual malarkey
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Apr 16, 2022
1 parent 9c27e3e commit 5e46bd1
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions Marlin/src/sd/cardreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,47 +472,49 @@ void CardReader::mount() {
#endif

void CardReader::manage_media() {
static bool first_mount = true; // Check first successful mount
static uint8_t prev_stat = 2; // First call, no prior state
uint8_t stat = uint8_t(IS_SD_INSERTED());
if (stat == prev_stat) return;
static struct {
uint8_t prev:2; // First call, no prior state
bool did1st:1;
} stat = { 2, false }; // 2=first

DEBUG_ECHOLNPGM("SD: Status changed from ", prev_stat, " to ", stat);
uint8_t curr = uint8_t(IS_SD_INSERTED()); // 1=full 0=empty
if (curr == stat.prev) return; // Neither changed nor first call?

flag.workDirIsRoot = true; // Return to root on mount/release

if (ui.detected()) {

uint8_t old_stat = prev_stat;
prev_stat = stat; // Change now to prevent re-entry
const uint8_t old_stat = stat.prev;
stat.prev = curr; // Change now to prevent re-entry

if (stat) { // Media Inserted
if (curr) { // Media Inserted
safe_delay(500); // Some boards need a delay to get settled
if (TERN1(SD_IGNORE_AT_STARTUP, old_stat != 2))
if (TERN1(SD_IGNORE_AT_STARTUP, stat.did1st))
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?
if (!isMounted()) curr = 0; // Not mounted?
}
else {
#if PIN_EXISTS(SD_DETECT)
release(); // Card is released
release(); // Card is released (or first init)
#endif
}

ui.media_changed(old_stat, stat); // Update the UI
DEBUG_ECHOLNPGM("SD: Status changed from ", old_stat, " to ", curr);
ui.media_changed(old_stat, curr); // Update the UI

if (stat) {
if (curr) {
TERN_(SDCARD_EEPROM_EMULATION, settings.first_load());
if (first_mount) { // First mount?
if (!stat.did1st) { // First mount handled?
DEBUG_ECHOLNPGM("First mount.");
stat.did1st = true; // Set did-first-mount flag
#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
first_mount = false; // clear first mount flag
}
}
}
Expand Down

0 comments on commit 5e46bd1

Please sign in to comment.