Refactoring

This commit is contained in:
Renze Nicolai 2022-05-30 23:56:23 +02:00
parent d61750ceed
commit 3a63e59751
39 changed files with 884 additions and 20552 deletions

3
.gitmodules vendored
View file

@ -34,3 +34,6 @@
[submodule "components/mch2022-bsp"] [submodule "components/mch2022-bsp"]
path = components/mch2022-bsp path = components/mch2022-bsp
url = https://github.com/badgeteam/esp32-component-mch2022-bsp.git url = https://github.com/badgeteam/esp32-component-mch2022-bsp.git
[submodule "components/pax-codecs"]
path = components/pax-codecs
url = https://github.com/robotman2412/pax-codecs.git

@ -1 +1 @@
Subproject commit 3eacf810ace52d0b3e11e6d5706adcadf5708626 Subproject commit f775f174c39426f3635f0e363b63504a1ac029e5

@ -1 +1 @@
Subproject commit 931b0bb849668a31bd5ab29e1e7b347ba986b539 Subproject commit cb7764d8435051ec2c0c8baa9e7e7920c4b3cdbe

@ -1 +1 @@
Subproject commit a7775f401057b64fad50476aa418eb046815f9a0 Subproject commit 8f74fa692d12566c79adbf36a08d17eb42859d7b

1
components/pax-codecs Submodule

@ -0,0 +1 @@
Subproject commit b79a40bd2952b82b686225bf3d9c5650f4a79362

@ -1 +1 @@
Subproject commit 8253eb498138dbae22d99d56b645a0ad567295c4 Subproject commit 8e603e9ca73a282a80e45e74160ab6c7c7fa4fff

View file

@ -9,8 +9,24 @@ idf_component_register(
"settings.c" "settings.c"
"system_wrapper.c" "system_wrapper.c"
"wifi_connection.c" "wifi_connection.c"
"wifi_connect.c"
"wifi_ota.c" "wifi_ota.c"
"fpga_download.c" "fpga_download.c"
INCLUDE_DIRS "." "include" "audio.c"
EMBED_TXTFILES ${project_dir}/server_certs/isrgrootx1.pem "bootscreen.c"
"menus/launcher.c"
"menus/settings.c"
"menus/start.c"
"menus/dev.c"
"menus/wifi.c"
INCLUDE_DIRS "."
"include"
"menus"
EMBED_TXTFILES ${project_dir}/resources/isrgrootx1.pem
EMBED_FILES ${project_dir}/resources/wallpaper.png
${project_dir}/resources/fpga_selftest.bin
${project_dir}/resources/rp2040_firmware.bin
${project_dir}/resources/boot.snd
${project_dir}/resources/mch2022_logo.png
${project_dir}/resources/logo_screen.png
) )

92
main/audio.c Normal file
View file

@ -0,0 +1,92 @@
#include "audio.h"
#include "freertos/FreeRTOS.h"
#include <freertos/task.h>
#include "esp_system.h"
#include "driver/i2s.h"
#include "driver/rtc_io.h"
#include <stdio.h>
#include <string.h>
void _audio_init(int i2s_num) {
i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_TX,
.sample_rate = 8000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
.dma_buf_count = 8,
.dma_buf_len = 256,
.intr_alloc_flags = 0,
.use_apll = false,
.bits_per_chan = I2S_BITS_PER_SAMPLE_16BIT
};
i2s_driver_install(i2s_num, &i2s_config, 0, NULL);
i2s_pin_config_t pin_config = {
.mck_io_num = 0,
.bck_io_num = 4,
.ws_io_num = 12,
.data_out_num = 13,
.data_in_num = I2S_PIN_NO_CHANGE
};
i2s_set_pin(i2s_num, &pin_config);
}
typedef struct _audio_player_cfg {
uint8_t* buffer;
size_t size;
bool free_buffer;
} audio_player_cfg_t;
void audio_player_task(void* arg) {
audio_player_cfg_t* config = (audio_player_cfg_t*) arg;
size_t sample_length = config->size;
uint8_t* sample_buffer = config->buffer;
size_t count;
size_t position = 0;
while (position < sample_length) {
size_t length = sample_length - position;
if (length > 256) length = 256;
uint8_t buffer[256];
memcpy(buffer, &sample_buffer[position], length);
for (size_t l = 0; l < length; l+=2) {
int16_t* sample = (int16_t*) &buffer[l];
*sample *= 0.50;
}
i2s_write(0, buffer, length, &count, portMAX_DELAY);
if (count != length) {
printf("i2s_write_bytes: count (%d) != length (%d)\n", count, length);
abort();
}
position += length;
}
i2s_zero_dma_buffer(0); // Fill buffer with silence
if (config->free_buffer) free(sample_buffer);
vTaskDelete(NULL); // Tell FreeRTOS that the task is done
}
void audio_init() {
_audio_init(0);
}
extern const uint8_t boot_snd_start[] asm("_binary_boot_snd_start");
extern const uint8_t boot_snd_end[] asm("_binary_boot_snd_end");
audio_player_cfg_t bootsound;
void play_bootsound() {
TaskHandle_t handle;
bootsound.buffer = boot_snd_start,
bootsound.size = boot_snd_end - boot_snd_start;
bootsound.free_buffer = false;
xTaskCreate(&audio_player_task, "Audio player", 4096, (void*) &bootsound, 10, &handle);
}

22
main/bootscreen.c Normal file
View file

