So with a bit of research, I develop this simple code that makes exactly what I was asking here:
#include <M5Core2.h>
#define DEVICE_TIMEOUT 120000
float Time_bat = 0;
void setup{
M5.begin();
M5.Axp.SetBusPowerMode(1);
}
void loop{
M5.Lcd.drawString(("Battery: " + String(M5.Axp.GetBatteryLevel())), 0, 120, 2);
if(!M5.Axp.isVBUS() && !M5.Axp.isCharging()){
M5.Lcd.drawString(String((millis() - Time_bat)/1000,0) + " ", 1, 154, 1);
}else{M5.Lcd.drawString("Feeded ", 1, 154, 1);}
if(!M5.Axp.isVBUS() && !M5.Axp.isCharging()){
if(Time_bat == 0){Time_bat = millis();}
if((millis() - Time_bat) > DEVICE_TIMEOUT){M5.shutdown();}
}else{
Time_bat = 0;
}
}
Hope it helps someone!