2017-09-18 01:44:59 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-18 01:44:59 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-18 01:44:59 -05:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-09-18 01:44:59 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
|
|
|
#if ENABLED(USE_CONTROLLER_FAN)
|
|
|
|
|
2020-03-18 13:41:12 -05:00
|
|
|
#include "controllerfan.h"
|
2019-08-31 19:44:45 -05:00
|
|
|
#include "../module/stepper/indirection.h"
|
2017-10-07 07:26:36 -05:00
|
|
|
#include "../module/temperature.h"
|
|
|
|
|
2020-03-18 13:41:12 -05:00
|
|
|
ControllerFan controllerFan;
|
2018-02-16 02:18:50 -06:00
|
|
|
|
2020-03-18 13:41:12 -05:00
|
|
|
uint8_t ControllerFan::speed;
|
|
|
|
|
|
|
|
#if ENABLED(CONTROLLER_FAN_EDITABLE)
|
|
|
|
controllerFan_settings_t ControllerFan::settings; // {0}
|
2020-11-10 03:32:08 -06:00
|
|
|
#else
|
|
|
|
const controllerFan_settings_t &ControllerFan::settings = controllerFan_defaults;
|
2020-03-18 13:41:12 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void ControllerFan::setup() {
|
|
|
|
SET_OUTPUT(CONTROLLER_FAN_PIN);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerFan::set_fan_speed(const uint8_t s) {
|
|
|
|
speed = s < (CONTROLLERFAN_SPEED_MIN) ? 0 : s; // Fan OFF below minimum
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerFan::update() {
|
|
|
|
static millis_t lastMotorOn = 0, // Last time a motor was turned on
|
2017-09-18 01:44:59 -05:00
|
|
|
nextMotorCheck = 0; // Last time the state was checked
|
|
|
|
const millis_t ms = millis();
|
|
|
|
if (ELAPSED(ms, nextMotorCheck)) {
|
|
|
|
nextMotorCheck = ms + 2500UL; // Not a time critical function, so only check every 2.5s
|
2018-08-29 01:49:30 -05:00
|
|
|
|
2020-03-18 13:41:12 -05:00
|
|
|
#define MOTOR_IS_ON(A,B) (A##_ENABLE_READ() == bool(B##_ENABLE_ON))
|
|
|
|
#define _OR_ENABLED_E(N) || MOTOR_IS_ON(E##N,E)
|
|
|
|
|
2020-07-23 21:27:40 -05:00
|
|
|
const bool motor_on = (
|
|
|
|
( DISABLED(CONTROLLER_FAN_IGNORE_Z) &&
|
|
|
|
( MOTOR_IS_ON(Z,Z)
|
|
|
|
|| TERN0(HAS_Z2_ENABLE, MOTOR_IS_ON(Z2,Z))
|
|
|
|
|| TERN0(HAS_Z3_ENABLE, MOTOR_IS_ON(Z3,Z))
|
|
|
|
|| TERN0(HAS_Z4_ENABLE, MOTOR_IS_ON(Z4,Z))
|
|
|
|
)
|
|
|
|
) || (
|
|
|
|
DISABLED(CONTROLLER_FAN_USE_Z_ONLY) &&
|
|
|
|
( MOTOR_IS_ON(X,X) || MOTOR_IS_ON(Y,Y)
|
|
|
|
|| TERN0(HAS_X2_ENABLE, MOTOR_IS_ON(X2,X))
|
|
|
|
|| TERN0(HAS_Y2_ENABLE, MOTOR_IS_ON(Y2,Y))
|
2020-03-18 14:35:52 -05:00
|
|
|
#if E_STEPPERS
|
|
|
|
REPEAT(E_STEPPERS, _OR_ENABLED_E)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
)
|
2020-07-23 21:27:40 -05:00
|
|
|
);
|
2019-08-16 21:57:19 -05:00
|
|
|
|
2020-03-18 14:35:52 -05:00
|
|
|
// If any of the drivers or the heated bed are enabled...
|
2020-04-22 16:35:03 -05:00
|
|
|
if (motor_on || TERN0(HAS_HEATED_BED, thermalManager.temp_bed.soft_pwm_amount > 0))
|
|
|
|
lastMotorOn = ms; //... set time to NOW so the fan will turn on
|
2017-09-18 01:44:59 -05:00
|
|
|
|
2020-03-18 13:41:12 -05:00
|
|
|
// Fan Settings. Set fan > 0:
|
|
|
|
// - If AutoMode is on and steppers have been enabled for CONTROLLERFAN_IDLE_TIME seconds.
|
|
|
|
// - If System is on idle and idle fan speed settings is activated.
|
|
|
|
set_fan_speed(
|
2020-04-03 19:49:45 -05:00
|
|
|
settings.auto_mode && lastMotorOn && PENDING(ms, lastMotorOn + SEC_TO_MS(settings.duration))
|
2020-03-18 13:41:12 -05:00
|
|
|
? settings.active_speed : settings.idle_speed
|
2019-08-16 21:57:19 -05:00
|
|
|
);
|
2017-09-18 01:44:59 -05:00
|
|
|
|
2019-08-16 21:57:19 -05:00
|
|
|
// Allow digital or PWM fan output (see M42 handling)
|
2020-03-18 13:41:12 -05:00
|
|
|
WRITE(CONTROLLER_FAN_PIN, speed);
|
|
|
|
analogWrite(pin_t(CONTROLLER_FAN_PIN), speed);
|
2017-09-18 01:44:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_CONTROLLER_FAN
|