From 73cd05827ab62287d9f655fb9a6f19e7d062e98f Mon Sep 17 00:00:00 2001 From: Xuan Son Nguyen Date: Thu, 12 Feb 2026 13:19:37 +0100 Subject: [PATCH] move IDLE_POWER_SAVING_MS --- lib/hal/HalPowerManager.cpp | 7 ++++--- lib/hal/HalPowerManager.h | 9 +++++---- src/main.cpp | 3 +-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/hal/HalPowerManager.cpp b/lib/hal/HalPowerManager.cpp index 5c945f59..745579a3 100644 --- a/lib/hal/HalPowerManager.cpp +++ b/lib/hal/HalPowerManager.cpp @@ -1,6 +1,7 @@ +#include "HalPowerManager.h" + #include -#include "HalPowerManager.h" #include "HalGPIO.h" void HalPowerManager::begin() { @@ -10,7 +11,7 @@ void HalPowerManager::begin() { void HalPowerManager::setPowerSaving(bool enabled) { if (normalFreq <= 0) { - return; // invalid state + return; // invalid state } if (enabled && !isLowPower) { Serial.printf("[%lu] [PWR] Going to low-power mode\n", millis()); @@ -29,7 +30,7 @@ void HalPowerManager::setPowerSaving(bool enabled) { isLowPower = enabled; } -void HalPowerManager::startDeepSleep(HalGPIO &gpio) const { +void HalPowerManager::startDeepSleep(HalGPIO& gpio) const { // Ensure that the power button has been released to avoid immediately turning back on if you're holding it while (gpio.isPressed(HalGPIO::BTN_POWER)) { delay(50); diff --git a/lib/hal/HalPowerManager.h b/lib/hal/HalPowerManager.h index 792bb449..636b2c97 100644 --- a/lib/hal/HalPowerManager.h +++ b/lib/hal/HalPowerManager.h @@ -7,19 +7,20 @@ #include "HalGPIO.h" class HalPowerManager { - static constexpr int LOW_POWER_FREQ = 10; // MHz - - int normalFreq = 0; // MHz + int normalFreq = 0; // MHz bool isLowPower = false; public: + static constexpr int LOW_POWER_FREQ = 10; // MHz + static constexpr unsigned long IDLE_POWER_SAVING_MS = 3000; // ms + void begin(); // Control CPU frequency for power saving void setPowerSaving(bool enabled); // Setup wake up GPIO and enter deep sleep - void startDeepSleep(HalGPIO &gpio) const; + void startDeepSleep(HalGPIO& gpio) const; // Get battery percentage (range 0-100) int getBatteryPercentage() const; diff --git a/src/main.cpp b/src/main.cpp index 85e0d1b2..d5746acb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -414,8 +414,7 @@ void loop() { if (currentActivity && currentActivity->skipLoopDelay()) { yield(); // Give FreeRTOS a chance to run tasks, but return immediately } else { - static constexpr unsigned long IDLE_POWER_SAVING_MS = 3000; // 3 seconds - if (millis() - lastActivityTime >= IDLE_POWER_SAVING_MS) { + if (millis() - lastActivityTime >= HalPowerManager::IDLE_POWER_SAVING_MS) { // If we've been inactive for a while, increase the delay to save power powerManager.setPowerSaving(true); // Lower CPU frequency after extended inactivity delay(50);