power_manager.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #pragma once
  2. #include <vector>
  3. #include <functional>
  4. #include <esp_log.h>
  5. #include <esp_timer.h>
  6. #include <driver/gpio.h>
  7. #include <esp_adc/adc_oneshot.h>
  8. #include <driver/temperature_sensor.h>
  9. #include "application.h"
  10. #include "zhengchen_lcd_display.h"
  11. class PowerManager {
  12. private:
  13. // 定时器句柄
  14. esp_timer_handle_t timer_handle_;
  15. std::function<void(bool)> on_charging_status_changed_;
  16. std::function<void(bool)> on_low_battery_status_changed_;
  17. std::function<void(float)> on_temperature_changed_;
  18. gpio_num_t charging_pin_ = GPIO_NUM_NC;
  19. std::vector<uint16_t> adc_values_;
  20. uint32_t battery_level_ = 0;
  21. bool is_charging_ = false;
  22. bool is_low_battery_ = false;
  23. float current_temperature_ = 0.0f;
  24. int ticks_ = 0;
  25. const int kBatteryAdcInterval = 60;
  26. const int kBatteryAdcDataCount = 3;
  27. const int kLowBatteryLevel = 20;
  28. const int kTemperatureReadInterval = 10; // 每 10 秒读取一次温度
  29. adc_oneshot_unit_handle_t adc_handle_;
  30. temperature_sensor_handle_t temp_sensor_ = NULL;
  31. void CheckBatteryStatus() {
  32. // Get charging status
  33. bool new_charging_status = gpio_get_level(charging_pin_) == 1;
  34. if (new_charging_status != is_charging_) {
  35. is_charging_ = new_charging_status;
  36. if (on_charging_status_changed_) {
  37. on_charging_status_changed_(is_charging_);
  38. }
  39. ReadBatteryAdcData();
  40. return;
  41. }
  42. // 如果电池电量数据不足,则读取电池电量数据
  43. if (adc_values_.size() < kBatteryAdcDataCount) {
  44. ReadBatteryAdcData();
  45. return;
  46. }
  47. // 如果电池电量数据充足,则每 kBatteryAdcInterval 个 tick 读取一次电池电量数据
  48. ticks_++;
  49. if (ticks_ % kBatteryAdcInterval == 0) {
  50. ReadBatteryAdcData();
  51. }
  52. // 新增:周期性读取温度
  53. if (ticks_ % kTemperatureReadInterval == 0) {
  54. ReadTemperature();
  55. }
  56. }
  57. void ReadBatteryAdcData() {
  58. // 读取 ADC 值
  59. int adc_value;
  60. ESP_ERROR_CHECK(adc_oneshot_read(adc_handle_, ADC_CHANNEL_7, &adc_value));
  61. // 将 ADC 值添加到队列中
  62. adc_values_.push_back(adc_value);
  63. if (adc_values_.size() > kBatteryAdcDataCount) {
  64. adc_values_.erase(adc_values_.begin());
  65. }
  66. uint32_t average_adc = 0;
  67. for (auto value : adc_values_) {
  68. average_adc += (value + 80);
  69. }
  70. average_adc /= adc_values_.size();
  71. // 定义电池电量区间
  72. const struct {
  73. uint16_t adc;
  74. uint8_t level;
  75. } levels[] = {
  76. {2030, 0},
  77. {2134, 20},
  78. {2252, 40},
  79. {2370, 60},
  80. {2488, 80},
  81. {2606, 100}
  82. };
  83. // 低于最低值时
  84. if (average_adc < levels[0].adc) {
  85. battery_level_ = 0;
  86. }
  87. // 高于最高值时
  88. else if (average_adc >= levels[5].adc) {
  89. battery_level_ = 100;
  90. } else {
  91. // 线性插值计算中间值
  92. for (int i = 0; i < 5; i++) {
  93. if (average_adc >= levels[i].adc && average_adc < levels[i+1].adc) {
  94. float ratio = static_cast<float>(average_adc - levels[i].adc) / (levels[i+1].adc - levels[i].adc);
  95. battery_level_ = levels[i].level + ratio * (levels[i+1].level - levels[i].level);
  96. break;
  97. }
  98. }
  99. }
  100. // 检查是否达到低电量阈值
  101. if (adc_values_.size() >= kBatteryAdcDataCount) {
  102. bool new_low_battery_status = battery_level_ <= kLowBatteryLevel;
  103. if (new_low_battery_status != is_low_battery_) {
  104. is_low_battery_ = new_low_battery_status;
  105. if (on_low_battery_status_changed_) {
  106. on_low_battery_status_changed_(is_low_battery_);
  107. }
  108. }
  109. }
  110. ESP_LOGI("PowerManager", "ADC value: %d average: %ld level: %ld", adc_value, average_adc, battery_level_);
  111. }
  112. void ReadTemperature() {
  113. float temperature = 0.0f;
  114. ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor_, &temperature));
  115. if (abs(temperature - current_temperature_) >= 3.5f) { // 温度变化超过3.5°C才触发回调
  116. current_temperature_ = temperature;
  117. if (on_temperature_changed_) {
  118. on_temperature_changed_(current_temperature_);
  119. }
  120. ESP_LOGI("PowerManager", "Temperature updated: %.1f°C", current_temperature_);
  121. }
  122. }
  123. public:
  124. PowerManager(gpio_num_t pin) : charging_pin_(pin) {
  125. // 初始化充电引脚
  126. gpio_config_t io_conf = {};
  127. io_conf.intr_type = GPIO_INTR_DISABLE;
  128. io_conf.mode = GPIO_MODE_INPUT;
  129. io_conf.pin_bit_mask = (1ULL << charging_pin_);
  130. io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
  131. io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
  132. gpio_config(&io_conf);
  133. // 创建电池电量检查定时器
  134. esp_timer_create_args_t timer_args = {
  135. .callback = [](void* arg) {
  136. PowerManager* self = static_cast<PowerManager*>(arg);
  137. self->CheckBatteryStatus();
  138. },
  139. .arg = this,
  140. .dispatch_method = ESP_TIMER_TASK,
  141. .name = "battery_check_timer",
  142. .skip_unhandled_events = true,
  143. };
  144. ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle_));
  145. ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle_, 1000000));
  146. // 初始化 ADC
  147. adc_oneshot_unit_init_cfg_t init_config = {
  148. .unit_id = ADC_UNIT_1,
  149. .ulp_mode = ADC_ULP_MODE_DISABLE,
  150. };
  151. ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle_));
  152. adc_oneshot_chan_cfg_t chan_config = {
  153. .atten = ADC_ATTEN_DB_12,
  154. .bitwidth = ADC_BITWIDTH_12,
  155. };
  156. ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle_, ADC_CHANNEL_7, &chan_config));
  157. // 初始化温度传感器
  158. temperature_sensor_config_t temp_config = {
  159. .range_min = 10,
  160. .range_max = 80,
  161. .clk_src = TEMPERATURE_SENSOR_CLK_SRC_DEFAULT
  162. };
  163. ESP_ERROR_CHECK(temperature_sensor_install(&temp_config, &temp_sensor_));
  164. ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor_));
  165. ESP_LOGI("PowerManager", "Temperature sensor initialized (new driver)");
  166. }
  167. ~PowerManager() {
  168. if (timer_handle_) {
  169. esp_timer_stop(timer_handle_);
  170. esp_timer_delete(timer_handle_);
  171. }
  172. if (adc_handle_) {
  173. adc_oneshot_del_unit(adc_handle_);
  174. }
  175. if (temp_sensor_) {
  176. temperature_sensor_disable(temp_sensor_);
  177. temperature_sensor_uninstall(temp_sensor_);
  178. }
  179. }
  180. bool IsCharging() {
  181. // 如果电量已经满了,则不再显示充电中
  182. if (battery_level_ == 100) {
  183. return false;
  184. }
  185. return is_charging_;
  186. }
  187. bool IsDischarging() {
  188. // 没有区分充电和放电,所以直接返回相反状态
  189. return !is_charging_;
  190. }
  191. // 获取电池电量
  192. uint8_t GetBatteryLevel() {
  193. // 返回电池电量
  194. return battery_level_;
  195. }
  196. float GetTemperature() const { return current_temperature_; } // 获取当前温度
  197. void OnTemperatureChanged(std::function<void(float)> callback) {
  198. on_temperature_changed_ = callback;
  199. }
  200. void OnLowBatteryStatusChanged(std::function<void(bool)> callback) {
  201. on_low_battery_status_changed_ = callback;
  202. }
  203. void OnChargingStatusChanged(std::function<void(bool)> callback) {
  204. on_charging_status_changed_ = callback;
  205. }
  206. };