From aee971bcaf2d8b7157985f36f6705015ef334238 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <thinkyhead@users.noreply.github.com>
Date: Sat, 22 May 2021 17:02:21 -0500
Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Combine=20M104/M109=20and=20M140?=
 =?UTF-8?q?/M190=20code?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Marlin/src/gcode/gcode.h            | 10 ++--
 Marlin/src/gcode/temp/M104_M109.cpp | 78 ++++-------------------------
 Marlin/src/gcode/temp/M140_M190.cpp | 57 +++++----------------
 3 files changed, 27 insertions(+), 118 deletions(-)

diff --git a/Marlin/src/gcode/gcode.h b/Marlin/src/gcode/gcode.h
index c32575e81ee..6f8568730b4 100644
--- a/Marlin/src/gcode/gcode.h
+++ b/Marlin/src/gcode/gcode.h
@@ -648,8 +648,9 @@ private:
   #endif
 
   #if HAS_EXTRUDERS
-    static void M104();
-    static void M109();
+    static void M104_M109(const bool isM109);
+    FORCE_INLINE static void M104() { M104_M109(false); }
+    FORCE_INLINE static void M109() { M104_M109(true); }
   #endif
 
   static void M105();
@@ -699,8 +700,9 @@ private:
   #endif
 
   #if HAS_HEATED_BED
-    static void M140();
-    static void M190();
+    static void M140_M190(const bool isM190);
+    FORCE_INLINE static void M140() { M140_M190(false); }
+    FORCE_INLINE static void M190() { M140_M190(true); }
   #endif
 
   #if HAS_HEATED_CHAMBER
diff --git a/Marlin/src/gcode/temp/M104_M109.cpp b/Marlin/src/gcode/temp/M104_M109.cpp
index eb350a577af..b6a3fa85071 100644
--- a/Marlin/src/gcode/temp/M104_M109.cpp
+++ b/Marlin/src/gcode/temp/M104_M109.cpp
@@ -51,89 +51,29 @@
 
 /**
  * M104: Set Hotend Temperature target and return immediately
- *
- * Parameters:
- *  I<preset> : Material Preset index (if material presets are defined)
- *  T<index>  : Tool index. If omitted, applies to the active tool
- *  S<target> : The target temperature in current units
- */
-void GcodeSuite::M104() {
-
-  if (DEBUGGING(DRYRUN)) return;
-
-  #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1
-    constexpr int8_t target_extruder = 0;
-  #else
-    const int8_t target_extruder = get_target_extruder_from_command();
-    if (target_extruder < 0) return;
-  #endif
-
-  bool got_temp = false;
-  celsius_t temp = 0;
-
-  // Accept 'I' if temperature presets are defined
-  #if PREHEAT_COUNT
-    got_temp = parser.seenval('I');
-    if (got_temp) {
-      const uint8_t index = parser.value_byte();
-      temp = ui.material_preset[_MIN(index, PREHEAT_COUNT - 1)].hotend_temp;
-    }
-  #endif
-
-  // If no 'I' get the temperature from 'S'
-  if (!got_temp) {
-    got_temp = parser.seenval('S');
-    if (got_temp) temp = parser.value_celsius();
-  }
-
-  if (got_temp) {
-    #if ENABLED(SINGLENOZZLE_STANDBY_TEMP)
-      thermalManager.singlenozzle_temp[target_extruder] = temp;
-      if (target_extruder != active_extruder) return;
-    #endif
-    thermalManager.setTargetHotend(temp, target_extruder);
-
-    #if ENABLED(DUAL_X_CARRIAGE)
-      if (idex_is_duplicating() && target_extruder == 0)
-        thermalManager.setTargetHotend(temp ? temp + duplicate_extruder_temp_offset : 0, 1);
-    #endif
-
-    #if ENABLED(PRINTJOB_TIMER_AUTOSTART)
-      /**
-       * Stop the timer at the end of print. Start is managed by 'heat and wait' M109.
-       * Hotends use EXTRUDE_MINTEMP / 2 to allow nozzles to be put into hot standby
-       * mode, for instance in a dual extruder setup, without affecting the running
-       * print timer.
-       */
-      thermalManager.auto_job_check_timer(false, true);
-    #endif
-  }
-
-  TERN_(AUTOTEMP, planner.autotemp_M104_M109());
-}
-
-/**
  * M109: Set Hotend Temperature target and wait
  *
  * Parameters
  *  I<preset> : Material Preset index (if material presets are defined)
  *  T<index>  : Tool index. If omitted, applies to the active tool
- *  S<target> : The target temperature in current units. Wait for heating only.
- *  R<target> : The target temperature in current units. Wait for heating and cooling.
+ *  S<target> : The target temperature in current units. For M109, only wait when heating up.
  *
  * With AUTOTEMP...
  *  F<factor> : Autotemp Scaling Factor. Set non-zero to enable Auto-temp.
  *  S<min>    : Minimum temperature, in current units.
  *  B<max>    : Maximum temperature, in current units.
  *
+ * M109 Parameters
+ *  R<target> : The target temperature in current units. Wait for heating and cooling.
+ *
  * Examples
- *  M109 S100 : Set target to 100°. Wait until the hotend is at or above 100°.
+ *  M104 S100 : Set target to 100° and return.
  *  M109 R150 : Set target to 150°. Wait until the hotend gets close to 150°.
  *
  * With PRINTJOB_TIMER_AUTOSTART turning on heaters will start the print job timer
  *  (used by printingIsActive, etc.) and turning off heaters will stop the timer.
  */
