From fd5f1f1f5d3a25f73105c35eb3eaaac73d868e7e Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Sun, 28 Feb 2021 19:50:17 -0600
Subject: [PATCH] Filament sensor cleanup

---
 Marlin/src/feature/pause.cpp                |  6 +-----
 Marlin/src/feature/runout.cpp               |  6 +-----
 Marlin/src/feature/runout.h                 | 12 ++++++------
 Marlin/src/gcode/feature/pause/M600.cpp     |  2 +-
 Marlin/src/inc/Conditionals_adv.h           |  3 +++
 Marlin/src/lcd/extui/ui_api.cpp             |  8 ++++++--
 Marlin/src/lcd/extui/ui_api.h               |  1 +
 Marlin/src/pins/ramps/pins_RAMPS_CREALITY.h |  2 +-
 8 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/Marlin/src/feature/pause.cpp b/Marlin/src/feature/pause.cpp
index c1f951e7647..a6a28925eda 100644
--- a/Marlin/src/feature/pause.cpp
+++ b/Marlin/src/feature/pause.cpp
@@ -192,11 +192,7 @@ bool load_filament(const float &slow_load_length/*=0*/, const float &fast_load_l
     KEEPALIVE_STATE(PAUSED_FOR_USER);
     wait_for_user = true;    // LCD click or M108 will clear this
     #if ENABLED(HOST_PROMPT_SUPPORT)
-      const char tool = '0'
-        #if NUM_RUNOUT_SENSORS > 1
-          + active_extruder
-        #endif
-      ;
+      const char tool = '0' + TERN0(MULTI_FILAMENT_SENSOR, active_extruder);
       host_prompt_do(PROMPT_USER_CONTINUE, PSTR("Load Filament T"), tool, CONTINUE_STR);
     #endif
 
diff --git a/Marlin/src/feature/runout.cpp b/Marlin/src/feature/runout.cpp
index be769d2dc82..17d2c74003b 100644
--- a/Marlin/src/feature/runout.cpp
+++ b/Marlin/src/feature/runout.cpp
@@ -88,11 +88,7 @@ void event_filament_runout() {
   TERN_(EXTENSIBLE_UI, ExtUI::onFilamentRunout(ExtUI::getActiveTool()));
 
   #if EITHER(HOST_PROMPT_SUPPORT, HOST_ACTION_COMMANDS)
-    const char tool = '0'
-      #if NUM_RUNOUT_SENSORS > 1
-        + active_extruder
-      #endif
-    ;
+    const char tool = '0' + TERN0(MULTI_FILAMENT_SENSOR, active_extruder);
   #endif
 
   //action:out_of_filament
diff --git a/Marlin/src/feature/runout.h b/Marlin/src/feature/runout.h
index ecf47b22c2e..d253071ef4a 100644
--- a/Marlin/src/feature/runout.h
+++ b/Marlin/src/feature/runout.h
@@ -230,7 +230,7 @@ class FilamentSensorBase {
                       change    = old_state ^ new_state;
         old_state = new_state;
 
-        #ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
+        #if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
           if (change) {
             SERIAL_ECHOPGM("Motion detected:");
             LOOP_L_N(e, NUM_RUNOUT_SENSORS)
@@ -266,12 +266,12 @@ class FilamentSensorBase {
     private:
       static inline bool poll_runout_state(const uint8_t extruder) {
         const uint8_t runout_states = poll_runout_states();
-        #if NUM_RUNOUT_SENSORS == 1
-          UNUSED(extruder);
-        #else
+        #if MULTI_FILAMENT_SENSOR
           if ( !TERN0(DUAL_X_CARRIAGE, idex_is_duplicating())
             && !TERN0(MULTI_NOZZLE_DUPLICATION, extruder_duplication_enabled)
           ) return TEST(runout_states, extruder); // A specific extruder ran out
+        #else
+          UNUSED(extruder);
         #endif
         return !!runout_states;                   // Any extruder ran out
       }
@@ -282,7 +282,7 @@ class FilamentSensorBase {
       static inline void run() {
         const bool out = poll_runout_state(active_extruder);
         if (!out) filament_present(active_extruder);
-        #ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
+        #if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
           static bool was_out = false;
           if (out != was_out) {
             was_out = out;
@@ -315,7 +315,7 @@ class FilamentSensorBase {
       }
 
       static inline void run() {
-        #ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
+        #if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
           static millis_t t = 0;
           const millis_t ms = millis();
           if (ELAPSED(ms, t)) {
diff --git a/Marlin/src/gcode/feature/pause/M600.cpp b/Marlin/src/gcode/feature/pause/M600.cpp
index 1c282f2052c..1033025fe34 100644
--- a/Marlin/src/gcode/feature/pause/M600.cpp
+++ b/Marlin/src/gcode/feature/pause/M600.cpp
@@ -83,7 +83,7 @@ void GcodeSuite::M600() {
     int8_t DXC_ext = target_extruder;
     if (!parser.seen('T')) {  // If no tool index is specified, M600 was (probably) sent in response to filament runout.
                               // In this case, for duplicating modes set DXC_ext to the extruder that ran out.
-      #if HAS_FILAMENT_SENSOR && NUM_RUNOUT_SENSORS > 1
+      #if MULTI_FILAMENT_SENSOR
         if (idex_is_duplicating())
           DXC_ext = (READ(FIL_RUNOUT2_PIN) == FIL_RUNOUT2_STATE) ? 1 : 0;
       #else
diff --git a/Marlin/src/inc/Conditionals_adv.h b/Marlin/src/inc/Conditionals_adv.h
index b8464df3dd5..4738cda20ab 100644
--- a/Marlin/src/inc/Conditionals_adv.h
+++ b/Marlin/src/inc/Conditionals_adv.h
@@ -128,6 +128,9 @@
 
 #if ENABLED(FILAMENT_RUNOUT_SENSOR)
   #define HAS_FILAMENT_SENSOR 1
+  #if NUM_RUNOUT_SENSORS > 1
+    #define MULTI_FILAMENT_SENSOR 1
+  #endif
   #ifdef FILAMENT_RUNOUT_DISTANCE_MM
     #define HAS_FILAMENT_RUNOUT_DISTANCE 1
   #endif
diff --git a/Marlin/src/lcd/extui/ui_api.cpp b/Marlin/src/lcd/extui/ui_api.cpp
index d1ffb4c4379..7c178a0339b 100644
--- a/Marlin/src/lcd/extui/ui_api.cpp
+++ b/Marlin/src/lcd/extui/ui_api.cpp
@@ -340,8 +340,10 @@ namespace ExtUI {
     #endif
   }
 
-  extruder_t getActiveTool() {
-    switch (active_extruder) {
+  extruder_t getTool(const uint8_t extruder) {
+    switch (extruder) {
+      case 7:  return E7;
+      case 6:  return E6;
       case 5:  return E5;
       case 4:  return E4;
       case 3:  return E3;
@@ -351,6 +353,8 @@ namespace ExtUI {
     }
   }
 
+  extruder_t getActiveTool() { return getTool(active_extruder); }
+
   bool isMoving() { return planner.has_blocks_queued(); }
 
   bool canMove(const axis_t axis) {
diff --git a/Marlin/src/lcd/extui/ui_api.h b/Marlin/src/lcd/extui/ui_api.h
index bfd658b0d9b..4214ba5821d 100644
--- a/Marlin/src/lcd/extui/ui_api.h
+++ b/Marlin/src/lcd/extui/ui_api.h
@@ -215,6 +215,7 @@ namespace ExtUI {
     void setAxisMaxJerk_mm_s(const float, const extruder_t);
   #endif
 
+  extruder_t getTool(const uint8_t extruder);
   extruder_t getActiveTool();
   void setActiveTool(const extruder_t, bool no_move);
 
diff --git a/Marlin/src/pins/ramps/pins_RAMPS_CREALITY.h b/Marlin/src/pins/ramps/pins_RAMPS_CREALITY.h
index 1dc898e6ca9..05de208ca1e 100644
--- a/Marlin/src/pins/ramps/pins_RAMPS_CREALITY.h
+++ b/Marlin/src/pins/ramps/pins_RAMPS_CREALITY.h
@@ -35,7 +35,7 @@
 #define MOSFET_D_PIN                           7
 
 #define FIL_RUNOUT_PIN                         2
-#if NUM_RUNOUT_SENSORS > 1
+#if NUM_RUNOUT_SENSORS >= 2
   #define FIL_RUNOUT2_PIN                     15  // Creality CR-X can use dual runout sensors
 #endif