2018-02-06 07:22:30 +01:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 15:00:57 +01:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2018-02-06 07:22:30 +01:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2018-02-06 07:22:30 +01: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-23 05:20:14 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-02-06 07:22:30 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* power.cpp - power control
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
|
|
|
#if ENABLED(AUTO_POWER_CONTROL)
|
|
|
|
|
|
|
|
#include "power.h"
|
2018-02-10 22:41:18 +01:00
|
|
|
#include "../module/temperature.h"
|
2019-09-01 02:44:45 +02:00
|
|
|
#include "../module/stepper/indirection.h"
|
2020-01-03 02:01:38 +01:00
|
|
|
#include "../MarlinCore.h"
|
2018-02-06 07:22:30 +01:00
|
|
|
|
2021-06-25 21:50:09 +02:00
|
|
|
#if ENABLED(PS_OFF_SOUND)
|
|
|
|
#include "../libs/buzzer.h"
|
|
|
|
#endif
|
|
|
|
|
2020-10-21 22:03:11 +02:00
|
|
|
#if defined(PSU_POWERUP_GCODE) || defined(PSU_POWEROFF_GCODE)
|
|
|
|
#include "../gcode/gcode.h"
|
|
|
|
#endif
|
|
|
|
|
2020-07-09 02:21:04 +02:00
|
|
|
#if BOTH(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
|
|
|
|
#include "controllerfan.h"
|
|
|
|
#endif
|
|
|
|
|
2018-02-06 07:22:30 +01:00
|
|
|
Power powerManager;
|
|
|
|
|
|
|
|
millis_t Power::lastPowerOn;
|
|
|
|
|
|
|
|
bool Power::is_power_needed() {
|
2021-06-29 19:58:11 +02:00
|
|
|
|
|
|
|
if (printJobOngoing() || printingIsPaused()) return true;
|
|
|
|
|
2018-02-06 07:22:30 +01:00
|
|
|
#if ENABLED(AUTO_POWER_FANS)
|
2019-01-12 07:41:48 +01:00
|
|
|
FANS_LOOP(i) if (thermalManager.fan_speed[i]) return true;
|
2018-02-06 07:22:30 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLED(AUTO_POWER_E_FANS)
|
2018-10-07 22:34:41 +02:00
|
|
|
HOTEND_LOOP() if (thermalManager.autofan_speed[e]) return true;
|
2018-02-06 07:22:30 +01:00
|
|
|
#endif
|
|
|
|
|
2020-03-18 19:41:12 +01:00
|
|
|
#if BOTH(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
|
|
|
|
if (controllerFan.state()) return true;
|
2018-02-06 07:22:30 +01:00
|
|
|
#endif
|
|
|
|
|
2020-04-22 23:35:03 +02:00
|
|
|
if (TERN0(AUTO_POWER_CHAMBER_FAN, thermalManager.chamberfan_speed))
|
|
|
|
return true;
|
2019-05-05 05:51:47 +02:00
|
|
|
|
2021-03-06 21:13:28 +01:00
|
|
|
if (TERN0(AUTO_POWER_COOLER_FAN, thermalManager.coolerfan_speed))
|
|
|
|
return true;
|
|
|
|
|
2018-08-27 09:25:47 +02:00
|
|
|
// If any of the drivers or the bed are enabled...
|
2019-08-17 04:57:19 +02:00
|
|
|
if (X_ENABLE_READ() == X_ENABLE_ON || Y_ENABLE_READ() == Y_ENABLE_ON || Z_ENABLE_READ() == Z_ENABLE_ON
|
2019-11-10 00:59:04 +01:00
|
|
|
#if HAS_X2_ENABLE
|
|
|
|
|| X2_ENABLE_READ() == X_ENABLE_ON
|
|
|
|
#endif
|
|
|
|
#if HAS_Y2_ENABLE
|
|
|
|
|| Y2_ENABLE_READ() == Y_ENABLE_ON
|
|
|
|
#endif
|
|
|
|
#if HAS_Z2_ENABLE
|
|
|
|
|| Z2_ENABLE_READ() == Z_ENABLE_ON
|
|
|
|
#endif
|
|
|
|
#if E_STEPPERS
|
|
|
|
#define _OR_ENABLED_E(N) || E##N##_ENABLE_READ() == E_ENABLE_ON
|
|
|
|
REPEAT(E_STEPPERS, _OR_ENABLED_E)
|
|
|
|
#endif
|
2018-02-06 07:22:30 +01:00
|
|
|
) return true;
|
|
|
|
|
2021-06-28 06:02:11 +02:00
|
|
|
#if HAS_HOTEND
|
|
|
|
HOTEND_LOOP() if (thermalManager.degTargetHotend(e) > 0 || thermalManager.temp_hotend[e].soft_pwm_amount > 0) return true;
|
|
|
|
#endif
|
|
|
|
|
2020-08-19 09:49:46 +02:00
|
|
|
if (TERN0(HAS_HEATED_BED, thermalManager.degTargetBed() > 0 || thermalManager.temp_bed.soft_pwm_amount > 0)) return true;
|
2018-02-06 07:22:30 +01:00
|
|
|
|
2020-04-28 05:35:25 +02:00
|
|
|
#if HAS_HOTEND && AUTO_POWER_E_TEMP
|
2021-04-24 02:06:55 +02:00
|
|
|
HOTEND_LOOP() if (thermalManager.degHotend(e) >= (AUTO_POWER_E_TEMP)) return true;
|
2019-06-27 23:28:07 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAS_HEATED_CHAMBER && AUTO_POWER_CHAMBER_TEMP
|
2021-04-24 02:06:55 +02:00
|
|
|
if (thermalManager.degChamber() >= (AUTO_POWER_CHAMBER_TEMP)) return true;
|
2019-06-27 23:28:07 +02:00
|
|
|
#endif
|
|
|
|
|
2021-03-06 21:13:28 +01:00
|
|
|
#if HAS_COOLER && AUTO_POWER_COOLER_TEMP
|
2021-04-24 02:06:55 +02:00
|
|
|
if (thermalManager.degCooler() >= (AUTO_POWER_COOLER_TEMP)) return true;
|
2021-03-06 21:13:28 +01:00
|
|
|
#endif
|
|
|
|
|
2018-02-06 07:22:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-02 09:06:55 +02:00
|
|
|
#ifndef POWER_TIMEOUT
|
|
|
|
#define POWER_TIMEOUT 0
|
|
|
|
#endif
|
|
|
|
|
2021-06-29 19:58:11 +02:00
|
|
|
void Power::check(const bool pause) {
|
|
|
|
static bool _pause = false;
|
2018-02-06 07:22:30 +01:00
|
|
|
static millis_t nextPowerCheck = 0;
|
2021-06-29 19:58:11 +02:00
|
|
|
const millis_t now = millis();
|
|
|
|
#if POWER_TIMEOUT > 0
|
|
|
|
if (pause != _pause) {
|
|
|
|
lastPowerOn = now + !now;
|
|
|
|
_pause = pause;
|
|
|
|
}
|
|
|
|
if (pause) return;
|
|
|
|
#endif
|
2021-06-26 21:28:50 +02:00
|
|
|
if (ELAPSED(now, nextPowerCheck)) {
|
|
|
|
nextPowerCheck = now + 2500UL;
|
2018-02-06 07:22:30 +01:00
|
|
|
if (is_power_needed())
|
|
|
|
power_on();
|
2021-06-26 21:28:50 +02:00
|
|
|
else if (!lastPowerOn || (POWER_TIMEOUT > 0 && ELAPSED(now, lastPowerOn + SEC_TO_MS(POWER_TIMEOUT))))
|
2018-02-06 07:22:30 +01:00
|
|
|
power_off();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Power::power_on() {
|
2021-06-29 19:58:11 +02:00
|
|
|
const millis_t now = millis();
|
|
|
|
lastPowerOn = now + !now;
|
2018-04-17 11:34:31 +02:00
|
|
|
if (!powersupply_on) {
|
|
|
|
PSU_PIN_ON();
|
2020-07-17 19:33:45 +02:00
|
|
|
safe_delay(PSU_POWERUP_DELAY);
|
2020-04-20 06:37:05 +02:00
|
|
|
restore_stepper_drivers();
|
2020-07-17 19:33:45 +02:00
|
|
|
TERN_(HAS_TRINAMIC_CONFIG, safe_delay(PSU_POWERUP_DELAY));
|
2020-10-21 22:03:11 +02:00
|
|
|
#ifdef PSU_POWERUP_GCODE
|
|
|
|
GcodeSuite::process_subcommands_now_P(PSTR(PSU_POWERUP_GCODE));
|
|
|
|
#endif
|
2018-04-17 11:34:31 +02:00
|
|
|
}
|
2018-02-06 07:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Power::power_off() {
|
2020-10-21 22:03:11 +02:00
|
|
|
if (powersupply_on) {
|
|
|
|
#ifdef PSU_POWEROFF_GCODE
|
|
|
|
GcodeSuite::process_subcommands_now_P(PSTR(PSU_POWEROFF_GCODE));
|
|
|
|
#endif
|
2021-06-25 21:50:09 +02:00
|
|
|
|
|
|
|
#if ENABLED(PS_OFF_SOUND)
|
|
|
|
BUZZ(1000, 659);
|
|
|
|
#endif
|
|
|
|
|
2021-01-13 04:48:34 +01:00
|
|
|
PSU_PIN_OFF();
|
2020-10-21 22:03:11 +02:00
|
|
|
}
|
2018-02-06 07:22:30 +01:00
|
|
|
}
|
|
|
|
|
2020-11-09 01:37:41 +01:00
|
|
|
void Power::power_off_soon() {
|
|
|
|
#if POWER_OFF_DELAY
|
|
|
|
lastPowerOn = millis() - SEC_TO_MS(POWER_TIMEOUT) + SEC_TO_MS(POWER_OFF_DELAY);
|
2021-06-29 19:58:11 +02:00
|
|
|
//if (!lastPowerOn) ++lastPowerOn;
|
2020-11-09 01:37:41 +01:00
|
|
|
#else
|
|
|
|
power_off();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-02-06 07:22:30 +01:00
|
|
|
#endif // AUTO_POWER_CONTROL
|