pdf-icon

Arduino入門

2. デバイス&サンプル

Fire Battery

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

サンプルプログラム

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
#include "M5Unified.h"

void setup()
{
    auto cfg = M5.config();
    M5.begin(cfg);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextFont(&fonts::Orbitron_Light_24);
    M5.Display.setTextSize(1);
}

void loop()
{
    M5.Display.clear();
    
    bool isCharging = M5.Power.isCharging();
    int vol_per = M5.Power.getBatteryLevel();

    M5.Display.setCursor(0, 30);
    M5.Display.printf("Charging: %s \n\n", isCharging ? "Yes" : "No");
    M5.Display.setCursor(0, 60);
    M5.Display.printf("BAT: %d%%", vol_per);
    delay(5000);
}      

そのプログラムは、バッテリーが充電中かどうか、充電パーセンテージを画面に表示し、5秒ごとに更新します。ハードウェアの制限により、Fireはバッテリーの電圧や電流情報を読み取ることができません。

API

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

On This Page