forked from xiaozhi/xiaozhi-esp32
Optimize AdcBatteryMonitor to work without charge detection pin. (#1276)
Co-authored-by: Yuv Zhao <admin@yuvcloud.com>
This commit is contained in:
@@ -3,7 +3,8 @@
|
|||||||
AdcBatteryMonitor::AdcBatteryMonitor(adc_unit_t adc_unit, adc_channel_t adc_channel, float upper_resistor, float lower_resistor, gpio_num_t charging_pin)
|
AdcBatteryMonitor::AdcBatteryMonitor(adc_unit_t adc_unit, adc_channel_t adc_channel, float upper_resistor, float lower_resistor, gpio_num_t charging_pin)
|
||||||
: charging_pin_(charging_pin) {
|
: charging_pin_(charging_pin) {
|
||||||
|
|
||||||
// Initialize charging pin
|
// Initialize charging pin (only if it's not NC)
|
||||||
|
if (charging_pin_ != GPIO_NUM_NC) {
|
||||||
gpio_config_t gpio_cfg = {
|
gpio_config_t gpio_cfg = {
|
||||||
.pin_bit_mask = 1ULL << charging_pin,
|
.pin_bit_mask = 1ULL << charging_pin,
|
||||||
.mode = GPIO_MODE_INPUT,
|
.mode = GPIO_MODE_INPUT,
|
||||||
@@ -12,12 +13,13 @@ AdcBatteryMonitor::AdcBatteryMonitor(adc_unit_t adc_unit, adc_channel_t adc_chan
|
|||||||
.intr_type = GPIO_INTR_DISABLE,
|
.intr_type = GPIO_INTR_DISABLE,
|
||||||
};
|
};
|
||||||
ESP_ERROR_CHECK(gpio_config(&gpio_cfg));
|
ESP_ERROR_CHECK(gpio_config(&gpio_cfg));
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize ADC battery estimation
|
// Initialize ADC battery estimation
|
||||||
adc_battery_estimation_t adc_cfg = {
|
adc_battery_estimation_t adc_cfg = {
|
||||||
.internal = {
|
.internal = {
|
||||||
.adc_unit = adc_unit,
|
.adc_unit = adc_unit,
|
||||||
.adc_bitwidth = ADC_BITWIDTH_12,
|
.adc_bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||||
.adc_atten = ADC_ATTEN_DB_12,
|
.adc_atten = ADC_ATTEN_DB_12,
|
||||||
},
|
},
|
||||||
.adc_channel = adc_channel,
|
.adc_channel = adc_channel,
|
||||||
@@ -26,6 +28,9 @@ AdcBatteryMonitor::AdcBatteryMonitor(adc_unit_t adc_unit, adc_channel_t adc_chan
|
|||||||
};
|
};
|
||||||
adc_cfg.charging_detect_cb = [](void *user_data) -> bool {
|
adc_cfg.charging_detect_cb = [](void *user_data) -> bool {
|
||||||
AdcBatteryMonitor *self = (AdcBatteryMonitor *)user_data;
|
AdcBatteryMonitor *self = (AdcBatteryMonitor *)user_data;
|
||||||
|
if(self->charging_pin_ == GPIO_NUM_NC)
|
||||||
|
return false;
|
||||||
|
else
|
||||||
return gpio_get_level(self->charging_pin_) == 1;
|
return gpio_get_level(self->charging_pin_) == 1;
|
||||||
};
|
};
|
||||||
adc_cfg.charging_detect_user_data = this;
|
adc_cfg.charging_detect_user_data = this;
|
||||||
@@ -48,11 +53,31 @@ AdcBatteryMonitor::~AdcBatteryMonitor() {
|
|||||||
if (adc_battery_estimation_handle_) {
|
if (adc_battery_estimation_handle_) {
|
||||||
ESP_ERROR_CHECK(adc_battery_estimation_destroy(adc_battery_estimation_handle_));
|
ESP_ERROR_CHECK(adc_battery_estimation_destroy(adc_battery_estimation_handle_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (timer_handle_) {
|
||||||
|
esp_timer_stop(timer_handle_);
|
||||||
|
esp_timer_delete(timer_handle_);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AdcBatteryMonitor::IsCharging() {
|
bool AdcBatteryMonitor::IsCharging() {
|
||||||
|
// 如果没有充电检测引脚,直接返回false
|
||||||
|
if (charging_pin_ == GPIO_NUM_NC) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果句柄无效,回退到直接读取GPIO
|
||||||
|
if (adc_battery_estimation_handle_ == nullptr) {
|
||||||
|
return gpio_get_level(charging_pin_) == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试通过adc_battery_estimation获取状态
|
||||||
bool is_charging = false;
|
bool is_charging = false;
|
||||||
ESP_ERROR_CHECK(adc_battery_estimation_get_charging_state(adc_battery_estimation_handle_, &is_charging));
|
esp_err_t err = adc_battery_estimation_get_charging_state(adc_battery_estimation_handle_, &is_charging);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
// 如果调用失败,回退到直接读取GPIO引脚状态
|
||||||
|
return gpio_get_level(charging_pin_) == 1;
|
||||||
|
}
|
||||||
return is_charging;
|
return is_charging;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,9 +86,17 @@ bool AdcBatteryMonitor::IsDischarging() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t AdcBatteryMonitor::GetBatteryLevel() {
|
uint8_t AdcBatteryMonitor::GetBatteryLevel() {
|
||||||
|
// 如果句柄无效,返回默认值
|
||||||
|
if (adc_battery_estimation_handle_ == nullptr) {
|
||||||
|
return 100;
|
||||||
|
}
|
||||||
|
|
||||||
float capacity = 0;
|
float capacity = 0;
|
||||||
ESP_ERROR_CHECK(adc_battery_estimation_get_capacity(adc_battery_estimation_handle_, &capacity));
|
esp_err_t err = adc_battery_estimation_get_capacity(adc_battery_estimation_handle_, &capacity);
|
||||||
return capacity;
|
if (err != ESP_OK) {
|
||||||
|
return 100; // 出错时返回默认值
|
||||||
|
}
|
||||||
|
return (uint8_t)capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdcBatteryMonitor::OnChargingStatusChanged(std::function<void(bool)> callback) {
|
void AdcBatteryMonitor::OnChargingStatusChanged(std::function<void(bool)> callback) {
|
||||||
@@ -71,6 +104,11 @@ void AdcBatteryMonitor::OnChargingStatusChanged(std::function<void(bool)> callba
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AdcBatteryMonitor::CheckBatteryStatus() {
|
void AdcBatteryMonitor::CheckBatteryStatus() {
|
||||||
|
// 避免在GPIO_NUM_NC情况下进行检查
|
||||||
|
if (charging_pin_ == GPIO_NUM_NC) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
bool new_charging_status = IsCharging();
|
bool new_charging_status = IsCharging();
|
||||||
if (new_charging_status != is_charging_) {
|
if (new_charging_status != is_charging_) {
|
||||||
is_charging_ = new_charging_status;
|
is_charging_ = new_charging_status;
|
||||||
|
|||||||
Reference in New Issue
Block a user