m5stackで組込み!!

Arduinoによるm5stack開発のいろいろと...

充電状態の表示

今回は充電状態を表示をしてみましょう。

初期化

初期化に関しては前々回の電池残量を表示したときの記事を参考にしてください。
m5stack-build.hatenablog.com

充電状態を取得するAPI

充電状態を取得する関数はこちらです。戻り値にtrueが返ってきたら充電中です。

  M5.Power.isCharging();

サンプルコード

#include <M5Stack.h>

#define STS_OFF 0
#define STS_ON  1

static unsigned char gucChargingSts = STS_OFF;
static int giBattery = 0;
static int giBatteryOld = 0xFF;

void setup() {
  M5.begin();
  M5.Power.begin();
  if(!M5.Power.canControl()) {
    //can't control.
    M5.Lcd.print("NG");
    return;
  }
  else
  {
    //M5.Lcd.print("OK");
  }
}
void loop() {

  giBattery = M5.Power.getBatteryLevel();
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.printf("%3d \%",giBattery);

  // 充電中
  if( true == M5.Power.isCharging() )
  {
    // 充電初回
    if( STS_OFF == gucChargingSts )
    {
      // 電池へそ
      M5.Lcd.fillRoundRect(40,75,25,50,5,WHITE);
      // 大外枠
      M5.Lcd.fillRoundRect(60,50,200,100,5,WHITE);
      // 中枠
      M5.Lcd.fillRoundRect(70,60,180,80,5,BLACK);
      // 充電マーク
      M5.Lcd.fillTriangle(160, 65, 140, 110, 160, 110, WHITE);
      M5.Lcd.fillTriangle(160, 135, 180, 90, 160, 90, WHITE);
      // 充電ON
      gucChargingSts = STS_ON;
    }
  }
  else
  {
    if( giBatteryOld != giBattery )
    {
      // 電池へそ
      M5.Lcd.fillRoundRect(40,75,25,50,5,WHITE);
      // 大外枠
      M5.Lcd.fillRoundRect(60,50,200,100,5,WHITE);
      // 10%以下
      if( giBattery <= 10 )
      {
        M5.Lcd.fillRect(210,55,40,90,RED);
      }
      else
      {
        M5.Lcd.fillRect(210,55,40,90,BLACK);
      }
      // 50%以上
      if( giBattery >= 50 )
      {
        M5.Lcd.fillRect(165,55,40,90,BLACK);
      }
      // 75%以上
      if( giBattery >= 75 )
      {
        M5.Lcd.fillRect(120,55,40,90,BLACK);
      }
      // 90%以上
      if( giBattery >= 90 )
      {
        M5.Lcd.fillRect(75,55,40,90,BLACK);
      }
    }
    // 前回値更新
    giBatteryOld = giBattery;
    // 充電OFF
    gucChargingSts = STS_OFF;
  }
}

マークがかわいい