#include "power.h"
#include "char_lcd.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() {
    if (!lipo.begin()) {
        ESP_LOGE(TAG, "Failed to init communication with the battery gas guage");
    }

    auto voltage = lipo.voltage();
    ESP_LOGI(TAG, "Battery Voltage: %d", voltage);
}


