ESP32S2_Cal_Demo/main/app_task_battery.c

49 lines
1.3 KiB
C

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "user_board.h"
#include "app_lib_bat_impl.h"
#define BATTERY_CAP 3000.0
#define MAH_LSB 0.0850 // Shunt 50mR
#define PRESCALER_M LTC2941_PRE_128
const static char *TAG = "TASK_BAT";
TaskHandle_t xTaskBatteryExampleHandle = NULL;
void vTasBatteryExample(void *pvParameters) {
app_lib_bat_impl_t impl = {
.i2c_num = BOARD_SYSTEM_I2C_NUM
};
ltc2941_t battery_fg = {
.user_data = &impl,
.cb = {
.read_register_cb = (ltc2941_ret_t (*)(void *, uint8_t, uint8_t *))app_lib_bat_read_register,
.write_register_cb = (ltc2941_ret_t (*)(void *, uint8_t, uint8_t))app_lib_bat_write_register
},
.config = {
.alert_level = LTC2941_ALERT_3_0V,
.alert_mode = LTC2941_ALERT_DISABLED,
.prescaler = PRESCALER_M
}
};
ltc2941_init(&battery_fg);
for(;;) {
uint16_t fg_data = 0x00;
ltc2941_read_charge(&battery_fg, &fg_data);
float mah = fg_data * MAH_LSB;
float percentage = fg_data * MAH_LSB * 100.0 / BATTERY_CAP;
ESP_LOGI(TAG, "Capacity: %04.2fmAh, Charge: %03.2f%%", mah, percentage);
vTaskDelay(pdMS_TO_TICKS(30000));
}
}