Files
Power-Pico/Power_Pico/User/GUI/screens/ui_mainPage.c

271 lines
10 KiB
C
Raw Normal View History

2025-09-22 21:45:02 +08:00
// 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"
2025-12-05 15:31:26 +08:00
#include "data_queue.h" // bsp/data_queue.h for voltage/current queues
2025-10-19 11:59:09 +08:00
#include "math.h"
2025-09-22 21:45:02 +08:00
lv_obj_t * ui_HomeScreen = NULL;
2025-09-28 11:28:17 +08:00
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;
2025-09-22 21:45:02 +08:00
static lv_timer_t * _flush_timer = NULL;
2025-10-07 10:30:02 +08:00
static uint8_t timecount = 0;
2025-09-22 21:45:02 +08:00
// other funtions
2025-10-19 11:59:09 +08:00
#include "key.h"
2025-12-02 19:24:02 +08:00
void ui_main_page_key_handler(void* key_event)
2025-10-19 11:59:09 +08:00
{
2025-12-02 19:46:45 +08:00
if(((key_event_t*)key_event)->id == KEY_ID_B && ((key_event_t*)key_event)->type == KEY_EVT_CLICK)
{
2025-11-26 19:22:06 +08:00
lv_lib_pm_next();
2025-12-02 19:46:45 +08:00
}
else if (((key_event_t*)key_event)->id == KEY_ID_L && ((key_event_t*)key_event)->type == KEY_EVT_CLICK)
{
2025-10-19 11:59:09 +08:00
// rotation - 90 degrees
// rotation LV_DISPLAY_ROTATION_0/90/180/270
2025-11-26 19:16:32 +08:00
uint16_t rotation = ui_get_display_rotation();
2025-11-02 17:02:19 +08:00
rotation = (rotation + 360 - 90) % 360;
// set rotation value
2025-11-26 19:16:32 +08:00
ui_set_display_rotation(rotation);
2025-11-02 17:02:19 +08:00
// save settings to eeprom
2025-11-26 19:16:32 +08:00
ui_system_settings_save();
2025-10-19 11:59:09 +08:00
ui_full_screen_refresh(ui_HomeScreen);
2025-12-02 19:46:45 +08:00
}
else if (((key_event_t*)key_event)->id == KEY_ID_R && ((key_event_t*)key_event)->type == KEY_EVT_CLICK)
{
2025-10-19 11:59:09 +08:00
// rotation + 90 degrees
2025-11-26 19:16:32 +08:00
uint16_t rotation = ui_get_display_rotation();
2025-11-02 17:02:19 +08:00
rotation = (rotation + 360 + 90) % 360;
// set rotation value
2025-11-26 19:16:32 +08:00
ui_set_display_rotation(rotation);
2025-11-02 17:02:19 +08:00
// save settings to eeprom
2025-11-26 19:16:32 +08:00
ui_system_settings_save();
2025-10-19 11:59:09 +08:00
ui_full_screen_refresh(ui_HomeScreen);
}
2025-09-22 21:45:02 +08:00
}
2025-11-02 16:20:55 +08:00
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);
}
2025-09-22 21:45:02 +08:00
static void set_val_cur_label(float voltage, float current)
{
2025-09-27 19:11:39 +08:00
char buf[16];
2025-11-02 16:20:55 +08:00
// voltage (单位: V) -> 4 位有效数字
fmt_sig4(buf, sizeof(buf), voltage);
2025-09-22 21:45:02 +08:00
lv_label_set_text(ui_LabelValt, buf);
2025-11-02 16:20:55 +08:00
// 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);
2025-09-22 21:45:02 +08:00
lv_label_set_text(ui_LabelCur, buf);
lv_label_set_text(ui_LabelUnitCur, "A");
2025-11-02 16:20:55 +08:00
} else if (cur_abs_uA >= 1e3f) {
cur_disp = cur_uA / 1e3f; // mA
fmt_sig4(buf, sizeof(buf), cur_disp);
2025-09-22 21:45:02 +08:00
lv_label_set_text(ui_LabelCur, buf);
lv_label_set_text(ui_LabelUnitCur, "mA");
2025-10-15 10:25:22 +08:00
} else {
2025-11-02 16:20:55 +08:00
cur_disp = cur_uA; // uA
fmt_sig4(buf, sizeof(buf), cur_disp);
2025-09-22 21:45:02 +08:00
lv_label_set_text(ui_LabelCur, buf);
lv_label_set_text(ui_LabelUnitCur, "uA");
}
2025-11-02 16:20:55 +08:00
// 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
2025-09-27 19:11:39 +08:00
lv_label_set_text(ui_LabelEnerge, buf);
lv_label_set_text(ui_LabelUnitEnerge, "W");
2025-11-02 16:20:55 +08:00
} else if (p_abs >= 1e-3f) {
fmt_sig4(buf, sizeof(buf), power_W * 1e3f); // mW
2025-09-27 19:11:39 +08:00
lv_label_set_text(ui_LabelEnerge, buf);
lv_label_set_text(ui_LabelUnitEnerge, "mW");
2025-10-15 10:25:22 +08:00
} else {
2025-11-02 16:20:55 +08:00
fmt_sig4(buf, sizeof(buf), power_W * 1e6f); // uW
2025-09-27 19:11:39 +08:00
lv_label_set_text(ui_LabelEnerge, buf);
lv_label_set_text(ui_LabelUnitEnerge, "uW");
}
2025-11-02 16:20:55 +08:00
// time
2025-09-27 19:11:39 +08:00
uint8_t hours = 0, minutes = 0, seconds = 0;
2025-11-26 19:16:32 +08:00
ui_GetElapsedTime_HMS(&hours, &minutes, &seconds);
2025-11-02 16:20:55 +08:00
snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hours, minutes, seconds);
2025-09-27 19:11:39 +08:00
lv_label_set_text(ui_LabelTime, buf);
2025-09-22 21:45:02 +08:00
}
// event funtions
static void _flush_timer_cb()
{
timecount++;
2025-10-09 19:54:56 +08:00
if(timecount >= 2) { // 1s
2025-09-22 21:45:02 +08:00
timecount = 0;
2025-10-19 11:59:09 +08:00
ui_full_screen_refresh(ui_HomeScreen);
2025-09-22 21:45:02 +08:00
}
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);
2025-10-15 10:25:22 +08:00
lv_label_set_text(ui_LabelValt, "0.00");
2025-09-22 21:45:02 +08: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);
2025-10-15 10:25:22 +08:00
lv_label_set_text(ui_LabelUnitCur, "uA");
2025-09-22 21:45:02 +08:00
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);
2025-10-15 10:25:22 +08:00
lv_label_set_text(ui_LabelCur, "0.00");
2025-09-22 21:45:02 +08: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);
2025-10-15 10:25:22 +08:00
lv_label_set_text(ui_LabelEnerge, "0.00");
2025-09-22 21:45:02 +08: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)
{
2025-10-07 10:30:02 +08:00
timecount = 0;
2025-12-03 18:31:07 +08:00
lv_timer_delete(_flush_timer);
2025-09-22 21:45:02 +08:00
}