@ -0,0 +1,22 @@
#include <stdio.h>
#include <string.h>
#include <sdkconfig.h>
#include "pax_gfx.h"
#include "pax_codecs.h"
#include "ili9341.h"
extern const uint8_t mch2022_logo_png_start[] asm("_binary_mch2022_logo_png_start");
extern const uint8_t mch2022_logo_png_end[] asm("_binary_mch2022_logo_png_end");
void display_boot_screen(pax_buf_t* pax_buffer, ILI9341* ili9341, const char* text) {
pax_noclip(pax_buffer);
pax_background(pax_buffer, 0xFFFFFF);
pax_buf_t logo;
pax_decode_png_buf(&logo, (void*) mch2022_logo_png_start, mch2022_logo_png_end - mch2022_logo_png_start, PAX_BUF_16_565RGB, 0);
pax_draw_image(pax_buffer, &logo, (320 / 2) - (212 / 2), ((240 - 32 - 10) / 2) - (160 / 2));
pax_buf_destroy(&logo);
pax_vec1_t size = pax_text_size(NULL, 18, text);
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, (320 / 2) - (size.x / 2), 240 - 32, text);
ili9341_write(ili9341, pax_buffer->buf);
}

File diff suppressed because it is too large Load diff

View file

@ -4,13 +4,14 @@
#include <esp_log.h> #include <esp_log.h>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/queue.h> #include <freertos/queue.h>
#include "fpga.h"
#include "selftest.h"
#include "ili9341.h" #include "ili9341.h"
#include "ice40.h" #include "ice40.h"
#include "rp2040.h" #include "rp2040.h"
#include "hardware.h" #include "hardware.h"
extern const uint8_t fpga_selftest_bin_start[] asm("_binary_fpga_selftest_bin_start");
extern const uint8_t fpga_selftest_bin_end[] asm("_binary_fpga_selftest_bin_end");
static const char *TAG = "fpga_test"; static const char *TAG = "fpga_test";
esp_err_t load_file_into_psram(ICE40* ice40, FILE* fd) { esp_err_t load_file_into_psram(ICE40* ice40, FILE* fd) {
@ -169,7 +170,6 @@ bool test_spi(ICE40* ice40) {
void fpga_test(ILI9341* ili9341, ICE40* ice40, xQueueHandle buttonQueue) { void fpga_test(ILI9341* ili9341, ICE40* ice40, xQueueHandle buttonQueue) {
esp_err_t res; esp_err_t res;
bool reload_fpga = false; bool reload_fpga = false;
bool load_old_bitstream = false;
do { do {
printf("Start FPGA test...\n"); printf("Start FPGA test...\n");
reload_fpga = false; reload_fpga = false;
@ -177,11 +177,7 @@ void fpga_test(ILI9341* ili9341, ICE40* ice40, xQueueHandle buttonQueue) {
ili9341_deinit(ili9341); ili9341_deinit(ili9341);
printf("FPGA load...\n"); printf("FPGA load...\n");
if (load_old_bitstream) { res = ice40_load_bitstream(ice40, fpga_selftest_bin_start, fpga_selftest_bin_end - fpga_selftest_bin_start);
res = ice40_load_bitstream(ice40, proto2_bin, sizeof(proto2_bin));
} else {
res = ice40_load_bitstream(ice40, selftest_sw_bin, sizeof(selftest_sw_bin));
}
if (res != ESP_OK) { if (res != ESP_OK) {
printf("Failed to load app bitstream into FPGA (%d)\n", res); printf("Failed to load app bitstream into FPGA (%d)\n", res);
ice40_disable(ice40); ice40_disable(ice40);
@ -214,12 +210,10 @@ void fpga_test(ILI9341* ili9341, ICE40* ice40, xQueueHandle buttonQueue) {
break; break;
case RP2040_INPUT_BUTTON_BACK: case RP2040_INPUT_BUTTON_BACK:
reload_fpga = true; reload_fpga = true;
load_old_bitstream = true;
waitForChoice = false; waitForChoice = false;
break; break;
case RP2040_INPUT_BUTTON_ACCEPT: case RP2040_INPUT_BUTTON_ACCEPT:
reload_fpga = true; reload_fpga = true;
load_old_bitstream = false;
waitForChoice = false; waitForChoice = false;
break; break;
default: default:

View file

@ -18,7 +18,7 @@ void render_message(pax_buf_t *pax_buffer, char* message, float aPosX, float aPo
esp_err_t graphics_task(pax_buf_t* pax_buffer, ILI9341* ili9341, menu_t* menu, char* message) { esp_err_t graphics_task(pax_buf_t* pax_buffer, ILI9341* ili9341, menu_t* menu, char* message) {
pax_background(pax_buffer, 0xCCCCCC); pax_background(pax_buffer, 0xCCCCCC);
if (menu != NULL) { if (menu != NULL) {
menu_render(pax_buffer, menu, 10, 10, 320-20, 240-20); menu_render(pax_buffer, menu, 10, 10, 320-20, 240-20, 0xFF000000);
} }
if (message != NULL) { if (message != NULL) {

3
main/include/audio.h Normal file
View file

@ -0,0 +1,3 @@
#pragma once
void audio_init();
void play_bootsound();

View file

@ -0,0 +1,4 @@
#include "pax_gfx.h"
#include "ili9341.h"
void display_boot_screen(pax_buf_t* pax_buffer, ILI9341* ili9341, const char* text);

View file

@ -40,7 +40,7 @@ size_t menu_get_position(menu_t* aMenu);
size_t menu_get_length(menu_t* aMenu); size_t menu_get_length(menu_t* aMenu);
void* menu_get_callback_args(menu_t* aMenu, size_t aPosition); void* menu_get_callback_args(menu_t* aMenu, size_t aPosition);
void menu_debug(menu_t* aMenu); void menu_debug(menu_t* aMenu);
void menu_render(pax_buf_t *aBuffer, menu_t *aMenu, float aPosX, float aPosY, float aWidth, float aHeight); void menu_render(pax_buf_t *aBuffer, menu_t *aMenu, float aPosX, float aPosY, float aWidth, float aHeight, pax_col_t aColor);
#ifdef __cplusplus #ifdef __cplusplus
} }

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,5 @@
#pragma once
#include <stdbool.h>
bool wifi_connect_to_stored();

View file

@ -40,7 +40,20 @@
#include "esp_vfs.h" #include "esp_vfs.h"
#include "esp_vfs_fat.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"; static const char *TAG = "main";
@ -113,113 +126,6 @@ void appfs_store_app(pax_buf_t* pax_buffer, ILI9341* ili9341, char* path, char*
return; 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) { 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_t* menu = menu_alloc("WiFi settings");
*menu_action = ACTION_NONE; *menu_action = ACTION_NONE;
@ -262,6 +168,7 @@ void menu_wifi_settings(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341
} }
break; break;
case RP2040_INPUT_BUTTON_ACCEPT: case RP2040_INPUT_BUTTON_ACCEPT:
case RP2040_INPUT_JOYSTICK_PRESS:
if (value) { if (value) {
menuArgs = menu_get_callback_args(menu, menu_get_position(menu)); 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) { 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; render = false;
} }
@ -289,13 +197,6 @@ void menu_wifi_settings(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341
menu_free(menu); 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) { 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_noclip(pax_buffer);
pax_background(pax_buffer, 0xa85a32); 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); 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) { void list_files_in_folder(const char* path) {
DIR* dir = opendir(path); DIR* dir = opendir(path);
if (dir == NULL) { if (dir == NULL) {
@ -488,6 +320,7 @@ void uninstall_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341*
} }
break; break;
case RP2040_INPUT_BUTTON_ACCEPT: case RP2040_INPUT_BUTTON_ACCEPT:
case RP2040_INPUT_JOYSTICK_PRESS:
if (value) { if (value) {
menuArgs = menu_get_callback_args(menu, menu_get_position(menu)); 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) { 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; render = false;
} }
@ -604,6 +438,7 @@ void file_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9
} }
break; break;
case RP2040_INPUT_BUTTON_ACCEPT: case RP2040_INPUT_BUTTON_ACCEPT:
case RP2040_INPUT_JOYSTICK_PRESS:
if (value) { if (value) {
menuArgs = menu_get_callback_args(menu, menu_get_position(menu)); 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) { 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; 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) { if (bsp_rp2040_init() != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize the RP2040 co-processor"); 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]); 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) { if (bsp_ice40_init() != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize the ICE40 FPGA"); ESP_LOGE(TAG, "Failed to initialize the ICE40 FPGA");
display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "ICE40 FPGA error", NULL, NULL); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "ICE40 FPGA error", NULL, NULL);
@ -719,6 +557,8 @@ void app_main(void) {
esp_restart(); esp_restart();
} }
display_boot_screen(pax_buffer, ili9341, "Initializing BNO055...");
if (bsp_bno055_init() != ESP_OK) { if (bsp_bno055_init() != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize the BNO055 position sensor"); ESP_LOGE(TAG, "Failed to initialize the BNO055 position sensor");
display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "BNO055 sensor error", "Check I2C bus", "Remove SAO and try again"); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "BNO055 sensor error", "Check I2C bus", "Remove SAO and try again");
@ -731,6 +571,8 @@ void app_main(void) {
esp_restart(); esp_restart();
} }
display_boot_screen(pax_buffer, ili9341, "Initializing BME680...");
if (bsp_bme680_init() != ESP_OK) { if (bsp_bme680_init() != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize the BME680 position sensor"); 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"); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "BME680 sensor error", "Check I2C bus", "Remove SAO and try again");
@ -743,6 +585,8 @@ void app_main(void) {
esp_restart(); esp_restart();
} }
display_boot_screen(pax_buffer, ili9341, "Initializing AppFS...");
/* Start AppFS */ /* Start AppFS */
res = appfs_init(); res = appfs_init();
if (res != ESP_OK) { if (res != ESP_OK) {
@ -751,6 +595,8 @@ void app_main(void) {
esp_restart(); esp_restart();
} }
display_boot_screen(pax_buffer, ili9341, "Initializing NVS...");
/* Start NVS */ /* Start NVS */
res = nvs_init(); res = nvs_init();
if (res != ESP_OK) { if (res != ESP_OK) {
@ -759,6 +605,8 @@ void app_main(void) {
esp_restart(); esp_restart();
} }
display_boot_screen(pax_buffer, ili9341, "Initializing filesystem...");
/* Start internal filesystem */ /* Start internal filesystem */
const esp_partition_t* fs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "locfd"); const esp_partition_t* fs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "locfd");
@ -788,6 +636,8 @@ void app_main(void) {
//list_files_in_folder("/sd"); //list_files_in_folder("/sd");
} }
display_boot_screen(pax_buffer, ili9341, "Initializing LEDs...");
/* Start LEDs */ /* Start LEDs */
ws2812_init(GPIO_LED_DATA); ws2812_init(GPIO_LED_DATA);
uint8_t ledBuffer[15] = {50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0}; uint8_t ledBuffer[15] = {50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0};
@ -798,30 +648,12 @@ void app_main(void) {
/* Launcher menu */ /* Launcher menu */
while (true) { while (true) {
menu_action_t menu_action; menu_action_t menu_action = ACTION_NONE;
appfs_handle_t appfs_fd; appfs_handle_t appfs_fd = -1;
menu_launcher(rp2040->queue, pax_buffer, ili9341, &menu_action, &appfs_fd); menu_start(rp2040->queue, pax_buffer, ili9341);
if (menu_action == ACTION_APPFS) { if (menu_action == ACTION_FPGA) {
appfs_boot_app(appfs_fd);
} else if (menu_action == ACTION_FPGA) {
graphics_task(pax_buffer, ili9341, NULL, "Loading..."); graphics_task(pax_buffer, ili9341, NULL, "Loading...");
fpga_test(ili9341, ice40, rp2040->queue); 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) { } else if (menu_action == ACTION_FILE_BROWSER) {
file_browser(rp2040->queue, pax_buffer, ili9341, "/sd"); file_browser(rp2040->queue, pax_buffer, ili9341, "/sd");
} else if (menu_action == ACTION_FILE_BROWSER_INT) { } else if (menu_action == ACTION_FILE_BROWSER_INT) {

View file

@ -22,9 +22,6 @@ menu_t* menu_alloc(const char* aTitle) {
void _menu_free_item(menu_item_t* aMenuItem) { void _menu_free_item(menu_item_t* aMenuItem) {
free(aMenuItem->label); free(aMenuItem->label);
//if (aMenuItem->callbackArgs != NULL) {
// free(aMenuItem->callbackArgs);
//}
free(aMenuItem); free(aMenuItem);
} }
@ -177,53 +174,58 @@ void menu_debug(menu_t* aMenu) {
printf("------\n"); printf("------\n");
} }
void menu_render(pax_buf_t *aBuffer, menu_t* aMenu, float aPosX, float aPosY, float aWidth, float aHeight) { void menu_render(pax_buf_t *aBuffer, menu_t* aMenu, float aPosX, float aPosY, float aWidth, float aHeight, pax_col_t aColor) {
pax_col_t fgColor = 0xFF000000; pax_col_t fgColor = aColor;
pax_col_t bgColor = 0xFFFFFFFF; pax_col_t bgColor = 0xFFFFFFFF;
pax_col_t borderColor = 0xFF000000; pax_col_t bgTextColor = 0xFFFFFFFF;
pax_col_t borderColor = 0x88000000;
pax_col_t titleColor = 0xFFFFFFFF; pax_col_t titleColor = 0xFFFFFFFF;
pax_col_t scrollbarBgColor = 0xFF555555; pax_col_t titleBgColor = aColor;
pax_col_t scrollbarFgColor = 0xFFCCCCCC; pax_col_t scrollbarBgColor = 0xFFCCCCCC;
pax_col_t scrollbarSlColor = 0xFFFFFFFF; pax_col_t scrollbarFgColor = 0xFF555555;
float entry_height = 18 + 2; float entry_height = 18 + 2;
size_t maxItems = aHeight / entry_height; size_t maxItems = aHeight / entry_height;
float posY = aPosY; float posY = aPosY;
pax_clip(aBuffer, aPosX, aPosY, aWidth, aHeight); pax_noclip(aBuffer);
pax_simple_rect(aBuffer, bgColor, aPosX, aPosY, aWidth, aHeight);
if (maxItems > 1) { if (maxItems > 1) {
maxItems--; maxItems--;
pax_simple_rect(aBuffer, borderColor, aPosX, posY, aWidth, entry_height); pax_simple_rect(aBuffer, titleBgColor, aPosX, posY, aWidth, entry_height);
pax_simple_line(aBuffer, titleColor, aPosX + 1, aPosY + entry_height, aPosX + aWidth - 2, aPosY + entry_height - 1); pax_simple_line(aBuffer, titleColor, aPosX + 1, aPosY + entry_height, aPosX + aWidth - 2, aPosY + entry_height - 1);
pax_clip(aBuffer, aPosX + 1, posY + 1, aWidth - 2, entry_height - 2); pax_clip(aBuffer, aPosX + 1, posY + 1, aWidth - 2, entry_height - 2);
pax_draw_text(aBuffer, titleColor, NULL, entry_height - 2, aPosX + 1, posY + 1, aMenu->title); pax_draw_text(aBuffer, titleColor, NULL, entry_height - 2, aPosX + 1, posY + 1, aMenu->title);
pax_noclip(aBuffer);
posY += entry_height; posY += entry_height;
} }
pax_clip(aBuffer, aPosX, posY, aWidth, aHeight);
pax_outline_rect(aBuffer, borderColor, aPosX, aPosY, aWidth, aHeight);
size_t itemOffset = 0; size_t itemOffset = 0;
if (aMenu->position >= maxItems) { if (aMenu->position >= maxItems) {
itemOffset = aMenu->position - maxItems + 1; itemOffset = aMenu->position - maxItems + 1;
} }
pax_outline_rect(aBuffer, borderColor, aPosX, aPosY, aWidth, aHeight);
pax_simple_rect(aBuffer, bgColor, aPosX, posY, aWidth, aHeight - posY + aPosY);
for (size_t index = itemOffset; (index < itemOffset + maxItems) && (index < aMenu->length); index++) { for (size_t index = itemOffset; (index < itemOffset + maxItems) && (index < aMenu->length); index++) {
menu_item_t* item = _menu_find_item(aMenu, index); menu_item_t* item = _menu_find_item(aMenu, index);
if (item == NULL) { if (item == NULL) {
printf("Render error: item is NULL at %u\n", index); printf("Render error: item is NULL at %u\n", index);
break; break;
} }
pax_clip(aBuffer, aPosX, posY, aWidth, entry_height);
if (index == aMenu->position) { if (index == aMenu->position) {
pax_simple_rect(aBuffer, fgColor, aPosX + 1, posY, aWidth - 2, entry_height); pax_simple_rect(aBuffer, fgColor, aPosX + 1, posY, aWidth - 2, entry_height);
pax_clip(aBuffer, aPosX + 1, posY + 1, aWidth - 4, entry_height - 2); pax_clip(aBuffer, aPosX + 1, posY + 1, aWidth - 4, entry_height - 2);
pax_draw_text(aBuffer, bgColor, NULL, entry_height - 2, aPosX + 1, posY + 1, item->label); pax_draw_text(aBuffer, bgTextColor, NULL, entry_height - 2, aPosX + 1, posY + 1, item->label);
pax_noclip(aBuffer);
} else { } else {
pax_simple_rect(aBuffer, bgColor, aPosX + 1, posY, aWidth - 2, entry_height); pax_simple_rect(aBuffer, bgColor, aPosX + 1, posY, aWidth - 2, entry_height);
pax_clip(aBuffer, aPosX + 1, posY + 1, aWidth - 4, entry_height - 2); pax_clip(aBuffer, aPosX + 1, posY + 1, aWidth - 4, entry_height - 2);
pax_draw_text(aBuffer, fgColor, NULL, entry_height - 2, aPosX + 1, posY + 1, item->label); pax_draw_text(aBuffer, fgColor, NULL, entry_height - 2, aPosX + 1, posY + 1, item->label);
pax_noclip(aBuffer);
} }
posY += entry_height; posY += entry_height;
} }
@ -237,12 +239,10 @@ void menu_render(pax_buf_t *aBuffer, menu_t* aMenu, float aPosX, float aPosY, fl
float scrollbarHeight = aHeight - entry_height; float scrollbarHeight = aHeight - entry_height;
float scrollbarStart = scrollbarHeight * fractionStart; float scrollbarStart = scrollbarHeight * fractionStart;
float scrollbarSelected = scrollbarHeight * fractionSelected;
float scrollbarEnd = scrollbarHeight * fractionEnd; float scrollbarEnd = scrollbarHeight * fractionEnd;
pax_simple_rect(aBuffer, scrollbarBgColor, aPosX + aWidth - 5, aPosY + entry_height - 1, 4, scrollbarHeight); pax_simple_rect(aBuffer, scrollbarBgColor, aPosX + aWidth - 5, aPosY + entry_height - 1, 4, scrollbarHeight);
pax_simple_rect(aBuffer, scrollbarFgColor, aPosX + aWidth - 5, aPosY + entry_height - 1 + scrollbarStart, 4, scrollbarEnd - scrollbarStart); pax_simple_rect(aBuffer, scrollbarFgColor, aPosX + aWidth - 5, aPosY + entry_height - 1 + scrollbarStart, 4, scrollbarEnd - scrollbarStart);
pax_simple_rect(aBuffer, scrollbarSlColor, aPosX + aWidth - 5, aPosY + entry_height - 1 + scrollbarSelected, 4, scrollbarHeight / aMenu->length);
pax_noclip(aBuffer); pax_noclip(aBuffer);
} }

99
main/menus/dev.c Normal file
View file

@ -0,0 +1,99 @@
#include <stdio.h>
#include <string.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_system.h>
#include <esp_err.h>
#include <esp_log.h>
#include "appfs.h"
#include "ili9341.h"
#include "pax_gfx.h"
#include "menu.h"
#include "rp2040.h"
#include "launcher.h"
#include "settings.h"
#include "dev.h"
#include "fpga_download.h"
#include "hardware.h"
typedef enum action {
ACTION_NONE,
ACTION_BACK,
ACTION_FPGA_DL
} menu_dev_action_t;
void render_dev_help(pax_buf_t* pax_buffer) {
pax_background(pax_buffer, 0xFFFFFF);
pax_noclip(pax_buffer);
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 5, 240 - 19, "[A] accept [B] back");
}
void menu_dev(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341) {
menu_t* menu = menu_alloc("Development tools");
menu_insert_item(menu, "FPGA download mode", NULL, (void*) ACTION_FPGA_DL, -1);
bool render = true;
menu_dev_action_t action = ACTION_NONE;
render_dev_help(pax_buffer);
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_HOME:
case RP2040_INPUT_BUTTON_BACK:
if (value) {
action = ACTION_BACK;
}
break;
case RP2040_INPUT_BUTTON_ACCEPT:
case RP2040_INPUT_JOYSTICK_PRESS:
case RP2040_INPUT_BUTTON_SELECT:
case RP2040_INPUT_BUTTON_START:
if (value) {
action = (menu_dev_action_t) menu_get_callback_args(menu, menu_get_position(menu));
}
break;
default:
break;
}
}
if (render) {
menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF000000);
ili9341_write(ili9341, pax_buffer->buf);
render = false;
}
if (action != ACTION_NONE) {
if (action == ACTION_FPGA_DL) {
fpga_download(buttonQueue, get_ice40(), pax_buffer, ili9341);
} else if (action == ACTION_BACK) {
break;
}
action = ACTION_NONE;
render = true;
render_dev_help(pax_buffer);
}
}
menu_free(menu);
}

8
main/menus/dev.h Normal file
View file

@ -0,0 +1,8 @@
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "pax_gfx.h"
#include "ili9341.h"
void menu_dev(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341);

110
main/menus/launcher.c Normal file
View file

@ -0,0 +1,110 @@
#include <stdio.h>
#include <string.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_system.h>
#include <esp_err.h>
#include <esp_log.h>
#include "appfs.h"
#include "ili9341.h"
#include "pax_gfx.h"
#include "menu.h"
#include "rp2040.h"
#include "appfs_wrapper.h"
typedef enum {
ACTION_NONE,
ACTION_APPFS,
ACTION_BACK
} menu_launcher_action_t;
typedef struct {
appfs_handle_t fd;
menu_launcher_action_t action;
} menu_launcher_args_t;
void menu_launcher(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341) {
menu_t* menu = menu_alloc("Apps");
menu_launcher_args_t* back_args = malloc(sizeof(menu_launcher_args_t));
back_args->action = ACTION_BACK;
menu_insert_item(menu, "< Back", NULL, back_args, -1);
appfs_handle_t appfs_fd = APPFS_INVALID_FD;
while (1) {
appfs_fd = appfsNextEntry(appfs_fd);
if (appfs_fd == APPFS_INVALID_FD) break;
const char* name = NULL;
appfsEntryInfo(appfs_fd, &name, NULL);
menu_launcher_args_t* args = malloc(sizeof(menu_launcher_args_t));
args->fd = appfs_fd;
args->action = ACTION_APPFS;
menu_insert_item(menu, name, NULL, (void*) args, -1);
}
bool render = true;
menu_launcher_args_t* menuArgs = NULL;
pax_background(pax_buffer, 0xFFFFFF);
pax_noclip(pax_buffer);
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 5, 240 - 19, "[A] start app [B] back");
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_HOME:
case RP2040_INPUT_BUTTON_BACK:
if (value) {
menuArgs = back_args;
}
break;
case RP2040_INPUT_BUTTON_ACCEPT:
case RP2040_INPUT_JOYSTICK_PRESS:
case RP2040_INPUT_BUTTON_SELECT:
case RP2040_INPUT_BUTTON_START:
if (value) {
menuArgs = menu_get_callback_args(menu, menu_get_position(menu));
}
break;
default:
break;
}
}
if (render) {
menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF72008a);
ili9341_write(ili9341, pax_buffer->buf);
render = false;
}
if (menuArgs != NULL) {
if (menuArgs->action == ACTION_APPFS) {
appfs_boot_app(menuArgs->fd);
}
break;
}
}
for (size_t index = 0; index < menu_get_length(menu); index++) {
free(menu_get_callback_args(menu, index));
}
menu_free(menu);
}

8
main/menus/launcher.h Normal file
View file

@ -0,0 +1,8 @@
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "pax_gfx.h"
#include "ili9341.h"
void menu_launcher(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341);

119
main/menus/settings.c Normal file
View file

@ -0,0 +1,119 @@
#include <stdio.h>
#include <string.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_system.h>
#include <esp_err.h>
#include <esp_log.h>
#include "appfs.h"
#include "ili9341.h"
#include "pax_gfx.h"
#include "menu.h"
#include "rp2040.h"
#include "appfs_wrapper.h"
#include "hardware.h"
#include "system_wrapper.h"
#include "bootscreen.h"
#include "wifi_connect.h"
#include "wifi_ota.h"
#include "wifi.h"
typedef enum action {
ACTION_NONE,
ACTION_BACK,
ACTION_WIFI,
ACTION_OTA,
ACTION_RP2040_BL,
ACTION_UNINSTALL
} menu_settings_action_t;
void render_settings_help(pax_buf_t* pax_buffer) {
pax_background(pax_buffer, 0xFFFFFF);
pax_noclip(pax_buffer);
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 5, 240 - 19, "[A] accept [B] back");
}
void menu_settings(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341) {
menu_t* menu = menu_alloc("Settings");
menu_insert_item(menu, "WiFi configuration", NULL, (void*) ACTION_WIFI, -1);
menu_insert_item(menu, "Firmware update", NULL, (void*) ACTION_OTA, -1);
menu_insert_item(menu, "Flash RP2040 firmware", NULL, (void*) ACTION_RP2040_BL, -1);
menu_insert_item(menu, "Uninstall app", NULL, (void*) ACTION_UNINSTALL, -1);
bool render = true;
menu_settings_action_t action = ACTION_NONE;
render_settings_help(pax_buffer);
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_HOME:
case RP2040_INPUT_BUTTON_BACK:
if (value) {
action = ACTION_BACK;
}
break;
case RP2040_INPUT_BUTTON_ACCEPT:
case RP2040_INPUT_JOYSTICK_PRESS:
case RP2040_INPUT_BUTTON_SELECT:
case RP2040_INPUT_BUTTON_START:
if (value) {
action = (menu_settings_action_t) menu_get_callback_args(menu, menu_get_position(menu));
}
break;
default:
break;
}
}
if (render) {
menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF2f55a8);
ili9341_write(ili9341, pax_buffer->buf);
render = false;
}
if (action != ACTION_NONE) {
if (action == ACTION_RP2040_BL) {
display_boot_screen(pax_buffer, ili9341, "Please wait...");
rp2040_reboot_to_bootloader(get_rp2040());
esp_restart();
} else if (action == ACTION_OTA) {
display_boot_screen(pax_buffer, ili9341, "Connecting to WiFi...");
if (wifi_connect_to_stored()) {
display_boot_screen(pax_buffer, ili9341, "Starting firmware update...");
ota_update();
} else {
display_boot_screen(pax_buffer, ili9341, "Failed to connect to WiFi");
vTaskDelay(500 / portTICK_PERIOD_MS);
}
} else if (action == ACTION_WIFI) {
menu_wifi(buttonQueue, pax_buffer, ili9341);
} else if (action == ACTION_BACK) {
break;
}
render = true;
action = ACTION_NONE;
render_settings_help(pax_buffer);
}
}
menu_free(menu);
}

