|
| 1 | +#ifndef __POWER_MANAGER_H__ |
| 2 | +#define __POWER_MANAGER_H__ |
| 3 | + |
| 4 | +#include <driver/gpio.h> |
| 5 | +#include <esp_adc/adc_oneshot.h> |
| 6 | +#include <esp_log.h> |
| 7 | +#include <esp_timer.h> |
| 8 | + |
| 9 | +class PowerManager { |
| 10 | +private: |
| 11 | + // 电池电量区间-分压电阻为2个100k |
| 12 | + static constexpr struct { |
| 13 | + uint16_t adc; |
| 14 | + uint8_t level; |
| 15 | + } BATTERY_LEVELS[] = {{1980, 0}, {2519, 100}}; |
| 16 | + static constexpr size_t BATTERY_LEVELS_COUNT = 2; |
| 17 | + static constexpr size_t ADC_VALUES_COUNT = 10; |
| 18 | + |
| 19 | + esp_timer_handle_t timer_handle_ = nullptr; |
| 20 | + gpio_num_t charging_pin_; |
| 21 | + gpio_num_t bat_led_pin_; |
| 22 | + adc_unit_t adc_unit_; |
| 23 | + adc_channel_t adc_channel_; |
| 24 | + uint16_t adc_values_[ADC_VALUES_COUNT]; |
| 25 | + size_t adc_values_index_ = 0; |
| 26 | + size_t adc_values_count_ = 0; |
| 27 | + uint8_t battery_level_ = 100; |
| 28 | + bool is_charging_ = false; |
| 29 | + |
| 30 | + static constexpr uint8_t MAX_CHANGE_COUNT = 8; |
| 31 | + static constexpr uint32_t TIME_LIMIT = 2000000; // 2 seconds in microseconds |
| 32 | + |
| 33 | + uint8_t change_count_ = 0; // 记录状态变化次数 |
| 34 | + uint64_t last_change_time_ = 0; // 最后一次状态变化的时间戳(微秒) |
| 35 | + |
| 36 | + adc_oneshot_unit_handle_t adc_handle_; |
| 37 | + |
| 38 | + void CheckBatteryStatus() { |
| 39 | + uint64_t current_time = esp_timer_get_time(); // 获取当前时间(微秒) |
| 40 | + |
| 41 | + // 如果时间间隔超过2秒,则重置状态变化计数 |
| 42 | + if (current_time - last_change_time_ > TIME_LIMIT) { |
| 43 | + change_count_ = 0; |
| 44 | + } |
| 45 | + |
| 46 | + if (change_count_ < MAX_CHANGE_COUNT) { |
| 47 | + bool new_is_charging = gpio_get_level(bat_led_pin_) != 0; // 检查LED引脚状态 |
| 48 | + |
| 49 | + // 判断充电引脚状态 |
| 50 | + if (new_is_charging) { |
| 51 | + new_is_charging = gpio_get_level(charging_pin_) == 1; |
| 52 | + } |
| 53 | + |
| 54 | + // 如果状态有变化 |
| 55 | + if (new_is_charging != is_charging_) { |
| 56 | + is_charging_ = new_is_charging; |
| 57 | + change_count_++; // 增加变化次数 |
| 58 | + last_change_time_ = current_time; // 更新最后变化时间 |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + ReadBatteryAdcData(); |
| 63 | + } |
| 64 | + void ReadBatteryAdcData() { |
| 65 | + int adc_value; |
| 66 | + ESP_ERROR_CHECK(adc_oneshot_read(adc_handle_, adc_channel_, &adc_value)); |
| 67 | + |
| 68 | + adc_values_[adc_values_index_] = adc_value; |
| 69 | + adc_values_index_ = (adc_values_index_ + 1) % ADC_VALUES_COUNT; |
| 70 | + if (adc_values_count_ < ADC_VALUES_COUNT) { |
| 71 | + adc_values_count_++; |
| 72 | + } |
| 73 | + |
| 74 | + uint32_t average_adc = 0; |
| 75 | + for (size_t i = 0; i < adc_values_count_; i++) { |
| 76 | + average_adc += adc_values_[i]; |
| 77 | + } |
| 78 | + average_adc /= adc_values_count_; |
| 79 | + |
| 80 | + CalculateBatteryLevel(average_adc); |
| 81 | + |
| 82 | + |
| 83 | + // ESP_LOGI("PowerManager", "ADC值: %d 平均值: %ld 电量: %u%%", adc_value, average_adc, |
| 84 | + // battery_level_); |
| 85 | + } |
| 86 | + |
| 87 | + void CalculateBatteryLevel(uint32_t average_adc) { |
| 88 | + if (average_adc <= BATTERY_LEVELS[0].adc) { |
| 89 | + battery_level_ = 0; |
| 90 | + } else if (average_adc >= BATTERY_LEVELS[BATTERY_LEVELS_COUNT - 1].adc) { |
| 91 | + battery_level_ = 100; |
| 92 | + } else { |
| 93 | + float ratio = static_cast<float>(average_adc - BATTERY_LEVELS[0].adc) / |
| 94 | + (BATTERY_LEVELS[1].adc - BATTERY_LEVELS[0].adc); |
| 95 | + battery_level_ = ratio * 100; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +public: |
| 100 | + PowerManager(gpio_num_t charging_pin, gpio_num_t bat_led_pin, adc_unit_t adc_unit = ADC_UNIT_2, |
| 101 | + adc_channel_t adc_channel = ADC_CHANNEL_3) |
| 102 | + : charging_pin_(charging_pin), bat_led_pin_(bat_led_pin), adc_unit_(adc_unit), adc_channel_(adc_channel) { |
| 103 | + |
| 104 | + // 配置充电引脚 |
| 105 | + gpio_config_t io_conf = {}; |
| 106 | + io_conf.intr_type = GPIO_INTR_DISABLE; |
| 107 | + io_conf.mode = GPIO_MODE_INPUT; |
| 108 | + io_conf.pin_bit_mask = (1ULL << charging_pin_); |
| 109 | + io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; |
| 110 | + io_conf.pull_up_en = GPIO_PULLUP_ENABLE; |
| 111 | + gpio_config(&io_conf); |
| 112 | + |
| 113 | + // 配置状态引脚 |
| 114 | + io_conf.pull_up_en = GPIO_PULLUP_DISABLE; |
| 115 | + io_conf.pin_bit_mask = (1ULL << bat_led_pin_); |
| 116 | + gpio_config(&io_conf); |
| 117 | + |
| 118 | + // 定时器配置 |
| 119 | + esp_timer_create_args_t timer_args = { |
| 120 | + .callback = |
| 121 | + [](void* arg) { |
| 122 | + PowerManager* self = static_cast<PowerManager*>(arg); |
| 123 | + self->CheckBatteryStatus(); |
| 124 | + }, |
| 125 | + .arg = this, |
| 126 | + .dispatch_method = ESP_TIMER_TASK, |
| 127 | + .name = "battery_check_timer", |
| 128 | + .skip_unhandled_events = true, |
| 129 | + }; |
| 130 | + ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle_)); |
| 131 | + ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle_, 500000)); // 1秒 |
| 132 | + |
| 133 | + // 初始化ADC |
| 134 | + InitializeAdc(); |
| 135 | + } |
| 136 | + |
| 137 | + void InitializeAdc() { |
| 138 | + adc_oneshot_unit_init_cfg_t init_config = { |
| 139 | + .unit_id = adc_unit_, |
| 140 | + .ulp_mode = ADC_ULP_MODE_DISABLE, |
| 141 | + }; |
| 142 | + ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle_)); |
| 143 | + |
| 144 | + adc_oneshot_chan_cfg_t chan_config = { |
| 145 | + .atten = ADC_ATTEN_DB_12, |
| 146 | + .bitwidth = ADC_BITWIDTH_12, |
| 147 | + }; |
| 148 | + |
| 149 | + ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle_, adc_channel_, &chan_config)); |
| 150 | + } |
| 151 | + |
| 152 | + ~PowerManager() { |
| 153 | + if (timer_handle_) { |
| 154 | + esp_timer_stop(timer_handle_); |
| 155 | + esp_timer_delete(timer_handle_); |
| 156 | + } |
| 157 | + if (adc_handle_) { |
| 158 | + adc_oneshot_del_unit(adc_handle_); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + bool IsCharging() { return is_charging_; } |
| 163 | + |
| 164 | + uint8_t GetBatteryLevel() { return battery_level_; } |
| 165 | +}; |
| 166 | +#endif // __POWER_MANAGER_H__ |
0 commit comments