Power.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /******************************************************************************
  2. * M5Snake : Power management *
  3. * -------------------------- *
  4. * Management of the charge of the battery to avoid over charging *
  5. * Author: Olivier Staquet *
  6. * Last version available on https://github.com/ostaquet/M5Snake *
  7. *****************************************************************************/
  8. #include "Power.h"
  9. /**
  10. * Initialize
  11. */
  12. void PowerClass::begin() {
  13. // Initialize the power management module (based on IP5306 chip)
  14. M5.Power.begin();
  15. // Check if the control can be operated
  16. canControl = M5.Power.canControl();
  17. }
  18. /**
  19. * Adapt charging mode to avoid excessive charging
  20. */
  21. void PowerClass::adaptChargeMode() {
  22. // If power management not available, ignore the routine
  23. if (!canControl) {
  24. return;
  25. }
  26. // Disable the charging if the battery is fully charged
  27. if (M5.Power.isChargeFull()) {
  28. M5.Power.setCharge(false);
  29. } else {
  30. M5.Power.setCharge(true);
  31. }
  32. // Define the shutdown time at 64s
  33. M5.Power.setLowPowerShutdownTime(M5.Power.ShutdownTime::SHUTDOWN_64S);
  34. }
  35. /**
  36. * Return battery level (0-100%)
  37. * (-1 if cannot communicate with the controller)
  38. */
  39. int8_t PowerClass::getBatteryLevel() { return M5.Power.getBatteryLevel(); }
  40. PowerClass Power;