-void GcodeSuite::M109() {
+void GcodeSuite::M104_M109(const bool isM109) {
 
   if (DEBUGGING(DRYRUN)) return;
 
@@ -160,7 +100,7 @@ void GcodeSuite::M109() {
   bool no_wait_for_cooling = false;
   if (!got_temp) {
     no_wait_for_cooling = parser.seenval('S');
-    got_temp = no_wait_for_cooling || parser.seenval('R');
+    got_temp = no_wait_for_cooling || (isM109 && parser.seenval('R'));
     if (got_temp) temp = parser.value_celsius();
   }
 
@@ -182,7 +122,7 @@ void GcodeSuite::M109() {
        * standby mode, (e.g., in a dual extruder setup) without affecting
        * the running print timer.
        */
-      thermalManager.auto_job_check_timer(true, true);
+      thermalManager.auto_job_check_timer(isM109, true);
     #endif
 
     #if HAS_STATUS_MESSAGE
@@ -193,7 +133,7 @@ void GcodeSuite::M109() {
 
   TERN_(AUTOTEMP, planner.autotemp_M104_M109());
 
-  if (got_temp)
+  if (isM109 && got_temp)
     (void)thermalManager.wait_for_hotend(target_extruder, no_wait_for_cooling);
 }
 
diff --git a/Marlin/src/gcode/temp/M140_M190.cpp b/Marlin/src/gcode/temp/M140_M190.cpp
index 3aed878a038..ddab003973a 100644
--- a/Marlin/src/gcode/temp/M140_M190.cpp
+++ b/Marlin/src/gcode/temp/M140_M190.cpp
@@ -35,62 +35,28 @@
 #include "../../lcd/marlinui.h"
 
 /**
- * M140: Set bed temperature
+ * M140 - Set Bed Temperature target and return immediately
+ * M190 - Set Bed Temperature target and wait
  *
  *  I<index>  : Preset index (if material presets are defined)
  *  S<target> : The target temperature in current units
- */
-void GcodeSuite::M140() {
-  if (DEBUGGING(DRYRUN)) return;
-
-  bool got_temp = false;
-  celsius_t temp = 0;
-
-  // Accept 'I' if temperature presets are defined
-  #if PREHEAT_COUNT
-    got_temp = parser.seenval('I');
-    if (got_temp) {
-      const uint8_t index = parser.value_byte();
-      temp = ui.material_preset[_MIN(index, PREHEAT_COUNT - 1)].bed_temp;
-    }
-  #endif
-
-  // If no 'I' get the temperature from 'S'
-  if (!got_temp) {
-    got_temp = parser.seenval('S');
-    if (got_temp) temp = parser.value_celsius();
-  }
-
-  if (got_temp) {
-    thermalManager.setTargetBed(temp);
-
-    #if ENABLED(PRINTJOB_TIMER_AUTOSTART)
-      /**
-       * Stop the timer at the end of print. Hotend, bed target, and chamber
-       * temperatures need to be set below mintemp. Order of M140, M104, and M141
-       * at the end of the print does not matter.
-       */
-      thermalManager.auto_job_check_timer(false, true);
-    #endif
-  }
-}
-
-/**
- * M190 - Set Bed Temperature target and wait
  *
- * Parameters:
+ * Parameters
  *  I<index>  : Preset index (if material presets are defined)
  *  S<target> : The target temperature in current units. Wait for heating only.
+ *
+ * M190 Parameters
  *  R<target> : The target temperature in current units. Wait for heating and cooling.
  *
- * Examples:
- *  M190 S60 : Set target to 60°. Wait until the bed is at or above 60°.
+ * Examples
+ *  M140 S60 : Set target to 60° and return right away.
  *  M190 R40 : Set target to 40°. Wait until the bed gets close to 40°.
  *
  * With PRINTJOB_TIMER_AUTOSTART turning on heaters will start the print job timer
  *  (used by printingIsActive, etc.) and turning off heaters will stop the timer.
  */
-void GcodeSuite::M190() {
+void GcodeSuite::M140_M190(const bool isM190) {
+
   if (DEBUGGING(DRYRUN)) return;
 
   bool got_temp = false;
@@ -109,7 +75,7 @@ void GcodeSuite::M190() {
   bool no_wait_for_cooling = false;
   if (!got_temp) {
     no_wait_for_cooling = parser.seenval('S');
-    got_temp = no_wait_for_cooling || parser.seenval('R');
+    got_temp = no_wait_for_cooling || (isM190 && parser.seenval('R'));
     if (got_temp) temp = parser.value_celsius();
   }
 
@@ -121,7 +87,8 @@ void GcodeSuite::M190() {
 
   ui.set_status_P(thermalManager.isHeatingBed() ? GET_TEXT(MSG_BED_HEATING) : GET_TEXT(MSG_BED_COOLING));
 
-  thermalManager.wait_for_bed(no_wait_for_cooling);
+  if (isM190)
+    thermalManager.wait_for_bed(no_wait_for_cooling);
 }
 
 #endif // HAS_HEATED_BED