FluxDC/components/FluxPower/FluxPower.c
2025-03-10 10:42:45 +08:00

69 lines
2.2 KiB
C

#include <stdio.h>
#include "FluxPower.h"
static const char *TAG = "POWER_IO";
extern lv_obj_t * ui_Batinfo_valueVoltage;
extern lv_obj_t * ui_Batinfo_valueVolume;
extern lv_obj_t * ui_Batinfo_SpinboxBAT;
extern lv_obj_t * ui_Batinfo_BarBAT;
extern lv_obj_t * ui_Home_LabelHeaderBatValue;
extern lv_obj_t * ui_Home_SliderHeaderBat;
/*没有返回值的电源管理任务*/
static int Bat_Adc_Value = 0;
static adc_oneshot_unit_handle_t adc1_handle;
static TaskHandle_t powerTaskHandle;
/*定义变量存储电池电压值*/
static float Bat_Voltage_Value = 0;
void powerTask(void)
{
while (1)
{
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, EXAMPLE_ADC1_CHAN8, &Bat_Adc_Value));
/*根据采样值计算电池电压*/
Bat_Voltage_Value = 2 * (float)Bat_Adc_Value * 1.767 / 4095.0 + 2.3;
vTaskDelay(pdMS_TO_TICKS(1000));
/*配置电池信息页显示*/
lv_bar_set_value(ui_Batinfo_BarBAT,Bat_Adc_Value,LV_ANIM_ON);
lv_spinbox_set_value(ui_Batinfo_SpinboxBAT,Bat_Adc_Value);
lv_label_set_text_fmt(ui_Batinfo_valueVoltage,"%.2fV",Bat_Voltage_Value);
lv_label_set_text_fmt(ui_Batinfo_valueVolume,"%.0fmAh",Bat_Voltage_Value/4.2*2500);
/*设置Home页的显示电量*/
lv_slider_set_value(ui_Home_SliderHeaderBat,Bat_Voltage_Value/4.2*100,LV_ANIM_ON);
lv_label_set_text_fmt(ui_Home_LabelHeaderBatValue,"%.0f%%",Bat_Voltage_Value/4.2*100);
ESP_LOGI("dd","%d",Bat_Adc_Value);
printf("in powerTask the min free stack size is %ld \r\n", (int32_t)uxTaskGetStackHighWaterMark(NULL));
}
}
void powerInit(void)
{
/*电源接口初始化*/
//-------------ADC1 Init---------------//
adc_oneshot_unit_init_cfg_t init_config1 = {
.unit_id = ADC_UNIT_1,
};
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
//-------------ADC1 Config---------------//
adc_oneshot_chan_cfg_t config = {
.bitwidth = ADC_BITWIDTH_12,
.atten = ADC_ATTEN_DB_6,
};
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, EXAMPLE_ADC1_CHAN8, &config));
xTaskCreate(powerTask,"powerTask",4*1024, NULL, 2, &powerTaskHandle);
}