FluxDC/components/FluxPower/FluxPower.c

79 lines
2.5 KiB
C
Raw Normal View History

#include <stdio.h>
#include "FluxPower.h"
#include "FluxWifi.h"
#include "FluxSpifs.h"
static const char *TAG = "POWER_IO";
2025-03-10 18:07:34 +08:00
/*电量变化引起的界面控件参数变化*/
extern lv_obj_t * ui_pageBattery_barBattery;
extern lv_obj_t * ui_pageBattery_spinboxBattery;
extern lv_obj_t * ui_pageBattery_labelCurrentVoltage;
extern lv_obj_t * ui_pageBattery_labelCurrentVolume;
extern lv_obj_t * ui_pageHome_sliderBattery;
extern lv_obj_t * ui_pageHome_labelBattery;
/*没有返回值的电源管理任务*/
2025-03-10 10:42:45 +08:00
static int Bat_Adc_Value = 0;
static adc_oneshot_unit_handle_t adc1_handle;
static TaskHandle_t powerTaskHandle;
2025-03-10 10:42:45 +08:00
/*定义变量存储电池电压值*/
static float Bat_Voltage_Value = 0;
void powerTask(void)
{
/*初始化存储系统*/
spifs_init();
/*进行WIFI扫描 扫描后立即触发下拉菜单事件*/
wifi_scan();
lv_event_send(ui_pageWifiConnect_dropdownWifiName,LV_EVENT_VALUE_CHANGED,NULL);
while (1)
{
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, EXAMPLE_ADC1_CHAN8, &Bat_Adc_Value));
2025-03-10 10:42:45 +08:00
/*根据采样值计算电池电压*/
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)
{
/*配置电池信息页显示*/
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);
}
2025-03-10 10:42:45 +08:00
/*设置Home页的显示电量*/
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);
}
}
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 = {
2025-03-10 10:42:45 +08:00
.bitwidth = ADC_BITWIDTH_12,
.atten = ADC_ATTEN_DB_6,
};
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, EXAMPLE_ADC1_CHAN8, &config));
2025-03-10 10:42:45 +08:00
xTaskCreate(powerTask,"powerTask",4*1024, NULL, 2, &powerTaskHandle);
}