Skip to content

Commit

Permalink
♻️ Fan class
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Aug 20, 2023
1 parent db5ead4 commit 7ef0802
Show file tree
Hide file tree
Showing 39 changed files with 482 additions and 480 deletions.
2 changes: 1 addition & 1 deletion Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ void stop() {
print_job_timer.stop();

#if ANY(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()) {
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/feature/pause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ bool pause_print(const_float_t retract, const xyz_pos_t &park_point, const bool
planner.synchronize();

#if ALL(ADVANCED_PAUSE_FANS_PAUSE, HAS_FAN)
thermalManager.set_fans_paused(true);
Fan::all_pause(true);
#endif

// Initial retract before move to filament change position
Expand Down Expand Up @@ -712,8 +712,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 ALL(ADVANCED_PAUSE_FANS_PAUSE, HAS_FAN)
Fan::all_pause(false);
#endif

TERN_(HAS_FILAMENT_SENSOR, runout.reset());
Expand Down
5 changes: 2 additions & 3 deletions Marlin/src/feature/power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,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;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/powerloss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,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
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/gcode/control/M42.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void GcodeSuite::M42() {

#if HAS_FAN
switch (pin) {
#define _CASE(N) case FAN##N##_PIN: thermalManager.fan_speed[N] = pin_status; return;
#define _CASE(N) case FAN##N##_PIN: fans[N].speed = pin_status; return;
REPEAT(FAN_COUNT, _CASE)
}
#endif
Expand Down
3 changes: 1 addition & 2 deletions Marlin/src/gcode/control/M80_M81.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ void GcodeSuite::M81() {
print_job_timer.stop();

#if ALL(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
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/gcode/temp/M106_M107.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,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;

Expand Down
3 changes: 3 additions & 0 deletions Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -2720,6 +2720,9 @@

#if FAN_COUNT > 0
#define HAS_FAN 1
#else
#undef FAN_SOFT_PWM
#undef FAN_SOFT_PWM_REQUIRED
#endif

#if PIN_EXISTS(FANMUX0)
Expand Down
30 changes: 15 additions & 15 deletions Marlin/src/lcd/HD44780/marlinui_HD44780.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,30 +1049,30 @@ void MarlinUI::draw_status_screen() {
rotate_progress();
#else
char c;
uint16_t per;
uint16_t pct;
#if HAS_FAN0
if (true
#if ALL(HAS_EXTRUDERS, 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_lchar(c);
lcd_put_u8str(i16tostr3rj(per));
lcd_put_u8str(i16tostr3rj(pct));
lcd_put_u8str(F("%"));
#endif
#endif
Expand Down Expand Up @@ -1270,14 +1270,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

Expand Down
10 changes: 5 additions & 5 deletions Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,11 +913,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
Expand All @@ -927,9 +927,9 @@ void MarlinUI::draw_status_screen() {
lcd_moveto(FANX, 5); lcd_put_u8str(F("FAN"));
lcd_moveto(FANX + 1, 6); lcd.write('%');
lcd_moveto(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;
Expand Down
12 changes: 6 additions & 6 deletions Marlin/src/lcd/dogm/status_screen_DOGM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,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))
Expand All @@ -674,7 +674,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
);
Expand Down Expand Up @@ -746,15 +746,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_lchar(c);
}
}
Expand Down
10 changes: 5 additions & 5 deletions Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,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);
Expand Down Expand Up @@ -826,12 +826,12 @@ void ST7920_Lite_Status_Screen::update_indicators(const bool forceUpdate) {
TERN_(HAS_HEATED_BED, draw_bed_temp(bed_temp, bed_target, forceUpdate));

// Update the fan and bed animations
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));
if (spd) draw_fan_icon(blink);
TERN_(HAS_HEATED_BED, draw_heat_icon(bed_target > 0 && blink, bed_target > 0));

Expand Down
18 changes: 9 additions & 9 deletions Marlin/src/lcd/e3v2/creality/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ void drawTuneMenu() {
#endif
#if HAS_FAN
drawMenuLine(TUNE_CASE_FAN, ICON_FanSpeed);
drawEditInteger3(TUNE_CASE_FAN, thermalManager.fan_speed[0]);
drawEditInteger3(TUNE_CASE_FAN, fans[0].speed);
#endif
#if HAS_ZOFFSET_ITEM
drawMenuLine(TUNE_CASE_ZOFF, ICON_Zoffset);
Expand Down Expand Up @@ -1686,8 +1686,8 @@ void updateVariable() {
#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 == ID_Tune) {
Expand Down Expand Up @@ -1748,7 +1748,7 @@ void updateVariable() {

#if HAS_FAN
if (_new_fanspeed) {
_fanspeed = thermalManager.fan_speed[0];
_fanspeed = fans[0].speed;
drawStatInt(195 + 2 * STAT_CHR_W, 384, _fanspeed);
}
#endif
Expand Down Expand Up @@ -1985,7 +1985,7 @@ void drawStatusArea(const bool with_update) {

#if HAS_FAN
dwinIconShow(ICON, ICON_FanSpeed, 187, 383);
drawStatInt(195 + 2 * STAT_CHR_W, 384, thermalManager.fan_speed[0]);
drawStatInt(195 + 2 * STAT_CHR_W, 384, fans[0].speed);
#endif

#if HAS_ZOFFSET_ITEM
Expand Down Expand Up @@ -2237,7 +2237,7 @@ void hmiSelectFile() {
// 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;
Expand Down Expand Up @@ -2832,7 +2832,7 @@ void drawTemperatureMenu() {
#endif
#if HAS_FAN
_TMENU_ICON(TEMP_CASE_FAN);
drawEditInteger3(i, thermalManager.fan_speed[0]);
drawEditInteger3(i, fans[0].speed);
#endif
#if HAS_PREHEAT
// PLA/ABS items have submenus
Expand Down Expand Up @@ -3061,7 +3061,7 @@ void hmiTemperature() {
#if HAS_FAN
case TEMP_CASE_FAN:
checkkey = ID_FanSpeed;
hmiValues.fanSpeed = thermalManager.fan_speed[0];
hmiValues.fanSpeed = fans[0].speed;
drawEditInteger3(3, hmiValues.fanSpeed, true);
encoderRate.enabled = true;
break;
Expand Down Expand Up @@ -3788,7 +3788,7 @@ void hmiTune() {
#if HAS_FAN
case TUNE_CASE_FAN: // Fan speed
checkkey = ID_FanSpeed;
hmiValues.fanSpeed = thermalManager.fan_speed[0];
hmiValues.fanSpeed = fans[0].speed;
drawEditInteger3(TUNE_CASE_FAN + MROWS - index_tune, hmiValues.fanSpeed, true);
encoderRate.enabled = true;
break;
Expand Down
26 changes: 13 additions & 13 deletions Marlin/src/lcd/e3v2/jyersui/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,9 @@ void JyersDWIN::drawStatusArea(const bool icons/*=false*/) {
fan = -1;
dwinIconShow(ICON, ICON_FanSpeed, 187, 383);
}
if (thermalManager.fan_speed[0] != fan) {
fan = thermalManager.fan_speed[0];
dwinDrawIntValue(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;
dwinDrawIntValue(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

Expand Down Expand Up @@ -2021,10 +2021,10 @@ void JyersDWIN::menuItemHandler(const uint8_t menu, const uint8_t item, bool dra
case TEMP_FAN:
if (draw) {
drawMenuItem(row, ICON_FanSpeed, F("Fan"));
drawFloat(thermalManager.fan_speed[0], row, false, 1);
drawFloat(fans[0].speed, row, false, 1);
}
else
modifyValue(thermalManager.fan_speed[0], MIN_FAN_SPEED, MAX_FAN_SPEED, 1);
modifyValue(fans[0].speed, MIN_FAN_SPEED, MAX_FAN_SPEED, 1);
break;
#endif
#if ANY(PIDTEMP, PIDTEMPBED)
Expand Down Expand Up @@ -3857,10 +3857,10 @@ void JyersDWIN::menuItemHandler(const uint8_t menu, const uint8_t item, bool dra
case TUNE_FAN:
if (draw) {
drawMenuItem(row, ICON_FanSpeed, F("Fan"));
drawFloat(thermalManager.fan_speed[0], row, false, 1);
drawFloat(fans[0].speed, row, false, 1);
}
else
modifyValue(thermalManager.fan_speed[0], MIN_FAN_SPEED, MAX_FAN_SPEED, 1);
modifyValue(fans[0].speed, MIN_FAN_SPEED, MAX_FAN_SPEED, 1);
break;
#endif

Expand Down Expand Up @@ -4483,7 +4483,7 @@ void JyersDWIN::printScreenControl() {
#if HAS_EXTRUDERS
gcode.process_subcommands_now(TS(F("M109 S"), pausetemp));
#endif
TERN_(HAS_FAN, thermalManager.fan_speed[0] = pausefan);
TERN_(HAS_FAN, fans[0].speed = pausefan);
planner.synchronize();
TERN_(HAS_MEDIA, queue.inject(F("M24")));
#endif
Expand Down Expand Up @@ -4533,7 +4533,7 @@ void JyersDWIN::popupControl() {
queue.inject(F("M25"));
TERN_(HAS_HOTEND, pausetemp = thermalManager.degTargetHotend(0));
TERN_(HAS_HEATED_BED, pausebed = thermalManager.degTargetBed());
TERN_(HAS_FAN, pausefan = thermalManager.fan_speed[0]);
TERN_(HAS_FAN, pausefan = fans[0].speed);
thermalManager.cooldown();
#endif
}
Expand Down Expand Up @@ -4917,8 +4917,8 @@ void JyersDWIN::screenUpdate() {
}
#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 != Proc_Value || selection != TEMP_HOTEND - scrollpos)
drawFloat(fanspeed, TEMP_FAN - scrollpos, false, 1);
Expand Down Expand Up @@ -4946,8 +4946,8 @@ void JyersDWIN::screenUpdate() {
}
#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 != Proc_Value || selection != TEMP_HOTEND - scrollpos)
drawFloat(fanspeed, TUNE_FAN - scrollpos, false, 1);
Expand Down
Loading

0 comments on commit 7ef0802

Please sign in to comment.