From e6ac9ff204b09c0a852f71b77417f99ce4f5473a Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 18 Mar 2023 17:13:06 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Misc.=20optimizations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Marlin/src/MarlinCore.cpp | 4 ++-- Marlin/src/core/serial.cpp | 5 ---- Marlin/src/core/serial.h | 7 +++++- Marlin/src/feature/bedlevel/ubl/ubl.cpp | 2 +- Marlin/src/module/temperature.cpp | 31 ++++++++++++++----------- Marlin/src/module/temperature.h | 6 ++--- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp index ef6bf6aad5..342f8812d8 100644 --- a/Marlin/src/MarlinCore.cpp +++ b/Marlin/src/MarlinCore.cpp @@ -520,8 +520,8 @@ inline void manage_inactivity(const bool no_stepper_sleep=false) { if (ELAPSED(ms, next_cub_ms_##N)) { \ next_cub_ms_##N = ms + CUB_DEBOUNCE_DELAY_##N; \ CODE; \ - queue.inject(F(BUTTON##N##_GCODE)); \ - TERN_(HAS_MARLINUI_MENU, ui.quick_feedback()); \ + queue.inject(F(BUTTON##N##_GCODE)); \ + TERN_(HAS_MARLINUI_MENU, ui.quick_feedback()); \ } \ } \ }while(0) diff --git a/Marlin/src/core/serial.cpp b/Marlin/src/core/serial.cpp index 727b191d04..64704c1e6c 100644 --- a/Marlin/src/core/serial.cpp +++ b/Marlin/src/core/serial.cpp @@ -85,11 +85,6 @@ void serial_offset(const_float_t v, const uint8_t sp/*=0*/) { SERIAL_DECIMAL(v); } -void serial_ternary(const bool onoff, FSTR_P const pre, FSTR_P const on, FSTR_P const off, FSTR_P const post/*=nullptr*/) { - if (pre) serial_print(pre); - serial_print(onoff ? on : off); - if (post) serial_print(post); -} void serialprint_onoff(const bool onoff) { serial_print(onoff ? F(STR_ON) : F(STR_OFF)); } void serialprintln_onoff(const bool onoff) { serialprint_onoff(onoff); SERIAL_EOL(); } void serialprint_truefalse(const bool tf) { serial_print(tf ? F("true") : F("false")); } diff --git a/Marlin/src/core/serial.h b/Marlin/src/core/serial.h index a1126d7461..a741d4b1e4 100644 --- a/Marlin/src/core/serial.h +++ b/Marlin/src/core/serial.h @@ -327,7 +327,12 @@ inline void serial_echolnpair(FSTR_P const fstr, T v) { serial_echolnpair_P(FTOP void serial_echo_start(); void serial_error_start(); -void serial_ternary(const bool onoff, FSTR_P const pre, FSTR_P const on, FSTR_P const off, FSTR_P const post=nullptr); +inline void serial_ternary(const bool onoff, FSTR_P const pre, FSTR_P const on, FSTR_P const off, FSTR_P const post=nullptr) { + if (pre) serial_print(pre); + if (onoff && on) serial_print(on); + if (!onoff && off) serial_print(off); + if (post) serial_print(post); +} void serialprint_onoff(const bool onoff); void serialprintln_onoff(const bool onoff); void serialprint_truefalse(const bool tf); diff --git a/Marlin/src/feature/bedlevel/ubl/ubl.cpp b/Marlin/src/feature/bedlevel/ubl/ubl.cpp index f2af1445b1..6d60e7a2ef 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl.cpp @@ -59,7 +59,7 @@ void unified_bed_leveling::report_current_mesh() { void unified_bed_leveling::report_state() { echo_name(); - SERIAL_ECHO_TERNARY(planner.leveling_active, " System v" UBL_VERSION " ", "", "in", "active\n"); + serial_ternary(planner.leveling_active, " System v" UBL_VERSION " ", nullptr, "in", "active\n"); serial_delay(50); } diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index 2bb88320b5..cc89d71e50 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -2890,7 +2890,7 @@ void Temperature::init() { * * TODO: Embed the last 3 parameters during init, if not less optimal */ - void Temperature::tr_state_machine_t::run(const_celsius_float_t current, const_celsius_float_t target, const heater_id_t heater_id, const uint16_t period_seconds, const celsius_t hysteresis_degc) { + void Temperature::tr_state_machine_t::run(const_celsius_float_t current, const_celsius_float_t target, const heater_id_t heater_id, const uint16_t period_seconds, const celsius_float_t hysteresis_degc) { #if HEATER_IDLE_HANDLER // Convert the given heater_id_t to an idle array index @@ -2959,21 +2959,26 @@ void Temperature::init() { // While the temperature is stable watch for a bad temperature case TRStable: { + const celsius_float_t rdiff = running_temp - current; + #if ENABLED(ADAPTIVE_FAN_SLOWING) if (adaptive_fan_slowing && heater_id >= 0) { - const int fan_index = _MIN(heater_id, FAN_COUNT - 1); - if (fan_speed[fan_index] == 0 || current >= running_temp - (hysteresis_degc * 0.25f)) - fan_speed_scaler[fan_index] = 128; - else if (current >= running_temp - (hysteresis_degc * 0.3335f)) - fan_speed_scaler[fan_index] = 96; - else if (current >= running_temp - (hysteresis_degc * 0.5f)) - fan_speed_scaler[fan_index] = 64; - else if (current >= running_temp - (hysteresis_degc * 0.8f)) - fan_speed_scaler[fan_index] = 32; + const int_fast8_t fan_index = _MIN(heater_id, FAN_COUNT - 1); + uint8_t scale; + if (fan_speed[fan_index] == 0 || rdiff <= hysteresis_degc * 0.25f) + scale = 128; + else if (rdiff <= hysteresis_degc * 0.3335f) + scale = 96; + else if (rdiff <= hysteresis_degc * 0.5f) + scale = 64; + else if (rdiff <= hysteresis_degc * 0.8f) + scale = 32; else - fan_speed_scaler[fan_index] = 0; + scale = 0; + + fan_speed_scaler[fan_index] = scale; } - #endif + #endif // ADAPTIVE_FAN_SLOWING const millis_t now = millis(); @@ -2993,7 +2998,7 @@ void Temperature::init() { } #endif - if (current >= running_temp - hysteresis_degc) { + if (rdiff <= hysteresis_degc) { timer = now + SEC_TO_MS(period_seconds); break; } diff --git a/Marlin/src/module/temperature.h b/Marlin/src/module/temperature.h index 0ab00ef768..4bf76e5d48 100644 --- a/Marlin/src/module/temperature.h +++ b/Marlin/src/module/temperature.h @@ -49,7 +49,7 @@ #define E_NAME TERN_(HAS_MULTI_HOTEND, e) // Element identifiers. Positive values are hotends. Negative values are other heaters or coolers. -typedef enum : int8_t { +typedef enum : int_fast8_t { H_REDUNDANT = HID_REDUNDANT, H_COOLER = HID_COOLER, H_PROBE = HID_PROBE, @@ -1319,12 +1319,12 @@ class Temperature { typedef struct { millis_t timer = 0; TRState state = TRInactive; - float running_temp; + celsius_float_t running_temp; #if ENABLED(THERMAL_PROTECTION_VARIANCE_MONITOR) millis_t variance_timer = 0; celsius_float_t last_temp = 0.0, variance = 0.0; #endif - void run(const_celsius_float_t current, const_celsius_float_t target, const heater_id_t heater_id, const uint16_t period_seconds, const celsius_t hysteresis_degc); + void run(const_celsius_float_t current, const_celsius_float_t target, const heater_id_t heater_id, const uint16_t period_seconds, const celsius_float_t hysteresis_degc); } tr_state_machine_t; static tr_state_machine_t tr_state_machine[NR_HEATER_RUNAWAY];