-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpowerManager.cpp
47 lines (37 loc) · 1.12 KB
/
powerManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "powerManager.h"
#include "userBoardDefines.h"
#ifdef M5STACK
#include <M5StickCPlus.h>
#endif
#ifdef GENERIC_ESP32
#include <Arduino.h>
#endif
#include <cmath>
#include "WiFiManager.h"
#include "esp32-hal-cpu.h"
#include "esp_wifi.h"
#include "esp_wifi_types.h"
const float BATTERY_MIN_VOLTAGE = 3.06;
const float BATTERY_MAX_VOLTAGE = 4.1;
void setWiFiMaxPowerSave() { esp_wifi_set_ps(WIFI_PS_MAX_MODEM); }
void setCpuNormalClock() { setCpuFrequencyMhz(80); }
void setCpuMaxPowerSave() { setCpuFrequencyMhz(40); }
#ifdef M5STACK
bool isCharging() {
float batteryCurrent = M5.Axp.GetBatCurrent();
return batteryCurrent >= 0;
}
int getBatteryLevel() {
return calculateBatteryPercentage(M5.Axp.GetBatVoltage());
}
int calculateBatteryPercentage(float voltage) {
// https://forum.micropython.org/viewtopic.php?f=2&t=7615#p43401
int batteryPercentage =
(int)trunc((voltage - BATTERY_MIN_VOLTAGE) * (100 - 0) /
(BATTERY_MAX_VOLTAGE - BATTERY_MIN_VOLTAGE) +
0);
if (batteryPercentage > 100) return 100;
if (batteryPercentage < 0) return 0;
return batteryPercentage;
}
#endif