FluxDC/components/FluxPower/FluxPower.c

100 lines
3.5 KiB
C

#include <stdio.h>
#include "FluxPower.h"
#include "FluxWifi.h"
#include "FluxSpifs.h"
static const char *TAG = "POWER_IO";
/* Changes in power level cause changes in UI widget parameters */
// 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;
/* Power management task with no return value */
static int Bat_Adc_Value = 0;
static adc_oneshot_unit_handle_t adc1_handle;
static TaskHandle_t powerTaskHandle;
/* Define a variable to store the battery voltage value */
static float Bat_Voltage_Value = 0;
void powerTask(void)
{
/* Initialize the storage system */
spifs_init();
/* Perform WIFI scanning and trigger dropdown menu event immediately after scanning */
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));
/* Calculate battery voltage based on the sampled 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);
}
}
void powerInit(void)
{
/* Power interface initialization */
//-------------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);
}
static 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));
}