Simplified fan handling code

2.0.x
Scott Lahteine 6 years ago
parent a8d68b7c8a
commit 4d5a1984e2

@ -287,11 +287,11 @@ enum ClockSource2 : char {
#if ANY_PIN(FAN, FAN1, FAN2) #if ANY_PIN(FAN, FAN1, FAN2)
#if PIN_EXISTS(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) #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 #else
#define PWM_CHK_FAN_A(P) (P == FAN_PIN) #define PWM_CHK_FAN_A(P) (P == FAN0_PIN)
#endif #endif
#else #else
#define PWM_CHK_FAN_A(P) false #define PWM_CHK_FAN_A(P) false

@ -56,7 +56,7 @@ void GcodeSuite::M42() {
#if FAN_COUNT > 0 #if FAN_COUNT > 0
switch (pin) { switch (pin) {
#if HAS_FAN0 #if HAS_FAN0
case FAN_PIN: thermalManager.fan_speed[0] = pin_status; break; case FAN0_PIN: thermalManager.fan_speed[0] = pin_status; break;
#endif #endif
#if HAS_FAN1 #if HAS_FAN1
case FAN1_PIN: thermalManager.fan_speed[1] = pin_status; break; case FAN1_PIN: thermalManager.fan_speed[1] = pin_status; break;

@ -1193,17 +1193,9 @@
#define FAN_COUNT 0 #define FAN_COUNT 0
#endif #endif
#if HAS_FAN0 #if FAN_COUNT > 0
#define WRITE_FAN(v) WRITE(FAN_PIN, (v) ^ FAN_INVERTING) #define WRITE_FAN(n, v) WRITE(FAN##n##_PIN, (v) ^ FAN_INVERTING)
#define WRITE_FAN0(v) WRITE_FAN(v)
#endif #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 * Part Cooling fan multipliexer

@ -1169,8 +1169,11 @@ void Planner::recalculate() {
* Maintain fans, paste extruder pressure, * Maintain fans, paste extruder pressure,
*/ */
void Planner::check_axes_activity() { void Planner::check_axes_activity() {
uint8_t axis_active[NUM_AXIS] = { 0 }, uint8_t axis_active[NUM_AXIS] = { 0 };
tail_fan_speed[FAN_COUNT];
#if FAN_COUNT > 0
uint8_t tail_fan_speed[FAN_COUNT];
#endif
#if ENABLED(BARICUDA) #if ENABLED(BARICUDA)
#if HAS_HEATER_1 #if HAS_HEATER_1
@ -1182,15 +1185,18 @@ void Planner::check_axes_activity() {
#endif #endif
if (has_blocks_queued()) { if (has_blocks_queued()) {
block_t* block;
#if FAN_COUNT > 0 || ENABLED(BARICUDA)
block = &block_buffer[block_buffer_tail];
#endif
#if FAN_COUNT > 0 #if FAN_COUNT > 0
FANS_LOOP(i) 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 #endif
block_t* block;
#if ENABLED(BARICUDA) #if ENABLED(BARICUDA)
block = &block_buffer[block_buffer_tail];
#if HAS_HEATER_1 #if HAS_HEATER_1
tail_valve_pressure = block->valve_pressure; tail_valve_pressure = block->valve_pressure;
#endif #endif
@ -1236,9 +1242,7 @@ void Planner::check_axes_activity() {
#if FAN_COUNT > 0 #if FAN_COUNT > 0
#if FAN_KICKSTART_TIME > 0 #if FAN_KICKSTART_TIME > 0
static millis_t fan_kick_end[FAN_COUNT] = { 0 }; static millis_t fan_kick_end[FAN_COUNT] = { 0 };
#define KICKSTART_FAN(f) \ #define KICKSTART_FAN(f) \
if (tail_fan_speed[f]) { \ if (tail_fan_speed[f]) { \
millis_t ms = millis(); \ millis_t ms = millis(); \
@ -1248,19 +1252,10 @@ void Planner::check_axes_activity() {
} else if (PENDING(ms, fan_kick_end[f])) \ } else if (PENDING(ms, fan_kick_end[f])) \
tail_fan_speed[f] = 255; \ tail_fan_speed[f] = 255; \
} else fan_kick_end[f] = 0 } else fan_kick_end[f] = 0
#else
#if HAS_FAN0 #define KICKSTART_FAN(f) NOOP
KICKSTART_FAN(0);
#endif
#if HAS_FAN1
KICKSTART_FAN(1);
#endif
#if HAS_FAN2
KICKSTART_FAN(2);
#endif #endif
#endif // FAN_KICKSTART_TIME > 0
#if FAN_MIN_PWM != 0 || FAN_MAX_PWM != 255 #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) #define CALC_FAN_SPEED(f) (tail_fan_speed[f] ? map(tail_fan_speed[f], 1, 255, FAN_MIN_PWM, FAN_MAX_PWM) : 0)
#else #else
@ -1268,43 +1263,24 @@ void Planner::check_axes_activity() {
#endif #endif
#if ENABLED(FAN_SOFT_PWM) #if ENABLED(FAN_SOFT_PWM)
#define _FAN_SET(F) thermalManager.soft_pwm_amount_fan[F] = CALC_FAN_SPEED(F);
#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
#elif ENABLED(FAST_PWM_FAN) #elif ENABLED(FAST_PWM_FAN)
#define _FAN_SET(F) set_pwm_duty(FAN##F##_PIN, CALC_FAN_SPEED(F));
#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
#else #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 #if HAS_FAN0
analogWrite(pin_t(FAN_PIN), CALC_FAN_SPEED(0)); FAN_SET(0);
#endif #endif
#if HAS_FAN1 #if HAS_FAN1
analogWrite(pin_t(FAN1_PIN), CALC_FAN_SPEED(1)); FAN_SET(1);
#endif #endif
#if HAS_FAN2 #if HAS_FAN2
analogWrite(pin_t(FAN2_PIN), CALC_FAN_SPEED(2)); FAN_SET(2);
#endif
#endif #endif
#else
UNUSED(tail_fan_speed);
#endif // FAN_COUNT > 0 #endif // FAN_COUNT > 0
#if ENABLED(AUTOTEMP) #if ENABLED(AUTOTEMP)

@ -2392,8 +2392,9 @@ void Temperature::isr() {
#if ENABLED(FAN_SOFT_PWM) #if ENABLED(FAN_SOFT_PWM)
#define _FAN_PWM(N) do{ \ #define _FAN_PWM(N) do{ \
soft_pwm_count_fan[N] = (soft_pwm_count_fan[N] & pwm_mask) + (soft_pwm_amount_fan[N] >> 1); \ uint8_t &spcf = soft_pwm_count_fan[N]; \
WRITE_FAN_N(N, soft_pwm_count_fan[N] > pwm_mask ? HIGH : LOW); \ spcf = (spcf & pwm_mask) + (soft_pwm_amount_fan[N] >> 1); \
WRITE_FAN(N, spcf > pwm_mask ? HIGH : LOW); \
}while(0) }while(0)
#if HAS_FAN0 #if HAS_FAN0
_FAN_PWM(0); _FAN_PWM(0);
@ -2438,13 +2439,13 @@ void Temperature::isr() {
#if ENABLED(FAN_SOFT_PWM) #if ENABLED(FAN_SOFT_PWM)
#if HAS_FAN0 #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 #endif
#if HAS_FAN1 #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 #endif
#if HAS_FAN2 #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
#endif #endif
} }
@ -2525,28 +2526,28 @@ void Temperature::isr() {
#if ENABLED(FAN_SOFT_PWM) #if ENABLED(FAN_SOFT_PWM)
if (pwm_count_tmp >= 127) { if (pwm_count_tmp >= 127) {
pwm_count_tmp = 0; pwm_count_tmp = 0;
#define _PWM_FAN(N,I) do{ \ #define _PWM_FAN(N) do{ \
soft_pwm_count_fan[I] = soft_pwm_amount_fan[I] >> 1; \ soft_pwm_count_fan[N] = soft_pwm_amount_fan[N] >> 1; \
WRITE_FAN##N(soft_pwm_count_fan[I] > 0 ? HIGH : LOW); \ WRITE_FAN(N, soft_pwm_count_fan[N] > 0 ? HIGH : LOW); \
}while(0) }while(0)
#if HAS_FAN0 #if HAS_FAN0
_PWM_FAN(,0); _PWM_FAN(0);
#endif #endif
#if HAS_FAN1 #if HAS_FAN1
_PWM_FAN(1,1); _PWM_FAN(1);
#endif #endif
#if HAS_FAN2 #if HAS_FAN2
_PWM_FAN(2,2); _PWM_FAN(2);
#endif #endif
} }
#if HAS_FAN0 #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 #endif
#if HAS_FAN1 #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 #endif
#if HAS_FAN2 #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
#endif // FAN_SOFT_PWM #endif // FAN_SOFT_PWM

@ -660,6 +660,7 @@
#ifndef FAN_PIN #ifndef FAN_PIN
#define FAN_PIN -1 #define FAN_PIN -1
#endif #endif
#define FAN0_PIN FAN_PIN
#ifndef FAN1_PIN #ifndef FAN1_PIN
#define FAN1_PIN -1 #define FAN1_PIN -1
#endif #endif

Loading…
Cancel
Save