8
main/menus/settings.h Normal file
View file

@ -0,0 +1,8 @@
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "pax_gfx.h"
#include "ili9341.h"
void menu_settings(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341);

96
main/menus/start.c Normal file
View file

@ -0,0 +1,96 @@
#include <stdio.h>
#include <string.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_system.h>
#include <esp_err.h>
#include <esp_log.h>
#include "appfs.h"
#include "ili9341.h"
#include "pax_gfx.h"
#include "menu.h"
#include "rp2040.h"
#include "launcher.h"
#include "settings.h"
#include "dev.h"
typedef enum action {
ACTION_NONE,
ACTION_APPS,
ACTION_DEV,
ACTION_SETTINGS
} menu_start_action_t;
void render_start_help(pax_buf_t* pax_buffer) {
pax_background(pax_buffer, 0xFFFFFF);
pax_noclip(pax_buffer);
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 5, 240 - 19, "[A] accept");
}
void menu_start(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341) {
menu_t* menu = menu_alloc("Main menu");
menu_insert_item(menu, "Apps", NULL, (void*) ACTION_APPS, -1);
menu_insert_item(menu, "Development tools", NULL, (void*) ACTION_DEV, -1);
menu_insert_item(menu, "Settings", NULL, (void*) ACTION_SETTINGS, -1);
bool render = true;
menu_start_action_t action = ACTION_NONE;
render_start_help(pax_buffer);
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:
case RP2040_INPUT_JOYSTICK_PRESS:
case RP2040_INPUT_BUTTON_SELECT:
case RP2040_INPUT_BUTTON_START:
if (value) {
action = (menu_start_action_t) menu_get_callback_args(menu, menu_get_position(menu));
}
break;
default:
break;
}
}
if (render) {
menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF000000);
ili9341_write(ili9341, pax_buffer->buf);
render = false;
}
if (action != ACTION_NONE) {
if (action == ACTION_APPS) {
menu_launcher(buttonQueue, pax_buffer, ili9341);
} else if (action == ACTION_SETTINGS) {
menu_settings(buttonQueue, pax_buffer, ili9341);
} else if (action == ACTION_DEV) {
menu_dev(buttonQueue, pax_buffer, ili9341);
}
action = ACTION_NONE;
render = true;
render_start_help(pax_buffer);
}
}
menu_free(menu);
}

