forked from xiaozhi/xiaozhi-esp32
Update to v1.9.4 (#1374)
* fix: Corrected the inverted touch screen parameter configuration of lichuang_S3_dev, which caused touch offset. (#1209) * ci: support multiple variants per board (#1036) * fix release.py * OTTO 左右腿反了 (#1239) * Change the button array to ADC buttons as in the board for esp32s3-korv2 (#1256) * Change the button array to ADC buttons as in the board for esp32s3-korv2 * Add MuteVol function to control audio volume * Optimize AdcBatteryMonitor to work without charge detection pin. (#1276) Co-authored-by: Yuv Zhao <admin@yuvcloud.com> * 修复charging_pin为NC充电时Battery Level不更新的问题 (#1316) Co-authored-by: Yuv Zhao <admin@yuvcloud.com> * Bump to 1.9.4 --------- Co-authored-by: ZhouShaoYuan <cnfalcon@qq.com> Co-authored-by: laride <198868291+laride@users.noreply.github.com> Co-authored-by: Toby <naivetoby@gmail.com> Co-authored-by: masc2008 <masc2008@gmail.com> Co-authored-by: konglingboy <konglingboy@sina.com> Co-authored-by: Yuv Zhao <admin@yuvcloud.com>
This commit is contained in:
@@ -3,32 +3,42 @@
|
||||
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) {
|
||||
|
||||
// Initialize charging pin
|
||||
gpio_config_t gpio_cfg = {
|
||||
.pin_bit_mask = 1ULL << charging_pin,
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
ESP_ERROR_CHECK(gpio_config(&gpio_cfg));
|
||||
// Initialize charging pin (only if it's not NC)
|
||||
if (charging_pin_ != GPIO_NUM_NC) {
|
||||
gpio_config_t gpio_cfg = {
|
||||
.pin_bit_mask = 1ULL << charging_pin,
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
ESP_ERROR_CHECK(gpio_config(&gpio_cfg));
|
||||
}
|
||||
|
||||
// Initialize ADC battery estimation
|
||||
adc_battery_estimation_t adc_cfg = {
|
||||
.internal = {
|
||||
.adc_unit = adc_unit,
|
||||
.adc_bitwidth = ADC_BITWIDTH_12,
|
||||
.adc_bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
.adc_atten = ADC_ATTEN_DB_12,
|
||||
},
|
||||
.adc_channel = adc_channel,
|
||||
.upper_resistor = upper_resistor,
|
||||
.lower_resistor = lower_resistor
|
||||
};
|
||||
adc_cfg.charging_detect_cb = [](void *user_data) -> bool {
|
||||
AdcBatteryMonitor *self = (AdcBatteryMonitor *)user_data;
|
||||
return gpio_get_level(self->charging_pin_) == 1;
|
||||
};
|
||||
adc_cfg.charging_detect_user_data = this;
|
||||
|
||||
// 在ADC配置部分进行条件设置
|
||||
if (charging_pin_ != GPIO_NUM_NC) {
|
||||
adc_cfg.charging_detect_cb = [](void *user_data) -> bool {
|
||||
AdcBatteryMonitor *self = (AdcBatteryMonitor *)user_data;
|
||||
return gpio_get_level(self->charging_pin_) == 1;
|
||||
};
|
||||
adc_cfg.charging_detect_user_data = this;
|
||||
} else {
|
||||
// 不设置回调,让adc_battery_estimation库使用软件估算
|
||||
adc_cfg.charging_detect_cb = nullptr;
|
||||
adc_cfg.charging_detect_user_data = nullptr;
|
||||
}
|
||||
adc_battery_estimation_handle_ = adc_battery_estimation_create(&adc_cfg);
|
||||
|
||||
// Initialize timer
|
||||
@@ -48,12 +58,29 @@ AdcBatteryMonitor::~AdcBatteryMonitor() {
|
||||
if (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 is_charging = false;
|
||||
ESP_ERROR_CHECK(adc_battery_estimation_get_charging_state(adc_battery_estimation_handle_, &is_charging));
|
||||
return is_charging;
|
||||
// 优先使用adc_battery_estimation库的功能
|
||||
if (adc_battery_estimation_handle_ != nullptr) {
|
||||
bool is_charging = false;
|
||||
esp_err_t err = adc_battery_estimation_get_charging_state(adc_battery_estimation_handle_, &is_charging);
|
||||
if (err == ESP_OK) {
|
||||
return is_charging;
|
||||
}
|
||||
}
|
||||
|
||||
// 回退到GPIO读取或返回默认值
|
||||
if (charging_pin_ != GPIO_NUM_NC) {
|
||||
return gpio_get_level(charging_pin_) == 1;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AdcBatteryMonitor::IsDischarging() {
|
||||
@@ -61,9 +88,17 @@ bool AdcBatteryMonitor::IsDischarging() {
|
||||
}
|
||||
|
||||
uint8_t AdcBatteryMonitor::GetBatteryLevel() {
|
||||
// 如果句柄无效,返回默认值
|
||||
if (adc_battery_estimation_handle_ == nullptr) {
|
||||
return 100;
|
||||
}
|
||||
|
||||
float capacity = 0;
|
||||
ESP_ERROR_CHECK(adc_battery_estimation_get_capacity(adc_battery_estimation_handle_, &capacity));
|
||||
return capacity;
|
||||
esp_err_t err = adc_battery_estimation_get_capacity(adc_battery_estimation_handle_, &capacity);
|
||||
if (err != ESP_OK) {
|
||||
return 100; // 出错时返回默认值
|
||||
}
|
||||
return (uint8_t)capacity;
|
||||
}
|
||||
|
||||
void AdcBatteryMonitor::OnChargingStatusChanged(std::function<void(bool)> callback) {
|
||||
|
||||
Reference in New Issue
Block a user