2020-07-28 15:36:55 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
* M5Snake : Power management *
|
|
|
|
|
* -------------------------- *
|
|
|
|
|
* Management of the charge of the battery to avoid over charging *
|
|
|
|
|
* Author: Olivier Staquet *
|
|
|
|
|
* Last version available on https://github.com/ostaquet/M5Snake *
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
#include "Power.h"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize
|
|
|
|
|
*/
|
|
|
|
|
void PowerClass::begin() {
|
2022-07-06 14:21:26 +08:00
|
|
|
// Initialize the power management module (based on IP5306 chip)
|
|
|
|
|
M5.Power.begin();
|
2020-07-28 15:36:55 +08:00
|
|
|
|
2022-07-06 14:21:26 +08:00
|
|
|
// Check if the control can be operated
|
|
|
|
|
canControl = M5.Power.canControl();
|
2020-07-28 15:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adapt charging mode to avoid excessive charging
|
|
|
|
|
*/
|
|
|
|
|
void PowerClass::adaptChargeMode() {
|
2022-07-06 14:21:26 +08:00
|
|
|
// If power management not available, ignore the routine
|
|
|
|
|
if (!canControl) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-28 15:36:55 +08:00
|
|
|
|
2022-07-06 14:21:26 +08:00
|
|
|
// Disable the charging if the battery is fully charged
|
|
|
|
|
if (M5.Power.isChargeFull()) {
|
|
|
|
|
M5.Power.setCharge(false);
|
|
|
|
|
} else {
|
|
|
|
|
M5.Power.setCharge(true);
|
|
|
|
|
}
|
2020-07-28 15:36:55 +08:00
|
|
|
|
2022-07-06 14:21:26 +08:00
|
|
|
// Define the shutdown time at 64s
|
|
|
|
|
M5.Power.setLowPowerShutdownTime(M5.Power.ShutdownTime::SHUTDOWN_64S);
|
2020-07-28 15:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return battery level (0-100%)
|
|
|
|
|
* (-1 if cannot communicate with the controller)
|
|
|
|
|
*/
|
2022-08-11 17:01:46 +08:00
|
|
|
int8_t PowerClass::getBatteryLevel() {
|
|
|
|
|
return M5.Power.getBatteryLevel();
|
|
|
|
|
}
|
2020-07-28 15:36:55 +08:00
|
|
|
|
|
|
|
|
PowerClass Power;
|