| 26 | } |
| 27 | |
| 28 | void HalPowerManager::setPowerSaving(bool enabled) { |
| 29 | if (normalFreq <= 0) { |
| 30 | return; // invalid state |
| 31 | } |
| 32 | |
| 33 | auto wifiMode = WiFi.getMode(); |
| 34 | if (wifiMode != WIFI_MODE_NULL) { |
| 35 | // Wifi is active, force disabling power saving |
| 36 | enabled = false; |
| 37 | } |
| 38 | |
| 39 | // Note: We don't use mutex here to avoid too much overhead, |
| 40 | // it's not very important if we read a slightly stale value for currentLockMode |
| 41 | const LockMode mode = currentLockMode; |
| 42 | |
| 43 | if (mode == None && enabled && !isLowPower) { |
| 44 | LOG_DBG("PWR", "Going to low-power mode"); |
| 45 | if (!setCpuFrequencyMhz(LOW_POWER_FREQ)) { |
| 46 | LOG_DBG("PWR", "Failed to set CPU frequency = %d MHz", LOW_POWER_FREQ); |
| 47 | return; |
| 48 | } |
| 49 | isLowPower = true; |
| 50 | |
| 51 | } else if ((!enabled || mode != None) && isLowPower) { |
| 52 | LOG_DBG("PWR", "Restoring normal CPU frequency"); |
| 53 | if (!setCpuFrequencyMhz(normalFreq)) { |
| 54 | LOG_DBG("PWR", "Failed to set CPU frequency = %d MHz", normalFreq); |
| 55 | return; |
| 56 | } |
| 57 | isLowPower = false; |
| 58 | } |
| 59 | |
| 60 | // Otherwise, no change needed |
| 61 | } |
| 62 | |
| 63 | void HalPowerManager::startDeepSleep(HalGPIO& gpio) const { |
| 64 | // Ensure that the power button has been released to avoid immediately turning back on if you're holding it |