Arduino入門
Core2のバッテリー状態読み取りに関するAPIとサンプルプログラム。
#include "M5Unified.h"
void setup()
{
auto cfg = M5.config();
M5.begin(cfg);
M5.Display.setTextDatum(middle_center);
M5.Display.setTextColor(TFT_BLACK);
M5.Display.setTextFont(&fonts::Orbitron_Light_24);
M5.Display.setTextSize(1);
}
void loop()
{
M5.Display.clear(TFT_WHITE);
bool isCharging = M5.Power.isCharging();
// Set LED based on charging status
if (isCharging) {
M5.Power.setLed(255); // Set LED to light when charging
} else {
M5.Power.setLed(0);
}
int vol_per = M5.Power.getBatteryLevel();
int vol = M5.Power.getBatteryVoltage();
int cur = M5.Power.getBatteryCurrent();
M5.Display.setCursor(0, 30);
M5.Display.printf("Charging: %s \n\n", isCharging ? "Yes" : "No");
M5.Display.setCursor(0, 60);
M5.Display.printf("Bat_level: %d%%", vol_per);
M5.Display.setCursor(0, 90);
M5.Display.printf("Bat_voltage: %d%mV", vol);
M5.Display.setCursor(0, 120);
M5.Display.printf("Bat_current: %d%mA", cur);
delay(2000);
}
このプログラムは、充電状態、バッテリーパーセンテージ、電圧、電流を画面に表示し、2秒ごとに更新します。
Core2の電源部分はM5UnifiedライブラリのPower_Class
を使用しています。関連APIについては以下のドキュメントを参照してください: