Skip to content

Commit 51fb221

Browse files
committed
♻️ Power-off Timer
1 parent 2c647ca commit 51fb221

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

Marlin/src/feature/power.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ bool Power::psu_on;
6161
#include "spindle_laser.h"
6262
#endif
6363

64-
millis_t Power::lastPowerOn;
64+
millis_t Power::lastPowerOn; // = 0
6565
#endif
6666

6767
#if PSU_TRACK_STATE_MS
@@ -169,8 +169,9 @@ void Power::power_off() {
169169
#if ANY(POWER_OFF_TIMER, POWER_OFF_WAIT_FOR_COOLDOWN)
170170

171171
#if ENABLED(POWER_OFF_TIMER)
172-
millis_t Power::power_off_time = 0;
173-
void Power::setPowerOffTimer(const millis_t delay_ms) { power_off_time = millis() + delay_ms; }
172+
millis_t Power::power_off_start_ms = 0;
173+
uint16_t power_off_secs = 0;
174+
void Power::setPowerOffTimer(const uint16_t delay_sec) { power_off_start_ms = millis(); power_off_secs = delay_sec; }
174175
#endif
175176

176177
#if ENABLED(POWER_OFF_WAIT_FOR_COOLDOWN)
@@ -179,14 +180,14 @@ void Power::power_off() {
179180
#endif
180181

181182
void Power::cancelAutoPowerOff() {
182-
TERN_(POWER_OFF_TIMER, power_off_time = 0);
183+
TERN_(POWER_OFF_TIMER, power_off_secs = 0);
183184
TERN_(POWER_OFF_WAIT_FOR_COOLDOWN, power_off_on_cooldown = false);
184185
}
185186

186187
void Power::checkAutoPowerOff() {
187-
if (TERN1(POWER_OFF_TIMER, !power_off_time) && TERN1(POWER_OFF_WAIT_FOR_COOLDOWN, !power_off_on_cooldown)) return;
188+
if (TERN1(POWER_OFF_TIMER, !power_off_secs) && TERN1(POWER_OFF_WAIT_FOR_COOLDOWN, !power_off_on_cooldown)) return;
188189
if (TERN0(POWER_OFF_WAIT_FOR_COOLDOWN, power_off_on_cooldown && is_cooling_needed())) return;
189-
if (TERN0(POWER_OFF_TIMER, power_off_time && PENDING(millis(), power_off_time))) return;
190+
if (TERN0(POWER_OFF_TIMER, power_off_secs && PENDING(millis(), power_off_start_ms, SEC_TO_MS(power_off_secs)))) return;
190191
power_off();
191192
}
192193

@@ -247,8 +248,9 @@ void Power::power_off() {
247248
* @param pause pause the 'timer'
248249
*/
249250
void Power::check(const bool pause) {
250-
static millis_t nextPowerCheck = 0;
251+
static MTimeout24 lastPowerCheck;
251252
const millis_t now = millis();
253+
252254
#if POWER_TIMEOUT > 0
253255
static bool _pause = false;
254256
if (pause != _pause) {
@@ -257,8 +259,9 @@ void Power::power_off() {
257259
}
258260
if (pause) return;
259261
#endif
260-
if (ELAPSED(now, nextPowerCheck)) {
261-
nextPowerCheck = now + 2500UL;
262+
263+
if (lastPowerCheck.elapsed(now)) {
264+
lastPowerCheck.start(2500);
262265
if (is_power_needed())
263266
power_on();
264267
else if (!lastPowerOn || (POWER_TIMEOUT > 0 && ELAPSED(now, lastPowerOn, SEC_TO_MS(POWER_TIMEOUT))))

Marlin/src/feature/power.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Power {
4747
#if ANY(POWER_OFF_TIMER, POWER_OFF_WAIT_FOR_COOLDOWN)
4848
#if ENABLED(POWER_OFF_TIMER)
4949
static millis_t power_off_time;
50-
static void setPowerOffTimer(const millis_t delay_ms);
50+
static void setPowerOffTimer(const uint16_t delay_sec);
5151
#endif
5252
#if ENABLED(POWER_OFF_WAIT_FOR_COOLDOWN)
5353
static bool power_off_on_cooldown;

Marlin/src/gcode/control/M80_M81.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@
7676
* M81: Turn off Power, including Power Supply, if there is one.
7777
*
7878
* This code should ALWAYS be available for FULL SHUTDOWN!
79+
*
80+
* With POWER_OFF_TIMER:
81+
* D<seconds> - Set timer for power-off to occur in some number of seconds
82+
*
83+
* With POWER_OFF_WAIT_FOR_COOLDOWN:
84+
* S<bool> - Enable / disable Power-Off when the machine has cooled down
7985
*/
8086
void GcodeSuite::M81() {
8187
planner.finish_and_disable();
@@ -102,10 +108,10 @@ void GcodeSuite::M81() {
102108

103109
#if ENABLED(POWER_OFF_TIMER)
104110
if (parser.seenval('D')) {
105-
uint16_t delay = parser.value_ushort();
106-
if (delay > 1) { // skip already observed 1s delay
111+
const uint16_t seconds = parser.value_ushort();
112+
if (seconds > 1) { // skip already observed 1s delay
107113
delayed_power_off = true;
108-
powerManager.setPowerOffTimer(SEC_TO_MS(delay - 1));
114+
powerManager.setPowerOffTimer(seconds - 1);
109115
}
110116
}
111117
#endif

0 commit comments

Comments
 (0)