Arduino入門

2. デバイス&サンプル

5. 拡張モジュール&サンプル

アクセサリー

6. アプリケーション

StickC-Plus Battery バッテリー

StickCPlusのバッテリー状態読み取りに関するAPIとサンプルプログラムです。

サンプルプログラム

ビルド要件

  • M5Stackボードパッケージ バージョン >= 3.3.8
  • ボード選択 = M5StickCPlus
  • M5Unifiedライブラリ バージョン >= 0.2.18
  • M5GFXライブラリ バージョン >= 0.2.24
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 38 39
#include <M5Unified.h>

void setup() {
    M5.begin();

    M5.Power.setBatteryCharge(true);

    M5.Display.setRotation(1);
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setTextFont(2);
    M5.Display.setTextSize(1);
    M5.Display.setTextColor(TFT_GREEN, TFT_BLACK);
}

void loop() {
    M5.update();

    const int32_t level = M5.Power.getBatteryLevel();
    const int32_t voltage = M5.Power.getBatteryVoltage();
    const int32_t current = M5.Power.getBatteryCurrent();

    M5.Display.setCursor(8, 6);
    M5.Display.print("BATTERY STATUS");

    M5.Display.setCursor(8, 32);
    M5.Display.printf("CHARGE : %-3s",
                      M5.Power.isCharging() ? "YES" : "NO");

    M5.Display.setCursor(8, 58);
    M5.Display.printf("BATTERY: %3d %%   ", level);

    M5.Display.setCursor(8, 84);
    M5.Display.printf("VOLTAGE: %5d mV", voltage);

    M5.Display.setCursor(8, 110);
    M5.Display.printf("CURRENT: %5d mA", current);

    delay(2000);
}

このプログラムはバッテリーの充電状態、残量パーセント、電圧(mV)、電流(mA)を画面に表示し、約2秒ごとに更新します。

API

StickC Plusの電源機能はM5UnifiedライブラリのPower_Classを使用しています。関連APIの詳細については、以下のドキュメントを参照してください:

Page Tools
PDF
On This Page