diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 410cd0a..43df122 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,6 +1,5 @@ idf_component_register( SRCS - "udp_server.c" "service_discovery.c" "app_nvs.c" "ch1116.c" @@ -13,6 +12,7 @@ idf_component_register( # "ambient_light.c" # "temperature.c" # "mqtt.c" + "desktop.c" "main.c" "wifi.c" "light.c" diff --git a/main/udp_server.c b/main/desktop.c similarity index 68% rename from main/udp_server.c rename to main/desktop.c index e752070..f588af1 100644 --- a/main/udp_server.c +++ b/main/desktop.c @@ -16,6 +16,7 @@ #include "lwip/sockets.h" #include "lwip/sys.h" #include "nvs_flash.h" +#include "ui_input.c" #define UDP_PORT 23042 @@ -24,6 +25,28 @@ static const char *UDP_SERVER_TAG = "UDP_SERVER"; static bool desktop_connected = false; static uint64_t last_desktop_ping_at = 0; +typedef struct desktop_value { + uint8_t value; + uint8_t max; + uint8_t min; +} desktop_value_t; + +static desktop_value_t display1_brightness = { + .value = 20, + .max = 100, + .min = 0, +}; +static desktop_value_t display2_brightness = { + .value = 20, + .max = 100, + .min = 0, +}; +static desktop_value_t computer_volume = { + .value = 20, + .max = 100, + .min = 0, +}; + static void udp_server_task(void *pvParameters) { char rx_buffer[1024]; char addr_str[128]; @@ -132,6 +155,71 @@ static void udp_server_task(void *pvParameters) { vTaskDelete(NULL); } +static uint8_t desktop_change_value(desktop_value_t *target, int8_t delta) { + if (delta > 0) { + if ((target->max - target->value) > delta) { + target->value += delta; + } else { + target->value = target->max; + } + } else { + if ((target->value - target->min) > -delta) { + target->value += delta; + } else { + target->value = target->min; + } + } + return target->value; +} + +static uint8_t change_display_brightness(uint8_t display_index, int8_t delta) { + switch (display_index) { + case 0: + uint8_t value = desktop_change_value(&display1_brightness, delta); + gui_change_display_brightness(display_index, value); + return value; + + case 1: + return desktop_change_value(&display1_brightness, delta); + gui_change_display_brightness(display_index, value); + return value; + + default: + ESP_LOGE(UDP_SERVER_TAG, "Invalid display index: %d", display_index); + return 0; + } +} + +static uint8_t change_volume(int8_t delta) { + uint8_t value = desktop_change_value(&computer_volume, delta); + gui_change_volume_level(value); + return value; +} + +static void desktop_watch_input_task(void *arg) { + s_ui_input_t input; + for (;;) { + if (xQueueReceive(ui_input_event, &input, portMAX_DELAY)) { + switch (input.key) { + case ui_input_key_display_0_brightness: + change_display_brightness(0, input.value); + break; + + case ui_input_key_display_1_brightness: + change_display_brightness(1, input.value); + break; + + case ui_input_key_computer_volume: + change_volume(input.value); + break; + + default: + break; + } + } + } +} + static void change_desktop_connection_status(bool connected) { desktop_connected = connected; if (connected) { @@ -154,6 +242,8 @@ static void desktop_online_check_task(void *pvParameters) { void udp_server_init(void) { xTaskCreate(udp_server_task, "udp_server", 4096, (void *)AF_INET, 5, NULL); - xTaskCreate(desktop_online_check_task, "desktop_online_check", 4096, NULL, 10, + xTaskCreate(desktop_online_check_task, "desktop_online_check", 1024, NULL, 10, + NULL); + xTaskCreate(desktop_watch_input_task, "desktop_watch_input", 4096, NULL, 7, NULL); } \ No newline at end of file diff --git a/main/gui.c b/main/gui.c index 78b3e07..82f768f 100644 --- a/main/gui.c +++ b/main/gui.c @@ -261,6 +261,22 @@ static void gui_change_strip_level(int32_t target_value) { lv_bar_set_value(value_setting_bar, target_value, LV_ANIM_ON); } +static void gui_change_display_brightness(int8_t display_index, + int32_t target_value) { + gui_create_value_setting_panel(); + lv_label_set_text_fmt(value_setting_label, "Display#%d Brightness", + display_index); + lv_bar_set_range(value_setting_bar, 0, 100); + lv_bar_set_value(value_setting_bar, target_value, LV_ANIM_ON); +} + +static void gui_change_volume_level(int32_t target_value) { + gui_create_value_setting_panel(); + lv_label_set_text(value_setting_label, "Volume Level"); + lv_bar_set_range(value_setting_bar, 0, 100); + 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); diff --git a/main/main.c b/main/main.c index 902970e..c493ee1 100644 --- a/main/main.c +++ b/main/main.c @@ -13,7 +13,7 @@ #include "sdkconfig.h" #include "service_discovery.c" // #include "temperature.c" -#include "udp_server.c" +#include "desktop.c" #include "ui_input.c" #include "wifi.c" diff --git a/main/ui_input.c b/main/ui_input.c index a69ef6e..dab1bfb 100644 --- a/main/ui_input.c +++ b/main/ui_input.c @@ -87,6 +87,8 @@ static void ui_input_update_embedded_display(void *arg) { strcpy(changing_str, "NC"); break; } + + ESP_LOGI(UI_INPUT_TAG, "%s", changing_str); } } } @@ -145,8 +147,6 @@ static void encoder_value_change(encoder_state_t *state) { return; } - ESP_LOGI(UI_INPUT_TAG, "key: %d, delta: %d", state->key, delta); - s_ui_input_t event = {.value = delta}; if (state->key == ui_input_raw_key_encoder_0) { if (state->value & 1) { @@ -164,6 +164,7 @@ static void encoder_value_change(encoder_state_t *state) { } } xQueueSend(ui_input_event, &event, NULL); + ESP_LOGI(UI_INPUT_TAG, "key: %d, delta: %d", state->key, delta); } static void ui_input_raw_handler(void *arg) { @@ -198,7 +199,7 @@ void ui_input_init(void) { // hook isr handler for specific gpio pin gpio_isr_handler_add(ENCODER_INT_GPIO, gpio_isr_handler, NULL); - xTaskCreate(ui_input_update_embedded_display, "ui_input_event", 2048, NULL, - 10, NULL); + // xTaskCreate(ui_input_update_embedded_display, "ui_input_event", 2048, NULL, + // 10, NULL); xTaskCreate(ui_input_raw_handler, "ui_input_event", 2048, NULL, 10, NULL); }