From 28240de3af04cf43ecec4293f3a560b3bdce5ce9 Mon Sep 17 00:00:00 2001 From: Kevin Jahaziel Leon Morales Date: Mon, 23 Sep 2024 14:19:42 -0600 Subject: [PATCH] feat: Fix WiFi screens - Fix bugs with delete WIFI AP --- firmware/components/cmd_wifi/cmd_wifi.c | 26 ++++- firmware/main/general/general_screens.c | 1 + .../settings/display/display_settings.c | 2 +- .../modules/settings/wifi/wifi_settings.c | 97 +++++++++++++------ 4 files changed, 91 insertions(+), 35 deletions(-) diff --git a/firmware/components/cmd_wifi/cmd_wifi.c b/firmware/components/cmd_wifi/cmd_wifi.c index ab81c78b..6fa556ff 100644 --- a/firmware/components/cmd_wifi/cmd_wifi.c +++ b/firmware/components/cmd_wifi/cmd_wifi.c @@ -135,6 +135,27 @@ static void cmd_wifi_delete_crendentials(int argc, char** argv) { preferences_remove(wifi_ssid); ESP_LOGI(__func__, "Deleted AP %s", wifi_ssid); + // Restore the AP indexes + int counter = 0; + for (int i = 0; i < count - 1; i++) { + char wifi_ap[100]; + char wifi_ssid[100]; + sprintf(wifi_ap, "wifi%d", i); + esp_err_t err = preferences_get_string(wifi_ap, wifi_ssid, 100); + if (err != ESP_OK) { + continue; + } + char wifi_pass[100]; + err = preferences_get_string(wifi_ssid, wifi_pass, 100); + if (err != ESP_OK) { + continue; + } + char wifi_ap_new[100]; + sprintf(wifi_ap_new, "wifi%d", counter); + preferences_put_string(wifi_ap_new, wifi_ssid); + counter++; + } + preferences_put_int("count_ap", count - 1); } @@ -164,8 +185,7 @@ static int cmd_wifi_show_aps(int argc, char** argv) { sprintf(wifi_ap, "wifi%d", i); esp_err_t err = preferences_get_string(wifi_ap, wifi_ssid, 100); if (err != ESP_OK) { - ESP_LOGW(__func__, "Error getting AP"); - return 1; + continue; } printf("[%i][%s] SSID: %s\n", i, wifi_ap, wifi_ssid); } @@ -199,8 +219,8 @@ static void event_handler(void* arg, xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { printf("Connected to AP"); - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); preferences_put_bool("wifi_connected", true); + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); } } diff --git a/firmware/main/general/general_screens.c b/firmware/main/general/general_screens.c index 2096ba48..7e192482 100644 --- a/firmware/main/general/general_screens.c +++ b/firmware/main/general/general_screens.c @@ -2,6 +2,7 @@ #include "general/bitmaps_general.h" #include "menus_module.h" #include "oled_screen.h" +#include "preferences.h" #define MAX_LINE_CHAR 16 diff --git a/firmware/main/modules/settings/display/display_settings.c b/firmware/main/modules/settings/display/display_settings.c index fa9fce7e..f90c9e8e 100644 --- a/firmware/main/modules/settings/display/display_settings.c +++ b/firmware/main/modules/settings/display/display_settings.c @@ -135,7 +135,7 @@ static void display_config_module_state_machine(uint8_t button_name, } switch (button_name) { case BUTTON_LEFT: - menus_module_restart(); + menus_module_exit_app(); break; case BUTTON_RIGHT: ESP_LOGI(TAG_DISPLAY_CONFIG, "Selected item: %d", selected_item); diff --git a/firmware/main/modules/settings/wifi/wifi_settings.c b/firmware/main/modules/settings/wifi/wifi_settings.c index c816c319..2809e86c 100644 --- a/firmware/main/modules/settings/wifi/wifi_settings.c +++ b/firmware/main/modules/settings/wifi/wifi_settings.c @@ -2,6 +2,7 @@ #include "bitmaps_general.h" #include "cmd_wifi.h" #include "esp_log.h" +#include "general/general_screens.h" #include "led_events.h" #include "menus_module.h" #include "modals_module.h" @@ -11,11 +12,15 @@ #define TAG_CONFIG_MODULE "CONFIG_MODULE" #ifdef CONFIG_RESOLUTION_128X64 - #define START_PAGE 2 - #define Y_N_OFFSET 4 + #define START_PAGE 2 + #define Y_N_OFFSET 4 + #define ITEMOFFSET 2 + #define ITEMSPERSCREEN 4 #else // CONFIG_RESOLUTION_128X32 - #define START_PAGE 0 - #define Y_N_OFFSET 1 + #define START_PAGE 0 + #define Y_N_OFFSET 2 + #define ITEMOFFSET 1 + #define ITEMSPERSCREEN 3 #endif static int selected_item = 0; @@ -64,9 +69,11 @@ static void config_module_wifi_display_selected_item(char* item_text, static void config_module_wifi_display_selected_item_center( char* item_text, uint8_t item_number) { - oled_screen_display_bitmap(minino_face, 36, (item_number * 8), 8, 8, + uint8_t x = (128 - (strlen(item_text) * 8)) / 2; + + oled_screen_display_bitmap(minino_face, x - 12, (item_number * 8), 8, 8, OLED_DISPLAY_NORMAL); - oled_screen_display_text(item_text, 56, item_number, OLED_DISPLAY_INVERT); + oled_screen_display_text(item_text, x, item_number, OLED_DISPLAY_INVERT); } static void config_module_wifi_display_not_wifi() { @@ -129,12 +136,17 @@ static void config_module_wifi_display_list() { return; } + int position = 1; + uint16_t start_item = (selected_item / ITEMSPERSCREEN) * ITEMSPERSCREEN; + ESP_LOGI(__func__, "Selected item: %d", validate_wifi_count()); - int index = (wifi_config_state.total_items > max_items) ? selected_item : 0; - int limit = (wifi_config_state.total_items > max_items) - ? (max_items + selected_item) - : max_items; - for (int i = index; i < limit; i++) { + // int index = (wifi_config_state.total_items > max_items) ? selected_item : + // 0; int limit = (wifi_config_state.total_items > max_items) + // ? (max_items + selected_item) + // : max_items; + for (int i = start_item; + i < start_item + ITEMSPERSCREEN && i < wifi_config_state.total_items; + i++) { char wifi_ap[100]; char wifi_ssid[100]; sprintf(wifi_ap, "wifi%d", i); @@ -146,14 +158,15 @@ static void config_module_wifi_display_list() { if (strlen(wifi_ssid) > 16) { wifi_ssid[16] = '\0'; } - int page = (wifi_config_state.total_items > max_items) - ? (i + 1) - selected_item - : (i + 1); + // int page = (wifi_config_state.total_items > max_items) + // ? (i + 1) - selected_item + // : (i + 1); if (i == selected_item) { - config_module_wifi_display_selected_item(wifi_ssid, page); + config_module_wifi_display_selected_item(wifi_ssid, position); } else { - oled_screen_display_text(wifi_ssid, 0, page, OLED_DISPLAY_NORMAL); + oled_screen_display_text(wifi_ssid, 0, position, OLED_DISPLAY_NORMAL); } + position = position + ITEMOFFSET; } } @@ -169,16 +182,18 @@ static void config_module_wifi_display_sel_options() { if (strlen(wifi_ssid) > 16) { wifi_ssid[16] = '\0'; } - oled_screen_clear(); - oled_screen_display_text("< Back", 0, 0, OLED_DISPLAY_NORMAL); - oled_screen_display_text_center(wifi_ssid, 1, OLED_DISPLAY_NORMAL); - int page = 3; + genera_screen_display_card_information(wifi_ssid, ""); + // oled_screen_clear(); + // oled_screen_display_text("< Back", 0, 0, OLED_DISPLAY_NORMAL); + // oled_screen_display_text_center(wifi_ssid, 1, OLED_DISPLAY_NORMAL); + int page = 2; for (int i = 0; options_wifi_menu[i] != NULL; i++) { if (i == selected_item) { - config_module_wifi_display_selected_item(options_wifi_menu[i], page); + config_module_wifi_display_selected_item_center(options_wifi_menu[i], + page); } else { - oled_screen_display_text(options_wifi_menu[i], 0, page, - OLED_DISPLAY_NORMAL); + oled_screen_display_text_center(options_wifi_menu[i], page, + OLED_DISPLAY_NORMAL); } page++; } @@ -197,14 +212,13 @@ static void config_module_wifi_display_forget_modal() { } static void config_module_wifi_display_connect_modal() { - oled_screen_clear(); - oled_screen_display_text_center("Connect this AP?", 1, OLED_DISPLAY_NORMAL); + genera_screen_display_card_information("Connect this AP?", ""); if (selected_item == 0) { - config_module_wifi_display_selected_item_center("YES", 4); - oled_screen_display_text_center("NO", 5, OLED_DISPLAY_NORMAL); + config_module_wifi_display_selected_item_center("YES", Y_N_OFFSET); + oled_screen_display_text_center("NO", Y_N_OFFSET + 1, OLED_DISPLAY_NORMAL); } else { - oled_screen_display_text_center("YES", 4, OLED_DISPLAY_NORMAL); - config_module_wifi_display_selected_item_center("NO", 5); + oled_screen_display_text_center("YES", Y_N_OFFSET, OLED_DISPLAY_NORMAL); + config_module_wifi_display_selected_item_center("NO", Y_N_OFFSET + 1); } } @@ -257,7 +271,6 @@ static void config_module_state_machine(uint8_t button_name, case BUTTON_UP: selected_item = (selected_item == 0) ? total_items - 1 : selected_item - 1; - config_module_wifi_display_list(); break; case BUTTON_DOWN: @@ -339,6 +352,7 @@ static void config_module_state_machine_config_modal_connect( connect_wifi(wifi_ssid, wifi_pass, config_module_wifi_handle_connection); menus_module_set_app_state(true, config_module_state_machine_config); + wifi_config_state.selected_item = 0; } else { selected_item = 0; wifi_config_state.state = WIFI_SETTING_IDLE; @@ -373,7 +387,7 @@ static void config_module_state_machine_config_modal_forget( ESP_LOGI(TAG_CONFIG_MODULE, "Selected item: %d", selected_item); if (selected_item == 0) { char wifi_ap[100]; - sprintf(wifi_ap, "wifi%d", selected_item); + sprintf(wifi_ap, "wifi%d", wifi_config_state.selected_item); char wifi_ssid[100]; preferences_get_string(wifi_ap, wifi_ssid, 100); esp_err_t err = preferences_remove(wifi_ssid); @@ -387,12 +401,33 @@ static void config_module_state_machine_config_modal_forget( return; } int count = preferences_get_int("count_ap", 0); + int counter = 0; + for (int i = 0; i < count - 1; i++) { + char wifi_ap[100]; + char wifi_ssid[100]; + sprintf(wifi_ap, "wifi%d", i); + esp_err_t err = preferences_get_string(wifi_ap, wifi_ssid, 100); + if (err != ESP_OK) { + continue; + } + char wifi_ap_new[100]; + sprintf(wifi_ap_new, "wifi%d", counter); + preferences_put_string(wifi_ap_new, wifi_ssid); + counter++; + } err = preferences_put_int("count_ap", count - 1); if (err != ESP_OK) { ESP_LOGW(__func__, "Error removing AP"); return; } } + int count = validate_wifi_count(); + if (count == 0) { + menus_module_set_app_state(true, only_exit_input_cb); + return; + } + wifi_config_state.total_items = count; + total_items = count; selected_item = 0; wifi_config_state.state = WIFI_SETTING_IDLE; menus_module_set_app_state(true, config_module_state_machine_config);