My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

controllerfan.cpp 3.3KB

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