diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index 5173db02a156..c4920c210024 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -628,6 +628,36 @@ volatile bool Temperature::raw_temps_ready = false; * Class and Instance Methods */ +#if ANY(HAS_PID_HEATING, MPC_AUTOTUNE) + + /** + * Run the minimal required activities during a tuning loop. + * TODO: Allow tuning routines to call idle() for more complete keepalive. + */ + bool Temperature::tuning_idle(const millis_t &ms) { + + // Run HAL idle tasks + hal.idletask(); + + const bool temp_ready = updateTemperaturesIfReady(); + + #if HAS_FAN_LOGIC + if (temp_ready) manage_extruder_fans(ms); + #else + UNUSED(ms); + #endif + + // Run Controller Fan check (normally handled by manage_inactivity) + TERN_(USE_CONTROLLER_FAN, controllerFan.update()); + + // Run UI update + ui.update(); + + return temp_ready; + } + +#endif + #if HAS_PID_HEATING inline void say_default_() { SERIAL_ECHOPGM("#define DEFAULT_"); } @@ -727,7 +757,11 @@ volatile bool Temperature::raw_temps_ready = false; const millis_t ms = millis(); - if (updateTemperaturesIfReady()) { // temp sample ready + // Run minimal necessary machine tasks + const bool temp_ready = tuning_idle(ms); + + // If a new sample has arrived process things + if (temp_ready) { // Get the current temperature and constrain it current_temp = GHV(degChamber(), degBed(), degHotend(heater_id)); @@ -738,8 +772,6 @@ volatile bool Temperature::raw_temps_ready = false; ONHEATING(start_temp, current_temp, target); #endif - TERN_(HAS_FAN_LOGIC, manage_extruder_fans(ms)); - if (heating && current_temp > target && ELAPSED(ms, t2 + 5000UL)) { heating = false; SHV((bias - d) >> 1); @@ -885,12 +917,6 @@ volatile bool Temperature::raw_temps_ready = false; goto EXIT_M303; } - - // Run HAL idle tasks - hal.idletask(); - - // Run UI update - ui.update(); } wait_for_heatup = false; @@ -1142,10 +1168,11 @@ volatile bool Temperature::raw_temps_ready = false; constexpr millis_t report_interval_ms = 1000UL; curr_time_ms = millis(); - if (updateTemperaturesIfReady()) { // temp sample ready - current_temp = degHotend(e); - TERN_(HAS_FAN_LOGIC, manage_extruder_fans(curr_time_ms)); - } + // Run minimal necessary machine tasks + const bool temp_ready = tuning_idle(curr_time_ms); + + // Set MPC temp if a new sample is ready + if (temp_ready) current_temp = degHotend(e); if (ELAPSED(curr_time_ms, next_report_ms)) { next_report_ms += report_interval_ms; @@ -1153,9 +1180,6 @@ volatile bool Temperature::raw_temps_ready = false; SERIAL_EOL(); } - hal.idletask(); - ui.update(); - if (!wait_for_heatup) { SERIAL_ECHOLNPGM(STR_MPC_AUTOTUNE_INTERRUPTED); TERN_(DWIN_LCD_PROUI, dwinMPCTuning(MPC_INTERRUPTED)); diff --git a/Marlin/src/module/temperature.h b/Marlin/src/module/temperature.h index c4d718e148ba..c69a272adfdf 100644 --- a/Marlin/src/module/temperature.h +++ b/Marlin/src/module/temperature.h @@ -1197,6 +1197,8 @@ class Temperature { static constexpr bool adaptive_fan_slowing = true; #endif + static bool tuning_idle(const millis_t &ms); + /** * M303 PID auto-tuning for hotends or bed */