Fix auto#.g file handling, add NO_SD_AUTOSTART (#20071)

This commit is contained in:
Scott Lahteine 2020-12-07 05:53:15 -06:00
parent 1932e70e72
commit 37dd0fd2d6
9 changed files with 78 additions and 56 deletions

View File

@ -1197,6 +1197,7 @@
#define SD_MENU_CONFIRM_START // Confirm the selected SD file before printing #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 MENU_ADDAUTOSTART // Add a menu option to run auto#.g files
//#define BROWSE_MEDIA_ON_INSERT // Open the file browser when media is inserted //#define BROWSE_MEDIA_ON_INSERT // Open the file browser when media is inserted

View File

@ -460,6 +460,7 @@ void startOrResumeJob() {
#if ENABLED(SDSUPPORT) #if ENABLED(SDSUPPORT)
inline void abortSDPrinting() { inline void abortSDPrinting() {
IF_DISABLED(NO_SD_AUTOSTART, card.autofile_cancel());
card.endFilePrint(TERN_(SD_RESORT, true)); card.endFilePrint(TERN_(SD_RESORT, true));
queue.clear(); queue.clear();
quickstop_stepper(); quickstop_stepper();
@ -585,7 +586,7 @@ inline void manage_inactivity(const bool ignore_stepper_queue=false) {
if (ELAPSED(ms, next_home_key_ms)) { if (ELAPSED(ms, next_home_key_ms)) {
next_home_key_ms = ms + HOME_DEBOUNCE_DELAY; next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
LCD_MESSAGEPGM(MSG_AUTO_HOME); LCD_MESSAGEPGM(MSG_AUTO_HOME);
queue.enqueue_now_P(G28_STR); queue.inject_P(G28_STR);
} }
} }
#endif #endif
@ -1354,7 +1355,6 @@ void loop() {
idle(); idle();
#if ENABLED(SDSUPPORT) #if ENABLED(SDSUPPORT)
card.checkautostart();
if (card.flag.abort_sd_printing) abortSDPrinting(); if (card.flag.abort_sd_printing) abortSDPrinting();
if (marlin_state == MF_SD_COMPLETE) finishSDPrinting(); if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
#endif #endif

View File

@ -152,7 +152,7 @@ class PrintJobRecovery {
static void resume(); static void resume();
static void purge(); 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 load();
static void save(const bool force=ENABLED(SAVE_EACH_CMD_MODE), const float zraise=0); static void save(const bool force=ENABLED(SAVE_EACH_CMD_MODE), const float zraise=0);

View File

@ -27,6 +27,10 @@
#include "../gcode.h" #include "../gcode.h"
#include "../../module/printcounter.h" #include "../../module/printcounter.h"
#if DISABLED(NO_SD_AUTOSTART)
#include "../../sd/cardreader.h"
#endif
#ifdef SD_FINISHED_RELEASECOMMAND #ifdef SD_FINISHED_RELEASECOMMAND
#include "../queue.h" #include "../queue.h"
#endif #endif
@ -60,6 +64,11 @@
* M1001: Execute actions for SD print completion * M1001: Execute actions for SD print completion
*/ */
void GcodeSuite::M1001() { 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 // Report total print time
const bool long_print = print_job_timer.duration() > 60; const bool long_print = print_job_timer.duration() > 60;
@ -71,9 +80,6 @@ void GcodeSuite::M1001() {
// Set the progress bar "done" state // Set the progress bar "done" state
TERN_(LCD_SET_PROGRESS_MANUALLY, ui.set_progress_done()); TERN_(LCD_SET_PROGRESS_MANUALLY, ui.set_progress_done());
// Purge the recovery file
TERN_(POWER_LOSS_RECOVERY, recovery.purge());
// Announce SD file completion // Announce SD file completion
{ {
PORT_REDIRECT(SERIAL_BOTH); PORT_REDIRECT(SERIAL_BOTH);
@ -93,7 +99,7 @@ void GcodeSuite::M1001() {
// Inject SD_FINISHED_RELEASECOMMAND, if any // Inject SD_FINISHED_RELEASECOMMAND, if any
#ifdef SD_FINISHED_RELEASECOMMAND #ifdef SD_FINISHED_RELEASECOMMAND
queue.inject_P(PSTR(SD_FINISHED_RELEASECOMMAND)); gcode.process_subcommands_now_P(PSTR(SD_FINISHED_RELEASECOMMAND));
#endif #endif
TERN_(EXTENSIBLE_UI, ExtUI::onPrintFinished()); TERN_(EXTENSIBLE_UI, ExtUI::onPrintFinished());

View File

@ -142,6 +142,10 @@
#undef SD_FINISHED_RELEASECOMMAND #undef SD_FINISHED_RELEASECOMMAND
#endif #endif
#if ENABLED(NO_SD_AUTOSTART)
#undef MENU_ADDAUTOSTART
#endif
#if EITHER(SDSUPPORT, LCD_SET_PROGRESS_MANUALLY) #if EITHER(SDSUPPORT, LCD_SET_PROGRESS_MANUALLY)
#define HAS_PRINT_PROGRESS 1 #define HAS_PRINT_PROGRESS 1
#endif #endif

View File

@ -136,7 +136,7 @@ void menu_main() {
// Run Auto Files // Run Auto Files
// //
#if ENABLED(MENU_ADDAUTOSTART) #if ENABLED(MENU_ADDAUTOSTART)
ACTION_ITEM(MSG_RUN_AUTO_FILES, card.beginautostart); ACTION_ITEM(MSG_RUN_AUTO_FILES, card.autofile_begin);
#endif #endif
if (card_detected) { if (card_detected) {
@ -238,7 +238,7 @@ void menu_main() {
// Autostart // Autostart
// //
#if ENABLED(MENU_ADDAUTOSTART) #if ENABLED(MENU_ADDAUTOSTART)
ACTION_ITEM(MSG_RUN_AUTO_FILES, card.beginautostart); ACTION_ITEM(MSG_RUN_AUTO_FILES, card.autofile_begin);
#endif #endif
if (card_detected) { if (card_detected) {

View File

@ -61,7 +61,8 @@
card_flags_t CardReader::flag; card_flags_t CardReader::flag;
char CardReader::filename[FILENAME_LENGTH], CardReader::longFilename[LONG_FILENAME_LENGTH]; 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) #if BOTH(HAS_MULTI_SERIAL, BINARY_FILE_TRANSFER)
int8_t CardReader::transfer_port_index; int8_t CardReader::transfer_port_index;
@ -135,17 +136,17 @@ CardReader::CardReader() {
//sort_reverse = false; //sort_reverse = false;
#endif #endif
#endif #endif
flag.sdprinting = flag.mounted = flag.saving = flag.logging = false; flag.sdprinting = flag.mounted = flag.saving = flag.logging = false;
filesize = sdpos = 0; filesize = sdpos = 0;
TERN_(HAS_MEDIA_SUBCALLS, file_subcall_ctr = 0); TERN_(HAS_MEDIA_SUBCALLS, file_subcall_ctr = 0);
IF_DISABLED(NO_SD_AUTOSTART, autofile_cancel());
workDirDepth = 0; workDirDepth = 0;
ZERO(workDirParents); ZERO(workDirParents);
// Disable autostart until card is initialized
autostart_index = -1;
#if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT) #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
SET_INPUT_PULLUP(SD_DETECT_PIN); SET_INPUT_PULLUP(SD_DETECT_PIN);
#endif #endif
@ -442,12 +443,14 @@ void CardReader::manage_media() {
if (stat) { if (stat) {
TERN_(SDCARD_EEPROM_EMULATION, settings.first_load()); TERN_(SDCARD_EEPROM_EMULATION, settings.first_load());
if (old_stat == 2) // First mount? if (old_stat == 2) { // First mount?
DEBUG_ECHOLNPGM("First mount."); DEBUG_ECHOLNPGM("First mount.");
TERN(POWER_LOSS_RECOVERY, #if ENABLED(POWER_LOSS_RECOVERY)
recovery.check(), // Check for PLR file. (If not there it will beginautostart) recovery.check(); // Check for PLR file. (If not there then call autofile_begin)
beginautostart() // Look for auto0.g on the next loop #elif DISABLED(NO_SD_AUTOSTART)
); autofile_begin(); // Look for auto0.g on the next loop
#endif
}
} }
} }
else else
@ -477,12 +480,12 @@ void CardReader::release() {
* Enqueues M23 and M24 commands to initiate a media print. * Enqueues M23 and M24 commands to initiate a media print.
*/ */
void CardReader::openAndPrintFile(const char *name) { 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[]; extern const char M23_STR[];
sprintf_P(cmd, M23_STR, name); sprintf_P(cmd, M23_STR, name);
for (char *c = &cmd[4]; *c; c++) *c = tolower(*c); for (char *c = &cmd[4]; *c; c++) *c = tolower(*c);
queue.enqueue_one_now(cmd); strcat_P(cmd, PSTR("\nM24"));
queue.enqueue_now_P(M24_STR); queue.inject(cmd);
} }
/** /**
@ -511,7 +514,7 @@ void CardReader::endFilePrint(TERN_(SD_RESORT, const bool re_sort/*=false*/)) {
void CardReader::openLogFile(char * const path) { void CardReader::openLogFile(char * const path) {
flag.logging = DISABLED(SDCARD_READONLY); flag.logging = DISABLED(SDCARD_READONLY);
TERN(SDCARD_READONLY,,openFileWrite(path)); IF_DISABLED(SDCARD_READONLY, openFileWrite(path));
} }
// //
@ -735,42 +738,48 @@ void CardReader::write_command(char * const buf) {
if (file.writeError) SERIAL_ERROR_MSG(STR_SD_ERR_WRITE_TO_FILE); if (file.writeError) SERIAL_ERROR_MSG(STR_SD_ERR_WRITE_TO_FILE);
} }
// #if DISABLED(NO_SD_AUTOSTART)
// Run the next autostart file. Called: /**
// - On boot after successful card init * Run all the auto#.g files. Called:
// - After finishing the previous autostart file * - On boot after successful card init.
// - From the LCD command to run the autostart file * - From the LCD command to Run Auto Files
// */
void CardReader::autofile_begin() {
autofile_index = 1;
(void)autofile_check();
}
void CardReader::checkautostart() { /**
* 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 (autostart_index < 0 || flag.sdprinting) return; if (!isMounted())
mount();
if (!isMounted()) mount(); else if (ENABLED(SDCARD_EEPROM_EMULATION))
TERN_(SDCARD_EEPROM_EMULATION, else settings.first_load()); settings.first_load();
// Don't run auto#.g when a PLR file exists // Don't run auto#.g when a PLR file exists
if (isMounted() && TERN1(POWER_LOSS_RECOVERY, !recovery.valid())) { if (isMounted() && TERN1(POWER_LOSS_RECOVERY, !recovery.valid())) {
char autoname[8]; char autoname[10];
sprintf_P(autoname, PSTR("auto%c.g"), autostart_index + '0'); sprintf_P(autoname, PSTR("/auto%c.g"), '0' + autofile_index - 1);
dir_t p; if (fileExists(autoname)) {
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) {
openAndPrintFile(autoname);
autostart_index++;
return;
}
}
}
autostart_index = -1;
}
void CardReader::beginautostart() {
autostart_index = 0;
cdroot(); cdroot();
openAndPrintFile(autoname);
autofile_index++;
return false;
} }
}
autofile_cancel();
return true;
}
#endif
void CardReader::closefile(const bool store_location/*=false*/) { void CardReader::closefile(const bool store_location/*=false*/) {
file.sync(); file.sync();

View File

@ -90,10 +90,12 @@ public:
static void openLogFile(char * const path); static void openLogFile(char * const path);
static void write_command(char * const buf); static void write_command(char * const buf);
// Auto-Start files #if DISABLED(NO_SD_AUTOSTART) // Auto-Start auto#.g file handling
static int8_t autostart_index; // Index of autoX.g files static uint8_t autofile_index; // Next auto#.g index to run, plus one. Ignored by autofile_check when zero.
static void beginautostart(); static void autofile_begin(); // Begin check. Called automatically after boot-up.
static void checkautostart(); 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 // Basic file ops
static void openFileRead(char * const path, const uint8_t subcall=0); static void openFileRead(char * const path, const uint8_t subcall=0);

View File

@ -49,7 +49,7 @@ opt_set TEMP_SENSOR_4 1000
opt_set TEMP_SENSOR_BED 1 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 \ 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 \ 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 \ EEPROM_SETTINGS EEPROM_CHITCHAT GCODE_MACROS CUSTOM_USER_MENUS \
MULTI_NOZZLE_DUPLICATION CLASSIC_JERK LIN_ADVANCE QUICK_HOME \ MULTI_NOZZLE_DUPLICATION CLASSIC_JERK LIN_ADVANCE QUICK_HOME \
LCD_SET_PROGRESS_MANUALLY PRINT_PROGRESS_SHOW_DECIMALS SHOW_REMAINING_TIME \ LCD_SET_PROGRESS_MANUALLY PRINT_PROGRESS_SHOW_DECIMALS SHOW_REMAINING_TIME \