From 646599d0d95a2c7dd47b869d5bc93bdb2c95cc4b Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Mon, 2 Mar 2020 16:51:47 -0600
Subject: [PATCH] Finish M900 updates

---
 Marlin/src/gcode/feature/advance/M900.cpp | 127 +++++++++++-----------
 buildroot/share/tests/mega2560-tests      |   4 +-
 2 files changed, 67 insertions(+), 64 deletions(-)

diff --git a/Marlin/src/gcode/feature/advance/M900.cpp b/Marlin/src/gcode/feature/advance/M900.cpp
index a8384108b5..bbba4f246e 100644
--- a/Marlin/src/gcode/feature/advance/M900.cpp
+++ b/Marlin/src/gcode/feature/advance/M900.cpp
@@ -47,6 +47,71 @@ void GcodeSuite::M900() {
     SERIAL_ECHOPGM(" value out of range");
     if (ten) SERIAL_ECHOPGM(" (0-10)");
     SERIAL_ECHOLNPGM(".");
+  };
+
+  #if EXTRUDERS < 2
+    constexpr uint8_t tool_index = 0;
+  #else
+    const uint8_t tool_index = parser.intval('T', active_extruder);
+    if (tool_index >= EXTRUDERS) {
+      echo_value_oor('T', false);
+      return;
+    }
+  #endif
+
+  float &kref = planner.extruder_advance_K[tool_index];
+
+  #if ENABLED(EXTRA_LIN_ADVANCE_K)
+    float &lref = other_extruder_advance_K[tool_index];
+  #endif
+
+  const float oldK = kref;
+  float newK = oldK;
+
+  #if ENABLED(EXTRA_LIN_ADVANCE_K)
+
+    const bool old_slot = TEST(lin_adv_slot, tool_index), // The tool's current slot (0 or 1)
+               new_slot = parser.boolval('S', old_slot);  // The passed slot (default = current)
+
+    // If a new slot is being selected swap the current and
+    // saved K values. Do here so K/L will apply correctly.
+    if (new_slot != old_slot) {                       // Not the same slot?
+      SET_BIT_TO(lin_adv_slot, tool_index, new_slot); // Update the slot for the tool
+      newK = lref;                                    // Get new K value from backup
+      lref = oldK;                                    // Save K to backup
+    }
+
+    // Set the main K value. Apply if the main slot is active.
+    if (parser.seenval('K')) {
+      const float K = parser.value_float();
+      if (!WITHIN(K, 0, 10)) echo_value_oor('K');
+      else if (new_slot)        lref = K;             // S1 Knn
+      else                      newK = K;             // S0 Knn
+    }
+
+    // Set the extra K value. Apply if the extra slot is active.
+    if (parser.seenval('L')) {
+      const float L = parser.value_float();
+      if (!WITHIN(L, 0, 10)) echo_value_oor('L');
+      else if (!new_slot)       lref = L;             // S0 Lnn
+      else                      newK = L;             // S1 Lnn
+    }
+
+  #else
+
+    if (parser.seenval('K')) {
+      const float K = parser.value_float();
+      if (WITHIN(K, 0, 10))
+        newK = K;
+      else
+        echo_value_oor('K');
+    }
+
+  #endif
+
+  if (newK != oldK) {
+    planner.synchronize();
+    kref = newK;
   }
 
   if (!parser.seen_any()) {
@@ -79,70 +144,8 @@ void GcodeSuite::M900() {
       #endif
 
     #endif
-
-    return;
   }
 
-  #if EXTRUDERS < 2
-    constexpr uint8_t tool_index = 0;
-  #else
-    const uint8_t tool_index = parser.intval('T', active_extruder);
-    if (tool_index >= EXTRUDERS) {
-      echo_value_oor('T', false);
-      return;
-    }
-  #endif
-
-  float &kref = planner.extruder_advance_K[tool_index],
-        &lref = other_extruder_advance_K[tool_index];
-  const float oldK = kref, oldOther = lref;
-  float newK = oldK;
-
-  #if ENABLED(EXTRA_LIN_ADVANCE_K)
-
-    const bool old_slot = TEST(lin_adv_slot, tool_index), // The tool's current slot (0 or 1)
-               new_slot = parser.boolval('S', old_slot);  // The passed slot (default = current)
-
-    // If a new slot is being selected swap the current and
-    // saved K values. Do here so K/L will apply correctly.
-    if (new_slot != old_slot) {                       // Not the same slot?
-      SET_BIT_TO(lin_adv_slot, tool_index, new_slot); // Update the slot for the tool
-      newK = oldOther;                                // Get new K value from backup
-      lref = oldK;                                    // Save K to backup
-    }
-
-    // Set the main K value. Apply if the main slot is active.
-    if (parser.seenval('K')) {
-      const float newK = parser.value_float();
-      if (!WITHIN(newK, 0, 10)) echo_value_oor('K');
-      else if (new_slot)        lref = newK;          // S1 Knn
-      else                      newK = newK;          // S0 Knn
-    }
-
-    // Set the extra K value. Apply if the extra slot is active.
-    if (parser.seenval('L')) {
-      const float newL = parser.value_float();
-      if (!WITHIN(newL, 0, 10)) echo_value_oor('L');
-      else if (!new_slot)       lref = newL;          // S0 Lnn
-      else                      newK = newL;          // S1 Lnn
-    }
-
-  #else
-
-    if (parser.seenval('K')) {
-      const float newK = parser.value_float();
-      if (WITHIN(newK, 0, 10))
-        newK = newK;
-      else
-        echo_value_oor('K');
-    }
-
-  #endif
-
-  if (newK != oldK) {
-    planner.synchronize();
-    kref = newK;
-  }
 }
 
 #endif // LIN_ADVANCE
