Skip to content

Commit

Permalink
Switch to callback-based approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Master92 committed Nov 13, 2024
1 parent 1ce4d07 commit 5a4a4a9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
10 changes: 7 additions & 3 deletions src/ui/page_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}

Expand All @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions src/ui/page_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -316,6 +316,10 @@ static void page_wifi_update_settings() {
system_script(WIFI_STA_ON);
}
}

if (complete_callback != NULL) {
complete_callback();
}
}

/**
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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,
};

/**
Expand Down
37 changes: 14 additions & 23 deletions src/ui/ui_main_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,39 +353,30 @@ 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;

for (uint32_t i = 0; i < PAGE_COUNT; i++) {
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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/ui/ui_main_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5a4a4a9

Please sign in to comment.