power.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * power.cpp - power control
  24. */
  25. #include "MarlinConfig.h"
  26. #if ENABLED(AUTO_POWER_CONTROL)
  27. #include "power.h"
  28. #include "temperature.h"
  29. #include "stepper_indirection.h"
  30. Power powerManager;
  31. millis_t Power::lastPowerOn;
  32. bool Power::is_power_needed() {
  33. #if ENABLED(AUTO_POWER_FANS)
  34. for (uint8_t i = 0; i < FAN_COUNT; i++) if (fanSpeeds[i] > 0) return true;
  35. #endif
  36. #if ENABLED(AUTO_POWER_E_FANS)
  37. HOTEND_LOOP() if (thermalManager.autofan_speed[e] > 0) return true;
  38. #endif
  39. #if ENABLED(AUTO_POWER_CONTROLLERFAN) && HAS_CONTROLLER_FAN && ENABLED(USE_CONTROLLER_FAN)
  40. if (controllerFanSpeed > 0) return true;
  41. #endif
  42. // If any of the drivers or the bed are enabled...
  43. if (X_ENABLE_READ == X_ENABLE_ON || Y_ENABLE_READ == Y_ENABLE_ON || Z_ENABLE_READ == Z_ENABLE_ON
  44. #if HAS_HEATED_BED
  45. || thermalManager.soft_pwm_amount_bed > 0
  46. #endif
  47. #if HAS_X2_ENABLE
  48. || X2_ENABLE_READ == X_ENABLE_ON
  49. #endif
  50. #if HAS_Y2_ENABLE
  51. || Y2_ENABLE_READ == Y_ENABLE_ON
  52. #endif
  53. #if HAS_Z2_ENABLE
  54. || Z2_ENABLE_READ == Z_ENABLE_ON
  55. #endif
  56. || E0_ENABLE_READ == E_ENABLE_ON
  57. #if E_STEPPERS > 1
  58. || E1_ENABLE_READ == E_ENABLE_ON
  59. #if E_STEPPERS > 2
  60. || E2_ENABLE_READ == E_ENABLE_ON
  61. #if E_STEPPERS > 3
  62. || E3_ENABLE_READ == E_ENABLE_ON
  63. #if E_STEPPERS > 4
  64. || E4_ENABLE_READ == E_ENABLE_ON
  65. #endif
  66. #endif
  67. #endif
  68. #endif
  69. ) return true;
  70. HOTEND_LOOP() if (thermalManager.degTargetHotend(e) > 0) return true;
  71. #if HAS_HEATED_BED
  72. if (thermalManager.degTargetBed() > 0) return true;
  73. #endif
  74. return false;
  75. }
  76. void Power::check() {
  77. static millis_t nextPowerCheck = 0;
  78. millis_t ms = millis();
  79. if (ELAPSED(ms, nextPowerCheck)) {
  80. nextPowerCheck = ms + 2500UL;
  81. if (is_power_needed())
  82. power_on();
  83. else if (!lastPowerOn || ELAPSED(ms, lastPowerOn + (POWER_TIMEOUT) * 1000UL))
  84. power_off();
  85. }
  86. }
  87. void Power::power_on() {
  88. lastPowerOn = millis();
  89. if (!powersupply_on) {
  90. PSU_PIN_ON();
  91. #if HAS_TRINAMIC
  92. delay(100); // Wait for power to settle
  93. restore_stepper_drivers();
  94. #endif
  95. }
  96. }
  97. void Power::power_off() {
  98. if (powersupply_on) PSU_PIN_OFF();
  99. }
  100. #endif // AUTO_POWER_CONTROL