diff --git a/buildroot/share/tests/mega2560-tests b/buildroot/share/tests/mega2560-tests
index a896c38733..b09560add5 100755
--- a/buildroot/share/tests/mega2560-tests
+++ b/buildroot/share/tests/mega2560-tests
@@ -45,7 +45,7 @@ opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER LCD_PROGRESS_BAR LCD_PROGRESS_BAR_TE
            BACKLASH_COMPENSATION BACKLASH_GCODE BAUD_RATE_GCODE BEZIER_CURVE_SUPPORT \
            FWRETRACT ARC_P_CIRCLES CNC_WORKSPACE_PLANES CNC_COORDINATE_SYSTEMS \
            PSU_CONTROL AUTO_POWER_CONTROL POWER_LOSS_RECOVERY POWER_LOSS_PIN POWER_LOSS_STATE \
-          SLOW_PWM_HEATERS THERMAL_PROTECTION_CHAMBER LIN_ADVANCE \
+          SLOW_PWM_HEATERS THERMAL_PROTECTION_CHAMBER LIN_ADVANCE EXTRA_LIN_ADVANCE_K \
            HOST_ACTION_COMMANDS HOST_PROMPT_SUPPORT PINS_DEBUGGING MAX7219_DEBUG M114_DETAIL
 exec_test $1 $2 "RAMPS | EXTRUDERS 2 | CHAR LCD + SD | FIX Probe | ABL-Linear | Advanced Pause | PLR | LEDs ..."
 
@@ -66,7 +66,7 @@ opt_enable AUTO_BED_LEVELING_UBL RESTORE_LEVELING_AFTER_G28 DEBUG_LEVELING_FEATU
            REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER LIGHTWEIGHT_UI STATUS_MESSAGE_SCROLLING BOOT_MARLIN_LOGO_SMALL \
            SDSUPPORT SDCARD_SORT_ALPHA USB_FLASH_DRIVE_SUPPORT SCROLL_LONG_FILENAMES CANCEL_OBJECTS \
            EEPROM_SETTINGS EEPROM_CHITCHAT GCODE_MACROS CUSTOM_USER_MENUS \
-           MULTI_NOZZLE_DUPLICATION CLASSIC_JERK LIN_ADVANCE QUICK_HOME \
+           MULTI_NOZZLE_DUPLICATION CLASSIC_JERK LIN_ADVANCE EXTRA_LIN_ADVANCE_K QUICK_HOME \
            LCD_SET_PROGRESS_MANUALLY PRINT_PROGRESS_SHOW_DECIMALS SHOW_REMAINING_TIME \
            BABYSTEPPING BABYSTEP_XY NANODLP_Z_SYNC I2C_POSITION_ENCODERS M114_DETAIL
 exec_test $1 $2 "Azteeg X3 Pro | EXTRUDERS 5 | RRDFGSC | UBL | LIN_ADVANCE ..."