APIs and example programs related to the battery and charging status of CoreP4X.
3.3.10-aM5CoreP4X0.3.10.3.1CoreP4X samples the battery and VBUS voltage through M5PM1. The charging status is read from M5IOE1 IO6 (CHG_STAT); low indicates charging. The charge current level is controlled by M5PM1 PYG1: 180mA by default, and 650mA when the output is low.
#include <M5Unified.h>
static const char* chargeStateText(int state)
{
// 0: not charging, 1: charging, 2: unknown
switch (state) {
case 0: return "Not charging";
case 1: return "Charging";
default: return "Unknown";
}
}
static int readChargeLimit(void)
{
uint8_t gpioBits = 0;
if (!M5.Power.M5pm1.getGPIOInputBits(&gpioBits)) {
return 0;
}
// PYG1 high-Z: 180mA / PYG1 low: 650mA
return (gpioBits & (1U << 1)) ? 180 : 650;
}
static void drawBattery(int level, int voltageMv, int vbusMv,
int chargeMa, int state)
{
const bool charging = state == 1;
const int centerX = M5.Display.width() / 2;
M5.Display.startWrite();
M5.Display.fillScreen(TFT_BLACK);
M5.Display.setTextDatum(middle_center);
M5.Display.setFont(&fonts::FreeSansBold24pt7b);
M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Display.drawString("Battery Status", centerX, 48);
// The body and terminal together are centered on the 480 x 480 display.
M5.Display.drawRect(151, 96, 160, 78, TFT_WHITE);
M5.Display.fillRect(315, 120, 14, 30, TFT_WHITE);
const int width = constrain(level, 0, 100) * 154 / 100;
if (width > 0) {
M5.Display.fillRect(155, 100, width, 70, charging ? TFT_GREEN : TFT_WHITE);
}
if (charging && (millis() / 400) % 2 == 0) {
M5.Display.drawRect(145, 90, 190, 90, TFT_GREEN);
}
char line[48];
M5.Display.setFont(&fonts::FreeSans18pt7b);
snprintf(line, sizeof(line), "Level: %d%%", level);
M5.Display.drawString(line, centerX, 210);
snprintf(line, sizeof(line), "Voltage: %d mV", voltageMv);
M5.Display.drawString(line, centerX, 258);
snprintf(line, sizeof(line), "VBUS: %d mV", vbusMv);
M5.Display.drawString(line, centerX, 306);
if (chargeMa > 0) {
snprintf(line, sizeof(line), "Charge limit: %d mA", chargeMa);
} else {
snprintf(line, sizeof(line), "Charge limit: Unknown");
}
M5.Display.setTextColor(TFT_CYAN, TFT_BLACK);
M5.Display.drawString(line, centerX, 354);
snprintf(line, sizeof(line), "State: %s", chargeStateText(state));
M5.Display.setTextColor(charging ? TFT_GREEN : TFT_WHITE, TFT_BLACK);
M5.Display.drawString(line, centerX, 402);
M5.Display.endWrite();
}
void setup()
{
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
M5.Display.setBrightness(128);
const int level = M5.Power.getBatteryLevel();
const int voltageMv = M5.Power.getBatteryVoltage();
const int vbusMv = M5.Power.getVBUSVoltage();
const int chargeMa = readChargeLimit();
const int state = static_cast<int>(M5.Power.isCharging());
drawBattery(level, voltageMv, vbusMv, chargeMa, state);
}
void loop()
{
M5.update();
static uint32_t lastRefresh = 0;
const uint32_t now = millis();
if (now - lastRefresh >= 1000) {
lastRefresh = now;
const int level = M5.Power.getBatteryLevel();
const int voltageMv = M5.Power.getBatteryVoltage();
const int vbusMv = M5.Power.getVBUSVoltage();
const int chargeMa = readChargeLimit();
// Charge status is read from M5IOE1 IO6.
const int state = static_cast<int>(M5.Power.isCharging());
drawBattery(level, voltageMv, vbusMv, chargeMa, state);
Serial.printf("Battery: %d mV, level: %d%%, state: %s\n",
voltageMv, level, chargeStateText(state));
Serial.printf("VBUS: %d mV, charge limit: ", vbusMv);
if (chargeMa > 0) {
Serial.printf("%d mA\n", chargeMa);
} else {
Serial.println("unknown");
}
}
delay(20);
}The screen shows the battery icon, remaining level, battery voltage, VBUS voltage, the current charge current limit level, and the charging state; while charging, the frame blinks and the level bar is displayed in green. The serial monitor also outputs the battery voltage, level, charge level, charging state, and VBUS voltage.
PYG1 and represents the charge current limit setting, not a real-time measurement of the charging current.
The battery status section of CoreP4X uses the Power_Class in the M5Unified library. For more related APIs, refer to the documentation below: