From 39ad53ab11f5671bcfcd3f96d835e171463595b2 Mon Sep 17 00:00:00 2001
From: Yuri D'Elia <wavexx@thregr.org>
Date: Thu, 7 Jul 2022 12:10:44 +0200
Subject: [PATCH] Simplify fan checks now that lcd pause works correctly

---
 Firmware/Marlin_main.cpp | 17 -----------------
 Firmware/fancheck.cpp    | 21 +++++++++++----------
 Firmware/fancheck.h      |  1 -
 Firmware/ultralcd.cpp    | 33 ++++++++++++++++++++++++---------
 4 files changed, 35 insertions(+), 37 deletions(-)

diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp
index 44051f35..6b9f26a9 100644
--- a/Firmware/Marlin_main.cpp
+++ b/Firmware/Marlin_main.cpp
@@ -1863,13 +1863,6 @@ void loop()
 		;
 	}
     
-#ifdef FANCHECK
-    if (fan_check_error && isPrintPaused && !IS_SD_PRINTING) {
-        KEEPALIVE_STATE(PAUSED_FOR_USER);
-        host_keepalive(); //prevent timeouts since usb processing is disabled until print is resumed. This is for a crude way of pausing a print on all hosts.
-    }
-#endif
-
 #ifdef PRUSA_M28
     if (prusa_sd_card_upload)
     {
@@ -4197,16 +4190,6 @@ There are reasons why some G Codes aren't in numerical order.
 
 void process_commands()
 {
-#ifdef FANCHECK
-    if(fan_check_error == EFCE_DETECTED) {
-        fan_check_error = EFCE_REPORTED;
-        if (usb_timer.running())
-            lcd_pause_usb_print();
-        else
-            lcd_pause_print();
-    }
-#endif
-
 	if (!buflen) return; //empty command
 
 #ifdef CMDBUFFER_DEBUG
diff --git a/Firmware/fancheck.cpp b/Firmware/fancheck.cpp
index 20228380..421216f3 100755
--- a/Firmware/fancheck.cpp
+++ b/Firmware/fancheck.cpp
@@ -82,21 +82,22 @@ static void fanSpeedErrorBeep(const char *serialMsg, const char *lcdMsg){
 }
 
 void fanSpeedError(unsigned char _fan) {
-    if (get_message_level() != 0 && isPrintPaused) return;
-    //to ensure that target temp. is not set to zero in case that we are resuming print
-    if (card.sdprinting || usb_timer.running()) {
-        if (heating_status != HeatingStatus::NO_HEATING) {
-            lcd_print_stop();
-        }
-        else {
-            fan_check_error = EFCE_DETECTED; //plans error for next processed command
+    if (fan_check_error == EFCE_REPORTED) return;
+    fan_check_error = EFCE_REPORTED;
+
+    if (IS_SD_PRINTING || usb_timer.running()) {
+        // A print is ongoing, pause the print normally
+        if(!isPrintPaused) {
+            if (usb_timer.running())
+                lcd_pause_usb_print();
+            else
+                lcd_pause_print();
         }
     }
     else {
-        // SERIAL_PROTOCOLLNRPGM(MSG_OCTOPRINT_PAUSED); //Why pause octoprint? usb_timer.running() would be true in that case, so there is no need for this.
+        // Nothing is going on, but still turn off heaters and report the error
         setTargetHotend0(0);
         heating_status = HeatingStatus::NO_HEATING;
-        fan_check_error = EFCE_REPORTED;
     }
     switch (_fan) {
     case 0:	// extracting the same code from case 0 and case 1 into a function saves 72B
diff --git a/Firmware/fancheck.h b/Firmware/fancheck.h
index 841ffe78..31214397 100755
--- a/Firmware/fancheck.h
+++ b/Firmware/fancheck.h
@@ -8,7 +8,6 @@
 enum {
 	EFCE_OK = 0,   //!< normal operation, both fans are ok
 	EFCE_FIXED,    //!< previous fan error was fixed
-	EFCE_DETECTED, //!< fan error detected, but not reported yet
 	EFCE_REPORTED  //!< fan error detected and reported to LCD and serial
 };
 extern volatile uint8_t fan_check_error;
diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp
index e2e1a468..8f8f0caa 100755
--- a/Firmware/ultralcd.cpp
+++ b/Firmware/ultralcd.cpp
@@ -5692,19 +5692,30 @@ static bool fan_error_selftest()
     return 0;
 }
 
+bool resume_print_checks() {
+    // reset the lcd status so that a newer error will be shown
+    lcd_return_to_status();
+    lcd_reset_alert_level();
+
+    // ensure thermal issues (temp or fan) are resolved before we allow to resume
+    if (get_temp_error()
+#ifdef FANCHECK
+        || fan_error_selftest()
+#endif
+        ) {
+        return false; // abort if error persists
+    }
+
+    return true;
+}
+
 //! @brief Resume paused print, send host action "resumed"
 //! @todo It is not good to call restore_print_from_ram_and_continue() from function called by lcd_update(),
 //! as restore_print_from_ram_and_continue() calls lcd_update() internally.
 void lcd_resume_print()
 {
-    lcd_return_to_status();
-    lcd_reset_alert_level();
-
-    // ensure thermal issues (temp or fan) are resolved before we allow to resume
-    if (get_temp_error() || fan_error_selftest()) {
-        if (usb_timer.running()) SERIAL_PROTOCOLLNRPGM(MSG_OCTOPRINT_PAUSED);
-        return; // abort if error persists
-    }
+    // reset lcd and ensure we can resume first
+    if (!resume_print_checks()) return;
 
     cmdqueue_serial_disabled = false;
     lcd_setstatuspgm(_T(MSG_FINISHING_MOVEMENTS));
@@ -5722,7 +5733,11 @@ void lcd_resume_print()
 //! @brief Resume paused USB/host print, send host action "resume"
 void lcd_resume_usb_print()
 {
-    SERIAL_PROTOCOLLNRPGM(MSG_OCTOPRINT_RESUME); //resume octoprint
+    // reset lcd and ensure we can resume first
+    if (!resume_print_checks()) return;
+
+    // resume the usb host
+    SERIAL_PROTOCOLLNRPGM(MSG_OCTOPRINT_RESUME);
 }
 
 static void change_sheet()