8
main/menus/start.h Normal file
View file

@ -0,0 +1,8 @@
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "pax_gfx.h"
#include "ili9341.h"
void menu_start(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341);

103
main/menus/wifi.c Normal file
View file

@ -0,0 +1,103 @@
#include <stdio.h>
#include <string.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_system.h>
#include <esp_err.h>
#include <esp_log.h>
#include "appfs.h"
#include "ili9341.h"
#include "pax_gfx.h"
#include "menu.h"
#include "rp2040.h"
#include "appfs_wrapper.h"
#include "hardware.h"
#include "system_wrapper.h"
#include "bootscreen.h"
#include "wifi_connect.h"
#include "wifi_ota.h"
typedef enum action {
ACTION_NONE,
ACTION_BACK,
ACTION_SHOW,
ACTION_SCAN,
ACTION_MANUAL
} menu_wifi_action_t;
void render_wifi_help(pax_buf_t* pax_buffer) {
pax_background(pax_buffer, 0xFFFFFF);
pax_noclip(pax_buffer);
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 5, 240 - 19, "[A] accept [B] back");
}
void menu_wifi(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341) {
menu_t* menu = menu_alloc("WiFi configuration");
menu_insert_item(menu, "Show current settings", NULL, (void*) ACTION_SHOW, -1);
menu_insert_item(menu, "Scan for networks", NULL, (void*) ACTION_SCAN, -1);
menu_insert_item(menu, "Configure manually", NULL, (void*) ACTION_MANUAL, -1);
bool render = true;
menu_wifi_action_t action = ACTION_NONE;
render_wifi_help(pax_buffer);
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_HOME:
case RP2040_INPUT_BUTTON_BACK:
if (value) {
action = ACTION_BACK;
}
break;
case RP2040_INPUT_BUTTON_ACCEPT:
case RP2040_INPUT_JOYSTICK_PRESS:
case RP2040_INPUT_BUTTON_SELECT:
case RP2040_INPUT_BUTTON_START:
if (value) {
action = (menu_wifi_action_t) menu_get_callback_args(menu, menu_get_position(menu));
}
break;
default:
break;
}
}
if (render) {
menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF2f55a8);
ili9341_write(ili9341, pax_buffer->buf);
render = false;
}
if (action != ACTION_NONE) {
if (action == ACTION_SHOW) {
} else if (action == ACTION_BACK) {
break;
}
render = true;
action = ACTION_NONE;
render_wifi_help(pax_buffer);
}
}
menu_free(menu);
}

