// 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 "math.h" 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; // other funtions #include "key.h" void ui_main_page_key_handler(void* key_event) { if(((key_event_t*)key_event)->id == KEY_ID_B && ((key_event_t*)key_event)->type == KEY_EVT_CLICK) { lv_lib_pm_next(); } // else if (((key_event_t*)key_event)->id == KEY_ID_L && ((key_event_t*)key_event)->type == KEY_EVT_CLICK) // { // // rotation - 90 degrees // // rotation – LV_DISPLAY_ROTATION_0/90/180/270 // uint16_t rotation = ui_get_display_rotation(); // rotation = (rotation + 360 - 90) % 360; // // set rotation value // ui_set_display_rotation(rotation); // // save settings to eeprom // ui_system_settings_save(); // ui_full_screen_refresh(ui_HomeScreen); // } // else if (((key_event_t*)key_event)->id == KEY_ID_R && ((key_event_t*)key_event)->type == KEY_EVT_CLICK) // { // // rotation + 90 degrees // uint16_t rotation = ui_get_display_rotation(); // rotation = (rotation + 360 + 90) % 360; // // set rotation value // ui_set_display_rotation(rotation); // // save settings to eeprom // ui_system_settings_save(); // ui_full_screen_refresh(ui_HomeScreen); // } } static void fmt_sig4(char *buf, size_t size, float v) { if (!isfinite(v)) { snprintf(buf, size, "--"); return; } if (fabsf(v) < 1e-9f) v = 0.0f; // 处理接近零的值 float av = fabsf(v); int int_digits = (av >= 1.0f) ? ((int)floorf(log10f(av)) + 1) : 0; if(int_digits >= 4) { snprintf(buf, size, "%.0f", roundf(v)); return; } int decimals = (int_digits == 0) ? 3 : (4 - int_digits); // 确保总数字为 4 snprintf(buf, size, "%.*f", decimals, v); } static void set_val_cur_label(float voltage, float current) { char buf[16]; // voltage (单位: V) -> 4 位有效数字 fmt_sig4(buf, sizeof(buf), voltage); lv_label_set_text(ui_LabelValt, buf); // current (原始单位: uA) float cur_uA = current; float cur_abs_uA = fabsf(cur_uA); float cur_disp = 0.0f; if (cur_abs_uA >= 1e6f) { cur_disp = cur_uA / 1e6f; // A fmt_sig4(buf, sizeof(buf), cur_disp); lv_label_set_text(ui_LabelCur, buf); lv_label_set_text(ui_LabelUnitCur, "A"); } else if (cur_abs_uA >= 1e3f) { cur_disp = cur_uA / 1e3f; // mA fmt_sig4(buf, sizeof(buf), cur_disp); lv_label_set_text(ui_LabelCur, buf); lv_label_set_text(ui_LabelUnitCur, "mA"); } else { cur_disp = cur_uA; // uA fmt_sig4(buf, sizeof(buf), cur_disp); lv_label_set_text(ui_LabelCur, buf); lv_label_set_text(ui_LabelUnitCur, "uA"); } // power = V * A,按 W/mW/uW 选择单位,4 位有效数字 float power_W = voltage * (cur_uA / 1e6f); float p_abs = fabsf(power_W); if (p_abs >= 1.0f) { fmt_sig4(buf, sizeof(buf), power_W); // W lv_label_set_text(ui_LabelEnerge, buf); lv_label_set_text(ui_LabelUnitEnerge, "W"); } else if (p_abs >= 1e-3f) { fmt_sig4(buf, sizeof(buf), power_W * 1e3f); // mW lv_label_set_text(ui_LabelEnerge, buf); lv_label_set_text(ui_LabelUnitEnerge, "mW"); } else { fmt_sig4(buf, sizeof(buf), power_W * 1e6f); // uW lv_label_set_text(ui_LabelEnerge, buf); lv_label_set_text(ui_LabelUnitEnerge, "uW"); } // time uint8_t hours = 0, minutes = 0, seconds = 0; ui_GetElapsedTime_HMS(&hours, &minutes, &seconds); snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hours, minutes, seconds); lv_label_set_text(ui_LabelTime, buf); } // event funtions static void _flush_timer_cb() { float voltage = 0.0; float current = 0.0; ui_get_vol_cur(&voltage, ¤t); 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, 250, NULL); } void ui_main_screen_destroy(void) { lv_timer_delete(_flush_timer); }