#include #include "Flux_Power.h" #include "FluxWifi.h" static const char *g_Tag = "power_management_task"; /** * @brief 电源管理任务函数 * * 用于实时检测锂电池电量 */ void power_management_task(void* arg) { /* 部分电路板不具有WIFI连接功能 */ #if CONFIG_WIFI_SCAN_ENABLE /* Perform WIFI scanning and trigger dropdown menu event immediately after scanning */ wifi_scan(); lv_event_send(ui_pageWifiConnect_dropdownWifiName, LV_EVENT_VALUE_CHANGED, NULL); #endif /* 函数中用到的变量 */ adc_oneshot_unit_handle_t adc1_handle; float Bat_Voltage_Value = 0; int Bat_Adc_Value = 0; /* 锂电池采样ADC初始化*/ adc_oneshot_unit_init_cfg_t init_config1 = { .unit_id = ADC_UNIT_1, }; ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle)); adc_oneshot_chan_cfg_t config = { .bitwidth = ADC_BITWIDTH_12, .atten = ADC_ATTEN_DB_6, }; ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, BATTERY_ADC_CHANNEL, &config)); while (1) { /* 读取当前锂电池电压值 */ ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, BATTERY_ADC_CHANNEL, &Bat_Adc_Value)); /* 根据采样值计算当前电压 */ Bat_Voltage_Value = 2 * (float)Bat_Adc_Value * 1.767 / 4095.0 + 2.3; vTaskDelay(pdMS_TO_TICKS(5000)); if (lv_scr_act() == ui_pageBattery) { /* Configure the display of the battery information page */ lv_bar_set_value(ui_pageBattery_barBattery, Bat_Adc_Value, LV_ANIM_ON); lv_spinbox_set_value(ui_pageBattery_spinboxBattery, Bat_Adc_Value); lv_label_set_text_fmt(ui_pageBattery_labelCurrentVoltage, "%.2fV", Bat_Voltage_Value); lv_label_set_text_fmt(ui_pageBattery_labelCurrentVolume, "%.0fmAh", Bat_Voltage_Value / 4.2 * 2500); } /* Set the displayed power level on the Home page */ lv_slider_set_value(ui_pageHome_sliderBattery, Bat_Voltage_Value / 4.2 * 100, LV_ANIM_ON); lv_label_set_text_fmt(ui_pageHome_labelBattery, "%.0f%%", Bat_Voltage_Value / 4.2 * 100); } } /** * @brief 背光调节初始化函数 * */ void example_ledc_init(void) { // Prepare and then apply the LEDC PWM timer configuration ledc_timer_config_t ledc_timer = { .speed_mode = LEDC_MODE, .duty_resolution = LEDC_DUTY_RES, .timer_num = LEDC_TIMER, .freq_hz = LEDC_FREQUENCY, // Set output frequency at 4 kHz .clk_cfg = LEDC_AUTO_CLK }; ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer)); // Prepare and then apply the LEDC PWM channel configuration ledc_channel_config_t ledc_channel = { .speed_mode = LEDC_MODE, .channel = LEDC_CHANNEL, .timer_sel = LEDC_TIMER, .intr_type = LEDC_INTR_DISABLE, .gpio_num = LEDC_OUTPUT_IO, .duty = 0, // Set duty to 0% .hpoint = 0 }; ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel)); }