change cpu freq on idle
This commit is contained in:
@@ -274,6 +274,7 @@ float CrossPointSettings::getReaderLineCompression() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned long CrossPointSettings::getSleepTimeoutMs() const {
|
unsigned long CrossPointSettings::getSleepTimeoutMs() const {
|
||||||
|
return 12UL * 60 * 60 * 1000; // TEST: 12 hours
|
||||||
switch (sleepTimeout) {
|
switch (sleepTimeout) {
|
||||||
case SLEEP_1_MIN:
|
case SLEEP_1_MIN:
|
||||||
return 1UL * 60 * 1000;
|
return 1UL * 60 * 1000;
|
||||||
|
|||||||
35
src/main.cpp
35
src/main.cpp
@@ -276,6 +276,37 @@ void setupDisplayAndFonts() {
|
|||||||
Serial.printf("[%lu] [ ] Fonts setup\n", millis());
|
Serial.printf("[%lu] [ ] Fonts setup\n", millis());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FOR TESTING ONLY
|
||||||
|
static bool isLowerFreq = false;
|
||||||
|
static int normalFreq = 160; // MHz
|
||||||
|
class HalPowerManager {
|
||||||
|
public:
|
||||||
|
static void setCpuFrequency(bool lower) {
|
||||||
|
bool changed = false;
|
||||||
|
if (lower && !isLowerFreq) {
|
||||||
|
bool success = setCpuFrequencyMhz(10);
|
||||||
|
if (!success) {
|
||||||
|
Serial.printf("[%lu] [PWR] Failed to set CPU frequency to 10 MHz\n", millis());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
isLowerFreq = true;
|
||||||
|
changed = true;
|
||||||
|
} else if (!lower && isLowerFreq) {
|
||||||
|
bool success = setCpuFrequencyMhz(normalFreq);
|
||||||
|
if (!success) {
|
||||||
|
Serial.printf("[%lu] [PWR] Failed to set CPU frequency to %d MHz\n", millis(), normalFreq);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
isLowerFreq = false;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed) {
|
||||||
|
Serial.printf("[%lu] [PWR] CPU frequency set to %u MHz\n", millis(), getCpuFrequencyMhz());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
t1 = millis();
|
t1 = millis();
|
||||||
|
|
||||||
@@ -301,6 +332,8 @@ void setup() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
normalFreq = getCpuFrequencyMhz();
|
||||||
|
|
||||||
SETTINGS.loadFromFile();
|
SETTINGS.loadFromFile();
|
||||||
KOREADER_STORE.loadFromFile();
|
KOREADER_STORE.loadFromFile();
|
||||||
UITheme::getInstance().reload();
|
UITheme::getInstance().reload();
|
||||||
@@ -371,6 +404,7 @@ void loop() {
|
|||||||
static unsigned long lastActivityTime = millis();
|
static unsigned long lastActivityTime = millis();
|
||||||
if (gpio.wasAnyPressed() || gpio.wasAnyReleased() || (currentActivity && currentActivity->preventAutoSleep())) {
|
if (gpio.wasAnyPressed() || gpio.wasAnyReleased() || (currentActivity && currentActivity->preventAutoSleep())) {
|
||||||
lastActivityTime = millis(); // Reset inactivity timer
|
lastActivityTime = millis(); // Reset inactivity timer
|
||||||
|
HalPowerManager::setCpuFrequency(false); // Set normal CPU frequency on user activity
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned long sleepTimeoutMs = SETTINGS.getSleepTimeoutMs();
|
const unsigned long sleepTimeoutMs = SETTINGS.getSleepTimeoutMs();
|
||||||
@@ -411,6 +445,7 @@ void loop() {
|
|||||||
static constexpr unsigned long IDLE_POWER_SAVING_MS = 3000; // 3 seconds
|
static constexpr unsigned long IDLE_POWER_SAVING_MS = 3000; // 3 seconds
|
||||||
if (millis() - lastActivityTime >= IDLE_POWER_SAVING_MS) {
|
if (millis() - lastActivityTime >= IDLE_POWER_SAVING_MS) {
|
||||||
// If we've been inactive for a while, increase the delay to save power
|
// If we've been inactive for a while, increase the delay to save power
|
||||||
|
HalPowerManager::setCpuFrequency(true); // Lower CPU frequency after extended inactivity
|
||||||
delay(50);
|
delay(50);
|
||||||
} else {
|
} else {
|
||||||
// Short delay to prevent tight loop while still being responsive
|
// Short delay to prevent tight loop while still being responsive
|
||||||
|
|||||||
Reference in New Issue
Block a user