Skip to content

Commit 5b46f9f

Browse files
committed
🚸 Interruptible PLR
1 parent 38e6330 commit 5b46f9f

File tree

1 file changed

+55
-41
lines changed

1 file changed

+55
-41
lines changed

Marlin/src/feature/powerloss.cpp

+55-41
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ PrintJobRecovery recovery;
7878
#define POWER_LOSS_RETRACT_LEN 0
7979
#endif
8080

81+
// Allow power-loss recovery to be aborted
82+
#define PLR_CAN_ABORT
83+
#if ENABLED(PLR_CAN_ABORT)
84+
#define PROCESS_SUBCOMMANDS_NOW(cmd) do { if (card.flag.abort_sd_printing) return; gcode.process_subcommands_now(cmd); }while(0)
85+
#else
86+
#define PROCESS_SUBCOMMANDS_NOW(cmd) gcode.process_subcommands_now(cmd)
87+
#endif
88+
8189
/**
8290
* Clear the recovery info
8391
*/
@@ -357,15 +365,15 @@ void PrintJobRecovery::resume() {
357365

358366
#if HAS_LEVELING
359367
// Make sure leveling is off before any G92 and G28
360-
gcode.process_subcommands_now(F("M420 S0 Z0"));
368+
PROCESS_SUBCOMMANDS_NOW(F("M420S0"));
361369
#endif
362370

363371
#if HAS_HEATED_BED
364372
const celsius_t bt = info.target_temperature_bed;
365373
if (bt) {
366374
// Restore the bed temperature
367375
sprintf_P(cmd, PSTR("M190S%i"), bt);
368-
gcode.process_subcommands_now(cmd);
376+
PROCESS_SUBCOMMANDS_NOW(cmd);
369377
}
370378
#endif
371379

@@ -376,10 +384,10 @@ void PrintJobRecovery::resume() {
376384
if (et) {
377385
#if HAS_MULTI_HOTEND
378386
sprintf_P(cmd, PSTR("T%iS"), e);
379-
gcode.process_subcommands_now(cmd);
387+
PROCESS_SUBCOMMANDS_NOW(cmd);
380388
#endif
381389
sprintf_P(cmd, PSTR("M109S%i"), et);
382-
gcode.process_subcommands_now(cmd);
390+
PROCESS_SUBCOMMANDS_NOW(cmd);
383391
}
384392
}
385393
#endif
@@ -393,7 +401,7 @@ void PrintJobRecovery::resume() {
393401
// establish the current position as best we can.
394402
//
395403

396-
gcode.process_subcommands_now(F("G92.9E0")); // Reset E to 0
404+
PROCESS_SUBCOMMANDS_NOW(F("G92.9E0")); // Reset E to 0
397405

398406
#if Z_HOME_TO_MAX
399407

@@ -404,7 +412,7 @@ void PrintJobRecovery::resume() {
404412
"G28R0\n" // Home all axes (no raise)
405413
"G1Z%sF1200" // Move Z down to (raised) height
406414
), dtostrf(z_now, 1, 3, str_1));
407-
gcode.process_subcommands_now(cmd);
415+
PROCESS_SUBCOMMANDS_NOW(cmd);
408416

409417
#elif DISABLED(BELTPRINTER)
410418

@@ -417,26 +425,26 @@ void PrintJobRecovery::resume() {
417425
#if !HOMING_Z_DOWN
418426
// Set Z to the real position
419427
sprintf_P(cmd, PSTR("G92.9Z%s"), dtostrf(z_now, 1, 3, str_1));
420-
gcode.process_subcommands_now(cmd);
428+
PROCESS_SUBCOMMANDS_NOW(cmd);
421429
#endif
422430

423431
// Does Z need to be raised now? It should be raised before homing XY.
424432
if (z_raised > z_now) {
425433
z_now = z_raised;
426434
sprintf_P(cmd, PSTR("G1Z%sF600"), dtostrf(z_now, 1, 3, str_1));
427-
gcode.process_subcommands_now(cmd);
435+
PROCESS_SUBCOMMANDS_NOW(cmd);
428436
}
429437

430438
// Home XY with no Z raise
431-
gcode.process_subcommands_now(F("G28R0XY")); // No raise during G28
439+
PROCESS_SUBCOMMANDS_NOW(F("G28R0XY")); // No raise during G28
432440

433441
#endif
434442

435443
#if HOMING_Z_DOWN
436444
// Move to a safe XY position and home Z while avoiding the print.
437445
const xy_pos_t p = xy_pos_t(POWER_LOSS_ZHOME_POS) TERN_(HOMING_Z_WITH_PROBE, - probe.offset_xy);
438446
sprintf_P(cmd, PSTR("G1X%sY%sF1000\nG28HZ"), dtostrf(p.x, 1, 3, str_1), dtostrf(p.y, 1, 3, str_2));
439-
gcode.process_subcommands_now(cmd);
447+
PROCESS_SUBCOMMANDS_NOW(cmd);
440448
#endif
441449

442450
// Mark all axes as having been homed (no effect on current_position)
@@ -447,37 +455,37 @@ void PrintJobRecovery::resume() {
447455
// Leveling may already be enabled due to the ENABLE_LEVELING_AFTER_G28 option.
448456
// TODO: Add a G28 parameter to leave leveling disabled.
449457
sprintf_P(cmd, PSTR("M420S%cZ%s"), '0' + (char)info.flag.leveling, dtostrf(info.fade, 1, 1, str_1));
450-
gcode.process_subcommands_now(cmd);
458+
PROCESS_SUBCOMMANDS_NOW(cmd);
451459

452460
#if !HOMING_Z_DOWN
453461
// The physical Z was adjusted at power-off so undo the M420S1 correction to Z with G92.9.
454462
sprintf_P(cmd, PSTR("G92.9Z%s"), dtostrf(z_now, 1, 1, str_1));
455-
gcode.process_subcommands_now(cmd);
463+
PROCESS_SUBCOMMANDS_NOW(cmd);
456464
#endif
457465
#endif
458466

459467
#if ENABLED(POWER_LOSS_RECOVER_ZHOME)
460468
// Z was homed down to the bed, so move up to the raised height.
461469
z_now = z_raised;
462470
sprintf_P(cmd, PSTR("G1Z%sF600"), dtostrf(z_now, 1, 3, str_1));
463-
gcode.process_subcommands_now(cmd);
471+
PROCESS_SUBCOMMANDS_NOW(cmd);
464472
#endif
465473

466474
// Recover volumetric extrusion state
467475
#if DISABLED(NO_VOLUMETRICS)
468476
#if HAS_MULTI_EXTRUDER
469477
EXTRUDER_LOOP() {
470478
sprintf_P(cmd, PSTR("M200T%iD%s"), e, dtostrf(info.filament_size[e], 1, 3, str_1));
471-
gcode.process_subcommands_now(cmd);
479+
PROCESS_SUBCOMMANDS_NOW(cmd);
472480
}
473481
if (!info.flag.volumetric_enabled) {
474482
sprintf_P(cmd, PSTR("M200T%iD0"), info.active_extruder);
475-
gcode.process_subcommands_now(cmd);
483+
PROCESS_SUBCOMMANDS_NOW(cmd);
476484
}
477485
#else
478486
if (info.flag.volumetric_enabled) {
479487
sprintf_P(cmd, PSTR("M200D%s"), dtostrf(info.filament_size[0], 1, 3, str_1));
480-
gcode.process_subcommands_now(cmd);
488+
PROCESS_SUBCOMMANDS_NOW(cmd);
481489
}
482490
#endif
483491
#endif
@@ -489,18 +497,18 @@ void PrintJobRecovery::resume() {
489497
if (et) {
490498
#if HAS_MULTI_HOTEND
491499
sprintf_P(cmd, PSTR("T%iS"), e);
492-
gcode.process_subcommands_now(cmd);
500+
PROCESS_SUBCOMMANDS_NOW(cmd);
493501
#endif
494502
sprintf_P(cmd, PSTR("M109S%i"), et);
495-
gcode.process_subcommands_now(cmd);
503+
PROCESS_SUBCOMMANDS_NOW(cmd);
496504
}
497505
}
498506
#endif
499507

500508
// Restore the previously active tool (with no_move)
501509
#if HAS_MULTI_EXTRUDER || HAS_MULTI_HOTEND
502510
sprintf_P(cmd, PSTR("T%i S"), info.active_extruder);
503-
gcode.process_subcommands_now(cmd);
511+
PROCESS_SUBCOMMANDS_NOW(cmd);
504512
#endif
505513

506514
// Restore print cooling fan speeds
@@ -509,7 +517,7 @@ void PrintJobRecovery::resume() {
509517
const int f = info.fan_speed[i];
510518
if (f) {
511519
sprintf_P(cmd, PSTR("M106P%iS%i"), i, f);
512-
gcode.process_subcommands_now(cmd);
520+
PROCESS_SUBCOMMANDS_NOW(cmd);
513521
}
514522
}
515523
#endif
@@ -531,37 +539,37 @@ void PrintJobRecovery::resume() {
531539

532540
// Un-retract if there was a retract at outage
533541
#if ENABLED(BACKUP_POWER_SUPPLY) && POWER_LOSS_RETRACT_LEN > 0
534-
gcode.process_subcommands_now(F("G1F3000E" STRINGIFY(POWER_LOSS_RETRACT_LEN)));
542+
PROCESS_SUBCOMMANDS_NOW(F("G1F3000E" STRINGIFY(POWER_LOSS_RETRACT_LEN)));
535543
#endif
536544

537545
// Additional purge on resume if configured
538546
#if POWER_LOSS_PURGE_LEN
539547
sprintf_P(cmd, PSTR("G1F3000E%d"), (POWER_LOSS_PURGE_LEN) + (POWER_LOSS_RETRACT_LEN));
540-
gcode.process_subcommands_now(cmd);
548+
PROCESS_SUBCOMMANDS_NOW(cmd);
541549
#endif
542550

543551
#if ENABLED(NOZZLE_CLEAN_FEATURE)
544-
gcode.process_subcommands_now(F("G12"));
552+
PROCESS_SUBCOMMANDS_NOW(F("G12"));
545553
#endif
546554

547555
// Move back over to the saved XY
548556
sprintf_P(cmd, PSTR("G1X%sY%sF3000"),
549557
dtostrf(info.current_position.x, 1, 3, str_1),
550558
dtostrf(info.current_position.y, 1, 3, str_2)
551559
);
552-
gcode.process_subcommands_now(cmd);
560+
PROCESS_SUBCOMMANDS_NOW(cmd);
553561

554562
// Move back down to the saved Z for printing
555563
sprintf_P(cmd, PSTR("G1Z%sF600"), dtostrf(z_print, 1, 3, str_1));
556-
gcode.process_subcommands_now(cmd);
564+
PROCESS_SUBCOMMANDS_NOW(cmd);
557565

558566
// Restore the feedrate
559567
sprintf_P(cmd, PSTR("G1F%d"), info.feedrate);
560-
gcode.process_subcommands_now(cmd);
568+
PROCESS_SUBCOMMANDS_NOW(cmd);
561569

562570
// Restore E position with G92.9
563571
sprintf_P(cmd, PSTR("G92.9E%s"), dtostrf(info.current_position.e, 1, 3, str_1));
564-
gcode.process_subcommands_now(cmd);
572+
PROCESS_SUBCOMMANDS_NOW(cmd);
565573

566574
TERN_(GCODE_REPEAT_MARKERS, repeat = info.stored_repeat);
567575
TERN_(HAS_HOME_OFFSET, home_offset = info.home_offset);
@@ -573,22 +581,28 @@ void PrintJobRecovery::resume() {
573581
// Relative axis modes
574582
gcode.axis_relative = info.axis_relative;
575583

576-
#if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
577-
const uint8_t old_flags = marlin_debug_flags;
578-
marlin_debug_flags |= MARLIN_DEBUG_ECHO;
579-
#endif
580-
581-
// Continue to apply PLR when a file is resumed!
582-
enable(true);
584+
{
585+
#if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
586+
struct OnExit {
587+
uint8_t old_flags;
588+
OnExit() {
589+
old_flags = marlin_debug_flags;
590+
marlin_debug_flags |= MARLIN_DEBUG_ECHO;
591+
}
592+
~OnExit() { marlin_debug_flags = old_flags; }
593+
} on_exit;
594+
#endif
583595

584-
// Resume the SD file from the last position
585-
char *fn = info.sd_filename;
586-
sprintf_P(cmd, M23_STR, fn);
587-
gcode.process_subcommands_now(cmd);
588-
sprintf_P(cmd, PSTR("M24S%ldT%ld"), resume_sdpos, info.print_job_elapsed);
589-
gcode.process_subcommands_now(cmd);
596+
// Continue to apply PLR when a file is resumed!
597+
enable(true);
590598

591-
TERN_(DEBUG_POWER_LOSS_RECOVERY, marlin_debug_flags = old_flags);
599+
// Resume the SD file from the last position
600+
char *fn = info.sd_filename;
601+
sprintf_P(cmd, M23_STR, fn);
602+
PROCESS_SUBCOMMANDS_NOW(cmd);
603+
sprintf_P(cmd, PSTR("M24S%ldT%ld"), resume_sdpos, info.print_job_elapsed);
604+
PROCESS_SUBCOMMANDS_NOW(cmd);
605+
}
592606
}
593607

594608
#if ENABLED(DEBUG_POWER_LOSS_RECOVERY)

0 commit comments

Comments
 (0)