From 4d5a1984e216098b49cf1c590272b45d4177e86f Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 28 Jun 2019 17:03:43 -0500 Subject: [PATCH] Simplified fan handling code --- Marlin/src/HAL/HAL_AVR/fastio_AVR.h | 6 +- Marlin/src/gcode/control/M42.cpp | 2 +- Marlin/src/inc/Conditionals_post.h | 12 +--- Marlin/src/module/planner.cpp | 96 +++++++++++------------------ Marlin/src/module/temperature.cpp | 29 ++++----- Marlin/src/pins/pins.h | 1 + 6 files changed, 58 insertions(+), 88 deletions(-) diff --git a/Marlin/src/HAL/HAL_AVR/fastio_AVR.h b/Marlin/src/HAL/HAL_AVR/fastio_AVR.h index e1f1166bc..1f2e820bd 100644 --- a/Marlin/src/HAL/HAL_AVR/fastio_AVR.h +++ b/Marlin/src/HAL/HAL_AVR/fastio_AVR.h @@ -287,11 +287,11 @@ enum ClockSource2 : char { #if ANY_PIN(FAN, FAN1, FAN2) #if PIN_EXISTS(FAN2) - #define PWM_CHK_FAN_A(P) (P == FAN_PIN || P == FAN1_PIN || P == FAN2_PIN) + #define PWM_CHK_FAN_A(P) (P == FAN0_PIN || P == FAN1_PIN || P == FAN2_PIN) #elif PIN_EXISTS(FAN1) - #define PWM_CHK_FAN_A(P) (P == FAN_PIN || P == FAN1_PIN) + #define PWM_CHK_FAN_A(P) (P == FAN0_PIN || P == FAN1_PIN) #else - #define PWM_CHK_FAN_A(P) (P == FAN_PIN) + #define PWM_CHK_FAN_A(P) (P == FAN0_PIN) #endif #else #define PWM_CHK_FAN_A(P) false diff --git a/Marlin/src/gcode/control/M42.cpp b/Marlin/src/gcode/control/M42.cpp index 9c93b2fed..0ee2ef707 100644 --- a/Marlin/src/gcode/control/M42.cpp +++ b/Marlin/src/gcode/control/M42.cpp @@ -56,7 +56,7 @@ void GcodeSuite::M42() { #if FAN_COUNT > 0 switch (pin) { #if HAS_FAN0 - case FAN_PIN: thermalManager.fan_speed[0] = pin_status; break; + case FAN0_PIN: thermalManager.fan_speed[0] = pin_status; break; #endif #if HAS_FAN1 case FAN1_PIN: thermalManager.fan_speed[1] = pin_status; break; diff --git a/Marlin/src/inc/Conditionals_post.h b/Marlin/src/inc/Conditionals_post.h index 169ceb9f4..1a17addbd 100644 --- a/Marlin/src/inc/Conditionals_post.h +++ b/Marlin/src/inc/Conditionals_post.h @@ -1193,17 +1193,9 @@ #define FAN_COUNT 0 #endif -#if HAS_FAN0 - #define WRITE_FAN(v) WRITE(FAN_PIN, (v) ^ FAN_INVERTING) - #define WRITE_FAN0(v) WRITE_FAN(v) +#if FAN_COUNT > 0 + #define WRITE_FAN(n, v) WRITE(FAN##n##_PIN, (v) ^ FAN_INVERTING) #endif -#if HAS_FAN1 - #define WRITE_FAN1(v) WRITE(FAN1_PIN, (v) ^ FAN_INVERTING) -#endif -#if HAS_FAN2 - #define WRITE_FAN2(v) WRITE(FAN2_PIN, (v) ^ FAN_INVERTING) -#endif -#define WRITE_FAN_N(n, v) WRITE_FAN##n(v) /** * Part Cooling fan multipliexer diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp index 06612bd8b..e6f07b66c 100644 --- a/Marlin/src/module/planner.cpp +++ b/Marlin/src/module/planner.cpp @@ -1169,8 +1169,11 @@ void Planner::recalculate() { * Maintain fans, paste extruder pressure, */ void Planner::check_axes_activity() { - uint8_t axis_active[NUM_AXIS] = { 0 }, - tail_fan_speed[FAN_COUNT]; + uint8_t axis_active[NUM_AXIS] = { 0 }; + + #if FAN_COUNT > 0 + uint8_t tail_fan_speed[FAN_COUNT]; + #endif #if ENABLED(BARICUDA) #if HAS_HEATER_1 @@ -1182,15 +1185,18 @@ void Planner::check_axes_activity() { #endif if (has_blocks_queued()) { + block_t* block; + + #if FAN_COUNT > 0 || ENABLED(BARICUDA) + block = &block_buffer[block_buffer_tail]; + #endif + #if FAN_COUNT > 0 FANS_LOOP(i) - tail_fan_speed[i] = thermalManager.scaledFanSpeed(i, block_buffer[block_buffer_tail].fan_speed[i]); + tail_fan_speed[i] = thermalManager.scaledFanSpeed(i, block->fan_speed[i]); #endif - block_t* block; - #if ENABLED(BARICUDA) - block = &block_buffer[block_buffer_tail]; #if HAS_HEATER_1 tail_valve_pressure = block->valve_pressure; #endif @@ -1236,30 +1242,19 @@ void Planner::check_axes_activity() { #if FAN_COUNT > 0 #if FAN_KICKSTART_TIME > 0 - static millis_t fan_kick_end[FAN_COUNT] = { 0 }; - - #define KICKSTART_FAN(f) \ - if (tail_fan_speed[f]) { \ - millis_t ms = millis(); \ - if (fan_kick_end[f] == 0) { \ + #define KICKSTART_FAN(f) \ + if (tail_fan_speed[f]) { \ + millis_t ms = millis(); \ + if (fan_kick_end[f] == 0) { \ fan_kick_end[f] = ms + FAN_KICKSTART_TIME; \ - tail_fan_speed[f] = 255; \ - } else if (PENDING(ms, fan_kick_end[f])) \ - tail_fan_speed[f] = 255; \ + tail_fan_speed[f] = 255; \ + } else if (PENDING(ms, fan_kick_end[f])) \ + tail_fan_speed[f] = 255; \ } else fan_kick_end[f] = 0 - - #if HAS_FAN0 - KICKSTART_FAN(0); - #endif - #if HAS_FAN1 - KICKSTART_FAN(1); - #endif - #if HAS_FAN2 - KICKSTART_FAN(2); - #endif - - #endif // FAN_KICKSTART_TIME > 0 + #else + #define KICKSTART_FAN(f) NOOP + #endif #if FAN_MIN_PWM != 0 || FAN_MAX_PWM != 255 #define CALC_FAN_SPEED(f) (tail_fan_speed[f] ? map(tail_fan_speed[f], 1, 255, FAN_MIN_PWM, FAN_MAX_PWM) : 0) @@ -1268,43 +1263,24 @@ void Planner::check_axes_activity() { #endif #if ENABLED(FAN_SOFT_PWM) - - #if HAS_FAN0 - thermalManager.soft_pwm_amount_fan[0] = CALC_FAN_SPEED(0); - #endif - #if HAS_FAN1 - thermalManager.soft_pwm_amount_fan[1] = CALC_FAN_SPEED(1); - #endif - #if HAS_FAN2 - thermalManager.soft_pwm_amount_fan[2] = CALC_FAN_SPEED(2); - #endif - + #define _FAN_SET(F) thermalManager.soft_pwm_amount_fan[F] = CALC_FAN_SPEED(F); #elif ENABLED(FAST_PWM_FAN) - - #if HAS_FAN0 - set_pwm_duty(FAN_PIN, CALC_FAN_SPEED(0)); - #endif - #if HAS_FAN1 - set_pwm_duty(FAN1_PIN, CALC_FAN_SPEED(1)); - #endif - #if HAS_FAN2 - set_pwm_duty(FAN2_PIN, CALC_FAN_SPEED(2)); - #endif - + #define _FAN_SET(F) set_pwm_duty(FAN##F##_PIN, CALC_FAN_SPEED(F)); #else + #define _FAN_SET(F) analogWrite(pin_t(FAN##F##_PIN), CALC_FAN_SPEED(F)); + #endif + #define FAN_SET(F) do{ KICKSTART_FAN(F); _FAN_SET(F); }while(0) - #if HAS_FAN0 - analogWrite(pin_t(FAN_PIN), CALC_FAN_SPEED(0)); - #endif - #if HAS_FAN1 - analogWrite(pin_t(FAN1_PIN), CALC_FAN_SPEED(1)); - #endif - #if HAS_FAN2 - analogWrite(pin_t(FAN2_PIN), CALC_FAN_SPEED(2)); - #endif + #if HAS_FAN0 + FAN_SET(0); #endif - #else - UNUSED(tail_fan_speed); + #if HAS_FAN1 + FAN_SET(1); + #endif + #if HAS_FAN2 + FAN_SET(2); + #endif + #endif // FAN_COUNT > 0 #if ENABLED(AUTOTEMP) diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index 828c34e4c..fc93319de 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -2392,8 +2392,9 @@ void Temperature::isr() { #if ENABLED(FAN_SOFT_PWM) #define _FAN_PWM(N) do{ \ - soft_pwm_count_fan[N] = (soft_pwm_count_fan[N] & pwm_mask) + (soft_pwm_amount_fan[N] >> 1); \ - WRITE_FAN_N(N, soft_pwm_count_fan[N] > pwm_mask ? HIGH : LOW); \ + 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); @@ -2438,13 +2439,13 @@ void Temperature::isr() { #if ENABLED(FAN_SOFT_PWM) #if HAS_FAN0 - if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW); + 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_FAN1(LOW); + 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_FAN2(LOW); + if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW); #endif #endif } @@ -2525,28 +2526,28 @@ void Temperature::isr() { #if ENABLED(FAN_SOFT_PWM) if (pwm_count_tmp >= 127) { pwm_count_tmp = 0; - #define _PWM_FAN(N,I) do{ \ - soft_pwm_count_fan[I] = soft_pwm_amount_fan[I] >> 1; \ - WRITE_FAN##N(soft_pwm_count_fan[I] > 0 ? HIGH : LOW); \ + #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); + _PWM_FAN(0); #endif #if HAS_FAN1 - _PWM_FAN(1,1); + _PWM_FAN(1); #endif #if HAS_FAN2 - _PWM_FAN(2,2); + _PWM_FAN(2); #endif } #if HAS_FAN0 - if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW); + 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_FAN1(LOW); + 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_FAN2(LOW); + if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW); #endif #endif // FAN_SOFT_PWM diff --git a/Marlin/src/pins/pins.h b/Marlin/src/pins/pins.h index 221a07cdf..364066efd 100644 --- a/Marlin/src/pins/pins.h +++ b/Marlin/src/pins/pins.h @@ -660,6 +660,7 @@ #ifndef FAN_PIN #define FAN_PIN -1 #endif +#define FAN0_PIN FAN_PIN #ifndef FAN1_PIN #define FAN1_PIN -1 #endif