power_manager.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef __POWER_MANAGER_H__
  2. #define __POWER_MANAGER_H__
  3. #include <driver/gpio.h>
  4. #include <esp_adc/adc_oneshot.h>
  5. #include <esp_log.h>
  6. #include <esp_timer.h>
  7. class PowerManager {
  8. private:
  9. // 电池电量区间-分压电阻为2个100k
  10. static constexpr struct {
  11. uint16_t adc;
  12. uint8_t level;
  13. } BATTERY_LEVELS[] = {{2150, 0}, {2450, 100}};
  14. static constexpr size_t BATTERY_LEVELS_COUNT = 2;
  15. static constexpr size_t ADC_VALUES_COUNT = 10;
  16. esp_timer_handle_t timer_handle_ = nullptr;
  17. gpio_num_t charging_pin_;
  18. adc_unit_t adc_unit_;
  19. adc_channel_t adc_channel_;
  20. uint16_t adc_values_[ADC_VALUES_COUNT];
  21. size_t adc_values_index_ = 0;
  22. size_t adc_values_count_ = 0;
  23. uint8_t battery_level_ = 100;
  24. bool is_charging_ = false;
  25. adc_oneshot_unit_handle_t adc_handle_;
  26. void CheckBatteryStatus() {
  27. is_charging_ = gpio_get_level(charging_pin_) == 0;
  28. ReadBatteryAdcData();
  29. }
  30. void ReadBatteryAdcData() {
  31. int adc_value;
  32. ESP_ERROR_CHECK(adc_oneshot_read(adc_handle_, adc_channel_, &adc_value));
  33. adc_values_[adc_values_index_] = adc_value;
  34. adc_values_index_ = (adc_values_index_ + 1) % ADC_VALUES_COUNT;
  35. if (adc_values_count_ < ADC_VALUES_COUNT) {
  36. adc_values_count_++;
  37. }
  38. uint32_t average_adc = 0;
  39. for (size_t i = 0; i < adc_values_count_; i++) {
  40. average_adc += adc_values_[i];
  41. }
  42. average_adc /= adc_values_count_;
  43. CalculateBatteryLevel(average_adc);
  44. // ESP_LOGI("PowerManager", "ADC值: %d 平均值: %ld 电量: %u%%", adc_value, average_adc,
  45. // battery_level_);
  46. }
  47. void CalculateBatteryLevel(uint32_t average_adc) {
  48. if (average_adc <= BATTERY_LEVELS[0].adc) {
  49. battery_level_ = 0;
  50. } else if (average_adc >= BATTERY_LEVELS[BATTERY_LEVELS_COUNT - 1].adc) {
  51. battery_level_ = 100;
  52. } else {
  53. float ratio = static_cast<float>(average_adc - BATTERY_LEVELS[0].adc) /
  54. (BATTERY_LEVELS[1].adc - BATTERY_LEVELS[0].adc);
  55. battery_level_ = ratio * 100;
  56. }
  57. }
  58. public:
  59. PowerManager(gpio_num_t charging_pin, adc_unit_t adc_unit = ADC_UNIT_2,
  60. adc_channel_t adc_channel = ADC_CHANNEL_3)
  61. : charging_pin_(charging_pin), adc_unit_(adc_unit), adc_channel_(adc_channel) {
  62. gpio_config_t io_conf = {};
  63. io_conf.intr_type = GPIO_INTR_DISABLE;
  64. io_conf.mode = GPIO_MODE_INPUT;
  65. io_conf.pin_bit_mask = (1ULL << charging_pin_);
  66. io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
  67. io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
  68. gpio_config(&io_conf);
  69. esp_timer_create_args_t timer_args = {
  70. .callback =
  71. [](void* arg) {
  72. PowerManager* self = static_cast<PowerManager*>(arg);
  73. self->CheckBatteryStatus();
  74. },
  75. .arg = this,
  76. .dispatch_method = ESP_TIMER_TASK,
  77. .name = "battery_check_timer",
  78. .skip_unhandled_events = true,
  79. };
  80. ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle_));
  81. ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle_, 1000000)); // 1秒
  82. InitializeAdc();
  83. }
  84. void InitializeAdc() {
  85. adc_oneshot_unit_init_cfg_t init_config = {
  86. .unit_id = adc_unit_,
  87. .ulp_mode = ADC_ULP_MODE_DISABLE,
  88. };
  89. ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle_));
  90. adc_oneshot_chan_cfg_t chan_config = {
  91. .atten = ADC_ATTEN_DB_12,
  92. .bitwidth = ADC_BITWIDTH_12,
  93. };
  94. ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle_, adc_channel_, &chan_config));
  95. }
  96. ~PowerManager() {
  97. if (timer_handle_) {
  98. esp_timer_stop(timer_handle_);
  99. esp_timer_delete(timer_handle_);
  100. }
  101. if (adc_handle_) {
  102. adc_oneshot_del_unit(adc_handle_);
  103. }
  104. }
  105. bool IsCharging() { return is_charging_; }
  106. uint8_t GetBatteryLevel() { return battery_level_; }
  107. };
  108. #endif // __POWER_MANAGER_H__