Skip to content

Commit

Permalink
♻️ Fan class
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed May 28, 2022
1 parent 1f20c11 commit 513cac1
Show file tree
Hide file tree
Showing 42 changed files with 503 additions and 544 deletions.
2 changes: 1 addition & 1 deletion Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
7 changes: 6 additions & 1 deletion Marlin/src/feature/controllerfan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "controllerfan.h"
#include "../module/stepper.h"
#include "../module/temperature.h"
#include "../module/fans.h"

ControllerFan controllerFan;

Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions Marlin/src/feature/controllerfan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/feature/pause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
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 @@ -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;
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 @@ -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
Expand Down
26 changes: 2 additions & 24 deletions Marlin/src/gcode/control/M42.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

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 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
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/gcode/temp/M106_M107.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
16 changes: 6 additions & 10 deletions Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2988,6 +2981,9 @@

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

/**
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 @@ -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
Expand Down Expand Up @@ -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

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 @@ -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
Expand All @@ -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;
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 @@ -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))
Expand All @@ -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
);
Expand Down Expand Up @@ -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);
}
}
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 @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 513cac1

Please sign in to comment.