From b80ed51fcc2ab08e1018ccc7f3dca0792acda401 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 15 Mar 2015 06:55:31 -0700 Subject: [PATCH] Fix controllerFan() code - Add support for 4th extruder - Fix parentheses matching - Apply coding standards - Address issue mentioned at #1575 --- Marlin/Marlin_main.cpp | 57 ++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 45208b03cd..4dc7b47df8 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -5137,41 +5137,34 @@ void prepare_arc_move(char isclockwise) { #endif #endif -unsigned long lastMotor = 0; //Save the time for when a motor was turned on last -unsigned long lastMotorCheck = 0; +unsigned long lastMotor = 0; // Last time a motor was turned on +unsigned long lastMotorCheck = 0; // Last time the state was checked -void controllerFan() -{ - if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms - { - lastMotorCheck = millis(); - - if((X_ENABLE_READ) == (X_ENABLE_ON)) || (Y_ENABLE_READ) == (Y_ENABLE_ON)) || (Z_ENABLE_READ) == (Z_ENABLE_ON)) || (soft_pwm_bed > 0) - #if EXTRUDERS > 2 - || (E2_ENABLE_READ) == (E_ENABLE_ON)) - #endif - #if EXTRUDER > 1 - #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1 - || (X2_ENABLE_READ) == (X_ENABLE_ON)) +void controllerFan() { + uint32_t ms = millis(); + if (ms >= lastMotorCheck + 2500) { // Not a time critical function, so we only check every 2500ms + lastMotorCheck = ms; + if (X_ENABLE_READ == X_ENABLE_ON || Y_ENABLE_READ == Y_ENABLE_ON || Z_ENABLE_READ == Z_ENABLE_ON || soft_pwm_bed > 0 + || E0_ENABLE_READ == E_ENABLE_ON // If any of the drivers are enabled... + #if EXTRUDERS > 1 + || E1_ENABLE_READ == E_ENABLE_ON + #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1 + || X2_ENABLE_READ == X_ENABLE_ON + #endif + #if EXTRUDERS > 2 + || E2_ENABLE_READ == E_ENABLE_ON + #if EXTRUDERS > 3 + || E3_ENABLE_READ == E_ENABLE_ON + #endif + #endif #endif - || (E1_ENABLE_READ) == (E_ENABLE_ON)) - #endif - || (E0_ENABLE_READ) == (E_ENABLE_ON))) //If any of the drivers are enabled... - { - lastMotor = millis(); //... set time to NOW so the fan will turn on - } - - if ((millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC... - { - digitalWrite(CONTROLLERFAN_PIN, 0); - analogWrite(CONTROLLERFAN_PIN, 0); - } - else - { - // allows digital or PWM fan output to be used (see M42 handling) - digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED); - analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED); + ) { + lastMotor = ms; //... set time to NOW so the fan will turn on } + uint8_t speed = (lastMotor == 0 || ms >= lastMotor + (CONTROLLERFAN_SECS * 1000UL)) ? 0 : CONTROLLERFAN_SPEED; + // allows digital or PWM fan output to be used (see M42 handling) + digitalWrite(CONTROLLERFAN_PIN, speed); + analogWrite(CONTROLLERFAN_PIN, speed); } } #endif