Arduino入門

2. デバイス&サンプル

5. 拡張モジュール&サンプル

アクセサリー

6. アプリケーション

CoreP4X Battery バッテリー状態

CoreP4X のバッテリーと充電状態に関する API とサンプルプログラムです。

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャーバージョン >= 3.3.10-a
  • ボード選択 = M5CoreP4X
  • M5Unified ライブラリバージョン >= 0.3.1
  • M5GFX ライブラリバージョン >= 0.3.1

CoreP4X は M5PM1 を介してバッテリー電圧と VBUS 電圧を取得します。充電状態は M5IOE1 IO6 (CHG_STAT) から読み取り、Low で充電中を表します。充電電流の設定は M5PM1 PYG1 で制御し、デフォルトは 180mA、出力が Low のときは 650mA です。

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#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 電圧が出力されます。

説明
ここでの充電レベルは M5PM1 PYG1 の現在の設定を表す充電電流リミットであり、リアルタイムに測定した充電電流ではありません。

API

CoreP4X のバッテリー状態は M5Unified ライブラリの Power_Class を使用します。その他の API については以下のドキュメントを参照してください:

Page Tools
PDF
On This Page