Skip to content

Commit

Permalink
Merge pull request hd-zero#425 from SumolX/optimize-bootup-time
Browse files Browse the repository at this point in the history
Decreased overall bootup time.
  • Loading branch information
ligenxxxx authored Jun 19, 2024
2 parents d33e8eb + d1ee550 commit 84fdd30
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 5 deletions.
13 changes: 11 additions & 2 deletions mkapp/app/services/startup.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#!/bin/sh

for FILE in $(ls /mnt/app/services/scripts/)
if [ ! -f /mnt/app/services/scripts/install/install.done ]; then
for FILE in $(ls /mnt/app/services/scripts/install/)
do
echo "Installing service: $FILE"
/mnt/app/services/scripts/install/$FILE &
done
touch /mnt/app/services/scripts/install/install.done
fi

for FILE in $(ls /mnt/app/services/scripts/runtime/)
do
echo "Starting service: $FILE"
/mnt/app/services/scripts/$FILE &
/mnt/app/services/scripts/runtime/$FILE &
done
6 changes: 5 additions & 1 deletion src/core/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "util/sdcard.h"
#include "util/system.h"

void (*sdcard_ready_cb)() = NULL;

///////////////////////////////////////////////////////////////////////////////
// SD card exist
static void detect_sdcard(void) {
Expand All @@ -52,7 +54,9 @@ static void detect_sdcard(void) {
// Only repair card at bootup or when inserted
if (g_init_done) {
if (sdcard_init_scan && g_sdcard_enable) {
page_storage_init_auto_sd_repair();
if (sdcard_ready_cb) {
sdcard_ready_cb();
}
sdcard_init_scan = false;
} else if (!g_sdcard_enable && sdcard_enable_last) {
sdcard_init_scan = true;
Expand Down
22 changes: 22 additions & 0 deletions src/ui/page_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "util/sdcard.h"
#include "util/system.h"

extern void (*sdcard_ready_cb)();

/**
* Types
*/
Expand Down Expand Up @@ -59,6 +61,7 @@ typedef struct {
bool disable_controls;
bool is_sd_repair_active;
bool is_auto_sd_repair_active;
bool is_sd_repair_complete;
} page_options_t;

/**
Expand Down Expand Up @@ -501,6 +504,18 @@ static void page_storage_on_click(uint8_t key, int sel) {
static void page_storage_on_right_button(bool is_short) {
}

/**
* Returns true once the thread has completed.
*/
static bool page_storage_is_sd_repair_complete() {
if (page_storage.is_sd_repair_complete) {
sdcard_ready_cb = page_storage_init_auto_sd_repair;
return true;
} else {
return false;
}
}

/**
* Main Menu page data structure, notice max is set to zero
* in order to allow us to override default user input logic.
Expand All @@ -519,6 +534,9 @@ page_pack_t pp_storage = {
.on_roller = page_storage_on_roller,
.on_click = page_storage_on_click,
.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,
};

/**
Expand All @@ -534,6 +552,7 @@ static void *page_storage_repair_thread(void *arg) {
lv_label_set_text(page_storage.note, "");
page_storage.is_auto_sd_repair_active = false;
}
page_storage.is_sd_repair_complete = true;
pthread_exit(NULL);
}

Expand All @@ -548,10 +567,13 @@ bool page_storage_is_sd_repair_active() {
* Once initialized detach until completed.
*/
void page_storage_init_auto_sd_repair() {
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)) {
pthread_detach(tid);
}
} else {
page_storage.is_sd_repair_complete = true;
}
}
4 changes: 3 additions & 1 deletion src/ui/page_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,6 @@ static lv_obj_t *page_wifi_create(lv_obj_t *parent, panel_arr_t *arr) {
page_wifi_create_page_3(cont);
page_wifi_sync_settings();
page_wifi_update_current_page(0);
page_wifi_update_settings();

return page;
}
Expand Down Expand Up @@ -1123,6 +1122,9 @@ page_pack_t pp_wifi = {
.on_roller = page_wifi_on_roller,
.on_click = page_wifi_on_click,
.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
46 changes: 45 additions & 1 deletion src/ui/ui_main_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ static page_pack_t *page_packs[] = {

#define PAGE_COUNT (sizeof(page_packs) / sizeof(page_packs[0]))

static page_pack_t* post_bootup_actions[PAGE_COUNT + 1];

static page_pack_t *find_pp(lv_obj_t *page) {
for (uint32_t i = 0; i < PAGE_COUNT; i++) {
if (page_packs[i]->page == page) {
Expand Down Expand Up @@ -301,8 +303,25 @@ void main_menu_init(void) {
lv_obj_t *section = lv_menu_section_create(root_page);
lv_obj_clear_flag(section, LV_OBJ_FLAG_SCROLLABLE);

for (uint32_t i = 0; i < PAGE_COUNT; i++) {
for (uint32_t i = 0, j = 0; i < PAGE_COUNT; i++) {
main_menu_create_entry(menu, section, page_packs[i]);
if (page_packs[i]->post_bootup_run_priority > 0) {
post_bootup_actions[j++] = page_packs[i];
}
}

// Resort based on priority
for (uint32_t i = 0; i < PAGE_COUNT; ++i) {
for (uint32_t j = 1; j < PAGE_COUNT; ++j) {
if (post_bootup_actions[i] && post_bootup_actions[j]) {
if (post_bootup_actions[j]->post_bootup_run_priority <
post_bootup_actions[i]->post_bootup_run_priority) {
post_bootup_actions[PAGE_COUNT] = post_bootup_actions[i];
post_bootup_actions[i] = post_bootup_actions[j];
post_bootup_actions[j] = post_bootup_actions[PAGE_COUNT];
}
}
}
}

lv_obj_add_style(section, &style_rootmenu, LV_PART_MAIN);
Expand Down Expand Up @@ -332,12 +351,37 @@ void main_menu_init(void) {

void main_menu_update() {
static uint32_t delta_ms = 0;
static uint32_t last_bootup_action = 0;
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;
}
}
}
}
}
delta_ms = now_ms;
}
Expand Down
4 changes: 4 additions & 0 deletions src/ui/ui_main_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ typedef struct {
void (*on_roller)(uint8_t key);
void (*on_click)(uint8_t key, int sel);
void (*on_right_button)(bool is_short);

int32_t post_bootup_run_priority;
void(*post_bootup_run_function)();
bool(*post_bootup_run_complete)();
} page_pack_t;

typedef struct {
Expand Down

0 comments on commit 84fdd30

Please sign in to comment.