diff options
author | Renze Nicolai <renze@rnplus.nl> | 2022-05-30 23:56:23 +0200 |
---|---|---|
committer | Renze Nicolai <renze@rnplus.nl> | 2022-05-30 23:56:23 +0200 |
commit | 3a63e597512ab1ee9c6fa6996609a6dd5e2cbaec (patch) | |
tree | 904bfd8ca8232967d7ebd14bd82b79aec54c453c /main/main.c | |
parent | d61750ceed9f2ae9dc9159a158a50c946a25360d (diff) | |
download | mch2022-3a63e597512ab1ee9c6fa6996609a6dd5e2cbaec.tar.gz mch2022-3a63e597512ab1ee9c6fa6996609a6dd5e2cbaec.tar.bz2 mch2022-3a63e597512ab1ee9c6fa6996609a6dd5e2cbaec.zip |
Refactoring
Diffstat (limited to 'main/main.c')
-rw-r--r-- | main/main.c | 252 |
1 files changed, 42 insertions, 210 deletions
diff --git a/main/main.c b/main/main.c index b7d4cb3..02caf18 100644 --- a/main/main.c +++ b/main/main.c @@ -40,7 +40,20 @@ #include "esp_vfs.h" #include "esp_vfs_fat.h" -#include "fpga_download.h" +#include <pax_codecs.h> + +#include "audio.h" + +#include "bootscreen.h" + +#include "menus/start.h" + +extern const uint8_t wallpaper_png_start[] asm("_binary_wallpaper_png_start"); +extern const uint8_t wallpaper_png_end[] asm("_binary_wallpaper_png_end"); + +extern const uint8_t logo_screen_png_start[] asm("_binary_logo_screen_png_start"); +extern const uint8_t logo_screen_png_end[] asm("_binary_logo_screen_png_end"); + static const char *TAG = "main"; @@ -113,113 +126,6 @@ void appfs_store_app(pax_buf_t* pax_buffer, ILI9341* ili9341, char* path, char* return; } -void menu_launcher(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341, menu_action_t* menu_action, appfs_handle_t* appfs_fd) { - menu_t* menu = menu_alloc("Main menu"); - *appfs_fd = APPFS_INVALID_FD; - *menu_action = ACTION_NONE; - - while (1) { - *appfs_fd = appfsNextEntry(*appfs_fd); - if (*appfs_fd == APPFS_INVALID_FD) break; - const char* name = NULL; - appfsEntryInfo(*appfs_fd, &name, NULL); - menu_args_t* args = malloc(sizeof(menu_args_t)); - args->fd = *appfs_fd; - args->action = ACTION_APPFS; - menu_insert_item(menu, name, NULL, (void*) args, -1); - } - *appfs_fd = APPFS_INVALID_FD; - - menu_args_t* install_args = malloc(sizeof(menu_args_t)); - install_args->action = ACTION_INSTALLER; - menu_insert_item(menu, "Hatchery", NULL, install_args, -1); - - menu_args_t* settings_args = malloc(sizeof(menu_args_t)); - settings_args->action = ACTION_SETTINGS; - menu_insert_item(menu, "WiFi settings", NULL, settings_args, -1); - - menu_args_t* ota_args = malloc(sizeof(menu_args_t)); - ota_args->action = ACTION_OTA; - menu_insert_item(menu, "Firmware update", NULL, ota_args, -1); - - menu_args_t* fpga_dl_args = malloc(sizeof(menu_args_t)); - fpga_dl_args->action = ACTION_FPGA_DL; - menu_insert_item(menu, "FPGA download", NULL, fpga_dl_args, -1); - - menu_args_t* fpga_args = malloc(sizeof(menu_args_t)); - fpga_args->action = ACTION_FPGA; - menu_insert_item(menu, "FPGA test", NULL, fpga_args, -1); - - menu_args_t* rp2040bl_args = malloc(sizeof(menu_args_t)); - rp2040bl_args->action = ACTION_RP2040_BL; - menu_insert_item(menu, "RP2040 bootloader", NULL, rp2040bl_args, -1); - - menu_args_t* wifi_connect_args = malloc(sizeof(menu_args_t)); - wifi_connect_args->action = ACTION_WIFI_CONNECT; - menu_insert_item(menu, "WiFi connect", NULL, wifi_connect_args, -1); - - menu_args_t* file_browser_args = malloc(sizeof(menu_args_t)); - file_browser_args->action = ACTION_FILE_BROWSER; - menu_insert_item(menu, "File browser (sd card)", NULL, file_browser_args, -1); - - menu_args_t* file_browser_int_args = malloc(sizeof(menu_args_t)); - file_browser_int_args->action = ACTION_FILE_BROWSER_INT; - menu_insert_item(menu, "File browser (internal)", NULL, file_browser_int_args, -1); - - menu_args_t* uninstall_args = malloc(sizeof(menu_args_t)); - uninstall_args->action = ACTION_UNINSTALL; - menu_insert_item(menu, "Uninstall app", NULL, uninstall_args, -1); - - bool render = true; - menu_args_t* menuArgs = NULL; - - while (1) { - rp2040_input_message_t buttonMessage = {0}; - if (xQueueReceive(buttonQueue, &buttonMessage, 16 / portTICK_PERIOD_MS) == pdTRUE) { - uint8_t pin = buttonMessage.input; - bool value = buttonMessage.state; - switch(pin) { - case RP2040_INPUT_JOYSTICK_DOWN: - if (value) { - menu_navigate_next(menu); - render = true; - } - break; - case RP2040_INPUT_JOYSTICK_UP: - if (value) { - menu_navigate_previous(menu); - render = true; - } - break; - case RP2040_INPUT_BUTTON_ACCEPT: - if (value) { - menuArgs = menu_get_callback_args(menu, menu_get_position(menu)); - } - break; - default: - break; - } - } - - if (render) { - graphics_task(pax_buffer, ili9341, menu, NULL); - render = false; - } - - if (menuArgs != NULL) { - *appfs_fd = menuArgs->fd; - *menu_action = menuArgs->action; - break; - } - } - - for (size_t index = 0; index < menu_get_length(menu); index++) { - free(menu_get_callback_args(menu, index)); - } - - menu_free(menu); -} - void menu_wifi_settings(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341, menu_action_t* menu_action) { menu_t* menu = menu_alloc("WiFi settings"); *menu_action = ACTION_NONE; @@ -262,6 +168,7 @@ void menu_wifi_settings(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341 } break; case RP2040_INPUT_BUTTON_ACCEPT: + case RP2040_INPUT_JOYSTICK_PRESS: if (value) { menuArgs = menu_get_callback_args(menu, menu_get_position(menu)); } @@ -272,7 +179,8 @@ void menu_wifi_settings(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341 } if (render) { - graphics_task(pax_buffer, ili9341, menu, NULL); + menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF72008a); + ili9341_write(ili9341, pax_buffer->buf); render = false; } @@ -289,13 +197,6 @@ void menu_wifi_settings(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341 menu_free(menu); } -void display_boot_screen(pax_buf_t* pax_buffer, ILI9341* ili9341) { - pax_noclip(pax_buffer); - pax_background(pax_buffer, 0x325aa8); - pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, "Starting launcher..."); - ili9341_write(ili9341, pax_buffer->buf); -} - void display_fatal_error(pax_buf_t* pax_buffer, ILI9341* ili9341, const char* line0, const char* line1, const char* line2, const char* line3) { pax_noclip(pax_buffer); pax_background(pax_buffer, 0xa85a32); @@ -306,75 +207,6 @@ void display_fatal_error(pax_buf_t* pax_buffer, ILI9341* ili9341, const char* li ili9341_write(ili9341, pax_buffer->buf); } -void wifi_connect_to_stored() { - // Open NVS. - nvs_handle_t handle; - nvs_open("system", NVS_READWRITE, &handle); - uint8_t use_ent; - char *ssid = NULL; - char *ident = NULL; - char *anon_ident = NULL; - char *password = NULL; - size_t len; - - // Read NVS. - esp_err_t res; - // Read SSID. - res = nvs_get_str(handle, "wifi.ssid", NULL, &len); - if (res) goto errcheck; - ssid = malloc(len); - res = nvs_get_str(handle, "wifi.ssid", ssid, &len); - if (res) goto errcheck; - // Check whether connection is enterprise. - res = nvs_get_u8(handle, "wifi.use_ent", &use_ent); - if (res) goto errcheck; - if (use_ent) { - // Read enterprise-specific parameters. - // Read identity. - res = nvs_get_str(handle, "wifi.ident", NULL, &len); - if (res) goto errcheck; - ident = malloc(len); - res = nvs_get_str(handle, "wifi.ident", ident, &len); - // Read anonymous identity. - res = nvs_get_str(handle, "wifi.anon_ident", NULL, &len); - if (res) goto errcheck; - anon_ident = malloc(len); - res = nvs_get_str(handle, "wifi.anon_ident", anon_ident, &len); - if (res) goto errcheck; - } - // Read password. - res = nvs_get_str(handle, "wifi.password", NULL, &len); - if (res) goto errcheck; - password = malloc(len); - res = nvs_get_str(handle, "wifi.password", password, &len); - if (res) goto errcheck; - - // Close NVS. - nvs_close(handle); - - // Open the appropriate connection. - if (use_ent) { - wifi_connect_ent(ssid, ident, anon_ident, password, 3); - } else { - wifi_connect(ssid, password, WIFI_AUTH_WPA2_PSK, 3); - } - - errcheck: - if (res == ESP_ERR_NVS_NOT_FOUND || res == ESP_ERR_NVS_NOT_INITIALIZED) { - // When NVS is not initialised. - ESP_LOGI(TAG, "WiFi settings not stored in NVS."); - } else if (res) { - // Other errors. - ESP_LOGE(TAG, "Error connecting to WiFi: %s", esp_err_to_name(res)); - } - - // Free memory. - if (ssid) free(ssid); - if (ident) free(ident); - if (anon_ident) free(anon_ident); - if (password) free(password); -} - void list_files_in_folder(const char* path) { DIR* dir = opendir(path); if (dir == NULL) { @@ -488,6 +320,7 @@ void uninstall_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* } break; case RP2040_INPUT_BUTTON_ACCEPT: + case RP2040_INPUT_JOYSTICK_PRESS: if (value) { menuArgs = menu_get_callback_args(menu, menu_get_position(menu)); } @@ -501,7 +334,8 @@ void uninstall_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* } if (render) { - graphics_task(pax_buffer, ili9341, menu, NULL); + menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF72008a); + ili9341_write(ili9341, pax_buffer->buf); render = false; } @@ -604,6 +438,7 @@ void file_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9 } break; case RP2040_INPUT_BUTTON_ACCEPT: + case RP2040_INPUT_JOYSTICK_PRESS: if (value) { menuArgs = menu_get_callback_args(menu, menu_get_position(menu)); } @@ -617,7 +452,8 @@ void file_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9 } if (render) { - graphics_task(pax_buffer, ili9341, menu, NULL); + menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF72008a); + ili9341_write(ili9341, pax_buffer->buf); render = false; } @@ -682,7 +518,7 @@ void app_main(void) { } - display_boot_screen(pax_buffer, ili9341); + display_boot_screen(pax_buffer, ili9341, "Initializing RP2040..."); if (bsp_rp2040_init() != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize the RP2040 co-processor"); @@ -707,6 +543,8 @@ void app_main(void) { printf("RP2040 UID: %02X%02X%02X%02X%02X%02X%02X%02X\n", rp2040_uid[0], rp2040_uid[1], rp2040_uid[2], rp2040_uid[3], rp2040_uid[4], rp2040_uid[5], rp2040_uid[6], rp2040_uid[7]); + display_boot_screen(pax_buffer, ili9341, "Initializing ICE40..."); + if (bsp_ice40_init() != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize the ICE40 FPGA"); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "ICE40 FPGA error", NULL, NULL); @@ -718,6 +556,8 @@ void app_main(void) { ESP_LOGE(TAG, "ice40 is NULL"); esp_restart(); } + + display_boot_screen(pax_buffer, ili9341, "Initializing BNO055..."); if (bsp_bno055_init() != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize the BNO055 position sensor"); @@ -731,6 +571,8 @@ void app_main(void) { esp_restart(); } + display_boot_screen(pax_buffer, ili9341, "Initializing BME680..."); + if (bsp_bme680_init() != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize the BME680 position sensor"); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "BME680 sensor error", "Check I2C bus", "Remove SAO and try again"); @@ -742,6 +584,8 @@ void app_main(void) { ESP_LOGE(TAG, "bme680 is NULL"); esp_restart(); } + + display_boot_screen(pax_buffer, ili9341, "Initializing AppFS..."); /* Start AppFS */ res = appfs_init(); @@ -750,6 +594,8 @@ void app_main(void) { display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "AppFS failed to initialize", "Flash may be corrupted", NULL); esp_restart(); } + + display_boot_screen(pax_buffer, ili9341, "Initializing NVS..."); /* Start NVS */ res = nvs_init(); @@ -759,6 +605,8 @@ void app_main(void) { esp_restart(); } + display_boot_screen(pax_buffer, ili9341, "Initializing filesystem..."); + /* Start internal filesystem */ const esp_partition_t* fs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "locfd"); @@ -787,6 +635,8 @@ void app_main(void) { ESP_LOGI(TAG, "SD card filesystem mounted"); //list_files_in_folder("/sd"); } + + display_boot_screen(pax_buffer, ili9341, "Initializing LEDs..."); /* Start LEDs */ ws2812_init(GPIO_LED_DATA); @@ -798,30 +648,12 @@ void app_main(void) { /* Launcher menu */ while (true) { - menu_action_t menu_action; - appfs_handle_t appfs_fd; - menu_launcher(rp2040->queue, pax_buffer, ili9341, &menu_action, &appfs_fd); - if (menu_action == ACTION_APPFS) { - appfs_boot_app(appfs_fd); - } else if (menu_action == ACTION_FPGA) { + menu_action_t menu_action = ACTION_NONE; + appfs_handle_t appfs_fd = -1; + menu_start(rp2040->queue, pax_buffer, ili9341); + if (menu_action == ACTION_FPGA) { graphics_task(pax_buffer, ili9341, NULL, "Loading..."); fpga_test(ili9341, ice40, rp2040->queue); - } else if (menu_action == ACTION_FPGA_DL) { - fpga_download(rp2040->queue, ice40, pax_buffer, ili9341); - } else if (menu_action == ACTION_RP2040_BL) { - graphics_task(pax_buffer, ili9341, NULL, "RP2040 update..."); - rp2040_reboot_to_bootloader(rp2040); - esp_restart(); - } else if (menu_action == ACTION_INSTALLER) { - graphics_task(pax_buffer, ili9341, NULL, "Not implemented"); - } else if (menu_action == ACTION_WIFI_CONNECT) { - graphics_task(pax_buffer, ili9341, NULL, "Connecting..."); - wifi_connect_to_stored(); - } else if (menu_action == ACTION_OTA) { - graphics_task(pax_buffer, ili9341, NULL, "Connecting..."); - wifi_connect_to_stored(); - graphics_task(pax_buffer, ili9341, NULL, "Firmware update..."); - ota_update(); } else if (menu_action == ACTION_FILE_BROWSER) { file_browser(rp2040->queue, pax_buffer, ili9341, "/sd"); } else if (menu_action == ACTION_FILE_BROWSER_INT) { |