Arduino入門

2. デバイス&サンプル

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

アクセサリー

6. アプリケーション

ToughC5 電源管理

ToughC5 の電源管理関連 API とサンプルプログラムです。

サンプルプログラム

コンパイル要件

  • M5Stack ボードパッケージのバージョン >= 3.3.9
  • ボードオプション = M5ToughC5
  • M5Unified ライブラリのバージョン >= 0.2.22
  • M5GFX ライブラリのバージョン >= 0.2.28

ToughC5 の充電電流は M5.Power.setChargeCurrent() で設定します。この例では、低電流の 180 mA と高電流の 650 mA の 2 つの充電電流を使用します。画面に触れると 2 つの設定が切り替わり、画面に表示されるステータスが更新されます。

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include <M5Unified.h>

constexpr int CHARGE_CURRENT_LOW_MA = 180;
constexpr int CHARGE_CURRENT_HIGH_MA = 650;

int chargeCurrentMa = CHARGE_CURRENT_LOW_MA;
uint32_t lastRefreshMs = 0;

void drawStatus()
{
  const auto chargeState = M5.Power.isCharging();
  const char* chargeText =
      chargeState == m5::Power_Class::is_charging ? "Charging" :
      chargeState == m5::Power_Class::is_discharging ? "Not charging" :
      "Unknown";

  const int batteryVoltage = M5.Power.getBatteryVoltage();
  const int batteryLevel = M5.Power.getBatteryLevel();
  const int vbusVoltage = M5.Power.getVBUSVoltage();

  M5.Display.clear();
  M5.Display.setTextSize(2);

  M5.Display.setTextColor(0xFFE0);

  M5.Display.setCursor(10, 8);
  M5.Display.printf("Charge current: %d mA", chargeCurrentMa);

  M5.Display.setCursor(10, 30);
  M5.Display.printf("Current option:");

  M5.Display.setCursor(10, 51);
  M5.Display.printf("180 / 650 mA");

  M5.Display.setCursor(10, 74);
  M5.Display.printf("Touch to change");

  M5.Display.setCursor(10, 95);
  M5.Display.printf("charge level");

  M5.Display.setTextColor(0xFFFF);

  M5.Display.setCursor(10, 123);
  M5.Display.printf("Charging: %s", chargeText);

  M5.Display.setCursor(10, 148);
  if (batteryVoltage < 0) {
    M5.Display.printf("Battery: Unknown");
  } else if (batteryVoltage == 0) {
    M5.Display.printf("Battery: No battery");
  } else {
    M5.Display.printf("Battery: %d mV", batteryVoltage);
  }

  M5.Display.setCursor(10, 173);
  if (batteryLevel < 0) {
    M5.Display.printf("Level: Unknown");
  } else {
    M5.Display.printf("Level: %d%%", batteryLevel);
  }

  M5.Display.setCursor(10, 198);
  if (vbusVoltage < 0) {
    M5.Display.printf("VBUS: Unsupported");
  } else {
    M5.Display.printf("VBUS: %d mV", vbusVoltage);
  }
}

void setup()
{
  auto cfg = M5.config();

  // If powered from the Grove port or DC input, set this to false as needed.
  // cfg.output_power = false;

  M5.begin(cfg);
  M5.Power.setChargeCurrent(chargeCurrentMa);

  drawStatus();
  lastRefreshMs = millis();
}

void loop()
{
  M5.update();

  bool redraw = false;

  if (M5.Touch.getCount() > 0 && M5.Touch.getDetail(0).wasPressed()) {
    chargeCurrentMa = (chargeCurrentMa == CHARGE_CURRENT_LOW_MA)
        ? CHARGE_CURRENT_HIGH_MA
        : CHARGE_CURRENT_LOW_MA;

    M5.Power.setChargeCurrent(chargeCurrentMa);
    redraw = true;
  }

  const uint32_t now = millis();
  if (redraw || now - lastRefreshMs >= 1000) {
    drawStatus();
    lastRefreshMs = now;
  }
}

起動後、プログラムは充電電流をデフォルトの 180 mA に設定し、充電状態、バッテリー電圧、バッテリー残量、VBUS 電圧を表示します。画面に 1 回触れると、もう一方の充電電流に切り替わり、画面がすぐに更新されます。そのほかのステータス情報も 1 秒ごとに更新されます。

API

ToughC5 のバッテリー状態関連の機能では、M5Unified ライブラリの Power_Class を使用します。詳しい API については、以下のドキュメントを参照してください。

Page Tools
PDF
On This Page