From 0ca9db705191d0b36dea513437ec518b0c0211a6 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Sun, 27 May 2018 17:56:21 -0500
Subject: [PATCH] Correct HOTENDS for SWITCHING_EXTRUDER

---
 .travis.yml                       |  2 --
 Marlin/src/Marlin.cpp             | 25 +++++++++++++++++++++----
 Marlin/src/inc/Conditionals_LCD.h | 28 +++++++++++++---------------
 Marlin/src/inc/SanityCheck.h      |  8 ++------
 Marlin/src/module/stepper.cpp     |  6 ++----
 Marlin/src/module/temperature.h   |  6 ++++++
 6 files changed, 44 insertions(+), 31 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 1ce6a2f5ba..7e1c72d483 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -167,7 +167,6 @@ script:
   - opt_set EXTRUDERS 2
   - opt_enable NUM_SERVOS
   - opt_set NUM_SERVOS 1
-  - opt_set TEMP_SENSOR_1 1
   - opt_enable SWITCHING_EXTRUDER ULTIMAKERCONTROLLER
   - build_marlin_pio ${TRAVIS_BUILD_DIR} ${TEST_PLATFORM}
   #
@@ -421,7 +420,6 @@ script:
   - opt_set EXTRUDERS 2
   - opt_enable NUM_SERVOS
   - opt_set NUM_SERVOS 1
-  - opt_set TEMP_SENSOR_1 1
   - opt_enable SWITCHING_EXTRUDER ULTIMAKERCONTROLLER
   - build_marlin_pio ${TRAVIS_BUILD_DIR} ${TEST_PLATFORM}
   #
