From 513cac1aa0289e5b2727086933cc68a54b43d9e1 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 29 Apr 2022 15:26:20 -0500 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Fan=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Marlin/src/MarlinCore.cpp | 2 +- Marlin/src/feature/controllerfan.cpp | 7 +- Marlin/src/feature/controllerfan.h | 3 + Marlin/src/feature/pause.cpp | 8 +- Marlin/src/feature/power.cpp | 5 +- Marlin/src/feature/powerloss.cpp | 2 +- Marlin/src/gcode/control/M42.cpp | 26 +- Marlin/src/gcode/control/M80_M81.cpp | 3 +- Marlin/src/gcode/temp/M106_M107.cpp | 6 +- Marlin/src/inc/Conditionals_post.h | 16 +- Marlin/src/lcd/HD44780/marlinui_HD44780.cpp | 30 +- Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp | 10 +- Marlin/src/lcd/dogm/status_screen_DOGM.cpp | 12 +- .../lcd/dogm/status_screen_lite_ST7920.cpp | 10 +- Marlin/src/lcd/e3v2/creality/dwin.cpp | 18 +- Marlin/src/lcd/e3v2/jyersui/dwin.cpp | 30 +- .../lcd/e3v2/marlinui/ui_status_480x272.cpp | 2 +- Marlin/src/lcd/e3v2/proui/dwin.cpp | 14 +- .../lcd/extui/dgus/fysetc/DGUSDisplayDef.cpp | 6 +- .../lcd/extui/dgus/hiprecy/DGUSDisplayDef.cpp | 6 +- .../src/lcd/extui/dgus/mks/DGUSDisplayDef.cpp | 6 +- .../lcd/extui/dgus/origin/DGUSDisplayDef.cpp | 6 +- Marlin/src/lcd/extui/mks_ui/draw_fan.cpp | 2 +- Marlin/src/lcd/extui/mks_ui/draw_printing.cpp | 2 +- .../src/lcd/extui/mks_ui/draw_ready_print.cpp | 2 +- Marlin/src/lcd/extui/mks_ui/mks_hardware.cpp | 4 +- Marlin/src/lcd/extui/ui_api.cpp | 4 +- Marlin/src/lcd/menu/menu_item.h | 4 +- Marlin/src/lcd/tft/touch.cpp | 2 +- Marlin/src/lcd/tft/ui_1024x600.cpp | 4 +- Marlin/src/lcd/tft/ui_320x240.cpp | 4 +- Marlin/src/lcd/tft/ui_480x320.cpp | 4 +- Marlin/src/module/fans.cpp | 89 +++++ Marlin/src/module/fans.h | 200 +++++++++++ Marlin/src/module/planner.cpp | 89 +---- Marlin/src/module/planner.h | 16 +- Marlin/src/module/probe.cpp | 2 +- Marlin/src/module/stepper.cpp | 2 +- Marlin/src/module/temperature.cpp | 328 ++++-------------- Marlin/src/module/temperature.h | 51 +-- Marlin/src/module/tool_change.cpp | 8 +- buildroot/tests/mega2560 | 2 +- 42 files changed, 503 insertions(+), 544 deletions(-) create mode 100644 Marlin/src/module/fans.cpp create mode 100644 Marlin/src/module/fans.h diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp index 4572dc9d7c87c..9c81095cee221 100644 --- a/Marlin/src/MarlinCore.cpp +++ b/Marlin/src/MarlinCore.cpp @@ -960,7 +960,7 @@ void stop() { print_job_timer.stop(); #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE) - thermalManager.set_fans_paused(false); // Un-pause fans for safety + Fan::all_pause(false); // Un-pause fans for safety #endif if (!IsStopped()) { diff --git a/Marlin/src/feature/controllerfan.cpp b/Marlin/src/feature/controllerfan.cpp index f42bf52ae40a0..f99c8016863de 100644 --- a/Marlin/src/feature/controllerfan.cpp +++ b/Marlin/src/feature/controllerfan.cpp @@ -27,6 +27,7 @@ #include "controllerfan.h" #include "../module/stepper.h" #include "../module/temperature.h" +#include "../module/fans.h" ControllerFan controllerFan; @@ -38,6 +39,10 @@ uint8_t ControllerFan::speed; const controllerFan_settings_t &ControllerFan::settings = controllerFan_defaults; #endif +#if ENABLED(FAN_SOFT_PWM) + uint8_t ControllerFan::soft_pwm_speed; +#endif + void ControllerFan::setup() { SET_OUTPUT(CONTROLLER_FAN_PIN); init(); @@ -73,7 +78,7 @@ void ControllerFan::update() { ); #if ENABLED(FAN_SOFT_PWM) - thermalManager.soft_pwm_controller_speed = speed; + soft_pwm_speed = speed; #else if (PWM_PIN(CONTROLLER_FAN_PIN)) hal.set_pwm_duty(pin_t(CONTROLLER_FAN_PIN), speed); diff --git a/Marlin/src/feature/controllerfan.h b/Marlin/src/feature/controllerfan.h index 55eb2359b0677..68502afa66678 100644 --- a/Marlin/src/feature/controllerfan.h +++ b/Marlin/src/feature/controllerfan.h @@ -60,6 +60,9 @@ class ControllerFan { #else static const controllerFan_settings_t &settings; #endif + #if ENABLED(FAN_SOFT_PWM) + static uint8_t soft_pwm_speed; + #endif static bool state() { return speed > 0; } static void init() { reset(); } static void reset() { TERN_(CONTROLLER_FAN_EDITABLE, settings = controllerFan_defaults); } diff --git a/Marlin/src/feature/pause.cpp b/Marlin/src/feature/pause.cpp index ac5852f91a1ac..9ae006529d7b0 100644 --- a/Marlin/src/feature/pause.cpp +++ b/Marlin/src/feature/pause.cpp @@ -438,8 +438,8 @@ bool pause_print(const_float_t retract, const xyz_pos_t &park_point, const bool // Wait for buffered blocks to complete planner.synchronize(); - #if ENABLED(ADVANCED_PAUSE_FANS_PAUSE) && HAS_FAN - thermalManager.set_fans_paused(true); + #if BOTH(ADVANCED_PAUSE_FANS_PAUSE, HAS_FAN) + Fan::all_pause(true); #endif // Initial retract before move to filament change position @@ -705,8 +705,8 @@ void resume_print(const_float_t slow_load_length/*=0*/, const_float_t fast_load_ } #endif - #if ENABLED(ADVANCED_PAUSE_FANS_PAUSE) && HAS_FAN - thermalManager.set_fans_paused(false); + #if BOTH(ADVANCED_PAUSE_FANS_PAUSE, HAS_FAN) + Fan::all_pause(false); #endif TERN_(HAS_FILAMENT_SENSOR, runout.reset()); diff --git a/Marlin/src/feature/power.cpp b/Marlin/src/feature/power.cpp index c2ed169aa809f..546ea6c2e3131 100644 --- a/Marlin/src/feature/power.cpp +++ b/Marlin/src/feature/power.cpp @@ -183,9 +183,8 @@ void Power::power_off() { if (printJobOngoing() || printingIsPaused()) return true; - #if ENABLED(AUTO_POWER_FANS) - FANS_LOOP(i) if (thermalManager.fan_speed[i]) return true; - #endif + // Do any fans need power? + if (TERN0(AUTO_POWER_FANS, Fan::is_power_needed())) return true; #if ENABLED(AUTO_POWER_E_FANS) HOTEND_LOOP() if (thermalManager.autofan_speed[e]) return true; diff --git a/Marlin/src/feature/powerloss.cpp b/Marlin/src/feature/powerloss.cpp index d4450adcd8539..c523b145f5a35 100644 --- a/Marlin/src/feature/powerloss.cpp +++ b/Marlin/src/feature/powerloss.cpp @@ -214,7 +214,7 @@ void PrintJobRecovery::save(const bool force/*=false*/, const float zraise/*=POW TERN_(HAS_HEATED_BED, info.target_temperature_bed = thermalManager.degTargetBed()); #if HAS_FAN - COPY(info.fan_speed, thermalManager.fan_speed); + FANS_LOOP(f) info.fan_speed[f] = fans[f].speed; #endif #if HAS_LEVELING diff --git a/Marlin/src/gcode/control/M42.cpp b/Marlin/src/gcode/control/M42.cpp index 1b3a29d10056a..32966de8a57fb 100644 --- a/Marlin/src/gcode/control/M42.cpp +++ b/Marlin/src/gcode/control/M42.cpp @@ -86,30 +86,8 @@ void GcodeSuite::M42() { #if HAS_FAN switch (pin) { - #if HAS_FAN0 - case FAN0_PIN: thermalManager.fan_speed[0] = pin_status; return; - #endif - #if HAS_FAN1 - case FAN1_PIN: thermalManager.fan_speed[1] = pin_status; return; - #endif - #if HAS_FAN2 - case FAN2_PIN: thermalManager.fan_speed[2] = pin_status; return; - #endif - #if HAS_FAN3 - case FAN3_PIN: thermalManager.fan_speed[3] = pin_status; return; - #endif - #if HAS_FAN4 - case FAN4_PIN: thermalManager.fan_speed[4] = pin_status; return; - #endif - #if HAS_FAN5 - case FAN5_PIN: thermalManager.fan_speed[5] = pin_status; return; - #endif - #if HAS_FAN6 - case FAN6_PIN: thermalManager.fan_speed[6] = pin_status; return; - #endif - #if HAS_FAN7 - case FAN7_PIN: thermalManager.fan_speed[7] = pin_status; return; - #endif + #define _CASE(N) case FAN##N##_PIN: fans[N].speed = pin_status; return; + REPEAT(FAN_COUNT, _CASE) } #endif diff --git a/Marlin/src/gcode/control/M80_M81.cpp b/Marlin/src/gcode/control/M80_M81.cpp index 90b25e7ed34db..bf2de7c6c25f3 100644 --- a/Marlin/src/gcode/control/M80_M81.cpp +++ b/Marlin/src/gcode/control/M80_M81.cpp @@ -80,8 +80,7 @@ void GcodeSuite::M81() { print_job_timer.stop(); #if BOTH(HAS_FAN, PROBING_FANS_OFF) - thermalManager.fans_paused = false; - ZERO(thermalManager.saved_fan_speed); + Fan::power_off(); #endif safe_delay(1000); // Wait 1 second before switching off diff --git a/Marlin/src/gcode/temp/M106_M107.cpp b/Marlin/src/gcode/temp/M106_M107.cpp index 98e87c415de92..14ba0e7c5a789 100644 --- a/Marlin/src/gcode/temp/M106_M107.cpp +++ b/Marlin/src/gcode/temp/M106_M107.cpp @@ -67,10 +67,10 @@ void GcodeSuite::M106() { #if ENABLED(EXTRA_FAN_SPEED) const uint16_t t = parser.intval('T'); - if (t > 0) return thermalManager.set_temp_fan_speed(pfan, t); + if (t > 0) return fans[pfan].set_temp_speed(t); #endif - const uint16_t dspeed = parser.seen_test('A') ? thermalManager.fan_speed[active_extruder] : 255; + const uint16_t dspeed = parser.seen_test('A') ? fans[active_extruder].speed : 255; uint16_t speed = dspeed; @@ -85,7 +85,7 @@ void GcodeSuite::M106() { if (!got_preset && parser.seenval('S')) speed = parser.value_ushort(); - TERN_(FOAMCUTTER_XYUV, speed *= 2.55); // Get command in % of max heat + TERN_(FOAMCUTTER_XYUV, speed *= 2.55f); // Get command in % of max heat // Set speed, with constraint thermalManager.set_fan_speed(pfan, speed); diff --git a/Marlin/src/inc/Conditionals_post.h b/Marlin/src/inc/Conditionals_post.h index 6c025b1dbbf9c..bd3ba60e2f5a0 100644 --- a/Marlin/src/inc/Conditionals_post.h +++ b/Marlin/src/inc/Conditionals_post.h @@ -2917,17 +2917,10 @@ #endif #define _NOT_E_AUTO(N,F) (E##N##_AUTO_FAN_PIN != FAN##F##_PIN) -#define _HAS_FAN(F) (PIN_EXISTS(FAN##F) \ +#define _HAS_FAN(F) (F < MAX_FANS && PIN_EXISTS(FAN##F) \ && CONTROLLER_FAN_PIN != FAN##F##_PIN \ - && _NOT_E_AUTO(0,F) \ - && _NOT_E_AUTO(1,F) \ - && _NOT_E_AUTO(2,F) \ - && _NOT_E_AUTO(3,F) \ - && _NOT_E_AUTO(4,F) \ - && _NOT_E_AUTO(5,F) \ - && _NOT_E_AUTO(6,F) \ - && _NOT_E_AUTO(7,F) \ - && F < MAX_FANS) + && _NOT_E_AUTO(0,F) && _NOT_E_AUTO(1,F) && _NOT_E_AUTO(2,F) && _NOT_E_AUTO(3,F) \ + && _NOT_E_AUTO(4,F) && _NOT_E_AUTO(5,F) && _NOT_E_AUTO(6,F) && _NOT_E_AUTO(7,F) ) #if PIN_EXISTS(FAN) #define HAS_FAN0 1 #endif @@ -2988,6 +2981,9 @@ #if FAN_COUNT > 0 #define HAS_FAN 1 +#else + #undef FAN_SOFT_PWM + #undef FAN_SOFT_PWM_REQUIRED #endif /** diff --git a/Marlin/src/lcd/HD44780/marlinui_HD44780.cpp b/Marlin/src/lcd/HD44780/marlinui_HD44780.cpp index f70b5b01749f0..ca01a9d37a7a7 100644 --- a/Marlin/src/lcd/HD44780/marlinui_HD44780.cpp +++ b/Marlin/src/lcd/HD44780/marlinui_HD44780.cpp @@ -946,30 +946,30 @@ void MarlinUI::draw_status_screen() { _draw_print_progress(); #else char c; - uint16_t per; + uint16_t pct; #if HAS_FAN0 if (true #if EXTRUDERS && ENABLED(ADAPTIVE_FAN_SLOWING) - && (blink || thermalManager.fan_speed_scaler[0] < 128) + && (blink || fans[0].speed_scaler < 128) #endif ) { - uint16_t spd = thermalManager.fan_speed[0]; + uint16_t spd = fans[0].speed; if (blink) c = 'F'; #if ENABLED(ADAPTIVE_FAN_SLOWING) - else { c = '*'; spd = thermalManager.scaledFanSpeed(0, spd); } + else { c = '*'; spd = fans[0].scaled_speed(spd); } #endif - per = thermalManager.pwmToPercent(spd); + pct = Fan::pwmToPercent(spd); } else #endif { #if HAS_EXTRUDERS c = 'E'; - per = planner.flow_percentage[0]; + pct = planner.flow_percentage[0]; #endif } lcd_put_wchar(c); - lcd_put_u8str(i16tostr3rj(per)); + lcd_put_u8str(i16tostr3rj(pct)); lcd_put_wchar('%'); #endif #endif @@ -1150,14 +1150,14 @@ void MarlinUI::draw_status_screen() { if (TERN0(HAS_HOTEND, thermalManager.degTargetHotend(0) > 0)) leds |= LED_B; #if HAS_FAN - if ( TERN0(HAS_FAN0, thermalManager.fan_speed[0]) - || TERN0(HAS_FAN1, thermalManager.fan_speed[1]) - || TERN0(HAS_FAN2, thermalManager.fan_speed[2]) - || TERN0(HAS_FAN3, thermalManager.fan_speed[3]) - || TERN0(HAS_FAN4, thermalManager.fan_speed[4]) - || TERN0(HAS_FAN5, thermalManager.fan_speed[5]) - || TERN0(HAS_FAN6, thermalManager.fan_speed[6]) - || TERN0(HAS_FAN7, thermalManager.fan_speed[7]) + if ( TERN0(HAS_FAN0, fans[0].speed) + || TERN0(HAS_FAN1, fans[1].speed) + || TERN0(HAS_FAN2, fans[2].speed) + || TERN0(HAS_FAN3, fans[3].speed) + || TERN0(HAS_FAN4, fans[4].speed) + || TERN0(HAS_FAN5, fans[5].speed) + || TERN0(HAS_FAN6, fans[6].speed) + || TERN0(HAS_FAN7, fans[7].speed) ) leds |= LED_C; #endif // HAS_FAN diff --git a/Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp b/Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp index ca830960cb8f9..eeeb03167a714 100644 --- a/Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp +++ b/Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp @@ -881,11 +881,11 @@ void MarlinUI::draw_status_screen() { #endif #if HAS_FAN - uint16_t spd = thermalManager.fan_speed[0]; + uint16_t spd = fans[0].speed; #if ENABLED(ADAPTIVE_FAN_SLOWING) - if (!blink) spd = thermalManager.scaledFanSpeed(0, spd); + if (!blink) spd = fans[0].scaled_speed(spd); #endif - uint16_t per = thermalManager.pwmToPercent(spd); + const uint16_t pct = Fan::pwmToPercent(spd); #if HOTENDS < 2 #define FANX 11 @@ -895,9 +895,9 @@ void MarlinUI::draw_status_screen() { lcd.setCursor(FANX, 5); lcd_put_u8str(F("FAN")); lcd.setCursor(FANX + 1, 6); lcd.write('%'); lcd.setCursor(FANX, 7); - lcd.print(i16tostr3rj(per)); + lcd.print(i16tostr3rj(pct)); - if (TERN0(HAS_FAN0, thermalManager.fan_speed[0]) || TERN0(HAS_FAN1, thermalManager.fan_speed[1]) || TERN0(HAS_FAN2, thermalManager.fan_speed[2])) + if (TERN0(HAS_FAN0, fans[0].speed) || TERN0(HAS_FAN1, fans[1].speed) || TERN0(HAS_FAN2, fans[2].speed)) picBits |= ICON_FAN; else picBits &= ~ICON_FAN; diff --git a/Marlin/src/lcd/dogm/status_screen_DOGM.cpp b/Marlin/src/lcd/dogm/status_screen_DOGM.cpp index 010a1397f620c..e8a672d0a6f2f 100644 --- a/Marlin/src/lcd/dogm/status_screen_DOGM.cpp +++ b/Marlin/src/lcd/dogm/status_screen_DOGM.cpp @@ -640,7 +640,7 @@ void MarlinUI::draw_status_screen() { static uint8_t fan_frame; if (old_blink != blink) { old_blink = blink; - if (!thermalManager.fan_speed[0] || ++fan_frame >= STATUS_FAN_FRAMES) fan_frame = 0; + if (!fans[0].speed || ++fan_frame >= STATUS_FAN_FRAMES) fan_frame = 0; } #endif if (PAGE_CONTAINS(STATUS_FAN_Y, STATUS_FAN_Y + STATUS_FAN_HEIGHT - 1)) @@ -652,7 +652,7 @@ void MarlinUI::draw_status_screen() { fan_frame == 3 ? status_fan3_bmp : #endif #elif STATUS_FAN_FRAMES > 1 - blink && thermalManager.fan_speed[0] ? status_fan1_bmp : + blink && fans[0].speed ? status_fan1_bmp : #endif status_fan0_bmp ); @@ -725,15 +725,15 @@ void MarlinUI::draw_status_screen() { #if DO_DRAW_FAN if (PAGE_CONTAINS(STATUS_FAN_TEXT_Y - INFO_FONT_ASCENT, STATUS_FAN_TEXT_Y - 1)) { char c = '%'; - uint16_t spd = thermalManager.fan_speed[0]; + uint16_t spd = fans[0].speed; if (spd) { #if ENABLED(ADAPTIVE_FAN_SLOWING) - if (!blink && thermalManager.fan_speed_scaler[0] < 128) { - spd = thermalManager.scaledFanSpeed(0, spd); + if (!blink && fans[0].speed_scaler < 128) { + spd = fans[0].scaled_speed(spd); c = '*'; } #endif - lcd_put_u8str(STATUS_FAN_TEXT_X, STATUS_FAN_TEXT_Y, i16tostr3rj(thermalManager.pwmToPercent(spd))); + lcd_put_u8str(STATUS_FAN_TEXT_X, STATUS_FAN_TEXT_Y, i16tostr3rj(Fan::pwmToPercent(spd))); lcd_put_wchar(c); } } diff --git a/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp b/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp index 492a79a311088..94ef16c7eae9b 100644 --- a/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp +++ b/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp @@ -698,7 +698,7 @@ bool ST7920_Lite_Status_Screen::indicators_changed() { // them only during blinks we gain a bit of stability. const bool blink = ui.get_blink(); const uint16_t feedrate_perc = feedrate_percentage; - const uint16_t fs = thermalManager.scaledFanSpeed(0); + const uint16_t fs = fans[0].scaled_speed(); const celsius_t extruder_1_target = thermalManager.degTargetHotend(0); #if HAS_MULTI_HOTEND const celsius_t extruder_2_target = thermalManager.degTargetHotend(1); @@ -736,12 +736,12 @@ void ST7920_Lite_Status_Screen::update_indicators(const bool forceUpdate) { TERN_(HAS_MULTI_HOTEND, draw_extruder_2_temp(extruder_2_temp, extruder_2_target, forceUpdate)); TERN_(HAS_HEATED_BED, draw_bed_temp(bed_temp, bed_target, forceUpdate)); - uint8_t spd = thermalManager.fan_speed[0]; + uint8_t spd = fans[0].speed; #if ENABLED(ADAPTIVE_FAN_SLOWING) - if (!blink && thermalManager.fan_speed_scaler[0] < 128) - spd = thermalManager.scaledFanSpeed(0, spd); + if (!blink && fans[0].speed_scaler < 128) + spd = fans[0].scaled_speed(spd); #endif - draw_fan_speed(thermalManager.pwmToPercent(spd)); + draw_fan_speed(Fan::pwmToPercent(spd)); // Draw elapsed/remaining time const bool show_remaining = ENABLED(SHOW_REMAINING_TIME) && (DISABLED(ROTATE_PROGRESS_DISPLAY) || blink); diff --git a/Marlin/src/lcd/e3v2/creality/dwin.cpp b/Marlin/src/lcd/e3v2/creality/dwin.cpp index b2cb1bee30ce5..7b7067d1ba3c1 100644 --- a/Marlin/src/lcd/e3v2/creality/dwin.cpp +++ b/Marlin/src/lcd/e3v2/creality/dwin.cpp @@ -1003,7 +1003,7 @@ void Draw_Tune_Menu() { #endif #if HAS_FAN Draw_Menu_Line(TUNE_CASE_FAN, ICON_FanSpeed); - Draw_Edit_Integer3(TUNE_CASE_FAN, thermalManager.fan_speed[0]); + Draw_Edit_Integer3(TUNE_CASE_FAN, fans[0].speed); #endif #if HAS_ZOFFSET_ITEM Draw_Menu_Line(TUNE_CASE_ZOFF, ICON_Zoffset); @@ -1691,8 +1691,8 @@ void update_variable() { #endif #if HAS_FAN static uint8_t _fanspeed = 0; - const bool _new_fanspeed = _fanspeed != thermalManager.fan_speed[0]; - if (_new_fanspeed) _fanspeed = thermalManager.fan_speed[0]; + const bool _new_fanspeed = _fanspeed != fans[0].speed; + if (_new_fanspeed) _fanspeed = fans[0].speed; #endif if (checkkey == Tune) { @@ -1753,7 +1753,7 @@ void update_variable() { #if HAS_FAN if (_new_fanspeed) { - _fanspeed = thermalManager.fan_speed[0]; + _fanspeed = fans[0].speed; Draw_Stat_Int(195 + 2 * STAT_CHR_W, 384, _fanspeed); } #endif @@ -1994,7 +1994,7 @@ void Draw_Status_Area(const bool with_update) { #if HAS_FAN DWIN_ICON_Show(ICON, ICON_FanSpeed, 187, 383); - Draw_Stat_Int(195 + 2 * STAT_CHR_W, 384, thermalManager.fan_speed[0]); + Draw_Stat_Int(195 + 2 * STAT_CHR_W, 384, fans[0].speed); #endif #if HAS_ZOFFSET_ITEM @@ -2246,7 +2246,7 @@ void HMI_SelectFile() { // All fans on for Ender 3 v2 ? // The slicer should manage this for us. //for (uint8_t i = 0; i < FAN_COUNT; i++) - // thermalManager.fan_speed[i] = 255; + // fans[i].speed = 255; #endif _card_percent = 0; @@ -2843,7 +2843,7 @@ void Draw_Temperature_Menu() { #endif #if HAS_FAN _TMENU_ICON(TEMP_CASE_FAN); - Draw_Edit_Integer3(i, thermalManager.fan_speed[0]); + Draw_Edit_Integer3(i, fans[0].speed); #endif #if HAS_PREHEAT // PLA/ABS items have submenus @@ -3065,7 +3065,7 @@ void HMI_Temperature() { #if HAS_FAN case TEMP_CASE_FAN: checkkey = FanSpeed; - HMI_ValueStruct.Fan_speed = thermalManager.fan_speed[0]; + HMI_ValueStruct.Fan_speed = fans[0].speed; Draw_Edit_Integer3(3, HMI_ValueStruct.Fan_speed, true); EncoderRate.enabled = true; break; @@ -3792,7 +3792,7 @@ void HMI_Tune() { #if HAS_FAN case TUNE_CASE_FAN: // Fan speed checkkey = FanSpeed; - HMI_ValueStruct.Fan_speed = thermalManager.fan_speed[0]; + HMI_ValueStruct.Fan_speed = fans[0].speed; Draw_Edit_Integer3(TUNE_CASE_FAN + MROWS - index_tune, HMI_ValueStruct.Fan_speed, true); EncoderRate.enabled = true; break; diff --git a/Marlin/src/lcd/e3v2/jyersui/dwin.cpp b/Marlin/src/lcd/e3v2/jyersui/dwin.cpp index 9fed9fee83d9f..0fb80bee3a1c0 100644 --- a/Marlin/src/lcd/e3v2/jyersui/dwin.cpp +++ b/Marlin/src/lcd/e3v2/jyersui/dwin.cpp @@ -846,9 +846,9 @@ void CrealityDWINClass::Draw_Status_Area(bool icons/*=false*/) { fan = -1; DWIN_ICON_Show(ICON, ICON_FanSpeed, 187, 383); } - if (thermalManager.fan_speed[0] != fan) { - fan = thermalManager.fan_speed[0]; - DWIN_Draw_IntValue(true, true, 0, DWIN_FONT_STAT, GetColor(eeprom_settings.status_area_text, Color_White), Color_Bg_Black, 3, 195 + 2 * STAT_CHR_W, 384, thermalManager.fan_speed[0]); + if (fans[0].speed != fan) { + fan = fans[0].speed; + DWIN_Draw_IntValue(true, true, 0, DWIN_FONT_STAT, GetColor(eeprom_settings.status_area_text, Color_White), Color_Bg_Black, 3, 195 + 2 * STAT_CHR_W, 384, fans[0].speed); } #endif @@ -1870,10 +1870,10 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/ case TEMP_FAN: if (draw) { Draw_Menu_Item(row, ICON_FanSpeed, F("Fan")); - Draw_Float(thermalManager.fan_speed[0], row, false, 1); + Draw_Float(fans[0].speed, row, false, 1); } else - Modify_Value(thermalManager.fan_speed[0], MIN_FAN_SPEED, MAX_FAN_SPEED, 1); + Modify_Value(fans[0].speed, MIN_FAN_SPEED, MAX_FAN_SPEED, 1); break; #endif #if HAS_HOTEND || HAS_HEATED_BED @@ -3507,10 +3507,10 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/ case TUNE_FAN: if (draw) { Draw_Menu_Item(row, ICON_FanSpeed, F("Fan")); - Draw_Float(thermalManager.fan_speed[0], row, false, 1); + Draw_Float(fans[0].speed, row, false, 1); } else - Modify_Value(thermalManager.fan_speed[0], MIN_FAN_SPEED, MAX_FAN_SPEED, 1); + Modify_Value(fans[0].speed, MIN_FAN_SPEED, MAX_FAN_SPEED, 1); break; #endif @@ -4124,7 +4124,7 @@ void CrealityDWINClass::Print_Screen_Control() { cmnd[sprintf_P(cmnd, PSTR("M109 S%i"), pausetemp)] = '\0'; gcode.process_subcommands_now(cmnd); #endif - TERN_(HAS_FAN, thermalManager.fan_speed[0] = pausefan); + TERN_(HAS_FAN, fans[0].speed = pausefan); planner.synchronize(); TERN_(SDSUPPORT, queue.inject(F("M24"))); #endif @@ -4174,7 +4174,7 @@ void CrealityDWINClass::Popup_Control() { queue.inject(F("M25")); TERN_(HAS_HOTEND, pausetemp = thermalManager.temp_hotend[0].target); TERN_(HAS_HEATED_BED, pausebed = thermalManager.temp_bed.target); - TERN_(HAS_FAN, pausefan = thermalManager.fan_speed[0]); + TERN_(HAS_FAN, pausefan = fans[0].speed); thermalManager.cooldown(); #endif } @@ -4557,11 +4557,11 @@ void CrealityDWINClass::Screen_Update() { } #endif #if HAS_FAN - if (thermalManager.fan_speed[0] != fanspeed) { - fanspeed = thermalManager.fan_speed[0]; + if (fans[0].speed != fanspeed) { + fanspeed = fans[0].speed; if (scrollpos <= TEMP_FAN && TEMP_FAN <= scrollpos + MROWS) { if (process != Value || selection != TEMP_HOTEND - scrollpos) - Draw_Float(thermalManager.fan_speed[0], TEMP_FAN - scrollpos, false, 1); + Draw_Float(fans[0].speed, TEMP_FAN - scrollpos, false, 1); } } #endif @@ -4586,11 +4586,11 @@ void CrealityDWINClass::Screen_Update() { } #endif #if HAS_FAN - if (thermalManager.fan_speed[0] != fanspeed) { - fanspeed = thermalManager.fan_speed[0]; + if (fans[0].speed != fanspeed) { + fanspeed = fans[0].speed; if (scrollpos <= TUNE_FAN && TUNE_FAN <= scrollpos + MROWS) { if (process != Value || selection != TEMP_HOTEND - scrollpos) - Draw_Float(thermalManager.fan_speed[0], TUNE_FAN - scrollpos, false, 1); + Draw_Float(fans[0].speed, TUNE_FAN - scrollpos, false, 1); } } #endif diff --git a/Marlin/src/lcd/e3v2/marlinui/ui_status_480x272.cpp b/Marlin/src/lcd/e3v2/marlinui/ui_status_480x272.cpp index 20dbd45d67f35..e94be90a25d19 100644 --- a/Marlin/src/lcd/e3v2/marlinui/ui_status_480x272.cpp +++ b/Marlin/src/lcd/e3v2/marlinui/ui_status_480x272.cpp @@ -167,7 +167,7 @@ FORCE_INLINE void _draw_axis_value(const AxisEnum axis, const char *value, const // FORCE_INLINE void _draw_fan_status(const uint16_t x, const uint16_t y) { const uint16_t fanx = (4 * STATUS_CHR_WIDTH - STATUS_FAN_WIDTH) / 2; - const uint8_t fan_pct = thermalManager.scaledFanSpeedPercent(0); + const uint8_t fan_pct = fans[0].speed_pct_scaled(); const bool fan_on = !!fan_pct; if (fan_on) { DWIN_ICON_Animation(0, fan_on, ICON, ICON_Fan0, ICON_Fan3, x + fanx, y, 25); diff --git a/Marlin/src/lcd/e3v2/proui/dwin.cpp b/Marlin/src/lcd/e3v2/proui/dwin.cpp index b4a1bad746bbe..925a88b9eb280 100644 --- a/Marlin/src/lcd/e3v2/proui/dwin.cpp +++ b/Marlin/src/lcd/e3v2/proui/dwin.cpp @@ -800,8 +800,8 @@ void update_variable() { #endif #if HAS_FAN static uint8_t _fanspeed = 0; - const bool _new_fanspeed = _fanspeed != thermalManager.fan_speed[0]; - if (_new_fanspeed) _fanspeed = thermalManager.fan_speed[0]; + const bool _new_fanspeed = _fanspeed != fans[0].speed; + if (_new_fanspeed) _fanspeed = fans[0].speed; #endif if (checkkey == Menu && (CurrentMenu == TuneMenu || CurrentMenu == TemperatureMenu)) { @@ -847,7 +847,7 @@ void update_variable() { #if HAS_FAN if (_new_fanspeed) { - _fanspeed = thermalManager.fan_speed[0]; + _fanspeed = fans[0].speed; DWINUI::Draw_Int(DWIN_FONT_STAT, HMI_data.Indicator_Color, HMI_data.Background_Color, 3, 195 + 2 * STAT_CHR_W, 384, _fanspeed); } #endif @@ -1082,7 +1082,7 @@ void DWIN_Draw_Dashboard() { #if HAS_FAN DWINUI::Draw_Icon(ICON_FanSpeed, 187, 383); - DWINUI::Draw_Int(DWIN_FONT_STAT, HMI_data.Indicator_Color, HMI_data.Background_Color, 3, 195 + 2 * STAT_CHR_W, 384, thermalManager.fan_speed[0]); + DWINUI::Draw_Int(DWIN_FONT_STAT, HMI_data.Indicator_Color, HMI_data.Background_Color, 3, 195 + 2 * STAT_CHR_W, 384, fans[0].speed); #endif #if BOTH(BABYSTEPPING, HAS_BED_PROBE) @@ -2314,7 +2314,7 @@ void SetSpeed() { SetPIntOnClick(MIN_PRINT_SPEED, MAX_PRINT_SPEED); } #if HAS_FAN void ApplyFanSpeed() { thermalManager.set_fan_speed(0, MenuData.Value); } - void SetFanSpeed() { SetIntOnClick(0, 255, thermalManager.fan_speed[0], ApplyFanSpeed); } + void SetFanSpeed() { SetIntOnClick(0, 255, fans[0].speed, ApplyFanSpeed); } #endif #if ENABLED(ADVANCED_PAUSE_FEATURE) @@ -3349,7 +3349,7 @@ void Draw_Tune_Menu() { BedTargetItem = EDIT_ITEM_F(ICON_BedTemp, MSG_UBL_SET_TEMP_BED, onDrawBedTemp, SetBedTemp, &thermalManager.temp_bed.target); #endif #if HAS_FAN - FanSpeedItem = EDIT_ITEM_F(ICON_FanSpeed, MSG_FAN_SPEED, onDrawFanSpeed, SetFanSpeed, &thermalManager.fan_speed[0]); + FanSpeedItem = EDIT_ITEM_F(ICON_FanSpeed, MSG_FAN_SPEED, onDrawFanSpeed, SetFanSpeed, &fans[0].speed); #endif #if HAS_ZOFFSET_ITEM && EITHER(BABYSTEP_ZPROBE_OFFSET, JUST_BABYSTEP) EDIT_ITEM_F(ICON_Zoffset, MSG_ZPROBE_ZOFFSET, onDrawZOffset, SetZOffset, &BABY_Z_VAR); @@ -3489,7 +3489,7 @@ void Draw_Temperature_Menu() { BedTargetItem = EDIT_ITEM_F(ICON_SetBedTemp, MSG_UBL_SET_TEMP_BED, onDrawBedTemp, SetBedTemp, &thermalManager.temp_bed.target); #endif #if HAS_FAN - FanSpeedItem = EDIT_ITEM_F(ICON_FanSpeed, MSG_FAN_SPEED, onDrawFanSpeed, SetFanSpeed, &thermalManager.fan_speed[0]); + FanSpeedItem = EDIT_ITEM_F(ICON_FanSpeed, MSG_FAN_SPEED, onDrawFanSpeed, SetFanSpeed, &fans[0].speed); #endif #if HAS_PREHEAT MENU_ITEM_F(ICON_SetPLAPreheat, MSG_PREHEAT_1_SETTINGS, onDrawPLAPreheatSubMenu, Draw_Preheat1_Menu); diff --git a/Marlin/src/lcd/extui/dgus/fysetc/DGUSDisplayDef.cpp b/Marlin/src/lcd/extui/dgus/fysetc/DGUSDisplayDef.cpp index a4c0997bf8a03..7171f8479e2ae 100644 --- a/Marlin/src/lcd/extui/dgus/fysetc/DGUSDisplayDef.cpp +++ b/Marlin/src/lcd/extui/dgus/fysetc/DGUSDisplayDef.cpp @@ -407,9 +407,9 @@ const struct DGUS_VP_Variable ListOfVP[] PROGMEM = { // Fan Data #if HAS_FAN #define FAN_VPHELPER(N) \ - VPHELPER(VP_Fan##N##_Percentage, &thermalManager.fan_speed[N], ScreenHandler.DGUSLCD_PercentageToUint8, ScreenHandler.DGUSLCD_SendPercentageToDisplay), \ - VPHELPER(VP_FAN##N##_CONTROL, &thermalManager.fan_speed[N], ScreenHandler.HandleFanControl, nullptr), \ - VPHELPER(VP_FAN##N##_STATUS, &thermalManager.fan_speed[N], nullptr, ScreenHandler.DGUSLCD_SendFanStatusToDisplay), + VPHELPER(VP_Fan##N##_Percentage, &fans[N].speed, ScreenHandler.DGUSLCD_PercentageToUint8, ScreenHandler.DGUSLCD_SendPercentageToDisplay), \ + VPHELPER(VP_FAN##N##_CONTROL, &fans[N].speed, ScreenHandler.HandleFanControl, nullptr), \ + VPHELPER(VP_FAN##N##_STATUS, &fans[N].speed, nullptr, ScreenHandler.DGUSLCD_SendFanStatusToDisplay), REPEAT(FAN_COUNT, FAN_VPHELPER) #endif diff --git a/Marlin/src/lcd/extui/dgus/hiprecy/DGUSDisplayDef.cpp b/Marlin/src/lcd/extui/dgus/hiprecy/DGUSDisplayDef.cpp index 4c850183da0f6..91db3b4b43bea 100644 --- a/Marlin/src/lcd/extui/dgus/hiprecy/DGUSDisplayDef.cpp +++ b/Marlin/src/lcd/extui/dgus/hiprecy/DGUSDisplayDef.cpp @@ -400,9 +400,9 @@ const struct DGUS_VP_Variable ListOfVP[] PROGMEM = { // Fan Data #if HAS_FAN #define FAN_VPHELPER(N) \ - VPHELPER(VP_Fan##N##_Percentage, &thermalManager.fan_speed[N], ScreenHandler.DGUSLCD_PercentageToUint8, ScreenHandler.DGUSLCD_SendPercentageToDisplay), \ - VPHELPER(VP_FAN##N##_CONTROL, &thermalManager.fan_speed[N], ScreenHandler.HandleFanControl, nullptr), \ - VPHELPER(VP_FAN##N##_STATUS, &thermalManager.fan_speed[N], nullptr, ScreenHandler.DGUSLCD_SendFanStatusToDisplay), + VPHELPER(VP_Fan##N##_Percentage, &fans[N].speed, ScreenHandler.DGUSLCD_PercentageToUint8, ScreenHandler.DGUSLCD_SendPercentageToDisplay), \ + VPHELPER(VP_FAN##N##_CONTROL, &fans[N].speed, ScreenHandler.HandleFanControl, nullptr), \ + VPHELPER(VP_FAN##N##_STATUS, &fans[N].speed, nullptr, ScreenHandler.DGUSLCD_SendFanStatusToDisplay), REPEAT(FAN_COUNT, FAN_VPHELPER) #endif diff --git a/Marlin/src/lcd/extui/dgus/mks/DGUSDisplayDef.cpp b/Marlin/src/lcd/extui/dgus/mks/DGUSDisplayDef.cpp index 86920d6841c9c..cb2a821cd6352 100644 --- a/Marlin/src/lcd/extui/dgus/mks/DGUSDisplayDef.cpp +++ b/Marlin/src/lcd/extui/dgus/mks/DGUSDisplayDef.cpp @@ -603,9 +603,9 @@ const struct DGUS_VP_Variable ListOfVP[] PROGMEM = { // Fan Data #if HAS_FAN #define FAN_VPHELPER(N) \ - VPHELPER(VP_Fan##N##_Percentage, &thermalManager.fan_speed[N], ScreenHandler.DGUSLCD_SetUint8, ScreenHandler.DGUSLCD_SendFanToDisplay), \ - VPHELPER(VP_FAN##N##_CONTROL, &thermalManager.fan_speed[N], ScreenHandler.HandleFanControl, nullptr), \ - VPHELPER(VP_FAN##N##_STATUS, &thermalManager.fan_speed[N], nullptr, ScreenHandler.DGUSLCD_SendFanStatusToDisplay), + VPHELPER(VP_Fan##N##_Percentage, &fans[N].speed, ScreenHandler.DGUSLCD_SetUint8, ScreenHandler.DGUSLCD_SendFanToDisplay), \ + VPHELPER(VP_FAN##N##_CONTROL, &fans[N].speed, ScreenHandler.HandleFanControl, nullptr), \ + VPHELPER(VP_FAN##N##_STATUS, &fans[N].speed, nullptr, ScreenHandler.DGUSLCD_SendFanStatusToDisplay), REPEAT(FAN_COUNT, FAN_VPHELPER) #endif diff --git a/Marlin/src/lcd/extui/dgus/origin/DGUSDisplayDef.cpp b/Marlin/src/lcd/extui/dgus/origin/DGUSDisplayDef.cpp index 0115d3b8c13b4..db9c076bc1291 100644 --- a/Marlin/src/lcd/extui/dgus/origin/DGUSDisplayDef.cpp +++ b/Marlin/src/lcd/extui/dgus/origin/DGUSDisplayDef.cpp @@ -208,9 +208,9 @@ const struct DGUS_VP_Variable ListOfVP[] PROGMEM = { // Fan Data #if HAS_FAN #define FAN_VPHELPER(N) \ - VPHELPER(VP_Fan##N##_Percentage, &thermalManager.fan_speed[N], ScreenHandler.DGUSLCD_PercentageToUint8, ScreenHandler.DGUSLCD_SendPercentageToDisplay), \ - VPHELPER(VP_FAN##N##_CONTROL, &thermalManager.fan_speed[N], ScreenHandler.HandleFanControl, nullptr), \ - VPHELPER(VP_FAN##N##_STATUS, &thermalManager.fan_speed[N], nullptr, ScreenHandler.DGUSLCD_SendFanStatusToDisplay), + VPHELPER(VP_Fan##N##_Percentage, &fans[N].speed, ScreenHandler.DGUSLCD_PercentageToUint8, ScreenHandler.DGUSLCD_SendPercentageToDisplay), \ + VPHELPER(VP_FAN##N##_CONTROL, &fans[N].speed, ScreenHandler.HandleFanControl, nullptr), \ + VPHELPER(VP_FAN##N##_STATUS, &fans[N].speed, nullptr, ScreenHandler.DGUSLCD_SendFanStatusToDisplay), REPEAT(FAN_COUNT, FAN_VPHELPER) #endif diff --git a/Marlin/src/lcd/extui/mks_ui/draw_fan.cpp b/Marlin/src/lcd/extui/mks_ui/draw_fan.cpp index 12b47d9e945fd..1e4463ba32852 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_fan.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_fan.cpp @@ -47,7 +47,7 @@ enum { uint8_t fanPercent = 0; static void event_handler(lv_obj_t *obj, lv_event_t event) { if (event != LV_EVENT_RELEASED) return; - const uint8_t temp = map(thermalManager.fan_speed[0], 0, 255, 0, 100); + const uint8_t temp = map(fans[0].speed, 0, 255, 0, 100); if (abs(fanPercent - temp) > 2) fanPercent = temp; switch (obj->mks_obj_id) { case ID_F_ADD: if (fanPercent < 100) fanPercent++; break; diff --git a/Marlin/src/lcd/extui/mks_ui/draw_printing.cpp b/Marlin/src/lcd/extui/mks_ui/draw_printing.cpp index be596c8740745..00d8075caf76f 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_printing.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_printing.cpp @@ -239,7 +239,7 @@ void disp_bed_temp() { } void disp_fan_speed() { - sprintf_P(public_buf_l, PSTR("%d%%"), (int)thermalManager.fanSpeedPercent(0)); + sprintf_P(public_buf_l, PSTR("%d%%"), (int)fans[0].speed_pct()); lv_label_set_text(labelFan, public_buf_l); } diff --git a/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp b/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp index 39f270840be4e..3e4b648e60400 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp @@ -233,7 +233,7 @@ void lv_temp_refr() { lv_obj_align(labelBed, buttonBedstate, LV_ALIGN_OUT_RIGHT_MID, 0, 0); #endif #if HAS_FAN - sprintf_P(public_buf_l, PSTR("%d%%"), (int)thermalManager.fanSpeedPercent(0)); + sprintf_P(public_buf_l, PSTR("%d%%"), (int)fans[0].speed_pct()); lv_label_set_text(labelFan, public_buf_l); lv_obj_align(labelFan, buttonFanstate, LV_ALIGN_OUT_RIGHT_MID, 0, 0); #endif diff --git a/Marlin/src/lcd/extui/mks_ui/mks_hardware.cpp b/Marlin/src/lcd/extui/mks_ui/mks_hardware.cpp index c679751ebe90f..21a9dcdcf3b55 100644 --- a/Marlin/src/lcd/extui/mks_ui/mks_hardware.cpp +++ b/Marlin/src/lcd/extui/mks_ui/mks_hardware.cpp @@ -239,7 +239,7 @@ void mks_hardware_test() { if (millis() % 2000 < 1000) { - thermalManager.fan_speed[0] = 255; + fans[0].speed = 255; WRITE(X_DIR_PIN, LOW); #if HAS_Y_AXIS WRITE(Y_DIR_PIN, LOW); @@ -264,7 +264,7 @@ #endif } else { - thermalManager.fan_speed[0] = 0; + fans[0].speed = 0; WRITE(X_DIR_PIN, HIGH); #if HAS_Y_AXIS WRITE(Y_DIR_PIN, HIGH); diff --git a/Marlin/src/lcd/extui/ui_api.cpp b/Marlin/src/lcd/extui/ui_api.cpp index 143f240c2d537..7d1a42a5c2d53 100644 --- a/Marlin/src/lcd/extui/ui_api.cpp +++ b/Marlin/src/lcd/extui/ui_api.cpp @@ -304,12 +304,12 @@ namespace ExtUI { float getTargetFan_percent(const fan_t fan) { UNUSED(fan); - return TERN0(HAS_FAN, thermalManager.fanSpeedPercent(fan - FAN0)); + return TERN0(HAS_FAN, fans[fan - FAN0].speed_pct()); } float getActualFan_percent(const fan_t fan) { UNUSED(fan); - return TERN0(HAS_FAN, thermalManager.scaledFanSpeedPercent(fan - FAN0)); + return TERN0(HAS_FAN, fans[fan - FAN0].speed_pct_scaled()); } float getAxisPosition_mm(const axis_t axis) { diff --git a/Marlin/src/lcd/menu/menu_item.h b/Marlin/src/lcd/menu/menu_item.h index 80a0872da652a..8b94c49b262e0 100644 --- a/Marlin/src/lcd/menu/menu_item.h +++ b/Marlin/src/lcd/menu/menu_item.h @@ -485,9 +485,9 @@ class MenuItem_bool : public MenuEditItemBase { #endif #define _FAN_EDIT_ITEMS(F,L) do{ \ - editable.uint8 = thermalManager.fan_speed[F]; \ + editable.uint8 = fans[F].speed; \ EDIT_ITEM_FAST_N(percent, F, MSG_##L, &editable.uint8, 0, 255, on_fan_update); \ - EDIT_EXTRA_FAN_SPEED(percent, F, MSG_EXTRA_##L, &thermalManager.extra_fan_speed[F].speed, 3, 255); \ + EDIT_EXTRA_FAN_SPEED(percent, F, MSG_EXTRA_##L, &fans[F].extra.speed, 3, 255); \ }while(0) #if FAN_COUNT > 1 diff --git a/Marlin/src/lcd/tft/touch.cpp b/Marlin/src/lcd/tft/touch.cpp index a152907a9dde2..f1b34cacd3b51 100644 --- a/Marlin/src/lcd/tft/touch.cpp +++ b/Marlin/src/lcd/tft/touch.cpp @@ -217,7 +217,7 @@ void Touch::touch(touch_control_t *control) { ui.clear_lcd(); static uint8_t fan, fan_speed; fan = 0; - fan_speed = thermalManager.fan_speed[fan]; + fan_speed = fans[fan].speed; MenuItem_percent::action(GET_TEXT_F(MSG_FIRST_FAN_SPEED), &fan_speed, 0, 255, []{ thermalManager.set_fan_speed(fan, fan_speed); }); break; case FEEDRATE: diff --git a/Marlin/src/lcd/tft/ui_1024x600.cpp b/Marlin/src/lcd/tft/ui_1024x600.cpp index e8ba4737a80fa..97b227cd7a351 100644 --- a/Marlin/src/lcd/tft/ui_1024x600.cpp +++ b/Marlin/src/lcd/tft/ui_1024x600.cpp @@ -199,7 +199,7 @@ void draw_fan_status(uint16_t x, uint16_t y, const bool blink) { tft.canvas(x, y, 80, 120); tft.set_background(COLOR_BACKGROUND); - uint8_t fanSpeed = thermalManager.fan_speed[0]; + const uint8_t fanSpeed = fans[0].speed; MarlinImage image; if (fanSpeed >= 127) @@ -211,7 +211,7 @@ void draw_fan_status(uint16_t x, uint16_t y, const bool blink) { tft.add_image(8, 20, image, COLOR_FAN); - tft_string.set((uint8_t *)ui8tostr4pctrj(thermalManager.fan_speed[0])); + tft_string.set((uint8_t *)ui8tostr4pctrj(fanSpeed)); tft_string.trim(); tft.add_text(tft_string.center(80) + 6, 82, COLOR_FAN, tft_string); } diff --git a/Marlin/src/lcd/tft/ui_320x240.cpp b/Marlin/src/lcd/tft/ui_320x240.cpp index 148d5d3964c18..179e9a1d4d7b1 100644 --- a/Marlin/src/lcd/tft/ui_320x240.cpp +++ b/Marlin/src/lcd/tft/ui_320x240.cpp @@ -199,7 +199,7 @@ void draw_fan_status(uint16_t x, uint16_t y, const bool blink) { tft.canvas(x, y, 64, 100); tft.set_background(COLOR_BACKGROUND); - uint8_t fanSpeed = thermalManager.fan_speed[0]; + const uint8_t fanSpeed = fans[0].speed; MarlinImage image; if (fanSpeed >= 127) @@ -211,7 +211,7 @@ void draw_fan_status(uint16_t x, uint16_t y, const bool blink) { tft.add_image(0, 10, image, COLOR_FAN); - tft_string.set((uint8_t *)ui8tostr4pctrj(thermalManager.fan_speed[0])); + tft_string.set((uint8_t *)ui8tostr4pctrj(fanSpeed)); tft_string.trim(); tft.add_text(tft_string.center(64) + 6, 72, COLOR_FAN, tft_string); } diff --git a/Marlin/src/lcd/tft/ui_480x320.cpp b/Marlin/src/lcd/tft/ui_480x320.cpp index 34502df540268..1b8ccfab68747 100644 --- a/Marlin/src/lcd/tft/ui_480x320.cpp +++ b/Marlin/src/lcd/tft/ui_480x320.cpp @@ -199,7 +199,7 @@ void draw_fan_status(uint16_t x, uint16_t y, const bool blink) { tft.canvas(x, y, 80, 120); tft.set_background(COLOR_BACKGROUND); - uint8_t fanSpeed = thermalManager.fan_speed[0]; + const uint8_t fanSpeed = fans[0].speed; MarlinImage image; if (fanSpeed >= 127) @@ -211,7 +211,7 @@ void draw_fan_status(uint16_t x, uint16_t y, const bool blink) { tft.add_image(8, 20, image, COLOR_FAN); - tft_string.set((uint8_t *)ui8tostr4pctrj(thermalManager.fan_speed[0])); + tft_string.set((uint8_t *)ui8tostr4pctrj(fanSpeed)); tft_string.trim(); tft.add_text(tft_string.center(80) + 6, 82, COLOR_FAN, tft_string); } diff --git a/Marlin/src/module/fans.cpp b/Marlin/src/module/fans.cpp new file mode 100644 index 0000000000000..09ba74d047913 --- /dev/null +++ b/Marlin/src/module/fans.cpp @@ -0,0 +1,89 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * fans.cpp - Fan class + */ + +#include "../inc/MarlinConfig.h" + +#if HAS_FAN + +#include "fans.h" + +#define _FANDEF(N) Fan(N), +Fan fans[FAN_COUNT] = { REPEAT(FAN_COUNT, _FANDEF) }; + +#if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE) + bool Fan::paused; // = false +#endif + +void Fan::init_pins() { + #define _INIT(N) fans[N].init(); + REPEAT(FAN_COUNT, _INIT) +} + +#if ENABLED(REPORT_FAN_CHANGE) + /** + * Report print fan speed for a target extruder + */ + void Fan::report_speed(const uint8_t fan_index) { + PORT_REDIRECT(SerialMask::All); + SERIAL_ECHOLNPGM("M106 P", fan_index, " S", speed); + } +#endif + +#if ENABLED(EXTRA_FAN_SPEED) + /** + * Handle the M106 P T command: + * T1 = Restore fan speed saved on the last T2 + * T2 = Save the fan speed, then set to the last T<3-255> value + * T<3-255> = Set the "extra fan speed" + */ + void Fan::set_temp_speed(const uint16_t command_or_speed) { + switch (command_or_speed) { + case 1: break; + case 2: extra.saved = speed; break; + default: extra.speed = _MIN(command_or_speed, 255U); break; + } + } +#endif + +#if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE) + void Fan::pause(const bool p) { + if (p) { + pause_speed = speed; + speed = 0; + } + else + speed = pause_speed; + } + + void Fan::all_pause(const bool p) { + if (p != paused) { + paused = p; + FANS_LOOP(i) fans[i].pause(p); + } + } +#endif + +#endif // HAS_FAN diff --git a/Marlin/src/module/fans.h b/Marlin/src/module/fans.h new file mode 100644 index 0000000000000..9cf06bc858b7f --- /dev/null +++ b/Marlin/src/module/fans.h @@ -0,0 +1,200 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +/** + * fans.h - Fan class + */ + +#include "../inc/MarlinConfig.h" + +#if EITHER(HAS_FAN, USE_CONTROLLER_FAN) + // Init fans according to whether they're native PWM or Software PWM + #ifdef BOARD_OPENDRAIN_MOSFETS + #define _INIT_SOFT_FAN(P) OUT_WRITE_OD(P, FAN_INVERTING ? LOW : HIGH) + #else + #define _INIT_SOFT_FAN(P) OUT_WRITE(P, FAN_INVERTING ? LOW : HIGH) + #endif + #if ENABLED(FAN_SOFT_PWM) + #define _INIT_FAN_PIN(P) _INIT_SOFT_FAN(P) + #else + #define _INIT_FAN_PIN(P) do{ if (PWM_PIN(P)) SET_PWM(P); else _INIT_SOFT_FAN(P); }while(0) + #endif + #if ENABLED(FAST_PWM_FAN) + #define SET_FAST_PWM_FREQ(P) hal.set_pwm_frequency(pin_t(P), FAST_PWM_FAN_FREQUENCY) + #else + #define SET_FAST_PWM_FREQ(P) NOOP + #endif + #define INIT_FAN_PIN(P) do{ _INIT_FAN_PIN(P); SET_FAST_PWM_FREQ(P); }while(0) +#endif + +#if HAS_FAN + + #define PWM_MASK TERN0(SOFT_PWM_DITHER, _BV(SOFT_PWM_SCALE) - 1) + + #if ENABLED(FAN_SOFT_PWM) + + #define _SOFT_PWM(N) do{ \ + soft_pwm_count &= PWM_MASK; \ + soft_pwm_count += (soft_pwm_amount >> 1); \ + write(N, soft_pwm_count > PWM_MASK ? HIGH : LOW); \ + }while(0) + + #define _SLOW_PWM(N) do{ \ + soft_pwm_count = soft_pwm_amount >> 1; \ + write(N, soft_pwm_count > 0 ? HIGH : LOW); \ + }while(0) + + #endif + + #define FANS_LOOP(I) LOOP_L_N(I, FAN_COUNT) + + class Fan; + extern Fan fans[FAN_COUNT]; + + class Fan { + public: + uint8_t index; + + Fan(const uint8_t fi) : index(fi) {} + + void init() { + #define _INIT(N) if (index == N) INIT_FAN_PIN(FAN##N##_PIN); + REPEAT(FAN_COUNT, _INIT) + #undef _INIT + } + static void init_pins(); + + static constexpr uint8_t pwmToPercent(const uint8_t spd) { return ui8_to_percent(spd); } + + #if ENABLED(FAN_SOFT_PWM) + uint8_t soft_pwm_amount, soft_pwm_count; + void soft_pwm_on() { + #define _CASE(N) if (index == N) _SOFT_PWM(N); + REPEAT(FAN_COUNT, _CASE) + #undef _CASE + } + void slow_soft_pwm() { + #define _CASE(N) if (index == N) _SLOW_PWM(N); + REPEAT(FAN_COUNT, _CASE) + #undef _CASE + } + void soft_pwm_off(const uint8_t count) { + #define _CASE(N) if (index == N) if (soft_pwm_count <= count) write(LOW); + REPEAT(FAN_COUNT, _CASE) + #undef _CASE + } + #endif + + uint8_t speed; + uint8_t speed_pct() { return pwmToPercent(speed); } + + #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE) + uint8_t pause_speed; + static bool paused; + static void all_pause(const bool p); + static void power_off() { paused = false; FANS_LOOP(i) fans[i].pause_speed = 0; } + void pause(const bool p); + #endif + + #if ENABLED(AUTO_POWER_FANS) + static bool is_power_needed() { + FANS_LOOP(f) if (fans[f].speed) return true; + return false; + } + #endif + + #if FAN_KICKSTART_TIME + millis_t kick_end_ms; + void kickstart(const millis_t &ms) { + if (speed) { + if (kick_end_ms == 0) { + kick_end_ms = ms + FAN_KICKSTART_TIME; + speed = 255; + } + else if (PENDING(ms, kick_end_ms)) + speed = 255; + } + else + kick_end_ms = 0; + } + #else + void kickstart(const millis_t&) {} + #endif + + #if ENABLED(EXTRA_FAN_SPEED) + struct { uint8_t saved, speed; } extra; + void set_temp_speed(const uint16_t command_or_speed); + #endif + + #if DISABLED(LASER_SYNCHRONOUS_M106_M107) + #define HAS_TAIL_FAN_SPEED 1 + uint8_t tail_speed = 13; + #endif + + #if ENABLED(ADAPTIVE_FAN_SLOWING) + uint8_t speed_scaler = 128; + uint8_t scaled_speed(const uint8_t fs) const { return (fs * uint16_t(speed_scaler)) >> 7; } + #else + static constexpr uint8_t scaled_speed(const uint8_t fs) { return fs; } + #endif + uint8_t scaled_speed() const { return scaled_speed(speed); } + uint8_t speed_pct_scaled() const { return ui8_to_percent(scaled_speed()); } + + uint8_t pwm() { + #if FAN_MIN_PWM != 0 || FAN_MAX_PWM != 255 + return speed ? map(speed, 1, 255, FAN_MIN_PWM, FAN_MAX_PWM) : FAN_OFF_PWM; + #else + return speed ?: FAN_OFF_PWM; + #endif + } + + void sync(const millis_t &ms, const pin_t pin) { + kickstart(ms); + #if ENABLED(FAN_SOFT_PWM) + UNUSED(pin); + soft_pwm_amount = pwm(); + #else + hal.set_pwm_duty(pin, pwm()); + #endif + } + + static void sync_speeds() { + const millis_t ms = millis(); + #define FAN_SET(F) fans[F].sync(ms, pin_t(FAN##F##_PIN)); + REPEAT(FAN_COUNT, FAN_SET) + #undef FAN_SET + } + static void sync_speeds(uint8_t (&fanspeed)[FAN_COUNT]) { + FANS_LOOP(f) fans[f].speed = fanspeed[f]; + sync_speeds(); + } + + static void write(const uint8_t fi, const uint8_t state) { + #define _CASE(N) if (fi == N) WRITE(FAN ##N## _PIN, (state) ^ FAN_INVERTING); + REPEAT(FAN_COUNT, _CASE) + #undef _CASE + } + void write(const uint8_t state) { write(index, state); } + }; + +#endif // HAS_FAN diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp index e11a760902364..6cf82849cd238 100644 --- a/Marlin/src/module/planner.cpp +++ b/Marlin/src/module/planner.cpp @@ -1244,57 +1244,6 @@ void Planner::recalculate() { recalculate_trapezoids(); } -/** - * Apply fan speeds - */ -#if HAS_FAN - - void Planner::sync_fan_speeds(uint8_t (&fan_speed)[FAN_COUNT]) { - - #if FAN_MIN_PWM != 0 || FAN_MAX_PWM != 255 - #define CALC_FAN_SPEED(f) (fan_speed[f] ? map(fan_speed[f], 1, 255, FAN_MIN_PWM, FAN_MAX_PWM) : FAN_OFF_PWM) - #else - #define CALC_FAN_SPEED(f) (fan_speed[f] ?: FAN_OFF_PWM) - #endif - - #if ENABLED(FAN_SOFT_PWM) - #define _FAN_SET(F) thermalManager.soft_pwm_amount_fan[F] = CALC_FAN_SPEED(F); - #else - #define _FAN_SET(F) hal.set_pwm_duty(pin_t(FAN##F##_PIN), CALC_FAN_SPEED(F)); - #endif - #define FAN_SET(F) do{ kickstart_fan(fan_speed, ms, F); _FAN_SET(F); }while(0) - - const millis_t ms = millis(); - TERN_(HAS_FAN0, FAN_SET(0)); - TERN_(HAS_FAN1, FAN_SET(1)); - TERN_(HAS_FAN2, FAN_SET(2)); - TERN_(HAS_FAN3, FAN_SET(3)); - TERN_(HAS_FAN4, FAN_SET(4)); - TERN_(HAS_FAN5, FAN_SET(5)); - TERN_(HAS_FAN6, FAN_SET(6)); - TERN_(HAS_FAN7, FAN_SET(7)); - } - - #if FAN_KICKSTART_TIME - - void Planner::kickstart_fan(uint8_t (&fan_speed)[FAN_COUNT], const millis_t &ms, const uint8_t f) { - static millis_t fan_kick_end[FAN_COUNT] = { 0 }; - if (fan_speed[f]) { - if (fan_kick_end[f] == 0) { - fan_kick_end[f] = ms + FAN_KICKSTART_TIME; - fan_speed[f] = 255; - } - else if (PENDING(ms, fan_kick_end[f])) - fan_speed[f] = 255; - } - else - fan_kick_end[f] = 0; - } - - #endif - -#endif // HAS_FAN - /** * Maintain fans, paste extruder pressure, */ @@ -1304,9 +1253,7 @@ void Planner::check_axes_activity() { xyze_bool_t axis_active = { false }; #endif - #if HAS_FAN && DISABLED(LASER_SYNCHRONOUS_M106_M107) - #define HAS_TAIL_FAN_SPEED 1 - static uint8_t tail_fan_speed[FAN_COUNT] = ARRAY_N_1(FAN_COUNT, 13); + #if HAS_TAIL_FAN_SPEED bool fans_need_update = false; #endif @@ -1327,10 +1274,11 @@ void Planner::check_axes_activity() { #if HAS_TAIL_FAN_SPEED FANS_LOOP(i) { - const uint8_t spd = thermalManager.scaledFanSpeed(i, block->fan_speed[i]); - if (tail_fan_speed[i] != spd) { + Fan &fan = fans[i]; + const uint8_t spd = fan.scaled_speed(block->fan_speed[i]); + if (fan.tail_speed != spd) { + fan.tail_speed = spd; fans_need_update = true; - tail_fan_speed[i] = spd; } } #endif @@ -1364,10 +1312,11 @@ void Planner::check_axes_activity() { #if HAS_TAIL_FAN_SPEED FANS_LOOP(i) { - const uint8_t spd = thermalManager.scaledFanSpeed(i); - if (tail_fan_speed[i] != spd) { + Fan &fan = fans[i]; + const uint8_t spd = fan.scaled_speed(); + if (fan.tail_speed != spd) { fans_need_update = true; - tail_fan_speed[i] = spd; + fan.tail_speed = spd; } } #endif @@ -1398,7 +1347,7 @@ void Planner::check_axes_activity() { // Update Fan speeds // Only if synchronous M106/M107 is disabled // - TERN_(HAS_TAIL_FAN_SPEED, if (fans_need_update) sync_fan_speeds(tail_fan_speed)); + TERN_(HAS_TAIL_FAN_SPEED, if (fans_need_update) Fan::sync_speeds()); TERN_(AUTOTEMP, autotemp_task()); @@ -2194,7 +2143,7 @@ bool Planner::_populate_block(block_t * const block, bool split_move, TERN_(HAS_CUTTER, block->cutter_power = cutter.power); #if HAS_FAN - FANS_LOOP(i) block->fan_speed[i] = thermalManager.fan_speed[i]; + FANS_LOOP(i) block->fan_speed[i] = fans[i].speed; #endif #if ENABLED(BARICUDA) @@ -2206,15 +2155,9 @@ bool Planner::_populate_block(block_t * const block, bool split_move, #if ENABLED(AUTO_POWER_CONTROL) if (NUM_AXIS_GANG( - block->steps.x, - || block->steps.y, - || block->steps.z, - || block->steps.i, - || block->steps.j, - || block->steps.k, - || block->steps.u, - || block->steps.v, - || block->steps.w + block->steps.x, || block->steps.y, || block->steps.z, + || block->steps.i, || block->steps.j, || block->steps.k, + || block->steps.u, || block->steps.v, || block->steps.w )) powerManager.power_on(); #endif @@ -2932,7 +2875,7 @@ void Planner::buffer_sync_block(TERN_(LASER_SYNCHRONOUS_M106_M107, uint8_t sync_ #endif #if BOTH(HAS_FAN, LASER_SYNCHRONOUS_M106_M107) - FANS_LOOP(i) block->fan_speed[i] = thermalManager.fan_speed[i]; + FANS_LOOP(i) block->fan_speed[i] = fans[i].speed; #endif // If this is the first added movement, reload the delay, otherwise, cancel it. @@ -3152,7 +3095,7 @@ bool Planner::buffer_line(const xyze_pos_t &cart, const_feedRate_t fr_mm_s, cons block->flag = BLOCK_FLAG_IS_PAGE; #if HAS_FAN - FANS_LOOP(i) block->fan_speed[i] = thermalManager.fan_speed[i]; + FANS_LOOP(i) block->fan_speed[i] = fans[i].speed; #endif E_TERN_(block->extruder = extruder); diff --git a/Marlin/src/module/planner.h b/Marlin/src/module/planner.h index dbc8592179ca4..5a5ecd899c9f2 100644 --- a/Marlin/src/module/planner.h +++ b/Marlin/src/module/planner.h @@ -46,6 +46,10 @@ #include "motion.h" #include "../gcode/queue.h" +#if HAS_FAN + #include "temperature.h" +#endif + #if ENABLED(DELTA) #include "delta.h" #elif ENABLED(POLARGRAPH) @@ -232,7 +236,7 @@ typedef struct block_t { #endif #if HAS_FAN - uint8_t fan_speed[FAN_COUNT]; + uint8_t fan_speed[FAN_COUNT]; // Speeds of all fans #endif #if ENABLED(BARICUDA) @@ -528,16 +532,6 @@ class Planner { // Manage fans, paste pressure, etc. static void check_axes_activity(); - // Apply fan speeds - #if HAS_FAN - static void sync_fan_speeds(uint8_t (&fan_speed)[FAN_COUNT]); - #if FAN_KICKSTART_TIME - static void kickstart_fan(uint8_t (&fan_speed)[FAN_COUNT], const millis_t &ms, const uint8_t f); - #else - FORCE_INLINE static void kickstart_fan(uint8_t (&)[FAN_COUNT], const millis_t &, const uint8_t) {} - #endif - #endif - #if ENABLED(FILAMENT_WIDTH_SENSOR) void apply_filament_width_sensor(const int8_t encoded_ratio); diff --git a/Marlin/src/module/probe.cpp b/Marlin/src/module/probe.cpp index dc97971a25b15..0d11a9c93ad94 100644 --- a/Marlin/src/module/probe.cpp +++ b/Marlin/src/module/probe.cpp @@ -269,7 +269,7 @@ xyz_pos_t Probe::offset; // Initialized by settings.load() void Probe::set_probing_paused(const bool dopause) { TERN_(PROBING_HEATERS_OFF, thermalManager.pause_heaters(dopause)); - TERN_(PROBING_FANS_OFF, thermalManager.set_fans_paused(dopause)); + TERN_(PROBING_FANS_OFF, Fan::all_pause(dopause)); TERN_(PROBING_ESTEPPERS_OFF, if (dopause) stepper.disable_e_steppers()); #if ENABLED(PROBING_STEPPERS_OFF) && DISABLED(DELTA) static uint8_t old_trusted; diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index d098179fc183a..ddae3f25262dc 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -2173,7 +2173,7 @@ uint32_t Stepper::block_phase_isr() { #if ENABLED(LASER_SYNCHRONOUS_M106_M107) const bool is_sync_fans = TEST(current_block->flag, BLOCK_BIT_SYNC_FANS); - if (is_sync_fans) planner.sync_fan_speeds(current_block->fan_speed); + if (is_sync_fans) Fan::sync_speeds(current_block->fan_speed); #else constexpr bool is_sync_fans = false; #endif diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index f3ce52e97d785..2c35d7d696870 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -316,69 +316,8 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED); uint8_t Temperature::coolerfan_speed; // = 0 #endif -#if BOTH(FAN_SOFT_PWM, USE_CONTROLLER_FAN) - uint8_t Temperature::soft_pwm_controller_speed; -#endif - -// Init fans according to whether they're native PWM or Software PWM -#ifdef BOARD_OPENDRAIN_MOSFETS - #define _INIT_SOFT_FAN(P) OUT_WRITE_OD(P, FAN_INVERTING ? LOW : HIGH) -#else - #define _INIT_SOFT_FAN(P) OUT_WRITE(P, FAN_INVERTING ? LOW : HIGH) -#endif -#if ENABLED(FAN_SOFT_PWM) - #define _INIT_FAN_PIN(P) _INIT_SOFT_FAN(P) -#else - #define _INIT_FAN_PIN(P) do{ if (PWM_PIN(P)) SET_PWM(P); else _INIT_SOFT_FAN(P); }while(0) -#endif -#if ENABLED(FAST_PWM_FAN) - #define SET_FAST_PWM_FREQ(P) hal.set_pwm_frequency(pin_t(P), FAST_PWM_FAN_FREQUENCY) -#else - #define SET_FAST_PWM_FREQ(P) NOOP -#endif -#define INIT_FAN_PIN(P) do{ _INIT_FAN_PIN(P); SET_FAST_PWM_FREQ(P); }while(0) - // HAS_FAN does not include CONTROLLER_FAN #if HAS_FAN - - uint8_t Temperature::fan_speed[FAN_COUNT]; // = { 0 } - - #if ENABLED(EXTRA_FAN_SPEED) - - Temperature::extra_fan_t Temperature::extra_fan_speed[FAN_COUNT]; - - /** - * Handle the M106 P T command: - * T1 = Restore fan speed saved on the last T2 - * T2 = Save the fan speed, then set to the last T<3-255> value - * T<3-255> = Set the "extra fan speed" - */ - void Temperature::set_temp_fan_speed(const uint8_t fan, const uint16_t command_or_speed) { - switch (command_or_speed) { - case 1: - set_fan_speed(fan, extra_fan_speed[fan].saved); - break; - case 2: - extra_fan_speed[fan].saved = fan_speed[fan]; - set_fan_speed(fan, extra_fan_speed[fan].speed); - break; - default: - extra_fan_speed[fan].speed = _MIN(command_or_speed, 255U); - break; - } - } - - #endif - - #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE) - bool Temperature::fans_paused; // = false; - uint8_t Temperature::saved_fan_speed[FAN_COUNT]; // = { 0 } - #endif - - #if ENABLED(ADAPTIVE_FAN_SLOWING) - uint8_t Temperature::fan_speed_scaler[FAN_COUNT] = ARRAY_N_1(FAN_COUNT, 128); - #endif - /** * Set the print fan speed for a target extruder */ @@ -397,37 +336,38 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED); if (fan >= FAN_COUNT) return; - fan_speed[fan] = speed; + fans[fan].speed = speed; #if REDUNDANT_PART_COOLING_FAN - if (fan == 0) fan_speed[REDUNDANT_PART_COOLING_FAN] = speed; + if (fan == 0) fans[REDUNDANT_PART_COOLING_FAN].speed = speed; #endif TERN_(REPORT_FAN_CHANGE, report_fan_speed(fan)); } + #if ENABLED(EXTRA_FAN_SPEED) + /** + * Handle the M106 P T command: + * T1 = Restore fan speed saved on the last T2 + * T2 = Save the fan speed, then set to the last T<3-255> value + * T<3-255> = Set the "extra fan speed" + */ + void Temperature::set_temp_fan_speed(const uint8_t fan, const uint16_t command_or_speed) { + fans[fan].set_temp_speed(command_or_speed); + switch (command_or_speed) { + case 1 ... 2: set_fan_speed(fan, fans[fan].extra.speed); + default: break; + } + } + #endif + #if ENABLED(REPORT_FAN_CHANGE) /** * Report print fan speed for a target extruder */ void Temperature::report_fan_speed(const uint8_t fan) { if (fan >= FAN_COUNT) return; - PORT_REDIRECT(SerialMask::All); - SERIAL_ECHOLNPGM("M106 P", fan, " S", fan_speed[fan]); - } - #endif - - #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE) - - void Temperature::set_fans_paused(const bool p) { - if (p != fans_paused) { - fans_paused = p; - if (p) - FANS_LOOP(i) { saved_fan_speed[i] = fan_speed[i]; fan_speed[i] = 0; } - else - FANS_LOOP(i) fan_speed[i] = saved_fan_speed[i]; - } + fans[fan].report_speed(fan); } - #endif #endif // HAS_FAN @@ -560,11 +500,6 @@ volatile bool Temperature::raw_temps_ready = false; millis_t Temperature::fan_update_ms = 0; #endif -#if ENABLED(FAN_SOFT_PWM) - uint8_t Temperature::soft_pwm_amount_fan[FAN_COUNT], - Temperature::soft_pwm_count_fan[FAN_COUNT]; -#endif - #if ENABLED(SINGLENOZZLE_STANDBY_TEMP) celsius_t Temperature::singlenozzle_temp[EXTRUDERS]; #endif @@ -607,34 +542,18 @@ volatile bool Temperature::raw_temps_ready = false; const bool isbed = (heater_id == H_BED), ischamber = (heater_id == H_CHAMBER); - #if ENABLED(PIDTEMPCHAMBER) - #define C_TERN(T,A,B) ((T) ? (A) : (B)) - #else - #define C_TERN(T,A,B) (B) - #endif - #if ENABLED(PIDTEMPBED) - #define B_TERN(T,A,B) ((T) ? (A) : (B)) - #else - #define B_TERN(T,A,B) (B) - #endif - #define GHV(C,B,H) C_TERN(ischamber, C, B_TERN(isbed, B, H)) - #define SHV(V) C_TERN(ischamber, temp_chamber.soft_pwm_amount = V, B_TERN(isbed, temp_bed.soft_pwm_amount = V, temp_hotend[heater_id].soft_pwm_amount = V)) - #define ONHEATINGSTART() C_TERN(ischamber, printerEventLEDs.onChamberHeatingStart(), B_TERN(isbed, printerEventLEDs.onBedHeatingStart(), printerEventLEDs.onHotendHeatingStart())) - #define ONHEATING(S,C,T) C_TERN(ischamber, printerEventLEDs.onChamberHeating(S,C,T), B_TERN(isbed, printerEventLEDs.onBedHeating(S,C,T), printerEventLEDs.onHotendHeating(S,C,T))) + #define C_CBH(T,A,B) TERN(PIDTEMPCHAMBER, ((T) ? (A) : (B)), (B)) + #define B_CBH(T,A,B) TERN(PIDTEMPBED, ((T) ? (A) : (B)), (B)) + #define CBH(C,B,H) C_CBH(ischamber, C, B_CBH(isbed, B, H)) + #define SHV(V) C_CBH(ischamber, temp_chamber.soft_pwm_amount = V, B_CBH(isbed, temp_bed.soft_pwm_amount = V, temp_hotend[heater_id].soft_pwm_amount = V)) + #define ONHEATINGSTART() C_CBH(ischamber, printerEventLEDs.onChamberHeatingStart(), B_CBH(isbed, printerEventLEDs.onBedHeatingStart(), printerEventLEDs.onHotendHeatingStart())) + #define ONHEATING(S,C,T) C_CBH(ischamber, printerEventLEDs.onChamberHeating(S,C,T), B_CBH(isbed, printerEventLEDs.onBedHeating(S,C,T), printerEventLEDs.onHotendHeating(S,C,T))) #define WATCH_PID DISABLED(NO_WATCH_PID_TUNING) && (BOTH(WATCH_CHAMBER, PIDTEMPCHAMBER) || BOTH(WATCH_BED, PIDTEMPBED) || BOTH(WATCH_HOTENDS, PIDTEMP)) #if WATCH_PID - #if BOTH(THERMAL_PROTECTION_CHAMBER, PIDTEMPCHAMBER) - #define C_GTV(T,A,B) ((T) ? (A) : (B)) - #else - #define C_GTV(T,A,B) (B) - #endif - #if BOTH(THERMAL_PROTECTION_BED, PIDTEMPBED) - #define B_GTV(T,A,B) ((T) ? (A) : (B)) - #else - #define B_GTV(T,A,B) (B) - #endif + #define C_GTV(T,A,B) TERN(THERMAL_PROTECTION_CHAMBER, C_CBH(T,A,B), (B)) + #define B_GTV(T,A,B) TERN(THERMAL_PROTECTION_BED, B_CBH(T,A,B), (B)) #define GTV(C,B,H) C_GTV(ischamber, C, B_GTV(isbed, B, H)) const uint16_t watch_temp_period = GTV(WATCH_CHAMBER_TEMP_PERIOD, WATCH_BED_TEMP_PERIOD, WATCH_TEMP_PERIOD); const uint8_t watch_temp_increase = GTV(WATCH_CHAMBER_TEMP_INCREASE, WATCH_BED_TEMP_INCREASE, WATCH_TEMP_INCREASE); @@ -649,7 +568,7 @@ volatile bool Temperature::raw_temps_ready = false; TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_STARTED)); TERN_(DWIN_LCD_PROUI, DWIN_PidTuning(isbed ? PID_BED_START : PID_EXTR_START)); - if (target > GHV(CHAMBER_MAX_TARGET, BED_MAX_TARGET, temp_range[heater_id].maxtemp - (HOTEND_OVERSHOOT))) { + if (target > CBH(CHAMBER_MAX_TARGET, BED_MAX_TARGET, temp_range[heater_id].maxtemp - (HOTEND_OVERSHOOT))) { SERIAL_ECHOPGM(STR_PID_AUTOTUNE); SERIAL_ECHOLNPGM(STR_PID_TEMP_TOO_HIGH); TERN_(EXTENSIBLE_UI, ExtUI::onPidTuning(ExtUI::result_t::PID_TEMP_TOO_HIGH)); @@ -664,11 +583,11 @@ volatile bool Temperature::raw_temps_ready = false; disable_all_heaters(); TERN_(AUTO_POWER_CONTROL, powerManager.power_on()); - long bias = GHV(MAX_CHAMBER_POWER, MAX_BED_POWER, PID_MAX) >> 1, d = bias; + long bias = CBH(MAX_CHAMBER_POWER, MAX_BED_POWER, PID_MAX) >> 1, d = bias; SHV(bias); #if ENABLED(PRINTER_EVENT_LEDS) - const celsius_float_t start_temp = GHV(degChamber(), degBed(), degHotend(heater_id)); + const celsius_float_t start_temp = CBH(degChamber(), degBed(), degHotend(heater_id)); LEDColor color = ONHEATINGSTART(); #endif @@ -685,7 +604,7 @@ volatile bool Temperature::raw_temps_ready = false; if (updateTemperaturesIfReady()) { // temp sample ready // Get the current temperature and constrain it - current_temp = GHV(degChamber(), degBed(), degHotend(heater_id)); + current_temp = CBH(degChamber(), degBed(), degHotend(heater_id)); NOLESS(maxT, current_temp); NOMORE(minT, current_temp); @@ -708,7 +627,7 @@ volatile bool Temperature::raw_temps_ready = false; t2 = ms; t_low = t2 - t1; if (cycles > 0) { - const long max_pow = GHV(MAX_CHAMBER_POWER, MAX_BED_POWER, PID_MAX); + const long max_pow = CBH(MAX_CHAMBER_POWER, MAX_BED_POWER, PID_MAX); bias += (d * (t_high - t_low)) / (t_low + t_high); LIMIT(bias, 20, max_pow - 20); d = (bias > max_pow >> 1) ? max_pow - 1 - bias : bias; @@ -798,7 +717,7 @@ volatile bool Temperature::raw_temps_ready = false; TERN_(HOST_PROMPT_SUPPORT, hostui.notify(GET_TEXT_F(MSG_PID_AUTOTUNE_DONE))); #if EITHER(PIDTEMPBED, PIDTEMPCHAMBER) - FSTR_P const estring = GHV(F("chamber"), F("bed"), FPSTR(NUL_STR)); + FSTR_P const estring = CBH(F("chamber"), F("bed"), FPSTR(NUL_STR)); say_default_(); SERIAL_ECHOF(estring); SERIAL_ECHOLNPGM("Kp ", tune_pid.Kp); say_default_(); SERIAL_ECHOF(estring); SERIAL_ECHOLNPGM("Ki ", tune_pid.Ki); say_default_(); SERIAL_ECHOF(estring); SERIAL_ECHOLNPGM("Kd ", tune_pid.Kd); @@ -837,7 +756,7 @@ volatile bool Temperature::raw_temps_ready = false; // Use the result? (As with "M303 U1") if (set_result) - GHV(_set_chamber_pid(tune_pid), _set_bed_pid(tune_pid), _set_hotend_pid(heater_id, tune_pid)); + CBH(_set_chamber_pid(tune_pid), _set_bed_pid(tune_pid), _set_hotend_pid(heater_id, tune_pid)); TERN_(PRINTER_EVENT_LEDS, printerEventLEDs.onPidTuningDone(color)); @@ -909,7 +828,7 @@ volatile bool Temperature::raw_temps_ready = false; temp_hotend[active_extruder].soft_pwm_amount = 0; #if HAS_FAN set_fan_speed(ANY(MPC_FAN_0_ALL_HOTENDS, MPC_FAN_0_ACTIVE_HOTEND) ? 0 : active_extruder, 0); - planner.sync_fan_speeds(fan_speed); + Fan::sync_speeds(); #endif do_z_clearance(MPC_TUNING_END_Z); @@ -926,7 +845,7 @@ volatile bool Temperature::raw_temps_ready = false; #if HAS_FAN zero_fan_speeds(); set_fan_speed(ANY(MPC_FAN_0_ALL_HOTENDS, MPC_FAN_0_ACTIVE_HOTEND) ? 0 : active_extruder, 255); - planner.sync_fan_speeds(fan_speed); + Fan::sync_speeds(); #endif gcode.home_all_axes(true); const xyz_pos_t tuningpos = MPC_TUNING_POS; @@ -954,7 +873,7 @@ volatile bool Temperature::raw_temps_ready = false; #if HAS_FAN set_fan_speed(ANY(MPC_FAN_0_ALL_HOTENDS, MPC_FAN_0_ACTIVE_HOTEND) ? 0 : active_extruder, 0); - planner.sync_fan_speeds(fan_speed); + Fan::sync_speeds(); #endif hotend.modeled_ambient_temp = ambient_temp; @@ -1036,7 +955,7 @@ volatile bool Temperature::raw_temps_ready = false; #if HAS_FAN else if (ELAPSED(ms, test_end_ms) && !fan0_done) { set_fan_speed(ANY(MPC_FAN_0_ALL_HOTENDS, MPC_FAN_0_ACTIVE_HOTEND) ? 0 : active_extruder, 255); - planner.sync_fan_speeds(fan_speed); + Fan::sync_speeds(); settle_end_ms = ms + settle_time; test_end_ms = settle_end_ms + test_duration; fan0_done = true; @@ -1387,8 +1306,8 @@ void Temperature::min_temp_error(const heater_id_t heater_id) { } #endif // PID_EXTRUSION_SCALING #if ENABLED(PID_FAN_SCALING) - if (fan_speed[active_extruder] > PID_FAN_SCALING_MIN_SPEED) { - work_pid[ee].Kf = PID_PARAM(Kf, ee) + (PID_FAN_SCALING_LIN_FACTOR) * fan_speed[active_extruder]; + if (fans[active_extruder].speed > PID_FAN_SCALING_MIN_SPEED) { + work_pid[ee].Kf = PID_PARAM(Kf, ee) + (PID_FAN_SCALING_LIN_FACTOR) * fans[active_extruder].speed; pid_output += work_pid[ee].Kf; } //pid_output -= work_pid[ee].Ki; @@ -1438,7 +1357,7 @@ void Temperature::min_temp_error(const heater_id_t heater_id) { float ambient_xfer_coeff = constants.ambient_xfer_coeff_fan0; #if ENABLED(MPC_INCLUDE_FAN) const uint8_t fan_index = ANY(MPC_FAN_0_ACTIVE_HOTEND, MPC_FAN_0_ALL_HOTENDS) ? 0 : ee; - const float fan_fraction = TERN_(MPC_FAN_0_ACTIVE_HOTEND, !this_hotend ? 0.0f : ) fan_speed[fan_index] * RECIPROCAL(255); + const float fan_fraction = TERN_(MPC_FAN_0_ACTIVE_HOTEND, !this_hotend ? 0.0f : ) fans[fan_index].speed * RECIPROCAL(255); ambient_xfer_coeff += fan_fraction * constants.fan255_adjustment; #endif @@ -2622,30 +2541,8 @@ void Temperature::init() { OUT_WRITE(COOLER_PIN, COOLER_INVERTING); #endif - #if HAS_FAN0 - INIT_FAN_PIN(FAN_PIN); - #endif - #if HAS_FAN1 - INIT_FAN_PIN(FAN1_PIN); - #endif - #if HAS_FAN2 - INIT_FAN_PIN(FAN2_PIN); - #endif - #if HAS_FAN3 - INIT_FAN_PIN(FAN3_PIN); - #endif - #if HAS_FAN4 - INIT_FAN_PIN(FAN4_PIN); - #endif - #if HAS_FAN5 - INIT_FAN_PIN(FAN5_PIN); - #endif - #if HAS_FAN6 - INIT_FAN_PIN(FAN6_PIN); - #endif - #if HAS_FAN7 - INIT_FAN_PIN(FAN7_PIN); - #endif + TERN_(HAS_FAN, Fan::init_pins()); + #if ENABLED(USE_CONTROLLER_FAN) INIT_FAN_PIN(CONTROLLER_FAN_PIN); #endif @@ -2899,16 +2796,16 @@ void Temperature::init() { #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; + if (fans[fan_index].speed == 0 || current >= running_temp - (hysteresis_degc * 0.25f)) + fans[fan_index].speed_scaler = 128; else if (current >= running_temp - (hysteresis_degc * 0.3335f)) - fan_speed_scaler[fan_index] = 96; + fans[fan_index].speed_scaler = 96; else if (current >= running_temp - (hysteresis_degc * 0.5f)) - fan_speed_scaler[fan_index] = 64; + fans[fan_index].speed_scaler = 64; else if (current >= running_temp - (hysteresis_degc * 0.8f)) - fan_speed_scaler[fan_index] = 32; + fans[fan_index].speed_scaler = 32; else - fan_speed_scaler[fan_index] = 0; + fans[fan_index].speed_scaler = 0; } #endif @@ -3036,8 +2933,8 @@ void Temperature::disable_all_heaters() { void Temperature::singlenozzle_change(const uint8_t old_tool, const uint8_t new_tool) { #if ENABLED(SINGLENOZZLE_STANDBY_FAN) - singlenozzle_fan_speed[old_tool] = fan_speed[0]; - fan_speed[0] = singlenozzle_fan_speed[new_tool]; + singlenozzle_fan_speed[old_tool] = fans[0].speed; + fans[0].speed = singlenozzle_fan_speed[new_tool]; #endif #if ENABLED(SINGLENOZZLE_STANDBY_TEMP) singlenozzle_temp[old_tool] = temp_hotend[0].target; @@ -3377,8 +3274,6 @@ void Temperature::isr() { static SoftPWM soft_pwm_controller; #endif - #define WRITE_FAN(n, v) WRITE(FAN##n##_PIN, (v) ^ FAN_INVERTING) - #if DISABLED(SLOW_PWM_HEATERS) #if ANY(HAS_HOTEND, HAS_HEATED_BED, HAS_HEATED_CHAMBER, HAS_COOLER, FAN_SOFT_PWM) @@ -3396,7 +3291,7 @@ void Temperature::isr() { pwm_count_tmp -= 127; #if HAS_HOTEND - #define _PWM_MOD_E(N) _PWM_MOD(N,soft_pwm_hotend[N],temp_hotend[N]); + #define _PWM_MOD_E(N) _PWM_MOD(N, soft_pwm_hotend[N], temp_hotend[N]); REPEAT(HOTENDS, _PWM_MOD_E); #endif @@ -3412,40 +3307,13 @@ void Temperature::isr() { _PWM_MOD(COOLER, soft_pwm_cooler, temp_cooler); #endif - #if BOTH(USE_CONTROLLER_FAN, FAN_SOFT_PWM) - WRITE(CONTROLLER_FAN_PIN, soft_pwm_controller.add(pwm_mask, soft_pwm_controller_speed)); - #endif - #if ENABLED(FAN_SOFT_PWM) - #define _FAN_PWM(N) do{ \ - uint8_t &spcf = soft_pwm_count_fan[N]; \ - spcf = (spcf & pwm_mask) + (soft_pwm_amount_fan[N] >> 1); \ - WRITE_FAN(N, spcf > pwm_mask ? HIGH : LOW); \ - }while(0) - #if HAS_FAN0 - _FAN_PWM(0); - #endif - #if HAS_FAN1 - _FAN_PWM(1); - #endif - #if HAS_FAN2 - _FAN_PWM(2); - #endif - #if HAS_FAN3 - _FAN_PWM(3); - #endif - #if HAS_FAN4 - _FAN_PWM(4); - #endif - #if HAS_FAN5 - _FAN_PWM(5); - #endif - #if HAS_FAN6 - _FAN_PWM(6); - #endif - #if HAS_FAN7 - _FAN_PWM(7); + #if ENABLED(USE_CONTROLLER_FAN) + WRITE(CONTROLLER_FAN_PIN, soft_pwm_controller.add(pwm_mask, controllerFan.soft_pwm_speed)); #endif + + #define _SOFT_PWM_ON(N) fans[N].soft_pwm_on(); + REPEAT(FAN_COUNT, _SOFT_PWM_ON) #endif } else { @@ -3468,30 +3336,8 @@ void Temperature::isr() { #endif #if ENABLED(FAN_SOFT_PWM) - #if HAS_FAN0 - if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0, LOW); - #endif - #if HAS_FAN1 - if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN(1, LOW); - #endif - #if HAS_FAN2 - if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW); - #endif - #if HAS_FAN3 - if (soft_pwm_count_fan[3] <= pwm_count_tmp) WRITE_FAN(3, LOW); - #endif - #if HAS_FAN4 - if (soft_pwm_count_fan[4] <= pwm_count_tmp) WRITE_FAN(4, LOW); - #endif - #if HAS_FAN5 - if (soft_pwm_count_fan[5] <= pwm_count_tmp) WRITE_FAN(5, LOW); - #endif - #if HAS_FAN6 - if (soft_pwm_count_fan[6] <= pwm_count_tmp) WRITE_FAN(6, LOW); - #endif - #if HAS_FAN7 - if (soft_pwm_count_fan[7] <= pwm_count_tmp) WRITE_FAN(7, LOW); - #endif + #define _SOFT_PWM_2(N) fans[N].soft_pwm_off(pwm_count_tmp); + REPEAT(FAN_COUNT, _SOFT_PWM_2) #if ENABLED(USE_CONTROLLER_FAN) if (soft_pwm_controller.count <= pwm_count_tmp) WRITE(CONTROLLER_FAN_PIN, LOW); #endif @@ -3562,60 +3408,10 @@ void Temperature::isr() { #if ENABLED(FAN_SOFT_PWM) if (pwm_count_tmp >= 127) { pwm_count_tmp = 0; - #define _PWM_FAN(N) do{ \ - soft_pwm_count_fan[N] = soft_pwm_amount_fan[N] >> 1; \ - WRITE_FAN(N, soft_pwm_count_fan[N] > 0 ? HIGH : LOW); \ - }while(0) - #if HAS_FAN0 - _PWM_FAN(0); - #endif - #if HAS_FAN1 - _PWM_FAN(1); - #endif - #if HAS_FAN2 - _PWM_FAN(2); - #endif - #if HAS_FAN3 - _FAN_PWM(3); - #endif - #if HAS_FAN4 - _FAN_PWM(4); - #endif - #if HAS_FAN5 - _FAN_PWM(5); - #endif - #if HAS_FAN6 - _FAN_PWM(6); - #endif - #if HAS_FAN7 - _FAN_PWM(7); - #endif + FANS_LOOP(f) fans[f].slow_soft_pwm(); } - #if HAS_FAN0 - if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0, LOW); - #endif - #if HAS_FAN1 - if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN(1, LOW); - #endif - #if HAS_FAN2 - if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW); - #endif - #if HAS_FAN3 - if (soft_pwm_count_fan[3] <= pwm_count_tmp) WRITE_FAN(3, LOW); - #endif - #if HAS_FAN4 - if (soft_pwm_count_fan[4] <= pwm_count_tmp) WRITE_FAN(4, LOW); - #endif - #if HAS_FAN5 - if (soft_pwm_count_fan[5] <= pwm_count_tmp) WRITE_FAN(5, LOW); - #endif - #if HAS_FAN6 - if (soft_pwm_count_fan[6] <= pwm_count_tmp) WRITE_FAN(6, LOW); - #endif - #if HAS_FAN7 - if (soft_pwm_count_fan[7] <= pwm_count_tmp) WRITE_FAN(7, LOW); - #endif - #endif // FAN_SOFT_PWM + FANS_LOOP(f) fans[f].soft_pwm_off(pwm_count_tmp); + #endif // SOFT_PWM_SCALE to frequency: // diff --git a/Marlin/src/module/temperature.h b/Marlin/src/module/temperature.h index da9d4a071b9f9..da518b2afe554 100644 --- a/Marlin/src/module/temperature.h +++ b/Marlin/src/module/temperature.h @@ -25,10 +25,11 @@ * temperature.h - temperature controller */ -#include "thermistor/thermistors.h" - #include "../inc/MarlinConfig.h" +#include "thermistor/thermistors.h" +#include "fans.h" + #if ENABLED(AUTO_POWER_CONTROL) #include "../feature/power.h" #endif @@ -421,15 +422,6 @@ class Temperature { static uint8_t coolerfan_speed; #endif - #if ENABLED(FAN_SOFT_PWM) - static uint8_t soft_pwm_amount_fan[FAN_COUNT], - soft_pwm_count_fan[FAN_COUNT]; - #endif - - #if BOTH(FAN_SOFT_PWM, USE_CONTROLLER_FAN) - static uint8_t soft_pwm_controller_speed; - #endif - #if BOTH(HAS_MARLINUI_MENU, PREVENT_COLD_EXTRUSION) && E_MANUAL > 0 static bool allow_cold_extrude_override; static void set_menu_cold_override(const bool allow) { allow_cold_extrude_override = allow; } @@ -658,49 +650,14 @@ class Temperature { #endif #if HAS_FAN - - static uint8_t fan_speed[FAN_COUNT]; - #define FANS_LOOP(I) LOOP_L_N(I, FAN_COUNT) - static void set_fan_speed(const uint8_t fan, const uint16_t speed); - #if ENABLED(REPORT_FAN_CHANGE) static void report_fan_speed(const uint8_t fan); #endif - - #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE) - static bool fans_paused; - static uint8_t saved_fan_speed[FAN_COUNT]; - #endif - - #if ENABLED(ADAPTIVE_FAN_SLOWING) - static uint8_t fan_speed_scaler[FAN_COUNT]; - #endif - - static uint8_t scaledFanSpeed(const uint8_t fan, const uint8_t fs) { - UNUSED(fan); // Potentially unused! - return (fs * uint16_t(TERN(ADAPTIVE_FAN_SLOWING, fan_speed_scaler[fan], 128))) >> 7; - } - - static uint8_t scaledFanSpeed(const uint8_t fan) { - return scaledFanSpeed(fan, fan_speed[fan]); - } - - static constexpr inline uint8_t pwmToPercent(const uint8_t speed) { return ui8_to_percent(speed); } - static uint8_t fanSpeedPercent(const uint8_t fan) { return ui8_to_percent(fan_speed[fan]); } - static uint8_t scaledFanSpeedPercent(const uint8_t fan) { return ui8_to_percent(scaledFanSpeed(fan)); } - #if ENABLED(EXTRA_FAN_SPEED) - typedef struct { uint8_t saved, speed; } extra_fan_t; - static extra_fan_t extra_fan_speed[FAN_COUNT]; static void set_temp_fan_speed(const uint8_t fan, const uint16_t command_or_speed); #endif - - #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE) - void set_fans_paused(const bool p); - #endif - - #endif // HAS_FAN + #endif static void zero_fan_speeds() { #if HAS_FAN diff --git a/Marlin/src/module/tool_change.cpp b/Marlin/src/module/tool_change.cpp index e2f1443048ed6..abd3591d027db 100644 --- a/Marlin/src/module/tool_change.cpp +++ b/Marlin/src/module/tool_change.cpp @@ -916,9 +916,9 @@ void fast_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0. // Cool down with fan inline void filament_swap_cooling() { #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0 - thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = toolchange_settings.fan_speed; + fans[TOOLCHANGE_FS_FAN].speed = toolchange_settings.fan_speed; gcode.dwell(SEC_TO_MS(toolchange_settings.fan_time)); - thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = 0; + fans[TOOLCHANGE_FS_FAN].speed = 0; #endif } @@ -1035,7 +1035,7 @@ void fast_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0 // Store and stop fan. Restored on any exit. - REMEMBER(fan, thermalManager.fan_speed[TOOLCHANGE_FS_FAN], 0); + REMEMBER(fan, fans[TOOLCHANGE_FS_FAN].speed, 0); #endif // Z raise @@ -1175,7 +1175,7 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) { #if BOTH(TOOLCHANGE_FILAMENT_SWAP, HAS_FAN) && TOOLCHANGE_FS_FAN >= 0 // Store and stop fan. Restored on any exit. - REMEMBER(fan, thermalManager.fan_speed[TOOLCHANGE_FS_FAN], 0); + REMEMBER(fan, fans[TOOLCHANGE_FS_FAN].speed, 0); #endif // Z raise before retraction diff --git a/buildroot/tests/mega2560 b/buildroot/tests/mega2560 index 3677abf60ef0b..5bee743408b81 100755 --- a/buildroot/tests/mega2560 +++ b/buildroot/tests/mega2560 @@ -59,7 +59,7 @@ opt_enable VIKI2 BOOT_MARLIN_LOGO_ANIMATED SDSUPPORT AUTO_REPORT_SD_STATUS \ EEPROM_SETTINGS EEPROM_CHITCHAT M114_DETAIL AUTO_REPORT_POSITION \ NO_VOLUMETRICS EXTENDED_CAPABILITIES_REPORT AUTO_REPORT_TEMPERATURES AUTOTEMP G38_PROBE_TARGET JOYSTICK \ DIRECT_STEPPING DETECT_BROKEN_ENDSTOP \ - FILAMENT_RUNOUT_SENSOR NOZZLE_PARK_FEATURE ADVANCED_PAUSE_FEATURE Z_SAFE_HOMING FIL_RUNOUT3_PULLUP + FILAMENT_RUNOUT_SENSOR NOZZLE_PARK_FEATURE ADVANCED_PAUSE_FEATURE ADVANCED_PAUSE_FANS_PAUSE Z_SAFE_HOMING FIL_RUNOUT3_PULLUP exec_test $1 $2 "Multiple runout sensors (x5) | Distinct runout states" "$3"