// This file was generated by SquareLine Studio
// SquareLine Studio version: SquareLine Studio 1.5.3
// LVGL version: 9.2.2
// Project name: PowerPico

#include "../ui.h"
#include "ui_mainPage.h"
#include "data_queue.h"
#include "math.h"
#include "BL24C02.h" // system settings

lv_obj_t * ui_HomeScreen = NULL;
static lv_obj_t * ui_ButVal = NULL;
static lv_obj_t * ui_LabelUnitVal = NULL;
static lv_obj_t * ui_LabelValt = NULL;
static lv_obj_t * ui_ButCur = NULL;
static lv_obj_t * ui_LabelUnitCur = NULL;
static lv_obj_t * ui_LabelCur = NULL;
static lv_obj_t * ui_ButEnerge = NULL;
static lv_obj_t * ui_LabelUnitEnerge = NULL;
static lv_obj_t * ui_LabelEnerge = NULL;
static lv_obj_t * ui_ButTime = NULL;
static lv_obj_t * ui_LabelTime = NULL;
static lv_timer_t * _flush_timer = NULL;

static uint8_t timecount = 0;

// other funtions

#include "key.h"
void ui_main_page_key_handler(uint8_t key_id)
{
    if(key_id == KEYB_NUM) {
        PageManager_next();
    } else if (key_id == KEYL_NUM) {
        // rotation - 90 degrees
        // rotation – LV_DISPLAY_ROTATION_0/90/180/270
        sys_settings.rotation = (sys_settings.rotation + 360 - 90) % 360;
        lv_display_rotation_t rotation = sys_settings.rotation / 90;
        lv_display_set_rotation(lv_display_get_default(), rotation);
        ui_full_screen_refresh(ui_HomeScreen);
    } else if (key_id == KEYR_NUM) {
        // rotation + 90 degrees
        sys_settings.rotation = (sys_settings.rotation + 360 + 90) % 360;
        lv_display_rotation_t rotation = sys_settings.rotation / 90;
        lv_display_set_rotation(lv_display_get_default(), rotation);
        ui_full_screen_refresh(ui_HomeScreen);
    }
}

static void set_val_cur_label(float voltage, float current)
{
    char buf[16];
    // voltage
    sprintf(buf, "%.2f", voltage);
    lv_label_set_text(ui_LabelValt, buf);
    // current
    if (fabs(current) >= 1000000.0) {
        current = current / 1000000.0;
        sprintf(buf, "%.2f", current); // 保留 2 位小数
        lv_label_set_text(ui_LabelCur, buf);
        lv_label_set_text(ui_LabelUnitCur, "A");
    } else if(fabs(current) >= 1000.0) {
        current = current / 1000.0;
        if(fabs(current) >= 100.0) {
            sprintf(buf, "%.1f", current);
        }
        else {
            sprintf(buf, "%.2f", current);
        }
        lv_label_set_text(ui_LabelCur, buf);
        lv_label_set_text(ui_LabelUnitCur, "mA");
    } else {
        if(fabs(current) >= 100.0) {
            sprintf(buf, "%.1f", current);
        }
        else {
            sprintf(buf, "%.2f", current);
        }
        lv_label_set_text(ui_LabelCur, buf);
        lv_label_set_text(ui_LabelUnitCur, "uA");
    }
    // set power
    float power = voltage * current; // in uW
    if(fabs(power) >= 1000000.0) {
        power = power / 1000000.0;
        sprintf(buf, "%.2f", power);
        lv_label_set_text(ui_LabelEnerge, buf);
        lv_label_set_text(ui_LabelUnitEnerge, "W");
    } else if(fabs(power) >= 1000.0) {
        power = power / 1000.0;
        if(fabs(power) >= 100.0) {
            sprintf(buf, "%.1f", power);
        }
        else {
            sprintf(buf, "%.2f", power);
        }
        lv_label_set_text(ui_LabelEnerge, buf);
        lv_label_set_text(ui_LabelUnitEnerge, "mW");
    } else {
        if(fabs(power) >= 100.0) {
            sprintf(buf, "%.1f", power);
        }
        else {
            sprintf(buf, "%.2f", power);
        }
        lv_label_set_text(ui_LabelEnerge, buf);
        lv_label_set_text(ui_LabelUnitEnerge, "uW");
    }
    // set time
    uint8_t hours = 0, minutes = 0, seconds = 0;
    GetElapsedTime_HMS(&hours, &minutes, &seconds);
    sprintf(buf, "%02d:%02d:%02d", hours, minutes, seconds);
    lv_label_set_text(ui_LabelTime, buf);
}