diff --git a/Marlin/src/Marlin.cpp b/Marlin/src/Marlin.cpp
index 6f7fc4dae6..e089e034c7 100644
--- a/Marlin/src/Marlin.cpp
+++ b/Marlin/src/Marlin.cpp
@@ -427,8 +427,16 @@ void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
       && !planner.has_blocks_queued()
     ) {
       #if ENABLED(SWITCHING_EXTRUDER)
-        const bool oldstatus = E0_ENABLE_READ;
-        enable_E0();
+        bool oldstatus;
+        switch (active_extruder) {
+          default: oldstatus = E0_ENABLE_READ; enable_E0(); break;
+          #if E_STEPPERS > 1
+            case 2: case 3: oldstatus = E1_ENABLE_READ; enable_E1(); break;
+            #if E_STEPPERS > 2
+              case 4: oldstatus = E2_ENABLE_READ; enable_E2(); break;
+            #endif // E_STEPPERS > 2
+          #endif // E_STEPPERS > 1
+        }
       #else // !SWITCHING_EXTRUDER
         bool oldstatus;
         switch (active_extruder) {
@@ -454,9 +462,18 @@ void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
       current_position[E_AXIS] = olde;
       planner.set_e_position_mm(olde);
       planner.synchronize();
+
       #if ENABLED(SWITCHING_EXTRUDER)
-        E0_ENABLE_WRITE(oldstatus);
-      #else
+        switch (active_extruder) {
+          default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
+          #if E_STEPPERS > 1
+            case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
+            #if E_STEPPERS > 2
+              case 4: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
+            #endif // E_STEPPERS > 2
+          #endif // E_STEPPERS > 1
+        }
+      #else // !SWITCHING_EXTRUDER
         switch (active_extruder) {
           case 0: E0_ENABLE_WRITE(oldstatus); break;
           #if E_STEPPERS > 1
diff --git a/Marlin/src/inc/Conditionals_LCD.h b/Marlin/src/inc/Conditionals_LCD.h
index 80c53cfa9c..69e396b61e 100644
--- a/Marlin/src/inc/Conditionals_LCD.h
+++ b/Marlin/src/inc/Conditionals_LCD.h
@@ -406,23 +406,8 @@
  *  E_MANUAL     - Number of E steppers for LCD move options
  *
  */
-#if ENABLED(SINGLENOZZLE) || ENABLED(MIXING_EXTRUDER)         // One hotend, one thermistor, no XY offset
-  #define HOTENDS       1
-  #undef TEMP_SENSOR_1_AS_REDUNDANT
-  #undef HOTEND_OFFSET_X
-  #undef HOTEND_OFFSET_Y
-#else                                                         // Two hotends
-  #define HOTENDS       EXTRUDERS
-#endif
-
 #define HOTEND_LOOP() for (int8_t e = 0; e < HOTENDS; e++)
 
-#if HOTENDS == 1
-  #define HOTEND_INDEX  0
-#else
-  #define HOTEND_INDEX  e
-#endif
-
 #if ENABLED(SWITCHING_EXTRUDER)                               // One stepper for every two EXTRUDERS
   #if EXTRUDERS > 4
     #define E_STEPPERS    3
@@ -431,6 +416,7 @@
   #else
     #define E_STEPPERS    1
   #endif
+  #define HOTENDS         E_STEPPERS
   #define E_MANUAL        EXTRUDERS
 #elif ENABLED(MIXING_EXTRUDER)
   #define E_STEPPERS      MIXING_STEPPERS
@@ -440,6 +426,18 @@
   #define E_MANUAL        EXTRUDERS
 #endif
 
+#if ENABLED(SINGLENOZZLE) || ENABLED(MIXING_EXTRUDER)         // One hotend, one thermistor, no XY offset
+  #undef HOTENDS
+  #define HOTENDS       1
+  #undef TEMP_SENSOR_1_AS_REDUNDANT
+  #undef HOTEND_OFFSET_X
+  #undef HOTEND_OFFSET_Y
+#endif
+
+#ifndef HOTENDS
+  #define HOTENDS EXTRUDERS
+#endif
+
 #define DO_SWITCH_EXTRUDER (ENABLED(SWITCHING_EXTRUDER) && (DISABLED(SWITCHING_NOZZLE) || SWITCHING_EXTRUDER_SERVO_NR != SWITCHING_NOZZLE_SERVO_NR))
 
 /**
diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h
index 26b7c8a5cb..d37f16b221 100644
--- a/Marlin/src/inc/SanityCheck.h
+++ b/Marlin/src/inc/SanityCheck.h
@@ -582,12 +582,8 @@ static_assert(X_MAX_LENGTH >= X_BED_SIZE && Y_MAX_LENGTH >= Y_BED_SIZE,
 /**
  * Single Stepper Dual Extruder with switching servo
  */
-#if ENABLED(SWITCHING_EXTRUDER)
-  #if ENABLED(SINGLENOZZLE)
-    #error "SWITCHING_EXTRUDER and SINGLENOZZLE are incompatible."
-  #elif NUM_SERVOS < 1
-    #error "SWITCHING_EXTRUDER requires NUM_SERVOS >= 1."
-  #endif
+#if ENABLED(SWITCHING_EXTRUDER) && NUM_SERVOS < 1
+  #error "SWITCHING_EXTRUDER requires NUM_SERVOS >= 1."
 #endif
 
 /**
diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp
index 4c0e70c3f7..ff534707cd 100644
--- a/Marlin/src/module/stepper.cpp
+++ b/Marlin/src/module/stepper.cpp
@@ -1841,10 +1841,8 @@ uint32_t Stepper::stepper_block_phase_isr() {
         current_adv_steps++;
         interval = eISR_Rate;
       }
-      else {
-        interval = ADV_NEVER;
-        eISR_Rate = ADV_NEVER;
-      }
+      else
+        interval = eISR_Rate = ADV_NEVER;
     }
     else
       interval = ADV_NEVER;
diff --git a/Marlin/src/module/temperature.h b/Marlin/src/module/temperature.h
index f6637d03ea..9be142cb82 100644
--- a/Marlin/src/module/temperature.h
+++ b/Marlin/src/module/temperature.h
@@ -42,6 +42,12 @@
   #define SOFT_PWM_SCALE 0
 #endif
 
+#if HOTENDS == 1
+  #define HOTEND_INDEX  0
+#else
+  #define HOTEND_INDEX  e
+#endif
+
 /**
  * States for ADC reading in the ISR
  */
-- 
GitLab