Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» CardReader::isStillPrinting
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Sep 2, 2024
1 parent 162ea81 commit a108bbe
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 47 deletions.
21 changes: 7 additions & 14 deletions Marlin/src/HAL/DUE/usb/sd_mmc_spi_mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ void sd_mmc_spi_mem_init() {
}

inline bool media_ready() {
return IS_SD_INSERTED() && !IS_SD_PRINTING() && !IS_SD_FILE_OPEN() && card.isMounted();
return DISABLED(DISABLE_DUE_SD_MMC)
&& IS_SD_MOUNTED() && IS_SD_INSERTED()
&& !IS_SD_FILE_OPEN() && !IS_SD_PRINTING();
}

bool sd_mmc_spi_unload(bool) { return true; }
Expand All @@ -29,17 +31,14 @@ bool sd_mmc_spi_wr_protect() { return false; }
bool sd_mmc_spi_removal() { return !media_ready(); }

Ctrl_status sd_mmc_spi_test_unit_ready() {
#ifdef DISABLE_DUE_SD_MMC
return CTRL_NO_PRESENT;
#endif
if (!media_ready()) return CTRL_NO_PRESENT;
if (sd_mmc_spi_removal()) return CTRL_NO_PRESENT;
return CTRL_GOOD;
}

// NOTE: This function is defined as returning the address of the last block
// in the card, which is cardSize() - 1
Ctrl_status sd_mmc_spi_read_capacity(uint32_t *nb_sector) {
if (!media_ready()) return CTRL_NO_PRESENT;
if (sd_mmc_spi_removal()) return CTRL_NO_PRESENT;
*nb_sector = card.diskIODriver()->cardSize() - 1;
return CTRL_GOOD;
}
Expand All @@ -58,10 +57,7 @@ uint8_t sector_buf[SD_MMC_BLOCK_SIZE];
// #define DEBUG_MMC

Ctrl_status sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector) {
#ifdef DISABLE_DUE_SD_MMC
return CTRL_NO_PRESENT;
#endif
if (!media_ready()) return CTRL_NO_PRESENT;
if (sd_mmc_spi_removal()) return CTRL_NO_PRESENT;

#ifdef DEBUG_MMC
{
Expand Down Expand Up @@ -97,10 +93,7 @@ Ctrl_status sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector) {
}

Ctrl_status sd_mmc_spi_usb_write_10(uint32_t addr, uint16_t nb_sector) {
#ifdef DISABLE_DUE_SD_MMC
return CTRL_NO_PRESENT;
#endif
if (!media_ready()) return CTRL_NO_PRESENT;
if (sd_mmc_spi_removal()) return CTRL_NO_PRESENT;

#ifdef DEBUG_MMC
{
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/pause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ bool pause_print(const_float_t retract, const xyz_pos_t &park_point, const bool

// Pause the print job and timer
#if HAS_MEDIA
const bool was_sd_printing = IS_SD_PRINTING();
const bool was_sd_printing = card.isStillPrinting();
if (was_sd_printing) {
card.pauseSDPrint();
++did_pause_print; // Indicate SD pause also
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/feature/powerloss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void PrintJobRecovery::enable(const bool onoff) {
void PrintJobRecovery::changed() {
if (!enabled)
purge();
else if (IS_SD_PRINTING())
else if (card.isStillPrinting())
save(true);
TERN_(EXTENSIBLE_UI, ExtUI::onSetPowerLoss(enabled));
}
Expand Down Expand Up @@ -198,7 +198,7 @@ void PrintJobRecovery::save(const bool force/*=false*/, const float zraise/*=POW

// Set Head and Foot to matching non-zero values
if (!++info.valid_head) ++info.valid_head; // non-zero in sequence
//if (!IS_SD_PRINTING()) info.valid_head = 0;
//if (!card.isStillPrinting()) info.valid_head = 0;
info.valid_foot = info.valid_head;

// Machine state
Expand Down Expand Up @@ -321,7 +321,7 @@ void PrintJobRecovery::save(const bool force/*=false*/, const float zraise/*=POW

// Save the current position, distance that Z was (or should be) raised,
// and a flag whether the raise was already done here.
if (IS_SD_PRINTING()) save(true, zraise, ENABLED(BACKUP_POWER_SUPPLY));
if (card.isStillPrinting()) save(true, zraise, ENABLED(BACKUP_POWER_SUPPLY));

// Tell the LCD about the outage, even though it is about to die
TERN_(EXTENSIBLE_UI, ExtUI::onPowerLoss());
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/gcode/feature/pause/M125.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void GcodeSuite::M125() {
park_point += hotend_offset[active_extruder];
#endif

const bool sd_printing = TERN0(HAS_MEDIA, IS_SD_PRINTING());
const bool sd_printing = IS_SD_PRINTING();

ui.pause_show_message(PAUSE_MESSAGE_PARKING, PAUSE_MODE_PAUSE_PRINT);

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/gcode/sd/M21_M22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void GcodeSuite::M21() {
* M22: Release SD Card
*/
void GcodeSuite::M22() {
if (!IS_SD_PRINTING()) card.release();
if (!card.isStillPrinting()) card.release();
}

#endif // HAS_MEDIA
2 changes: 1 addition & 1 deletion Marlin/src/gcode/sd/M24_M25.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void GcodeSuite::M25() {
#else

// Set initial pause flag to prevent more commands from landing in the queue while we try to pause
if (IS_SD_PRINTING()) card.pauseSDPrint();
if (card.isStillPrinting()) card.pauseSDPrint();

#if ENABLED(POWER_LOSS_RECOVERY) && DISABLED(DGUS_LCD_UI_MKS)
if (recovery.enabled) recovery.save(true);
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/gcode/sd/M524.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void GcodeSuite::M524() {

#else

if (IS_SD_PRINTING())
if (card.isStillPrinting())
card.abortFilePrintSoon();
else if (card.isMounted())
card.closefile();
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/e3v2/jyersui/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4653,7 +4653,7 @@ void JyersDWIN::popupControl() {
#if ENABLED(PARK_HEAD_ON_PAUSE)
popupHandler(Popup_Home, true);
#if HAS_MEDIA
if (IS_SD_PRINTING()) card.pauseSDPrint();
if (card.isStillPrinting()) card.pauseSDPrint();
#endif
planner.synchronize();
queue.inject(F("M125"));
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/lcd/extui/ui_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1178,10 +1178,10 @@ namespace ExtUI {
}

bool isPrintingFromMediaPaused() {
return TERN0(HAS_MEDIA, IS_SD_PAUSED());
return IS_SD_PAUSED();
}

bool isPrintingFromMedia() { return TERN0(HAS_MEDIA, IS_SD_PRINTING() || IS_SD_PAUSED()); }
bool isPrintingFromMedia() { return IS_SD_PRINTING() || IS_SD_PAUSED(); }

bool isPrinting() {
return commandsInQueue() || isPrintingFromMedia() || printJobOngoing() || printingIsPaused();
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ void MarlinUI::host_notify(const char * const cstr) {
if (printingIsPaused())
msg = GET_TEXT_F(MSG_PRINT_PAUSED);
#if HAS_MEDIA
else if (IS_SD_PRINTING())
else if (card.isStillPrinting())
return set_status_no_expire(card.longest_filename());
#endif
else if (print_job_timer.isRunning())
Expand Down Expand Up @@ -1674,7 +1674,7 @@ void MarlinUI::host_notify(const char * const cstr) {
void MarlinUI::abort_print() {
#if HAS_MEDIA
wait_for_heatup = wait_for_user = false;
if (IS_SD_PRINTING())
if (card.isStillPrinting())
card.abortFilePrintSoon();
else if (card.isMounted())
card.closefile();
Expand Down
18 changes: 10 additions & 8 deletions Marlin/src/sd/cardreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ CardReader::CardReader() {
}

//
// Get a DOS 8.3 filename in its useful form
// Get a DOS 8.3 filename in its useful form, e.g., "MYFILE.EXT"
//
char *createFilename(char * const buffer, const dir_t &p) {
char *pos = buffer;
Expand All @@ -193,10 +193,14 @@ char *createFilename(char * const buffer, const dir_t &p) {
if (i == 8) *pos++ = '.';
*pos++ = p.name[i];
}
*pos++ = 0;
*pos++ = '\0';
return buffer;
}

inline bool extIsBIN(char *ext) {
return ext[0] == 'B' && ext[1] == 'I' && ext[2] == 'N';
}

//
// Return 'true' if the item is a folder, G-code file or Binary file
//
Expand All @@ -215,9 +219,7 @@ bool CardReader::is_visible_entity(const dir_t &p OPTARG(CUSTOM_FIRMWARE_UPLOAD,
) return false;

flag.filenameIsDir = DIR_IS_SUBDIR(&p); // We know it's a File or Folder
setBinFlag(p.name[8] == 'B' && // List .bin files (a firmware file for flashing)
p.name[9] == 'I' &&
p.name[10]== 'N');
setBinFlag(extIsBIN((char *)&p.name[8])); // List .bin files (a firmware file for flashing)

return (
flag.filenameIsDir // All Directories are ok
Expand Down Expand Up @@ -576,7 +578,7 @@ void CardReader::manage_media() {
*/
void CardReader::release() {
// Card removed while printing? Abort!
if (IS_SD_PRINTING())
if (isStillPrinting())
abortFilePrintSoon();
else
endFilePrintNow();
Expand Down Expand Up @@ -993,7 +995,7 @@ void CardReader::selectFileByIndex(const int16_t nr) {
strcpy(filename, sortshort[nr]);
strcpy(longFilename, sortnames[nr]);
TERN_(HAS_FOLDER_SORTING, flag.filenameIsDir = IS_DIR(nr));
setBinFlag(strcmp_P(strrchr(filename, '.'), PSTR(".BIN")) == 0);
setBinFlag(extIsBIN(strrchr(filename, '.') + 1));
return;
}
#endif
Expand All @@ -1011,7 +1013,7 @@ void CardReader::selectFileByName(const char * const match) {
strcpy(filename, sortshort[nr]);
strcpy(longFilename, sortnames[nr]);
TERN_(HAS_FOLDER_SORTING, flag.filenameIsDir = IS_DIR(nr));
setBinFlag(strcmp_P(strrchr(filename, '.'), PSTR(".BIN")) == 0);
setBinFlag(extIsBIN(strrchr(filename, '.') + 1));
return;
}
#endif
Expand Down
27 changes: 15 additions & 12 deletions Marlin/src/sd/cardreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ extern const char M23_STR[], M24_STR[];
#endif

typedef struct {
bool saving:1,
logging:1,
sdprinting:1,
sdprintdone:1,
mounted:1,
filenameIsDir:1,
workDirIsRoot:1,
abort_sd_printing:1
bool saving:1, // Receiving a G-code file or logging commands during a print
logging:1, // Log enqueued commands to the open file. See GCodeQueue::advance()
sdprinting:1, // Actively printing from the open file
sdprintdone:1, // The active job has reached the end, 100%
mounted:1, // The card or flash drive is mounted and ready to read/write
filenameIsDir:1, // The working item is a directory
workDirIsRoot:1, // The working directory is / so there's no parent
abort_sd_printing:1 // Abort by calling abortSDPrinting() at the main loop()
#if DO_LIST_BIN_FILES
, filenameIsBin:1
, filenameIsBin:1 // The working item is a BIN file
#endif
#if ENABLED(BINARY_FILE_TRANSFER)
, binary_mode:1
, binary_mode:1 // Use the serial line buffer as BinaryStream input
#endif
;
} card_flags_t;
Expand Down Expand Up @@ -173,6 +173,7 @@ class CardReader {
static void abortFilePrintSoon() { flag.abort_sd_printing = isFileOpen(); }
static void pauseSDPrint() { flag.sdprinting = false; }
static bool isPrinting() { return flag.sdprinting; }
static bool isStillPrinting() { return flag.sdprinting && !flag.abort_sd_printing; }
static bool isPaused() { return isFileOpen() && !isPrinting(); }
#if HAS_PRINT_PROGRESS_PERMYRIAD
static uint16_t permyriadDone() {
Expand Down Expand Up @@ -367,15 +368,17 @@ class CardReader {
#define IS_SD_INSERTED() true
#endif

#define IS_SD_PRINTING() (card.flag.sdprinting && !card.flag.abort_sd_printing)
#define IS_SD_FETCHING() (!card.flag.sdprintdone && IS_SD_PRINTING())
#define IS_SD_MOUNTED() card.isMounted()
#define IS_SD_PRINTING() card.isStillPrinting()
#define IS_SD_FETCHING() (!card.flag.sdprintdone && card.isStillPrinting())
#define IS_SD_PAUSED() card.isPaused()
#define IS_SD_FILE_OPEN() card.isFileOpen()

extern CardReader card;

#else // !HAS_MEDIA

#define IS_SD_MOUNTED() false
#define IS_SD_PRINTING() false
#define IS_SD_FETCHING() false
#define IS_SD_PAUSED() false
Expand Down

0 comments on commit a108bbe

Please sign in to comment.