Skip to content

Commit

Permalink
Fix auto#.g file handling, add NO_SD_AUTOSTART (MarlinFirmware#20071)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and RFBomb committed Dec 23, 2020
1 parent 4733fd3 commit f872111
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 58 deletions.
1 change: 1 addition & 0 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@

#define SD_MENU_CONFIRM_START // Confirm the selected SD file before printing

//#define NO_SD_AUTOSTART // Remove auto#.g file support completely to save some Flash, SRAM
//#define MENU_ADDAUTOSTART // Add a menu option to run auto#.g files

//#define BROWSE_MEDIA_ON_INSERT // Open the file browser when media is inserted
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ void startOrResumeJob() {
#if ENABLED(SDSUPPORT)

inline void abortSDPrinting() {
IF_DISABLED(NO_SD_AUTOSTART, card.autofile_cancel());
card.endFilePrint(TERN_(SD_RESORT, true));
queue.clear();
quickstop_stepper();
Expand Down Expand Up @@ -585,7 +586,7 @@ inline void manage_inactivity(const bool ignore_stepper_queue=false) {
if (ELAPSED(ms, next_home_key_ms)) {
next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
LCD_MESSAGEPGM(MSG_AUTO_HOME);
queue.enqueue_now_P(G28_STR);
queue.inject_P(G28_STR);
}
}
#endif
Expand Down Expand Up @@ -1354,7 +1355,6 @@ void loop() {
idle();

#if ENABLED(SDSUPPORT)
card.checkautostart();
if (card.flag.abort_sd_printing) abortSDPrinting();
if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
#endif
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/powerloss.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class PrintJobRecovery {
static void resume();
static void purge();

static inline void cancel() { purge(); card.autostart_index = 0; }
static inline void cancel() { purge(); IF_DISABLED(NO_SD_AUTOSTART, card.autofile_begin()); }

static void load();
static void save(const bool force=ENABLED(SAVE_EACH_CMD_MODE), const float zraise=0);
Expand Down
14 changes: 10 additions & 4 deletions Marlin/src/gcode/sd/M1001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include "../gcode.h"
#include "../../module/printcounter.h"

#if DISABLED(NO_SD_AUTOSTART)
#include "../../sd/cardreader.h"
#endif

#ifdef SD_FINISHED_RELEASECOMMAND
#include "../queue.h"
#endif
Expand Down Expand Up @@ -60,6 +64,11 @@
* M1001: Execute actions for SD print completion
*/
void GcodeSuite::M1001() {
// If there's another auto#.g file to run...
if (TERN(NO_SD_AUTOSTART, false, card.autofile_check())) return;

// Purge the recovery file...
TERN_(POWER_LOSS_RECOVERY, recovery.purge());

// Report total print time
const bool long_print = print_job_timer.duration() > 60;
Expand All @@ -71,9 +80,6 @@ void GcodeSuite::M1001() {
// Set the progress bar "done" state
TERN_(LCD_SET_PROGRESS_MANUALLY, ui.set_progress_done());

// Purge the recovery file
TERN_(POWER_LOSS_RECOVERY, recovery.purge());

// Announce SD file completion
{
PORT_REDIRECT(SERIAL_BOTH);
Expand All @@ -93,7 +99,7 @@ void GcodeSuite::M1001() {

// Inject SD_FINISHED_RELEASECOMMAND, if any
#ifdef SD_FINISHED_RELEASECOMMAND
queue.inject_P(PSTR(SD_FINISHED_RELEASECOMMAND));
gcode.process_subcommands_now_P(PSTR(SD_FINISHED_RELEASECOMMAND));
#endif

TERN_(EXTENSIBLE_UI, ExtUI::onPrintFinished());
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/inc/Conditionals_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@
#undef SD_FINISHED_RELEASECOMMAND
#endif

#if ENABLED(NO_SD_AUTOSTART)
#undef MENU_ADDAUTOSTART
#endif

#if EITHER(SDSUPPORT, LCD_SET_PROGRESS_MANUALLY)
#define HAS_PRINT_PROGRESS 1
#endif
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/lcd/menu/menu_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void menu_main() {
// Run Auto Files
//
#if ENABLED(MENU_ADDAUTOSTART)
ACTION_ITEM(MSG_RUN_AUTO_FILES, card.beginautostart);
ACTION_ITEM(MSG_RUN_AUTO_FILES, card.autofile_begin);
#endif

if (card_detected) {
Expand Down Expand Up @@ -238,7 +238,7 @@ void menu_main() {
// Autostart
//
#if ENABLED(MENU_ADDAUTOSTART)
ACTION_ITEM(MSG_RUN_AUTO_FILES, card.beginautostart);
ACTION_ITEM(MSG_RUN_AUTO_FILES, card.autofile_begin);
#endif

if (card_detected) {
Expand Down
97 changes: 53 additions & 44 deletions Marlin/src/sd/cardreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@

card_flags_t CardReader::flag;
char CardReader::filename[FILENAME_LENGTH], CardReader::longFilename[LONG_FILENAME_LENGTH];
int8_t CardReader::autostart_index;

IF_DISABLED(NO_SD_AUTOSTART, uint8_t CardReader::autofile_index); // = 0

#if BOTH(HAS_MULTI_SERIAL, BINARY_FILE_TRANSFER)
int8_t CardReader::transfer_port_index;
Expand Down Expand Up @@ -135,17 +136,17 @@ CardReader::CardReader() {
//sort_reverse = false;
#endif
#endif

flag.sdprinting = flag.mounted = flag.saving = flag.logging = false;
filesize = sdpos = 0;

TERN_(HAS_MEDIA_SUBCALLS, file_subcall_ctr = 0);

IF_DISABLED(NO_SD_AUTOSTART, autofile_cancel());

workDirDepth = 0;
ZERO(workDirParents);

// Disable autostart until card is initialized
autostart_index = -1;

#if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
SET_INPUT_PULLUP(SD_DETECT_PIN);
#endif
Expand Down Expand Up @@ -575,12 +576,14 @@ void CardReader::manage_media() {

if (stat) {
TERN_(SDCARD_EEPROM_EMULATION, settings.first_load());
if (old_stat == 2) // First mount?
if (old_stat == 2) { // First mount?
DEBUG_ECHOLNPGM("First mount.");
TERN(POWER_LOSS_RECOVERY,
recovery.check(), // Check for PLR file. (If not there it will beginautostart)
beginautostart() // Look for auto0.g on the next loop
);
#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
Expand Down Expand Up @@ -610,12 +613,12 @@ void CardReader::release() {
* 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
char cmd[4 + strlen(name) + 1 + 3 + 1]; // Room for "M23 ", filename, "\n", "M24", and null
extern const char M23_STR[];
sprintf_P(cmd, M23_STR, name);
for (char *c = &cmd[4]; *c; c++) *c = tolower(*c);
queue.enqueue_one_now(cmd);
queue.enqueue_now_P(M24_STR);
strcat_P(cmd, PSTR("\nM24"));
queue.inject(cmd);
}

/**
Expand Down Expand Up @@ -644,7 +647,7 @@ void CardReader::endFilePrint(TERN_(SD_RESORT, const bool re_sort/*=false*/)) {

void CardReader::openLogFile(char * const path) {
flag.logging = DISABLED(SDCARD_READONLY);
TERN(SDCARD_READONLY,,openFileWrite(path));
IF_DISABLED(SDCARD_READONLY, openFileWrite(path));
}

//
Expand Down Expand Up @@ -868,42 +871,48 @@ void CardReader::write_command(char * const buf) {
if (file.writeError) SERIAL_ERROR_MSG(STR_SD_ERR_WRITE_TO_FILE);
}

//
// Run the next autostart file. Called:
// - On boot after successful card init
// - After finishing the previous autostart file
// - From the LCD command to run the autostart file
//

void CardReader::checkautostart() {

if (autostart_index < 0 || flag.sdprinting) return;

if (!isMounted()) mount();
TERN_(SDCARD_EEPROM_EMULATION, else settings.first_load());
#if DISABLED(NO_SD_AUTOSTART)
/**
* Run all the auto#.g files. Called:
* - On boot after successful card init.
* - From the LCD command to Run Auto Files
*/
void CardReader::autofile_begin() {
autofile_index = 1;
(void)autofile_check();
}

// Don't run auto#.g when a PLR file exists
if (isMounted() && TERN1(POWER_LOSS_RECOVERY, !recovery.valid())) {
char autoname[8];
sprintf_P(autoname, PSTR("auto%c.g"), autostart_index + '0');
dir_t p;
root.rewind();
while (root.readDir(&p, nullptr) > 0) {
for (int8_t i = (int8_t)strlen((char*)p.name); i--;) p.name[i] = tolower(p.name[i]);
if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
/**
* Run the next auto#.g file. Called:
* - On boot after successful card init
* - After finishing the previous auto#.g file
* - From the LCD command to begin the auto#.g files
*
* Return 'true' if there was nothing to do
*/
bool CardReader::autofile_check() {
if (!autofile_index) return true;

if (!isMounted())
mount();
else if (ENABLED(SDCARD_EEPROM_EMULATION))
settings.first_load();

// Don't run auto#.g when a PLR file exists
if (isMounted() && TERN1(POWER_LOSS_RECOVERY, !recovery.valid())) {
char autoname[10];
sprintf_P(autoname, PSTR("/auto%c.g"), '0' + autofile_index - 1);
if (fileExists(autoname)) {
cdroot();
openAndPrintFile(autoname);
autostart_index++;
return;
autofile_index++;
return false;
}
}
autofile_cancel();
return true;
}
autostart_index = -1;
}

void CardReader::beginautostart() {
autostart_index = 0;
cdroot();
}
#endif

void CardReader::closefile(const bool store_location/*=false*/) {
file.sync();
Expand Down
10 changes: 6 additions & 4 deletions Marlin/src/sd/cardreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ class CardReader {
static void openLogFile(char * const path);
static void write_command(char * const buf);

// Auto-Start files
static int8_t autostart_index; // Index of autoX.g files
static void beginautostart();
static void checkautostart();
#if DISABLED(NO_SD_AUTOSTART) // Auto-Start auto#.g file handling
static uint8_t autofile_index; // Next auto#.g index to run, plus one. Ignored by autofile_check when zero.
static void autofile_begin(); // Begin check. Called automatically after boot-up.
static bool autofile_check(); // Check for the next auto-start file and run it.
static inline void autofile_cancel() { autofile_index = 0; }
#endif

// Basic file ops
static void openFileRead(char * const path, const uint8_t subcall=0);
Expand Down
2 changes: 1 addition & 1 deletion buildroot/tests/mega2560-tests
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ opt_set TEMP_SENSOR_4 1000
opt_set TEMP_SENSOR_BED 1
opt_enable AUTO_BED_LEVELING_UBL RESTORE_LEVELING_AFTER_G28 DEBUG_LEVELING_FEATURE G26_MESH_VALIDATION ENABLE_LEVELING_FADE_HEIGHT SKEW_CORRECTION \
REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER LIGHTWEIGHT_UI STATUS_MESSAGE_SCROLLING BOOT_MARLIN_LOGO_SMALL \
SDSUPPORT SDCARD_SORT_ALPHA USB_FLASH_DRIVE_SUPPORT SCROLL_LONG_FILENAMES CANCEL_OBJECTS \
SDSUPPORT SDCARD_SORT_ALPHA USB_FLASH_DRIVE_SUPPORT SCROLL_LONG_FILENAMES CANCEL_OBJECTS NO_SD_AUTOSTART \
EEPROM_SETTINGS EEPROM_CHITCHAT GCODE_MACROS CUSTOM_USER_MENUS \
MULTI_NOZZLE_DUPLICATION CLASSIC_JERK LIN_ADVANCE QUICK_HOME \
LCD_SET_PROGRESS_MANUALLY PRINT_PROGRESS_SHOW_DECIMALS SHOW_REMAINING_TIME \
Expand Down

0 comments on commit f872111

Please sign in to comment.