
Arduino入門
CoreP4X のバッテリーと充電状態に関する API とサンプルプログラムです。
3.3.10-aM5CoreP4X0.3.10.3.1CoreP4X は M5PM1 を介してバッテリー電圧と VBUS 電圧を取得します。充電状態は M5IOE1 IO6 (CHG_STAT) から読み取り、Low で充電中を表します。充電電流の設定は M5PM1 PYG1 で制御し、デフォルトは 180mA、出力が Low のときは 650mA です。
#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);
}画面にはバッテリーアイコン、残量、バッテリー電圧、VBUS 電圧、現在の充電電流リミット、充電状態が表示されます。充電中は外枠が点滅し、残量バーが緑色で表示されます。シリアルモニターにもバッテリー電圧、残量、充電レベル、充電状態、VBUS 電圧が出力されます。
PYG1 の現在の設定を表す充電電流リミットであり、リアルタイムに測定した充電電流ではありません。
CoreP4X のバッテリー状態は M5Unified ライブラリの Power_Class を使用します。その他の API については以下のドキュメントを参照してください: