diff --git a/Power_Pico/User/GUI/screens/ui_SetPage.c b/Power_Pico/User/GUI/screens/ui_SetPage.c index be4f9a7..0fc7088 100644 --- a/Power_Pico/User/GUI/screens/ui_SetPage.c +++ b/Power_Pico/User/GUI/screens/ui_SetPage.c @@ -4,6 +4,7 @@ // Project name: PowerPico #include "../ui.h" +#include "BL24C02.h" // system settings lv_obj_t * ui_SetPage = NULL; // backlight @@ -18,11 +19,15 @@ static lv_obj_t * ui_LabelKS = NULL; static lv_obj_t * ui_PanelLang = NULL; static lv_obj_t * ui_SwitchLang = NULL; static lv_obj_t * ui_LabelLang = NULL; +// chose rotation +static lv_obj_t * ui_PanelRotate = NULL; +static lv_obj_t * ui_LabelRotation = NULL; +static lv_obj_t * ui_LabelRotNum = NULL; // about static lv_obj_t * ui_PanelAbout = NULL; static lv_obj_t * ui_LabelAbout = NULL; -static lv_obj_t * panels[4]; // 存储所有 panel 的指针 +static lv_obj_t * panels[5]; // 存储所有 panel 的指针 static int current_panel_index = 0; // 当前选中的 panel 索引 // event funtions @@ -31,55 +36,79 @@ static int current_panel_index = 0; // 当前选中的 panel 索引 void ui_set_page_key_handler(uint8_t key_id) { int panel_count = sizeof(panels) / sizeof(panels[0]); - if(key_id == KEYB_NUM) { + if(key_id == KEYB_NUM) { // key boot PageManager_next(); - } - if (key_id == KEYL_NUM) { + } if (key_id == KEYL_NUM) { // key left lv_obj_clear_state(panels[current_panel_index], LV_STATE_CHECKED); current_panel_index = (current_panel_index - 1 + panel_count) % panel_count; lv_obj_add_state(panels[current_panel_index], LV_STATE_CHECKED); lv_obj_scroll_to_view(panels[current_panel_index], LV_ANIM_ON); - } else if (key_id == KEYR_NUM) { + } else if (key_id == KEYR_NUM) { // key right lv_obj_clear_state(panels[current_panel_index], LV_STATE_CHECKED); current_panel_index = (current_panel_index + 1) % panel_count; lv_obj_add_state(panels[current_panel_index], LV_STATE_CHECKED); lv_obj_scroll_to_view(panels[current_panel_index], LV_ANIM_ON); - } else if (key_id == KEYY_NUM) { - if(current_panel_index == 0) { // Screen Brightness - int16_t slider_value = lv_slider_get_value(ui_SliderBL); - slider_value += 10; - if(slider_value > 100) slider_value = 100; - lv_slider_set_value(ui_SliderBL, slider_value, LV_ANIM_ON); - } else if(current_panel_index == 1) { // Key Sound - if(lv_obj_has_state(ui_SwitchKS, LV_STATE_CHECKED)) { - lv_obj_clear_state(ui_SwitchKS, LV_STATE_CHECKED); - } else { - lv_obj_add_state(ui_SwitchKS, LV_STATE_CHECKED); - } - } else if(current_panel_index == 2) { // Language - // do nothing - } else if(current_panel_index == 3) { // About - // do nothing - } - } else if (key_id == KEYN_NUM) { - if(current_panel_index == 0) { // Screen Brightness - int16_t slider_value = lv_slider_get_value(ui_SliderBL); - slider_value -= 10; - if(slider_value < 0) slider_value = 0; - lv_slider_set_value(ui_SliderBL, slider_value, LV_ANIM_ON); - } else if(current_panel_index == 1) { // Key Sound - if(lv_obj_has_state(ui_SwitchKS, LV_STATE_CHECKED)) { - lv_obj_clear_state(ui_SwitchKS, LV_STATE_CHECKED); - } else { - lv_obj_add_state(ui_SwitchKS, LV_STATE_CHECKED); - } - } else if(current_panel_index == 2) { // Language - // do nothing - } else if(current_panel_index == 3) { // About - // do nothing + } else { // key yes or key neg + switch(current_panel_index) { + // Screen Brightness + case 0: + if (key_id == KEYY_NUM) { + int16_t slider_value = lv_slider_get_value(ui_SliderBL); + slider_value += 10; + if(slider_value > 100) slider_value = 100; + lv_slider_set_value(ui_SliderBL, slider_value, LV_ANIM_ON); + } else if (key_id == KEYN_NUM) { + int16_t slider_value = lv_slider_get_value(ui_SliderBL); + slider_value -= 10; + if(slider_value < 0) slider_value = 10; + lv_slider_set_value(ui_SliderBL, slider_value, LV_ANIM_ON); + } + break; + // key sound + case 1: + if (key_id == KEYY_NUM) { + lv_obj_add_state(ui_SwitchKS, LV_STATE_CHECKED); + } else if(key_id == KEYN_NUM) { + lv_obj_clear_state(ui_SwitchKS, LV_STATE_CHECKED); + } + break; + + // language + case 2: + // do nothing + break; + + // chose rotation + case 3: + if (key_id == KEYY_NUM) { + 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_SetPage); + } else if(key_id == KEYN_NUM) { + 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_SetPage); + } + if (sys_settings.rotation == 0) { + lv_label_set_text(ui_LabelRotNum, "< 0 >"); + } else if (sys_settings.rotation == 90) { + lv_label_set_text(ui_LabelRotNum, "< 90 >"); + } else if (sys_settings.rotation == 180) { + lv_label_set_text(ui_LabelRotNum, "< 180 >"); + } else if (sys_settings.rotation == 270) { + lv_label_set_text(ui_LabelRotNum, "< 270 >"); + } + break; + + // about + case 4: + // do nothing + break; + } } - } // build funtions @@ -164,11 +193,36 @@ void ui_SetPage_screen_init(void) lv_label_set_text(ui_LabelLang, "Enable Chinese"); lv_obj_set_style_text_font(ui_LabelLang, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT); + ui_PanelRotate = lv_obj_create(ui_SetPage); + lv_obj_set_width(ui_PanelRotate, 234); + lv_obj_set_height(ui_PanelRotate, 45); + lv_obj_set_x(ui_PanelRotate, 0); + lv_obj_set_y(ui_PanelRotate, 175); + lv_obj_set_align(ui_PanelRotate, LV_ALIGN_TOP_MID); + lv_obj_add_flag(ui_PanelRotate, LV_OBJ_FLAG_CHECKABLE); /// Flags + lv_obj_remove_flag(ui_PanelRotate, LV_OBJ_FLAG_SCROLLABLE); /// Flags + lv_obj_set_style_bg_color(ui_PanelRotate, lv_color_hex(0x606060), LV_PART_MAIN | LV_STATE_CHECKED); + lv_obj_set_style_bg_opa(ui_PanelRotate, 255, LV_PART_MAIN | LV_STATE_CHECKED); + + ui_LabelRotation = lv_label_create(ui_PanelRotate); + lv_obj_set_width(ui_LabelRotation, LV_SIZE_CONTENT); /// 1 + lv_obj_set_height(ui_LabelRotation, LV_SIZE_CONTENT); /// 1 + lv_obj_set_align(ui_LabelRotation, LV_ALIGN_LEFT_MID); + lv_label_set_text(ui_LabelRotation, "Chose Rotation"); + lv_obj_set_style_text_font(ui_LabelRotation, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT); + + ui_LabelRotNum = lv_label_create(ui_PanelRotate); + lv_obj_set_width(ui_LabelRotNum, LV_SIZE_CONTENT); /// 1 + lv_obj_set_height(ui_LabelRotNum, LV_SIZE_CONTENT); /// 1 + lv_obj_set_align(ui_LabelRotNum, LV_ALIGN_RIGHT_MID); + lv_label_set_text(ui_LabelRotNum, "< 180 >"); + lv_obj_set_style_text_font(ui_LabelRotNum, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT); + ui_PanelAbout = lv_obj_create(ui_SetPage); lv_obj_set_width(ui_PanelAbout, 234); lv_obj_set_height(ui_PanelAbout, 100); lv_obj_set_x(ui_PanelAbout, 0); - lv_obj_set_y(ui_PanelAbout, 175); + lv_obj_set_y(ui_PanelAbout, 225); lv_obj_set_align(ui_PanelAbout, LV_ALIGN_TOP_MID); lv_obj_add_flag(ui_PanelAbout, LV_OBJ_FLAG_CHECKABLE); /// Flags lv_obj_remove_flag(ui_PanelAbout, LV_OBJ_FLAG_SCROLLABLE); /// Flags @@ -185,7 +239,8 @@ void ui_SetPage_screen_init(void) panels[0] = ui_PanelBL; panels[1] = ui_PanelKS; panels[2] = ui_PanelLang; - panels[3] = ui_PanelAbout; + panels[3] = ui_PanelRotate; + panels[4] = ui_PanelAbout; lv_obj_add_state(panels[current_panel_index], LV_STATE_CHECKED); lv_obj_scroll_to_view(panels[current_panel_index], LV_ANIM_ON); diff --git a/Power_Pico/User/GUI/screens/ui_mainPage.c b/Power_Pico/User/GUI/screens/ui_mainPage.c index 4c67240..689f611 100644 --- a/Power_Pico/User/GUI/screens/ui_mainPage.c +++ b/Power_Pico/User/GUI/screens/ui_mainPage.c @@ -6,6 +6,8 @@ #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; @@ -25,12 +27,25 @@ static uint8_t timecount = 0; // other funtions -static void full_screen_refresh(void) { - // 标记整个屏幕为脏区域 - lv_obj_invalidate(ui_HomeScreen); - - // 或者立即刷新整个屏幕 - lv_refr_now(NULL); +#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) @@ -40,14 +55,14 @@ static void set_val_cur_label(float voltage, float current) sprintf(buf, "%.2f", voltage); lv_label_set_text(ui_LabelValt, buf); // current - if (current >= 1000000.0) { + 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(current >= 1000.0) { + } else if(fabs(current) >= 1000.0) { current = current / 1000.0; - if(current >= 100.0) { + if(fabs(current) >= 100.0) { sprintf(buf, "%.1f", current); } else { @@ -56,7 +71,7 @@ static void set_val_cur_label(float voltage, float current) lv_label_set_text(ui_LabelCur, buf); lv_label_set_text(ui_LabelUnitCur, "mA"); } else { - if(current >= 100.0) { + if(fabs(current) >= 100.0) { sprintf(buf, "%.1f", current); } else { @@ -67,14 +82,14 @@ static void set_val_cur_label(float voltage, float current) } // set power float power = voltage * current; // in uW - if(power >= 1000000.0) { + 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(power >= 1000.0) { + } else if(fabs(power) >= 1000.0) { power = power / 1000.0; - if(power >= 100.0) { + if(fabs(power) >= 100.0) { sprintf(buf, "%.1f", power); } else { @@ -83,7 +98,7 @@ static void set_val_cur_label(float voltage, float current) lv_label_set_text(ui_LabelEnerge, buf); lv_label_set_text(ui_LabelUnitEnerge, "mW"); } else { - if(power >= 100.0) { + if(fabs(power) >= 100.0) { sprintf(buf, "%.1f", power); } else { @@ -106,7 +121,7 @@ static void _flush_timer_cb() timecount++; if(timecount >= 2) { // 1s timecount = 0; - full_screen_refresh(); + ui_full_screen_refresh(ui_HomeScreen); } float voltage = 0.0; float current = 0.0; @@ -240,11 +255,3 @@ void ui_main_screen_destroy(void) timecount = 0; lv_timer_del(_flush_timer); } - -#include "key.h" -void ui_main_page_key_handler(uint8_t key_id) -{ - if(key_id == KEYB_NUM) { - PageManager_next(); - } -} diff --git a/Power_Pico/User/GUI/ui.c b/Power_Pico/User/GUI/ui.c index 43d8eab..c00f0b9 100644 --- a/Power_Pico/User/GUI/ui.c +++ b/Power_Pico/User/GUI/ui.c @@ -40,6 +40,16 @@ Page_t pages[] = { #error "LV_COLOR_DEPTH should be 16bit to match SquareLine Studio's settings" #endif +///////////////////// help funtions //////////////////// + +void ui_full_screen_refresh(lv_obj_t * screen) { + // 标记整个屏幕为脏区域 + lv_obj_invalidate(screen); + // 或者立即刷新整个屏幕 + lv_refr_now(NULL); +} + + /////////////////////// Timer ////////////////////// /** * Main timer for Refreshing the screens diff --git a/Power_Pico/User/GUI/ui.h b/Power_Pico/User/GUI/ui.h index 5f2af04..04fa1e3 100644 --- a/Power_Pico/User/GUI/ui.h +++ b/Power_Pico/User/GUI/ui.h @@ -15,6 +15,7 @@ extern "C" { #include "./PageManager.h" void ui_init(void); +void ui_full_screen_refresh(lv_obj_t * screen); // FONTS LV_FONT_DECLARE(ui_font_HeiTi32);