pdf-icon

Arduino入門

2. デバイス&サンプル

Core2 Battery

Core2のバッテリー状態読み取りに関する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 25 26 27 28 29 30 31 32 33 34 35 36 37
#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秒ごとに更新します。

API

Core2の電源部分はM5UnifiedライブラリのPower_Classを使用しています。関連APIについては以下のドキュメントを参照してください:

On This Page