From 27270c4f6a8dc707ffc040ea505bcbc46706a71e Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Wed, 3 May 2023 18:10:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(gui):=20=E7=81=AF=E6=9D=A1=E4=BA=AE?= =?UTF-8?q?=E5=BA=A6=E8=B0=83=E6=95=B4=E7=95=8C=E9=9D=A2=E3=80=82=20close?= =?UTF-8?q?=20#6.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/CMakeLists.txt | 5 +-- main/gui.c | 97 ++++++++++++++++++++++++++++++++++++++++++++- main/main.c | 8 ++-- main/pca9555.c | 5 --- main/ui_input.c | 6 +-- 5 files changed, 104 insertions(+), 17 deletions(-) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 04d7f5c..410cd0a 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -5,18 +5,17 @@ idf_component_register( "app_nvs.c" "ch1116.c" # "apds_9960.c" - # "pca9555.c" + "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" - # "ui_input.c" + "ui_input.c" # "ambient_light.c" # "temperature.c" # "mqtt.c" "main.c" "wifi.c" "light.c" - # "mqtt.c" "led_strip_encoder/led_strip_encoder.c" "gui.c" "lvgl_demo_ui.c" diff --git a/main/gui.c b/main/gui.c index 5144cef..4fe4ee1 100644 --- a/main/gui.c +++ b/main/gui.c @@ -73,6 +73,13 @@ static lv_anim_t timer_animate; static lv_obj_t *desktop_label; static lv_anim_t desktop_animate; +static lv_obj_t *value_setting_panel; +static lv_obj_t *value_setting_bar; +static lv_obj_t *value_setting_label; +static lv_obj_t *value_setting_value_label; + +static lv_obj_t *scr; + static void example_lvgl_rounder(lv_disp_drv_t *disp_drv, lv_area_t *area) { area->y1 = area->y1 & (~0x7); area->y2 = area->y2 | 0x7; @@ -170,6 +177,92 @@ static void gui_set_server_disconnected() { lv_label_set_text(desktop_label, APP_DISCONNECTED_SYMBOL); } +static void gui_bar_value_update_cb(lv_event_t *e) { + lv_obj_draw_part_dsc_t *dsc = lv_event_get_param(e); + if (dsc->part != LV_PART_INDICATOR) return; + + lv_obj_t *obj = lv_event_get_target(e); + + lv_draw_label_dsc_t label_dsc; + lv_draw_label_dsc_init(&label_dsc); + label_dsc.font = &lv_font_montserrat_8; + + char buf[8]; + lv_snprintf(buf, sizeof(buf), "%ld", lv_bar_get_value(obj)); + + lv_point_t txt_size; + lv_txt_get_size(&txt_size, buf, label_dsc.font, label_dsc.letter_space, + label_dsc.line_space, LV_COORD_MAX, label_dsc.flag); + + lv_coord_t txt_x; + /*If the indicator is long enough put the text inside on the right*/ + if (lv_area_get_width(dsc->draw_area) > txt_size.x + 20) { + txt_x = dsc->draw_area->x2 - 8 - txt_size.x + 1; + lv_obj_set_style_text_color(value_setting_value_label, lv_color_white(), + LV_PART_MAIN); + label_dsc.color = lv_color_white(); + } + /*If the indicator is still short put the text out of it on the right*/ + else { + txt_x = dsc->draw_area->x2 - 8 + txt_size.x - 1; + lv_obj_set_style_text_color(value_setting_value_label, lv_color_black(), + LV_PART_MAIN); + } + + ESP_LOGI(GUI_TAG, "value_setting_value_label: %s", buf); + + lv_obj_align(value_setting_value_label, LV_ALIGN_LEFT_MID, txt_x, 0); + lv_label_set_text(value_setting_value_label, buf); + lv_obj_set_width(value_setting_value_label, txt_size.x); +} + +static void gui_create_value_setting_panel() { + if (value_setting_panel != NULL) { + return; + } + + value_setting_panel = lv_obj_create(scr); + lv_obj_set_size(value_setting_panel, 128, 40); + lv_obj_align(value_setting_panel, LV_ALIGN_BOTTOM_MID, 0, 0); + lv_obj_set_style_border_width(value_setting_panel, 1, LV_PART_MAIN); + lv_obj_set_style_border_color(value_setting_panel, lv_color_black(), + LV_PART_MAIN); + lv_obj_set_style_radius(value_setting_panel, 5, LV_PART_MAIN); + lv_obj_set_style_pad_all(value_setting_panel, 2, LV_PART_MAIN); + + value_setting_label = lv_label_create(value_setting_panel); + lv_obj_align(value_setting_label, LV_ALIGN_BOTTOM_LEFT, 0, 0); + + value_setting_bar = lv_bar_create(value_setting_panel); + lv_obj_set_size(value_setting_bar, 120, 12); + lv_obj_align(value_setting_bar, LV_ALIGN_TOP_MID, 0, 0); + lv_bar_set_range(value_setting_bar, 0, 100); + lv_bar_set_value(value_setting_bar, 50, LV_ANIM_ON); + lv_obj_set_style_bg_color(value_setting_bar, lv_color_white(), LV_PART_MAIN); + lv_obj_set_style_border_color(value_setting_bar, lv_color_black(), + LV_PART_MAIN); + lv_obj_set_style_border_width(value_setting_bar, 1, LV_PART_MAIN); + lv_obj_set_style_radius(value_setting_bar, 5, LV_PART_MAIN); + lv_obj_set_style_pad_hor(value_setting_bar, 0, LV_PART_MAIN); + lv_obj_set_style_pad_ver(value_setting_bar, 2, LV_PART_MAIN); + lv_obj_set_style_bg_color(value_setting_bar, lv_color_black(), + LV_PART_INDICATOR); + lv_obj_add_event_cb(value_setting_bar, gui_bar_value_update_cb, + LV_EVENT_DRAW_PART_END, NULL); + + value_setting_value_label = lv_label_create(value_setting_bar); + lv_obj_align(value_setting_value_label, LV_ALIGN_CENTER, 0, 0); + lv_obj_set_style_text_font(value_setting_value_label, &lv_font_montserrat_8, + LV_PART_MAIN); +} + +static void gui_change_strip_level(int32_t target_value) { + gui_create_value_setting_panel(); + lv_label_set_text(value_setting_label, "Strip Level"); + lv_bar_set_range(value_setting_bar, 0, 255); + lv_bar_set_value(value_setting_bar, target_value, LV_ANIM_ON); +} + void lv_example_bar_6(lv_obj_t *src) { static lv_style_t style_bar; lv_style_init(&style_bar); @@ -237,7 +330,7 @@ void gui_status_bar_create(lv_obj_t *scr) { } void example_lvgl_demo_ui(lv_disp_t *disp) { - lv_obj_t *scr = lv_disp_get_scr_act(disp); + scr = lv_disp_get_scr_act(disp); lv_obj_t *label = lv_label_create(scr); lv_label_set_long_mode(label, @@ -250,6 +343,8 @@ void example_lvgl_demo_ui(lv_disp_t *disp) { lv_example_bar_6(scr); gui_status_bar_create(scr); + + gui_create_value_setting_panel(); } static void gui_tick(void *pvParameters) { diff --git a/main/main.c b/main/main.c index bb7812e..902970e 100644 --- a/main/main.c +++ b/main/main.c @@ -14,7 +14,7 @@ #include "service_discovery.c" // #include "temperature.c" #include "udp_server.c" -// #include "ui_input.c" +#include "ui_input.c" #include "wifi.c" static const char *APP_TAG = "DisplayAmbientLight"; @@ -23,7 +23,7 @@ void app_main(void) { app_nvs_init(); light_init_strip(); - // gpio_install_isr_service(0); + gpio_install_isr_service(0); init_i2c(); i2c_check_slaves(); @@ -39,8 +39,8 @@ void app_main(void) { // apds_9960_init(); // apds_9960_auto_fetch(); // auto_fetch_temperature(); - // pca9555_init(); - // ui_input_init(); + pca9555_init(); + ui_input_init(); // xTaskCreate(mqtt_publish_ui_input, "mqtt_publish_ui_input", 2048, NULL, 10, // NULL); // vTaskDelay(pdMS_TO_TICKS(10)); diff --git a/main/pca9555.c b/main/pca9555.c index 9c351d5..8229d73 100644 --- a/main/pca9555.c +++ b/main/pca9555.c @@ -3,7 +3,6 @@ #include #include "driver/i2c.h" -#include "embedded_display.c" #include "esp_log.h" #include "i2c.c" @@ -79,19 +78,15 @@ void pca9555_fetch(void* arg) { esp_err_t error; uint8_t buff; char disp_str[17]; - display_fill_rect(0, 6, 128, 8, 0x00); for (;;) { error = pca9555_read_one_input(PCA95555_CMD_INPUT_PORT_1, &buff); if (error != ESP_OK) { ESP_LOGW(PCA_9555_TAG, "read failed. %x", error); } else { - sprintf(disp_str, "IO0: 0x%2x ", buff); - display_print8_str(8, 6, disp_str); ESP_LOGD(PCA_9555_TAG, "IO0: 0x%2x", buff); } vTaskDelay(pdMS_TO_TICKS(10)); } - display_fill_rect(0, 6, 128, 8, 0x00); } void pca9555_auto_fetch() { diff --git a/main/ui_input.c b/main/ui_input.c index 425c6da..a69ef6e 100644 --- a/main/ui_input.c +++ b/main/ui_input.c @@ -6,10 +6,10 @@ #include "config_key.h" #include "driver/gpio.h" -#include "embedded_display.c" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "freertos/task.h" +#include "gui.c" #include "light.c" #include "pca9555.c" @@ -72,7 +72,6 @@ static void ui_input_update_embedded_display(void *arg) { sprintf(changing_str, "CVol: % 3d", input.value); break; case ui_input_key_display_ambient_lighting_level: - sprintf(changing_str, "ALLv: % 3d", display_ambient_lighting_level); break; case ui_input_key_display_ambient_lighting_mode: sprintf(changing_str, "ALMd: % 3d", input.value); @@ -88,8 +87,6 @@ static void ui_input_update_embedded_display(void *arg) { strcpy(changing_str, "NC"); break; } - display_fill_rect(0, 6, 128, 8, 0); - display_print8_str(8, 6, changing_str); } } } @@ -161,6 +158,7 @@ static void encoder_value_change(encoder_state_t *state) { if (state->value & 1) { event.key = ui_input_key_display_ambient_lighting_level; led_strip_set_brightness(display_ambient_lighting_level + delta); + gui_change_strip_level(display_ambient_lighting_level); } else { event.key = ui_input_key_display_1_brightness; }