// event funtions

static void _flush_timer_cb()
{
    timecount++;
    if(timecount >= 2) { // 1s
        timecount = 0;
        ui_full_screen_refresh(ui_HomeScreen);
    }
    float voltage = 0.0;
    float current = 0.0;
    current = queue_average(global_current_queue);
    voltage = queue_average(global_voltage_queue);
    set_val_cur_label(voltage, current);
}

// build funtions

void ui_main_screen_init(void)
{
    ui_HomeScreen = lv_obj_create(NULL);
    lv_obj_remove_flag(ui_HomeScreen, LV_OBJ_FLAG_SCROLLABLE);      /// Flags

    ui_ButVal = lv_button_create(ui_HomeScreen);
    lv_obj_set_width(ui_ButVal, 234);
    lv_obj_set_height(ui_ButVal, 55);
    lv_obj_set_x(ui_ButVal, 0);
    lv_obj_set_y(ui_ButVal, 4);
    lv_obj_set_align(ui_ButVal, LV_ALIGN_TOP_MID);
    lv_obj_add_flag(ui_ButVal, LV_OBJ_FLAG_SCROLL_ON_FOCUS);     /// Flags
    lv_obj_remove_flag(ui_ButVal, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_bg_color(ui_ButVal, lv_color_hex(0xE36666), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_ButVal, 255, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_ButCur = lv_button_create(ui_HomeScreen);
    lv_obj_set_width(ui_ButCur, 234);
    lv_obj_set_height(ui_ButCur, 55);
    lv_obj_set_x(ui_ButCur, 0);
    lv_obj_set_y(ui_ButCur, 63);
    lv_obj_set_align(ui_ButCur, LV_ALIGN_TOP_MID);
    lv_obj_add_flag(ui_ButCur, LV_OBJ_FLAG_SCROLL_ON_FOCUS);     /// Flags
    lv_obj_remove_flag(ui_ButCur, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_bg_color(ui_ButCur, lv_color_hex(0x42AA49), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_ButCur, 255, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_ButEnerge = lv_button_create(ui_HomeScreen);
    lv_obj_set_width(ui_ButEnerge, 234);
    lv_obj_set_height(ui_ButEnerge, 55);
    lv_obj_set_x(ui_ButEnerge, 0);
    lv_obj_set_y(ui_ButEnerge, 122);
    lv_obj_set_align(ui_ButEnerge, LV_ALIGN_TOP_MID);
    lv_obj_add_flag(ui_ButEnerge, LV_OBJ_FLAG_SCROLL_ON_FOCUS);     /// Flags
    lv_obj_remove_flag(ui_ButEnerge, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_bg_color(ui_ButEnerge, lv_color_hex(0x4569C9), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_ButEnerge, 255, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_ButTime = lv_button_create(ui_HomeScreen);
    lv_obj_set_width(ui_ButTime, 234);
    lv_obj_set_height(ui_ButTime, 55);
    lv_obj_set_x(ui_ButTime, 0);
    lv_obj_set_y(ui_ButTime, 181);
    lv_obj_set_align(ui_ButTime, LV_ALIGN_TOP_MID);
    lv_obj_add_flag(ui_ButTime, LV_OBJ_FLAG_SCROLL_ON_FOCUS);     /// Flags
    lv_obj_remove_flag(ui_ButTime, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_bg_color(ui_ButTime, lv_color_hex(0x9F9960), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_ButTime, 255, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_LabelUnitVal = lv_label_create(ui_HomeScreen);
    lv_obj_set_width(ui_LabelUnitVal, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelUnitVal, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_LabelUnitVal, 70);
    lv_obj_set_y(ui_LabelUnitVal, 20);
    lv_obj_set_align(ui_LabelUnitVal, LV_ALIGN_TOP_MID);
    lv_label_set_text(ui_LabelUnitVal, "V");
    lv_obj_set_style_text_font(ui_LabelUnitVal, &ui_font_HeiTi32, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_LabelValt = lv_label_create(ui_HomeScreen);
    lv_obj_set_width(ui_LabelValt, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelValt, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_LabelValt, -30);
    lv_obj_set_y(ui_LabelValt, 14);
    lv_obj_set_align(ui_LabelValt, LV_ALIGN_TOP_MID);
    lv_label_set_text(ui_LabelValt, "0.00");
    lv_obj_set_style_text_font(ui_LabelValt, &ui_font_HeiTi48, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_LabelUnitCur = lv_label_create(ui_HomeScreen);
    lv_obj_set_width(ui_LabelUnitCur, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelUnitCur, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_LabelUnitCur, 70);
    lv_obj_set_y(ui_LabelUnitCur, 80);
    lv_obj_set_align(ui_LabelUnitCur, LV_ALIGN_TOP_MID);
    lv_label_set_text(ui_LabelUnitCur, "uA");
    lv_obj_set_style_text_font(ui_LabelUnitCur, &ui_font_HeiTi32, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_LabelCur = lv_label_create(ui_HomeScreen);
    lv_obj_set_width(ui_LabelCur, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelCur, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_LabelCur, -30);
    lv_obj_set_y(ui_LabelCur, 74);
    lv_obj_set_align(ui_LabelCur, LV_ALIGN_TOP_MID);
    lv_label_set_text(ui_LabelCur, "0.00");
    lv_obj_set_style_text_font(ui_LabelCur, &ui_font_HeiTi48, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_LabelUnitEnerge = lv_label_create(ui_HomeScreen);
    lv_obj_set_width(ui_LabelUnitEnerge, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelUnitEnerge, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_LabelUnitEnerge, 70);
    lv_obj_set_y(ui_LabelUnitEnerge, 140);
    lv_obj_set_align(ui_LabelUnitEnerge, LV_ALIGN_TOP_MID);
    lv_label_set_text(ui_LabelUnitEnerge, "mW");
    lv_obj_set_style_text_font(ui_LabelUnitEnerge, &ui_font_HeiTi32, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_LabelEnerge = lv_label_create(ui_HomeScreen);
    lv_obj_set_width(ui_LabelEnerge, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelEnerge, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_LabelEnerge, -30);
    lv_obj_set_y(ui_LabelEnerge, 133);
    lv_obj_set_align(ui_LabelEnerge, LV_ALIGN_TOP_MID);
    lv_label_set_text(ui_LabelEnerge, "0.00");
    lv_obj_set_style_text_font(ui_LabelEnerge, &ui_font_HeiTi48, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_LabelTime = lv_label_create(ui_HomeScreen);
    lv_obj_set_width(ui_LabelTime, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelTime, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_LabelTime, 0);
    lv_obj_set_y(ui_LabelTime, 190);
    lv_obj_set_align(ui_LabelTime, LV_ALIGN_TOP_MID);
    lv_label_set_text(ui_LabelTime, "00:00:00");
    lv_obj_set_style_text_font(ui_LabelTime, &ui_font_HeiTi48, LV_PART_MAIN | LV_STATE_DEFAULT);
    _flush_timer_cb(NULL);
    // flush timer
    _flush_timer = lv_timer_create(_flush_timer_cb, 500,  NULL);

}


void ui_main_screen_destroy(void)
{
    timecount = 0;
    lv_timer_del(_flush_timer);
}
