blk_box_tc/main/drivers/power.cpp
2025-07-13 14:37:03 -05:00

74 lines
1.5 KiB
C++

#include "power.h"
#include "char_lcd.h"
#include <esp_log.h>
static const char* TAG = "power";
void bat_monitor_task(void* arg) {
while (1) {
char str_buf[33] = {0};
uint16_t voltage = lipo.voltage();
sprintf(str_buf, "%d.%03dV", voltage / 1000, voltage % 1000);
lcd_clear();
lcd_print(1, 0, str_buf);
int16_t current = lipo.current(current_measure::AVG);
sprintf(str_buf, "%dmA", current);
lcd_print(1, 1, str_buf);
int16_t total_cap = lipo.capacity(capacity_measure::FULL);
sprintf(str_buf, "%dmAh", total_cap);
lcd_print(1, 2, str_buf);
int16_t soc = lipo.soc(soc_measure::FILTERED);
sprintf(str_buf, "%d%%", soc);
lcd_print(1, 3, str_buf);
vTaskDelay(pdMS_TO_TICKS(250));
}
vTaskDelete(NULL);
}
void init_power_board() {
ESP_LOGI(TAG, "Initializing power board...");
if (!lipo.begin()) {
ESP_LOGE(TAG, "Failed to init communication with the battery gas guage");
return;
}
auto voltage = lipo.voltage();
ESP_LOGI(TAG, "Battery Voltage: %d", voltage);
ESP_LOGI(TAG, "Power board initialized!");
}
uint16_t get_bat_voltage() {
return lipo.voltage();
}
void lcd_print_header_bat() {
if (!lcd_header_enabled()) return;
uint8_t soc = lipo.soc();
char buf[5];
if (soc < 5) {
sprintf(buf, "LOW ");
} else {
snprintf(buf, sizeof(buf), "%3d%%", soc);
}
if (lipo.chgFlag()) {
buf[4] = '+';
}
lcd_print(17, 1, buf);
}