From 4fce74140a55679318b09027d0e42ec7c0aa0cf6 Mon Sep 17 00:00:00 2001
From: Yuri D'Elia <wavexx@thregr.org>
Date: Tue, 3 Dec 2019 19:35:29 +0100
Subject: [PATCH 1/5] Also abort planning in stop_and_save_print_to_ram

Similarly to resume_print_from_ram_and_continue, abort any
planning in mesh_plan_buffer_line already in stop_and_save_print_to_ram
in case it is called for pausing only.
---
 Firmware/Marlin_main.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp
index fe94fab5..33f67684 100755
--- a/Firmware/Marlin_main.cpp
+++ b/Firmware/Marlin_main.cpp
@@ -10249,6 +10249,7 @@ void stop_and_save_print_to_ram(float z_move, float e_move)
     memcpy(current_position, saved_pos, sizeof(saved_pos));
     memcpy(destination, current_position, sizeof(destination));
 #endif
+    waiting_inside_plan_buffer_line_print_aborted = true; //unroll the stack
   }
 }
 

From 792d7ca6dc2029e7c6d81c6609eb89c06bb29ba7 Mon Sep 17 00:00:00 2001
From: Yuri D'Elia <wavexx@thregr.org>
Date: Tue, 3 Dec 2019 19:54:06 +0100
Subject: [PATCH 2/5] Turn off heaters just prior to retraction to limit oozing

---
 Firmware/Marlin_main.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp
index 33f67684..c8596bad 100755
--- a/Firmware/Marlin_main.cpp
+++ b/Firmware/Marlin_main.cpp
@@ -9555,9 +9555,11 @@ float temp_compensation_pinda_thermistor_offset(float temperature_pinda)
 void long_pause() //long pause print
 {
 	st_synchronize();
-	
 	start_pause_print = _millis();
 
+    // Stop heaters
+    setAllTargetHotends(0);
+
 	//retract
 	current_position[E_AXIS] -= default_retraction;
 	plan_buffer_line_curposXYZE(400, active_extruder);
@@ -9572,8 +9574,7 @@ void long_pause() //long pause print
 	current_position[Y_AXIS] = Y_PAUSE_POS;
 	plan_buffer_line_curposXYZE(50, active_extruder);
 
-	// Turn off the hotends and print fan
-    setAllTargetHotends(0);
+	// Turn off the print fan
 	fanSpeed = 0;
 }
 

From d60230e494e6e4a60387b7fceab8e8be24648b70 Mon Sep 17 00:00:00 2001
From: Yuri D'Elia <wavexx@thregr.org>
Date: Tue, 3 Dec 2019 19:55:33 +0100
Subject: [PATCH 3/5] Add some documentation

---
 Firmware/Marlin_main.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp
index c8596bad..9924b61c 100755
--- a/Firmware/Marlin_main.cpp
+++ b/Firmware/Marlin_main.cpp
@@ -10221,7 +10221,9 @@ void stop_and_save_print_to_ram(float z_move, float e_move)
 	sei();
 	if ((z_move != 0) || (e_move != 0)) { // extruder or z move
 #if 1
-    // Rather than calling plan_buffer_line directly, push the move into the command queue, 
+    // Rather than calling plan_buffer_line directly, push the move into the command queue so that
+    // the caller can continue processing. This is used during powerpanic to save the state as we
+    // move away from the print.
     char buf[48];
 
 	// First unretract (relative extrusion)

From 063d0421ac71801bf0d8eb31ca9e51ef3950737f Mon Sep 17 00:00:00 2001
From: Yuri D'Elia <wavexx@thregr.org>
Date: Tue, 3 Dec 2019 19:56:05 +0100
Subject: [PATCH 4/5] Call long_pause from the main loop (again)

After calling stop_and_save_print_to_ram, perform the parking moves
within the main loop as done before PR#1899 to avoid planning within
an interrupted move (which would clear the abort flag too soon).
---
 Firmware/ultralcd.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp
index cbfe017f..b9e86fc4 100755
--- a/Firmware/ultralcd.cpp
+++ b/Firmware/ultralcd.cpp
@@ -1100,6 +1100,7 @@ void lcd_commands()
 			lcd_setstatuspgm(_i("Print paused"));////MSG_PRINT_PAUSED c=20 r=1
             lcd_commands_type = LcdCommands::Idle;
             lcd_commands_step = 0;
+            long_pause();
 		}
 	}
 
@@ -1656,9 +1657,8 @@ void lcd_return_to_status()
 //! @brief Pause print, disable nozzle heater, move to park position
 void lcd_pause_print()
 {
-    lcd_return_to_status();
     stop_and_save_print_to_ram(0.0,0.0);
-    long_pause();
+    lcd_return_to_status();
     isPrintPaused = true;
     if (LcdCommands::Idle == lcd_commands_type)
     {

From 8448b8d41390301a6cd3a5e4b31260d52bfff896 Mon Sep 17 00:00:00 2001
From: Yuri D'Elia <wavexx@thregr.org>
Date: Tue, 3 Dec 2019 20:17:08 +0100
Subject: [PATCH 5/5] Keep destination updated in an aborted plan as well

When aborting the plan destination is updated anyway to reflect the latest
position. There's no use in this additional check.
---
 Firmware/Marlin_main.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp
index 9924b61c..5bdae1bb 100755
--- a/Firmware/Marlin_main.cpp
+++ b/Firmware/Marlin_main.cpp
@@ -8415,8 +8415,6 @@ void prepare_move()
      plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply*(1./(60.f*100.f)), active_extruder);
 #endif
   }
-  if (waiting_inside_plan_buffer_line_print_aborted)
-      return;
 
   set_current_to_destination();
 }