From 61e3b589923a8c61bc5f4894184551974fc06ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 7 May 2021 22:09:55 +0200 Subject: [PATCH] Use singly linked list dfor temperature profile file list --- stm-firmware/ui/gui.c | 81 +++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/stm-firmware/ui/gui.c b/stm-firmware/ui/gui.c index 74324b8..d3482fb 100644 --- a/stm-firmware/ui/gui.c +++ b/stm-firmware/ui/gui.c @@ -38,6 +38,8 @@ #include #include #include +#include +#include static char IN_SECTION(.ccm.bss) display_buffer[4][21] = {0}; static struct lcd_menu IN_SECTION(.ccm.bss) reflow_menu; @@ -404,44 +406,38 @@ static int safe_strncpy(char *dest, const char *src, size_t max_size) { return 0; } -/** - * @brief load_temperature_file_list_from_sdcard - * @return -1 File error, 0 successful - */ -static int load_temperature_file_list_from_sdcard(char (*list)[17], uint32_t len) +static void delete_file_list_entry(void *obj) +{ + if (obj) + free(obj); +} + +static SlList *load_temp_profile_list_from_sdcard(int *error) { - uint32_t i, j; DIR directory; FILINFO finfo; FRESULT fres; - - if (!list) - return -1001; - - /* Zero out the list */ - for (i = 0; i < len; i++) { - for (j = 0; j < sizeof(*list); j++) { - list[i][j] = 0; - } - } + SlList *list = NULL; + char *name; /* find the frist file */ fres = f_findfirst(&directory, &finfo, "/", "*.tpr"); - i = 0; while (fres == FR_OK && finfo.fname[0]) { - safe_strncpy(&list[i][0], finfo.fname, sizeof(*list)); + name = malloc(strlen(finfo.fname) + 1); + strcpy(name, finfo.fname); + list = sl_list_append(list, name); fres = f_findnext(&directory, &finfo); - i++; - if (i >= len) - break; } if (fres != FR_OK) { - return -1; + sl_list_free_full(list, delete_file_list_entry); + list = NULL; + if (error) + *error = -1; } - return (int)i; + return list; } static void gui_menu_temp_profile_execute(struct lcd_menu *menu, enum menu_entry_func_entry entry_type, void* parent) @@ -501,29 +497,29 @@ static void gui_menu_temp_profile_execute(struct lcd_menu *menu, enum menu_entry static void gui_menu_temp_profile_select(struct lcd_menu *menu, enum menu_entry_func_entry entry_type, void *parent) { static void *my_parent; - static char profile_list[10][17]; - static bool file_error = false; + static SlList *file_list = NULL; + static int file_error = 0; static enum pl_ret_val profile_ret_val = PL_RET_SUCCESS; static uint8_t currently_selected = 0U; static uint8_t loaded; int16_t delta; enum button_state button; - int res; - if (entry_type == MENU_ENTRY_FIRST_ENTER) { menu_display_clear(menu); my_parent = parent; - res = load_temperature_file_list_from_sdcard(profile_list, 10); - file_error = false; + file_error = 0; loaded = 0; - if (res < 0) { - file_error = true; + + if (file_list) { + sl_list_free_full(file_list, delete_file_list_entry); } + file_list = load_temp_profile_list_from_sdcard(&file_error); + currently_selected = 0u; profile_ret_val = PL_RET_SUCCESS; - loaded = (uint32_t)res; + loaded = sl_list_length(file_list); menu_lcd_outputf(menu, 0, "Select:"); } else if (entry_type == MENU_ENTRY_DROPBACK) { menu_entry_dropback(menu, my_parent); @@ -541,14 +537,20 @@ static void gui_menu_temp_profile_select(struct lcd_menu *menu, enum menu_entry_ if (file_error) { menu_lcd_outputf(menu, 0, "Disk Error"); menu_lcd_outputf(menu, 1, "SD inserted?"); - if (button == BUTTON_SHORT_RELEASED) + if (button == BUTTON_SHORT_RELEASED) { + sl_list_free_full(file_list, delete_file_list_entry); + file_list = NULL; menu_entry_dropback(menu, my_parent); + } return; } else if (loaded == 0) { menu_lcd_outputf(menu, 0, "No profiles"); menu_lcd_outputf(menu, 1, "found"); - if (button == BUTTON_SHORT_RELEASED) + if (button == BUTTON_SHORT_RELEASED) { + sl_list_free_full(file_list, delete_file_list_entry); + file_list = NULL; menu_entry_dropback(menu, my_parent); + } return; } else if (profile_ret_val != PL_RET_SUCCESS) { menu_lcd_outputf(menu, 0, "ERROR"); @@ -568,17 +570,22 @@ static void gui_menu_temp_profile_select(struct lcd_menu *menu, enum menu_entry_ break; } - if (button == BUTTON_SHORT_RELEASED) + if (button == BUTTON_SHORT_RELEASED) { + sl_list_free_full(file_list, delete_file_list_entry); + file_list = NULL; menu_entry_dropback(menu, my_parent); + } return; } else if (currently_selected < loaded) { /* Show currently selected profile */ - menu_lcd_outputf(menu, 1, "%s", &profile_list[currently_selected][0]); + menu_lcd_outputf(menu, 1, "%s", sl_list_nth(file_list, currently_selected)->data); if (button == BUTTON_SHORT_RELEASED) { /* Execute selected profile */ - profile_ret_val = temp_profile_executer_start(&profile_list[currently_selected][0]); + profile_ret_val = temp_profile_executer_start(sl_list_nth(file_list, currently_selected)->data); if (profile_ret_val == PL_RET_SUCCESS) { + sl_list_free_full(file_list, delete_file_list_entry); + file_list = NULL; menu_entry_enter(menu, gui_menu_temp_profile_execute, true); return; }