From fe95c1c11e76adee0c45689bf3874af08835be37 Mon Sep 17 00:00:00 2001 From: Master92 Date: Wed, 13 Nov 2024 10:46:37 +0000 Subject: [PATCH] Switch to callback-based approach --- src/ui/page_storage.c | 10 +++++++--- src/ui/page_wifi.c | 9 ++++++--- src/ui/ui_main_menu.c | 37 ++++++++++++++----------------------- src/ui/ui_main_menu.h | 3 +-- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/ui/page_storage.c b/src/ui/page_storage.c index 27ebfee7..2051d1c8 100644 --- a/src/ui/page_storage.c +++ b/src/ui/page_storage.c @@ -567,13 +567,13 @@ page_pack_t pp_storage = { .on_right_button = page_storage_on_right_button, .post_bootup_run_priority = 50, .post_bootup_run_function = page_storage_init_auto_sd_repair, - .post_bootup_run_complete = page_storage_is_sd_repair_complete, }; /** * Worker thread for repairing SD Card. */ static void *page_storage_repair_thread(void *arg) { + void (*complete_callback)() = arg; if (!page_storage.disable_controls) { page_storage.is_auto_sd_repair_active = true; disable_controls(); @@ -584,6 +584,10 @@ static void *page_storage_repair_thread(void *arg) { page_storage.is_auto_sd_repair_active = false; } page_storage.is_sd_repair_complete = true; + + if (complete_callback != NULL) { + complete_callback(); + } pthread_exit(NULL); } @@ -597,11 +601,11 @@ bool page_storage_is_sd_repair_active() { /** * Once initialized detach until completed. */ -void page_storage_init_auto_sd_repair() { +void page_storage_init_auto_sd_repair(void (*complete_callback)()) { page_storage.is_sd_repair_complete = false; if (!page_storage.is_auto_sd_repair_active) { pthread_t tid; - if (!pthread_create(&tid, NULL, page_storage_repair_thread, NULL)) { + if (!pthread_create(&tid, NULL, page_storage_repair_thread, complete_callback)) { pthread_detach(tid); } } else { diff --git a/src/ui/page_wifi.c b/src/ui/page_wifi.c index 9f4d5e18..d5336a47 100644 --- a/src/ui/page_wifi.c +++ b/src/ui/page_wifi.c @@ -265,7 +265,7 @@ static void page_wifi_mask_password(lv_obj_t *obj, int size) { * Note: This function will be invoked asynchronously post bootup and may * require additional APP_STATE checks to ensure integrity of execution. */ -static void page_wifi_update_settings() { +static void page_wifi_update_settings(void (*complete_callback)()) { g_setting.wifi.enable = btn_group_get_sel(&page_wifi.page_1.enable.button) == 0; g_setting.wifi.mode = btn_group_get_sel(&page_wifi.page_1.mode.button); g_setting.wifi.dhcp = btn_group_get_sel(&page_wifi.page_2.dhcp.button) == 0; @@ -316,6 +316,10 @@ static void page_wifi_update_settings() { system_script(WIFI_STA_ON); } } + + if (complete_callback != NULL) { + complete_callback(); + } } /** @@ -576,7 +580,7 @@ static void page_wifi_apply_settings_pending_cb(struct _lv_timer_t *timer) { * Callback invoked once `Apply Settings` is triggered and confirmed via the menu. */ static void page_wifi_apply_settings_timer_cb(struct _lv_timer_t *timer) { - page_wifi_update_settings(); + page_wifi_update_settings(NULL); page_wifi_dirty_flag_reset(); page_wifi_apply_settings_reset(); } @@ -1129,7 +1133,6 @@ page_pack_t pp_wifi = { .on_right_button = page_wifi_on_right_button, .post_bootup_run_priority = 100, .post_bootup_run_function = page_wifi_update_settings, - .post_bootup_run_complete = NULL, }; /** diff --git a/src/ui/ui_main_menu.c b/src/ui/ui_main_menu.c index c71bad34..8b45e3ae 100644 --- a/src/ui/ui_main_menu.c +++ b/src/ui/ui_main_menu.c @@ -353,9 +353,18 @@ void main_menu_init(void) { keyboard_init(); } +static void handle_bootup_action() { + static page_pack_t **next_bootup_action = &post_bootup_actions[0]; + if (next_bootup_action - &post_bootup_actions[0] >= post_bootup_actions_count) { + return; + } + + (*next_bootup_action++)->post_bootup_run_function(handle_bootup_action); +} + void main_menu_update() { static uint32_t delta_ms = 0; - static uint32_t last_bootup_action = 0; + static bool bootup_actions_fired = false; uint32_t now_ms = time_ms(); delta_ms = now_ms - delta_ms; @@ -363,29 +372,11 @@ void main_menu_update() { if (page_packs[i]->on_update) { page_packs[i]->on_update(delta_ms); } + } - if (last_bootup_action == i && post_bootup_actions[i]) { - if (post_bootup_actions[i] != NULL) { - // Function invokation - if (post_bootup_actions[i]->post_bootup_run_complete == NULL) { - if (post_bootup_actions[i]->post_bootup_run_function != NULL) { - post_bootup_actions[i]->post_bootup_run_function(); - post_bootup_actions[i]->post_bootup_run_function = NULL; - post_bootup_actions[i] = NULL; - ++last_bootup_action; - } - } else { // Thread invokation - if (post_bootup_actions[i]->post_bootup_run_function){ - post_bootup_actions[i]->post_bootup_run_function(); - post_bootup_actions[i]->post_bootup_run_function = NULL; - } else if (post_bootup_actions[i]->post_bootup_run_complete()) { - post_bootup_actions[i]->post_bootup_run_complete = NULL; - post_bootup_actions[i] = NULL; - ++last_bootup_action; - } - } - } - } + if (!bootup_actions_fired) { + handle_bootup_action(); + bootup_actions_fired = true; } delta_ms = now_ms; } diff --git a/src/ui/ui_main_menu.h b/src/ui/ui_main_menu.h index 73f2da1f..2f172190 100644 --- a/src/ui/ui_main_menu.h +++ b/src/ui/ui_main_menu.h @@ -29,8 +29,7 @@ typedef struct { void (*on_right_button)(bool is_short); int32_t post_bootup_run_priority; - void(*post_bootup_run_function)(); - bool(*post_bootup_run_complete)(); + void(*post_bootup_run_function)(void(*complete_callback)()); } page_pack_t; typedef struct {