pdf-icon

Arduino入門

2. デバイス&サンプル

6. アプリケーション

Cardputer Battery 状態

Cardputer のバッテリー状態に関する API とサンプルプログラムです。Cardputer および Cardputer-Adv の両方に対応しています。

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャのバージョン >= 3.2.3
  • ボードの選択 = M5Cardputer
  • M5Cardputer ライブラリのバージョン >= 1.1.1
  • M5Unified ライブラリのバージョン >= 0.2.10
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
#include <M5Cardputer.h>

void setup() {
  M5Cardputer.begin();
  M5Cardputer.Display.setFont(&fonts::FreeMonoBold9pt7b);
  M5Cardputer.Display.setCursor(0, 0);

  M5Cardputer.Display.print("   Cardputer (-Adv)\n");
  M5Cardputer.Display.print("    Battery Status\n\n");
  M5Cardputer.Display.print("  Percent:\n");
  M5Cardputer.Display.print("  Voltage:\n");
}

void loop() {
  M5Cardputer.update();

  bool isCharging = M5Cardputer.Power.isCharging();
  int batteryLevel = M5Cardputer.Power.getBatteryLevel();      // 0 - 100 %
  int batteryVoltage = M5Cardputer.Power.getBatteryVoltage();  // unit: mV

  M5Cardputer.Display.setCursor(120, 55);
  M5Cardputer.Display.printf("%3d %%", batteryLevel);
  M5Cardputer.Display.setCursor(120, 72);
  M5Cardputer.Display.printf("%4d mV", batteryVoltage);

  delay(1000);
}

このプログラムは、画面上にバッテリー残量(パーセンテージ)とバッテリー電圧を表示し、1 秒ごとに更新します。

ハードウェアの制限により、Cardputer および Cardputer-Adv はバッテリーの充電状態や電流情報を取得することができません。
また、Cardputer または Cardputer-Adv をパソコンや電源に接続する際は、上面のスイッチをオンにしなければ充電されません。スイッチがオフのままではバッテリーが切り離され、外部電源で動作します。

API

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

On This Page