diff --git a/Marlin/src/gcode/feature/advance/M900.cpp b/Marlin/src/gcode/feature/advance/M900.cpp index fd500261b6..a8384108b5 100644 --- a/Marlin/src/gcode/feature/advance/M900.cpp +++ b/Marlin/src/gcode/feature/advance/M900.cpp @@ -29,11 +29,10 @@ #include "../../../module/stepper.h" #if ENABLED(EXTRA_LIN_ADVANCE_K) - float saved_extruder_advance_K[EXTRUDERS]; + float other_extruder_advance_K[EXTRUDERS]; uint8_t lin_adv_slot = 0; #endif - /** * M900: Get or Set Linear Advance K-factor * T Which tool to address @@ -43,100 +42,107 @@ */ void GcodeSuite::M900() { + auto echo_value_oor = [] (const char ltr, const bool ten=true) { + SERIAL_CHAR('?'); SERIAL_CHAR(ltr); + SERIAL_ECHOPGM(" value out of range"); + if (ten) SERIAL_ECHOPGM(" (0-10)"); + SERIAL_ECHOLNPGM("."); + } + + if (!parser.seen_any()) { + + #if ENABLED(EXTRA_LIN_ADVANCE_K) + + #if EXTRUDERS < 2 + SERIAL_ECHOLNPAIR("Advance S", int(new_slot), " K", kref, "(S", int(!new_slot), " K", lref, ")"); + #else + LOOP_L_N(i, EXTRUDERS) { + const bool slot = TEST(lin_adv_slot, i); + SERIAL_ECHOLNPAIR("Advance T", int(i), " S", int(slot), " K", planner.extruder_advance_K[i], + "(S", int(!slot), " K", other_extruder_advance_K[i], ")"); + SERIAL_EOL(); + } + #endif + + #else + + SERIAL_ECHO_START(); + #if EXTRUDERS < 2 + SERIAL_ECHOLNPAIR("Advance K=", planner.extruder_advance_K[0]); + #else + SERIAL_ECHOPGM("Advance K"); + LOOP_L_N(i, EXTRUDERS) { + SERIAL_CHAR(' ', '0' + i, ':'); + SERIAL_ECHO(planner.extruder_advance_K[i]); + } + SERIAL_EOL(); + #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) { - SERIAL_ECHOLNPGM("?T value out of range."); + 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) - bool ext_slot = TEST(lin_adv_slot, tool_index); - - if (parser.seenval('S')) { - const bool slot = parser.value_bool(); - if (ext_slot != slot) { - ext_slot = slot; - SET_BIT_TO(lin_adv_slot, tool_index, slot); - planner.synchronize(); - const float temp = planner.extruder_advance_K[tool_index]; - planner.extruder_advance_K[tool_index] = saved_extruder_advance_K[tool_index]; - saved_extruder_advance_K[tool_index] = temp; - } + 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)) { - if (ext_slot) - saved_extruder_advance_K[tool_index] = newK; - else { - planner.synchronize(); - planner.extruder_advance_K[tool_index] = newK; - } - } - else - SERIAL_ECHOLNPGM("?K value out of range (0-10)."); + 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)) { - if (!ext_slot) - saved_extruder_advance_K[tool_index] = newL; - else { - planner.synchronize(); - planner.extruder_advance_K[tool_index] = newL; - } - } - else - SERIAL_ECHOLNPGM("?L value out of range (0-10)."); - } - - if (!parser.seen_any()) { - #if EXTRUDERS < 2 - SERIAL_ECHOLNPAIR("Advance S", ext_slot, " K", planner.extruder_advance_K[0], - "(Slot ", 1 - ext_slot, " K", saved_extruder_advance_K[0], ")"); - #else - LOOP_L_N(i, EXTRUDERS) { - const int slot = (int)TEST(lin_adv_slot, i); - SERIAL_ECHOLNPAIR("Advance T", int(i), " S", slot, " K", planner.extruder_advance_K[i], - "(Slot ", 1 - slot, " K", saved_extruder_advance_K[i], ")"); - SERIAL_EOL(); - } - #endif + 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)) { - planner.synchronize(); - planner.extruder_advance_K[tool_index] = newK; - } + if (WITHIN(newK, 0, 10)) + newK = newK; else - SERIAL_ECHOLNPGM("?K value out of range (0-10)."); - } - else { - SERIAL_ECHO_START(); - #if EXTRUDERS < 2 - SERIAL_ECHOLNPAIR("Advance K=", planner.extruder_advance_K[0]); - #else - SERIAL_ECHOPGM("Advance K"); - LOOP_L_N(i, EXTRUDERS) { - SERIAL_CHAR(' '); SERIAL_ECHO(int(i)); - SERIAL_CHAR('='); SERIAL_ECHO(planner.extruder_advance_K[i]); - } - SERIAL_EOL(); - #endif + echo_value_oor('K'); } #endif + + if (newK != oldK) { + planner.synchronize(); + kref = newK; + } } #endif // LIN_ADVANCE diff --git a/Marlin/src/module/configuration_store.cpp b/Marlin/src/module/configuration_store.cpp index 346fd64de3..77fc18bb53 100644 --- a/Marlin/src/module/configuration_store.cpp +++ b/Marlin/src/module/configuration_store.cpp @@ -101,7 +101,7 @@ #endif #if ENABLED(EXTRA_LIN_ADVANCE_K) - extern float saved_extruder_advance_K[EXTRUDERS]; + extern float other_extruder_advance_K[EXTRUDERS]; #endif #if EXTRUDERS > 1 @@ -2717,7 +2717,7 @@ void MarlinSettings::reset() { LOOP_L_N(i, EXTRUDERS) { planner.extruder_advance_K[i] = LIN_ADVANCE_K; #if ENABLED(EXTRA_LIN_ADVANCE_K) - saved_extruder_advance_K[i] = LIN_ADVANCE_K; + other_extruder_advance_K[i] = LIN_ADVANCE_K; #endif } #endif