#include <M5Unified.h>
static int32_t batteryLevel;
static int16_t batteryVoltage; 
static int32_t oldBatteryLevel;
static int16_t oldBatteryVoltage;
void setup() {
  // put your setup code here, to run once:
  M5.begin();
  M5.Display.setRotation(0);
  M5.Display.setFont(&fonts::FreeMonoBold18pt7b);
  M5.Display.setEpdMode(epd_text);  // epd_quality, epd_text, epd_fast, epd_fastest
  M5.Display.clear();

  M5.Display.setCursor(0, 200);
  M5.Display.println("     Paper Realtime");
  M5.Display.println("     Battery Status\n\n");
  M5.Display.println(" Battery   Level:\n");
  M5.Display.println(" Battery Voltage:\n");
  batteryLevel = M5.Power.getBatteryLevel();
  batteryVoltage = M5.Power.getBatteryVoltage(); 
  oldBatteryLevel = -1;
  oldBatteryVoltage = -1; 
}

void loop() {
  M5.update();
  if (batteryLevel != oldBatteryLevel){
    M5.Display.setCursor(360, 340);
    M5.Display.printf(" %3d %%", batteryLevel);
  }
  if (batteryVoltage != oldBatteryVoltage) {
    M5.Display.setCursor(360, 410);
    M5.Display.printf("%4d mV", batteryVoltage);
  }
  oldBatteryLevel = batteryLevel;
  oldBatteryVoltage = batteryVoltage;
  delay(2000);
}