pdf-icon

Arduino入門

2. デバイス&サンプル

PaperS3 Battery バッテリー状態

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

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャのバージョン >= 2.1.4
  • 使用するボード = M5PaperS3
  • M5Unified ライブラリのバージョン >= 0.2.5
  • M5GFX ライブラリのバージョン >= 0.2.7
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
#include <M5Unified.h>
#include <M5GFX.h>
void setup() {
M5.begin();
M5.Display.setRotation(0);
M5.Display.setFont(&fonts::FreeMonoBold18pt7b);
M5.Display.setCursor(0, 200);
M5.Display.print(" PaperS3 Realtime\n");
M5.Display.print(" Battery Status\n\n\n");
M5.Display.print(" Battery Charging:\n\n");
M5.Display.print(" Battery Level:\n\n");
M5.Display.print(" Battery Voltage:\n\n");
}
void loop() {
M5.update();
bool isCharging = M5.Power.isCharging();
int32_t batteryLevel = M5.Power.getBatteryLevel(); // 0 - 100 %
int16_t batteryVoltage = M5.Power.getBatteryVoltage(); // unit: mV
M5.Display.setCursor(380, 340);
M5.Display.printf("%s \n\n", isCharging ? "Yes" : "No");
M5.Display.setCursor(380, 410);
M5.Display.printf("%d %% \n\n", batteryLevel);
M5.Display.setCursor(380, 480);
M5.Display.printf("%d mV \n\n", batteryVoltage);
delay(2000);
}

このプログラムは、バッテリーの充電状態、電池残量(パーセンテージ)、電圧を画面に表示し、2秒ごとに更新します。ハードウェアの制限により、PaperS3 はバッテリー電流を読み取ることができません。

API

PaperS3 のバッテリー状態部分では、M5Unified ライブラリの Power_Class を使用しています。詳細は以下のドキュメントをご参照ください:

On This Page