From d0886f75bea08f37d4d33c9ed172e14ed54780f4 Mon Sep 17 00:00:00 2001 From: vovodroid Date: Sun, 29 Jun 2025 09:28:57 +0300 Subject: [PATCH 1/2] PLR: trigger power lost by timeout --- Marlin/Configuration_adv.h | 2 ++ Marlin/src/feature/powerloss.h | 12 +++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 1cd89a9714..a0e1f10cf2 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -1808,6 +1808,8 @@ //#define POWER_LOSS_RETRACT_LEN 10 // (mm) Length of filament to retract on fail #endif + #define POWER_LOSS_TIMEOUT 10 // (ms) Power loss duration to raise event + // Enable if Z homing is needed for proper recovery. 99.9% of the time this should be disabled! //#define POWER_LOSS_RECOVER_ZHOME #if ENABLED(POWER_LOSS_RECOVER_ZHOME) diff --git a/Marlin/src/feature/powerloss.h b/Marlin/src/feature/powerloss.h index 8e1e299ac0..5c0fa56fb5 100644 --- a/Marlin/src/feature/powerloss.h +++ b/Marlin/src/feature/powerloss.h @@ -211,14 +211,12 @@ class PrintJobRecovery { #if PIN_EXISTS(POWER_LOSS) static void outage() { - static constexpr uint8_t OUTAGE_THRESHOLD = 3; - static uint8_t outage_counter = 0; - if (enabled && READ(POWER_LOSS_PIN) == POWER_LOSS_STATE) { - outage_counter++; - if (outage_counter >= OUTAGE_THRESHOLD) _outage(); - } + static unsigned long last_power_on; + if ( enabled && READ(POWER_LOSS_PIN) == POWER_LOSS_STATE + && millis() - last_power_on >= POWER_LOSS_TIMEOUT + ) _outage(); else - outage_counter = 0; + last_power_on = millis(); } #endif From d198f1fdc5114bb64765171ebdc6c7efb1606d60 Mon Sep 17 00:00:00 2001 From: vovodroid Date: Sun, 29 Jun 2025 13:03:05 +0300 Subject: [PATCH 2/2] Make POWER_LOSS_TIMEOUT optional --- Marlin/Configuration_adv.h | 2 +- Marlin/src/feature/powerloss.h | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index a0e1f10cf2..8fe13fe46f 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -1808,7 +1808,7 @@ //#define POWER_LOSS_RETRACT_LEN 10 // (mm) Length of filament to retract on fail #endif - #define POWER_LOSS_TIMEOUT 10 // (ms) Power loss duration to raise event + //#define POWER_LOSS_TIMEOUT 10 // (ms) Power loss duration to raise event, three idle() calls otherwise // Enable if Z homing is needed for proper recovery. 99.9% of the time this should be disabled! //#define POWER_LOSS_RECOVER_ZHOME diff --git a/Marlin/src/feature/powerloss.h b/Marlin/src/feature/powerloss.h index 5c0fa56fb5..c8b9e44a50 100644 --- a/Marlin/src/feature/powerloss.h +++ b/Marlin/src/feature/powerloss.h @@ -211,12 +211,23 @@ class PrintJobRecovery { #if PIN_EXISTS(POWER_LOSS) static void outage() { - static unsigned long last_power_on; - if ( enabled && READ(POWER_LOSS_PIN) == POWER_LOSS_STATE - && millis() - last_power_on >= POWER_LOSS_TIMEOUT - ) _outage(); - else - last_power_on = millis(); + #if defined(POWER_LOSS_TIMEOUT) + static unsigned long last_power_on; + if ( enabled && READ(POWER_LOSS_PIN) == POWER_LOSS_STATE) { + if (millis() - last_power_on >= POWER_LOSS_TIMEOUT) _outage(); + } + else + last_power_on = millis(); + #else + static constexpr uint8_t OUTAGE_THRESHOLD = 3; + static uint8_t outage_counter = 0; + if (enabled && READ(POWER_LOSS_PIN) == POWER_LOSS_STATE) { + outage_counter++; + if (outage_counter >= OUTAGE_THRESHOLD) _outage(); + } + else + outage_counter = 0; + #endif } #endif