From 228883c70086131f10aada71fce9cf17ea2abb6f Mon Sep 17 00:00:00 2001 From: imi415 Date: Mon, 3 Jan 2022 10:52:35 +0800 Subject: [PATCH] Optimized interface event routines. --- main/impl/impl_lvgl.c | 2 +- main/interface/if_standby.c | 58 +++++++++++++++++++++++++----------- main/interface/if_standby.h | 23 ++++++-------- main/service/service_clock.c | 19 ++++-------- main/service/service_dht.c | 11 ++----- main/service/service_wifi.c | 10 ++----- 6 files changed, 59 insertions(+), 64 deletions(-) diff --git a/main/impl/impl_lvgl.c b/main/impl/impl_lvgl.c index d7e8088..9fa1ff4 100644 --- a/main/impl/impl_lvgl.c +++ b/main/impl/impl_lvgl.c @@ -10,7 +10,7 @@ #define EPD_DISPLAY_FRAME_SIZE (EPD_DISPLAY_PIXEL_COUNT * 2 / 8) #define EPD_DISPLAY_GS 0 -#define EPD_DISPLAY_MAX_PARTIAL 32 /* !! Must be even !! */ +#define EPD_DISPLAY_MAX_PARTIAL 64 /* !! Must be even !! */ static impl_epd_handle_t s_epd_impl; diff --git a/main/interface/if_standby.c b/main/interface/if_standby.c index 87118ab..590f91b 100644 --- a/main/interface/if_standby.c +++ b/main/interface/if_standby.c @@ -11,10 +11,15 @@ #define IF_EVENT_WIFI_BUTTON 0 #define IF_EVENT_BAT_BUTTON 1 +typedef struct { + if_standby_component_t component; + char payload[IF_STANDBY_PAYLOAD_LENGTH]; +} if_standby_request_t; + static TaskHandle_t s_if_standby_handle; +static QueueHandle_t s_standby_screen_queue; lv_obj_t *g_standby_screen; -QueueHandle_t g_standby_screen_queue; void if_standby_event_handler(lv_event_t *ev) { int button_event = (int)ev->user_data; @@ -147,35 +152,35 @@ void if_standby_task(void *pvParameters) { ESP_LOGI(LOG_TAG, "Standby screen created."); - if_standby_queue_t queue_item; + if_standby_request_t update_request; for (;;) { // Read from queue for UI element updates. - xQueueReceive(g_standby_screen_queue, &queue_item, portMAX_DELAY); + xQueueReceive(s_standby_screen_queue, &update_request, portMAX_DELAY); impl_lvgl_lock(); - switch (queue_item.item) { - case IF_STANDBY_ITEM_TEMP: - lv_label_set_text(temp_label, queue_item.payload); + switch (update_request.component) { + case IF_STANDBY_COMPONENT_TEMP: + lv_label_set_text(temp_label, update_request.payload); lv_obj_align(temp_label, LV_ALIGN_CENTER, 0, 16); break; - case IF_STANDBY_ITEM_HUMID: - lv_label_set_text(humid_label, queue_item.payload); + case IF_STANDBY_COMPONENT_HUMID: + lv_label_set_text(humid_label, update_request.payload); lv_obj_align(humid_label, LV_ALIGN_CENTER, 0, 16); break; - case IF_STANDBY_ITEM_TIME: - lv_label_set_text(time_label, queue_item.payload); + case IF_STANDBY_COMPONENT_TIME: + lv_label_set_text(time_label, update_request.payload); lv_obj_set_align(time_label, LV_ALIGN_CENTER); break; - case IF_STANDBY_ITEM_DATE: - lv_label_set_text(date_label, queue_item.payload); + case IF_STANDBY_COMPONENT_DATE: + lv_label_set_text(date_label, update_request.payload); lv_obj_set_align(date_label, LV_ALIGN_CENTER); break; - case IF_STANDBY_ITEM_WEEKDAY: - lv_label_set_text(weekday_label, queue_item.payload); + case IF_STANDBY_COMPONENT_WEEKDAY: + lv_label_set_text(weekday_label, update_request.payload); lv_obj_set_align(weekday_label, LV_ALIGN_CENTER); break; - case IF_STANDBY_ITEM_WIFI: - lv_label_set_text(network_status_label, queue_item.payload); + case IF_STANDBY_COMPONENT_WIFI: + lv_label_set_text(network_status_label, update_request.payload); lv_obj_set_align(network_status_label, LV_ALIGN_CENTER); break; default: @@ -191,8 +196,8 @@ esp_err_t if_standby_init(void) { lv_disp_load_scr(g_standby_screen); impl_lvgl_unlock(); - g_standby_screen_queue = xQueueCreate(8, sizeof(if_standby_queue_t)); - if (g_standby_screen_queue == NULL) { + s_standby_screen_queue = xQueueCreate(8, sizeof(if_standby_request_t)); + if (s_standby_screen_queue == NULL) { return ESP_FAIL; } @@ -200,5 +205,22 @@ esp_err_t if_standby_init(void) { return ESP_FAIL; } + return ESP_OK; +} + +esp_err_t if_standby_component_update(if_standby_component_t component, const char *fmt, ...) { + if_standby_request_t req; + va_list ap; + + req.component = component; + + va_start(ap, fmt); + vsnprintf(req.payload, IF_STANDBY_PAYLOAD_LENGTH, fmt, ap); + va_end(ap); + + if(xQueueSendToBack(s_standby_screen_queue, &req, 0) != pdPASS) { + return ESP_FAIL; + } + return ESP_OK; } \ No newline at end of file diff --git a/main/interface/if_standby.h b/main/interface/if_standby.h index e3795ac..1f3fdfa 100644 --- a/main/interface/if_standby.h +++ b/main/interface/if_standby.h @@ -11,23 +11,18 @@ #define IF_STANDBY_PAYLOAD_LENGTH 16 typedef enum { - IF_STANDBY_ITEM_WIFI, - IF_STANDBY_ITEM_WEEKDAY, - IF_STANDBY_ITEM_DATE, - IF_STANDBY_ITEM_BAT, - IF_STANDBY_ITEM_TIME, - IF_STANDBY_ITEM_TEMP, - IF_STANDBY_ITEM_HUMID, -} if_standby_item_t ; - -typedef struct { - if_standby_item_t item; - char payload[IF_STANDBY_PAYLOAD_LENGTH]; -} if_standby_queue_t; + IF_STANDBY_COMPONENT_WIFI, + IF_STANDBY_COMPONENT_WEEKDAY, + IF_STANDBY_COMPONENT_DATE, + IF_STANDBY_COMPONENT_BAT, + IF_STANDBY_COMPONENT_TIME, + IF_STANDBY_COMPONENT_TEMP, + IF_STANDBY_COMPONENT_HUMID, +} if_standby_component_t; extern lv_obj_t *g_standby_screen; -extern QueueHandle_t g_standby_screen_queue; esp_err_t if_standby_init(void); +esp_err_t if_standby_component_update(if_standby_component_t component, const char *fmt, ...); #endif \ No newline at end of file diff --git a/main/service/service_clock.c b/main/service/service_clock.c index 2a83fc8..a643e02 100644 --- a/main/service/service_clock.c +++ b/main/service/service_clock.c @@ -9,7 +9,7 @@ static TaskHandle_t s_clock_task_handle; -static char *s_wday_array[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI"}; +static const char *s_wday_array[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI"}; void clock_task(void *pvParameters) { struct tm timeinfo; @@ -19,8 +19,6 @@ void clock_task(void *pvParameters) { uint8_t prev_mday = 0xFF; uint8_t prev_wday = 0xFF; - if_standby_queue_t queue_item; - setenv("TZ", "CST-8", 1); tzset(); @@ -29,25 +27,18 @@ void clock_task(void *pvParameters) { localtime_r(&now, &timeinfo); if ((timeinfo.tm_min != prev_min) || (prev_min == 0xFF)) { - queue_item.item = IF_STANDBY_ITEM_TIME; - snprintf(queue_item.payload, IF_STANDBY_PAYLOAD_LENGTH, "%02u:%02u", timeinfo.tm_hour, timeinfo.tm_min); - xQueueSendToBack(g_standby_screen_queue, &queue_item, 0); + if_standby_component_update(IF_STANDBY_COMPONENT_TIME, "%02u:%02u", timeinfo.tm_hour, timeinfo.tm_min); prev_min = timeinfo.tm_min; } if ((timeinfo.tm_mday != prev_mday) || (prev_mday == 0xFF)) { - queue_item.item = IF_STANDBY_ITEM_DATE; - snprintf(queue_item.payload, IF_STANDBY_PAYLOAD_LENGTH, "%04u/%02d/%02d", - (uint16_t)(timeinfo.tm_year + 1900), (uint8_t)(timeinfo.tm_mon + 1), (uint8_t)timeinfo.tm_mday); - xQueueSendToBack(g_standby_screen_queue, &queue_item, 0); + if_standby_component_update(IF_STANDBY_COMPONENT_DATE, "%04u/%02d/%02d", timeinfo.tm_year + 1900, + timeinfo.tm_mon + 1, timeinfo.tm_mday); prev_mday = timeinfo.tm_mday; } if ((timeinfo.tm_wday != prev_wday) || (prev_wday == 0xFF)) { - queue_item.item = IF_STANDBY_ITEM_WEEKDAY; - memcpy(queue_item.payload, s_wday_array[timeinfo.tm_wday], 4); - xQueueSendToBack(g_standby_screen_queue, &queue_item, 0); - + if_standby_component_update(IF_STANDBY_COMPONENT_WEEKDAY, s_wday_array[timeinfo.tm_wday]); prev_wday = timeinfo.tm_wday; } diff --git a/main/service/service_dht.c b/main/service/service_dht.c index 40204bd..0461a95 100644 --- a/main/service/service_dht.c +++ b/main/service/service_dht.c @@ -19,8 +19,6 @@ static htu21d_t s_htu = { static TaskHandle_t s_dht_task_handle; void dht_task(void *pvParameters) { - if_standby_queue_t queue_item; - htu21d_result_t result; for (;;) { @@ -28,13 +26,8 @@ void dht_task(void *pvParameters) { vTaskDelay(pdMS_TO_TICKS(100)); } - queue_item.item = IF_STANDBY_ITEM_TEMP, - snprintf(queue_item.payload, IF_STANDBY_PAYLOAD_LENGTH, "%.2f", result.temperature); - xQueueSendToBack(g_standby_screen_queue, &queue_item, 0); - - queue_item.item = IF_STANDBY_ITEM_HUMID; - snprintf(queue_item.payload, IF_STANDBY_PAYLOAD_LENGTH, "%.2f", result.humidity); - xQueueSendToBack(g_standby_screen_queue, &queue_item, 0); + if_standby_component_update(IF_STANDBY_COMPONENT_TEMP, "%.2f", result.temperature); + if_standby_component_update(IF_STANDBY_COMPONENT_HUMID, "%.2f", result.humidity); vTaskDelay(pdMS_TO_TICKS(30000)); } diff --git a/main/service/service_wifi.c b/main/service/service_wifi.c index ba22b57..89e9652 100644 --- a/main/service/service_wifi.c +++ b/main/service/service_wifi.c @@ -66,12 +66,8 @@ void service_wifi_task(void *pvParameters) { } } - if_standby_queue_t queue_item; - if (!wifi_config_valid) { - queue_item.item = IF_STANDBY_ITEM_WIFI; - snprintf(queue_item.payload, IF_STANDBY_PAYLOAD_LENGTH, "\U000F092D"); - xQueueSend(g_standby_screen_queue, &queue_item, 0); + if_standby_component_update(IF_STANDBY_COMPONENT_WIFI, "\U000F092D"); } wifi_config_t wifi_config = { @@ -97,9 +93,7 @@ void service_wifi_task(void *pvParameters) { if (xQueueReceive(s_service_wifi_event_queue, &queue_event, portMAX_DELAY) == pdPASS) { switch (queue_event) { case SERVICE_WIFI_EVENT_CONNECTED: - queue_item.item = IF_STANDBY_ITEM_WIFI; - snprintf(queue_item.payload, IF_STANDBY_PAYLOAD_LENGTH, "\U000F0928"); - xQueueSend(g_standby_screen_queue, &queue_item, 0); + if_standby_component_update(IF_STANDBY_COMPONENT_WIFI, "\U000F0928"); break; default: break;