8
main/menus/wifi.h Normal file
View file

@ -0,0 +1,8 @@
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "pax_gfx.h"
#include "ili9341.h"
void menu_wifi(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341);

View file

@ -13,12 +13,15 @@
#include "pax_gfx.h" #include "pax_gfx.h"
#include "rp2040.h" #include "rp2040.h"
#include "rp2040bl.h" #include "rp2040bl.h"
#include "rp2040_firmware.h"
#include "system_wrapper.h" #include "system_wrapper.h"
#include "graphics_wrapper.h" #include "graphics_wrapper.h"
#include "esp32/rom/crc.h" #include "esp32/rom/crc.h"
extern const uint8_t rp2040_firmware_bin_start[] asm("_binary_rp2040_firmware_bin_start");
extern const uint8_t rp2040_firmware_bin_end[] asm("_binary_rp2040_firmware_bin_end");
void rp2040_updater(RP2040* rp2040, pax_buf_t* pax_buffer, ILI9341* ili9341) { void rp2040_updater(RP2040* rp2040, pax_buf_t* pax_buffer, ILI9341* ili9341) {
size_t firmware_size = rp2040_firmware_bin_end - rp2040_firmware_bin_start;
char message[64]; char message[64];
uint8_t fw_version; uint8_t fw_version;
@ -158,7 +161,7 @@ void rp2040_updater(RP2040* rp2040, pax_buf_t* pax_buffer, ILI9341* ili9341) {
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 13, 0, 20*1, message); pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 13, 0, 20*1, message);
ili9341_write(ili9341, pax_buffer->buf); ili9341_write(ili9341, pax_buffer->buf);
uint32_t erase_length = sizeof(mch2022_firmware_bin); uint32_t erase_length = firmware_size;
erase_length = erase_length + erase_size - (erase_length % erase_size); // Round up to erase size erase_length = erase_length + erase_size - (erase_length % erase_size); // Round up to erase size
if (erase_length > flash_size - erase_size) { if (erase_length > flash_size - erase_size) {
@ -188,13 +191,13 @@ void rp2040_updater(RP2040* rp2040, pax_buf_t* pax_buffer, ILI9341* ili9341) {
uint32_t totalLength = 0; uint32_t totalLength = 0;
while (true) { while (true) {
if ((sizeof(mch2022_firmware_bin) - position) < txSize) { if ((firmware_size - position) < txSize) {
txSize = sizeof(mch2022_firmware_bin) - position; txSize = firmware_size - position;
} }
if (txSize == 0) break; if (txSize == 0) break;
uint8_t percentage = position * 100 / sizeof(mch2022_firmware_bin); uint8_t percentage = position * 100 / firmware_size;
pax_noclip(pax_buffer); pax_noclip(pax_buffer);
pax_background(pax_buffer, 0x325aa8); pax_background(pax_buffer, 0x325aa8);
@ -206,7 +209,7 @@ void rp2040_updater(RP2040* rp2040, pax_buf_t* pax_buffer, ILI9341* ili9341) {
uint32_t checkCrc = 0; uint32_t checkCrc = 0;
memset(txBuffer, 0, write_size); memset(txBuffer, 0, write_size);
memcpy(txBuffer, &mch2022_firmware_bin[position], txSize); memcpy(txBuffer, &rp2040_firmware_bin_start[position], txSize);
blockCrc = crc32_le(0, txBuffer, write_size); blockCrc = crc32_le(0, txBuffer, write_size);
totalCrc = crc32_le(totalCrc, txBuffer, write_size); totalCrc = crc32_le(totalCrc, txBuffer, write_size);
totalLength += write_size; totalLength += write_size;

File diff suppressed because it is too large Load diff

89
main/wifi_connect.c Normal file
View file

@ -0,0 +1,89 @@
#include <stdio.h>
#include <string.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_system.h>
#include <esp_err.h>
#include <esp_log.h>
#include <nvs_flash.h>
#include <nvs.h>
#include "pax_gfx.h"
#include "system_wrapper.h"
#include "settings.h"
#include "wifi_connection.h"
static const char *TAG = "wifi_connect";
bool wifi_connect_to_stored() {
bool result = false;
// 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) {
result = wifi_connect_ent(ssid, ident, anon_ident, password, 3);
} else {
result = 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);
return result;
}

BIN
resources/boot.snd Normal file

Binary file not shown.

BIN
resources/fpga_selftest.bin Normal file

Binary file not shown.

BIN
resources/logo_screen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

BIN
resources/mch2022_logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
resources/rp2040_firmware.bin Executable file

Binary file not shown.

BIN
resources/wallpaper.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 KiB