
M5Stackには標準でリポバッテリーが内蔵されている。あまり容量は多くない上かえって動作の邪魔になる事もある。
バッテリー駆動していると気になるのがバッテリー残量である。下記掲示板によると、M5Stackの製造がFireモデルより後であればバッテリー残量が大まかな4段階であるが取得できる様だ。
https://github.com/m5stack/M5Stack/issues/74
手持ちのM5Stackの製造を見ると2018.03となっており、上記掲示板のスケッチで残量らしきデータが取得できた。

これをベースに、少しグラフィカルに残量を表示する様にしたスケッチが以下の通り。25%以下になると赤色表示になる。
#include <M5Stack.h> #define RGB(r,g,b) (int16_t)(b + (g << 5) + (r << 11)) //色0~31 緑0~63 unsigned long currentMillis; unsigned long lastupdateMillis = 0; int lastbattery = -1; void displayBatteryLevel() { if (currentMillis - lastupdateMillis > 1000) { int battlevel = 0; byte retval; Wire.beginTransmission(0x75); Wire.write(0x78); if (Wire.endTransmission(false) == 0 && Wire.requestFrom(0x75, 1)) { retval = Wire.read() & 0xF0; if (retval == 0xE0) battlevel = 25; else if (retval == 0xC0) battlevel = 50; else if (retval == 0x80) battlevel = 75; else if (retval == 0x00) battlevel = 100; } if (lastbattery != battlevel){ M5.Lcd.fillRect(250, 5, 56, 21, RGB(31, 63, 31)); M5.Lcd.fillRect(306, 9, 4, 13, RGB(31, 63, 31)); M5.Lcd.fillRect(252, 7, 52, 17, RGB(0, 0, 0)); if (battlevel <= 25) M5.Lcd.fillRect(253, 8, battlevel/2, 15, RGB(31, 20, 10)); else M5.Lcd.fillRect(253, 8, battlevel/2, 15, RGB(20, 40, 31)); lastbattery = battlevel; } lastupdateMillis = currentMillis; } } void setup() { M5.begin(); Wire.begin(); M5.Lcd.clear(); } void loop() { currentMillis = millis(); displayBatteryLevel(); }
2020.02.24 スケッチにミスがあったので修正。