power_save_timer.h 859 B

123456789101112131415161718192021222324252627282930313233
  1. #pragma once
  2. #include <functional>
  3. #include <esp_timer.h>
  4. #include <esp_pm.h>
  5. class PowerSaveTimer {
  6. public:
  7. PowerSaveTimer(int cpu_max_freq, int seconds_to_sleep = 20, int seconds_to_shutdown = -1);
  8. ~PowerSaveTimer();
  9. void SetEnabled(bool enabled);
  10. void OnEnterSleepMode(std::function<void()> callback);
  11. void OnExitSleepMode(std::function<void()> callback);
  12. void OnShutdownRequest(std::function<void()> callback);
  13. void WakeUp();
  14. private:
  15. void PowerSaveCheck();
  16. esp_timer_handle_t power_save_timer_ = nullptr;
  17. bool enabled_ = false;
  18. bool in_sleep_mode_ = false;
  19. int ticks_ = 0;
  20. int cpu_max_freq_;
  21. int seconds_to_sleep_;
  22. int seconds_to_shutdown_;
  23. std::function<void()> on_enter_sleep_mode_;
  24. std::function<void()> on_exit_sleep_mode_;
  25. std::function<void()> on_shutdown_request_;
  26. };