Merge pull request #49 from badgeteam/renze/refactoring

Refactoring
This commit is contained in:
Renze Nicolai 2022-05-31 22:47:28 +02:00 committed by GitHub
commit cb475bea49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
46 changed files with 1930 additions and 21212 deletions

3
.gitmodules vendored
View file

@ -34,3 +34,6 @@
[submodule "components/mch2022-bsp"]
path = components/mch2022-bsp
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 2882f91df13c6155062b6c94871fdf68e1c62675

1
components/pax-codecs Submodule

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

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

View file

@ -9,8 +9,26 @@ idf_component_register(
"settings.c"
"system_wrapper.c"
"wifi_connection.c"
"wifi_connect.c"
"wifi_ota.c"
"fpga_download.c"
INCLUDE_DIRS "." "include"
EMBED_TXTFILES ${project_dir}/server_certs/isrgrootx1.pem
"audio.c"
"bootscreen.c"
"menus/launcher.c"
"menus/settings.c"
"menus/start.c"
"menus/dev.c"
"menus/wifi.c"
"uninstall.c"
"file_browser.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
)

View file

@ -1,8 +1,27 @@
#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 "esp_sleep.h"
#include "soc/rtc.h"
#include "soc/rtc_cntl_reg.h"
static const char *TAG = "appfs wrapper";
esp_err_t appfs_init(void) {
return appfsInit(APPFS_PART_TYPE, APPFS_PART_SUBTYPE);
}
@ -29,3 +48,47 @@ void appfs_boot_app(int fd) {
esp_deep_sleep_start();
}
void appfs_store_app(pax_buf_t* pax_buffer, ILI9341* ili9341, char* path, char* label) {
display_boot_screen(pax_buffer, ili9341, "Installing app...");
esp_err_t res;
appfs_handle_t handle;
FILE* app_fd = fopen(path, "rb");
if (app_fd == NULL) {
display_boot_screen(pax_buffer, ili9341, "Failed to open file");
ESP_LOGE(TAG, "Failed to open file");
vTaskDelay(100 / portTICK_PERIOD_MS);
return;
}
size_t app_size;
uint8_t* app = load_file_to_ram(app_fd, &app_size);
if (app == NULL) {
display_boot_screen(pax_buffer, ili9341, "Failed to load app to RAM");
ESP_LOGE(TAG, "Failed to load application into RAM");
vTaskDelay(100 / portTICK_PERIOD_MS);
return;
}
ESP_LOGI(TAG, "Application size %d", app_size);
res = appfsCreateFile(label, app_size, &handle);
if (res != ESP_OK) {
display_boot_screen(pax_buffer, ili9341, "Failed to create on AppFS");
ESP_LOGE(TAG, "Failed to create file on AppFS (%d)", res);
vTaskDelay(100 / portTICK_PERIOD_MS);
free(app);
return;
}
res = appfsWrite(handle, 0, app, app_size);
if (res != ESP_OK) {
display_boot_screen(pax_buffer, ili9341, "Failed to write to AppFS");
ESP_LOGE(TAG, "Failed to write to file on AppFS (%d)", res);
vTaskDelay(100 / portTICK_PERIOD_MS);
free(app);
return;
}
free(app);
ESP_LOGI(TAG, "Application is now stored in AppFS");
display_boot_screen(pax_buffer, ili9341, "App installed!");
vTaskDelay(100 / portTICK_PERIOD_MS);
return;
}

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);
}

217
main/file_browser.c Normal file
View file

@ -0,0 +1,217 @@
#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 "esp_vfs.h"
#include "esp_vfs_fat.h"
#include "appfs.h"
#include "ili9341.h"
#include "pax_gfx.h"
#include "menu.h"
#include "rp2040.h"
#include "appfs_wrapper.h"
static const char *TAG = "file browser";
void list_files_in_folder(const char* path) {
DIR* dir = opendir(path);
if (dir == NULL) {
ESP_LOGE(TAG, "Failed to open directory %s", path);
return;
}
struct dirent *ent;
char type;
char size[12];
char tpath[255];
char tbuffer[80];
struct stat sb;
struct tm *tm_info;
char *lpath = NULL;
int statok;
uint64_t total = 0;
int nfiles = 0;
printf("T Size Date/Time Name\n");
printf("-----------------------------------\n");
while ((ent = readdir(dir)) != NULL) {
sprintf(tpath, path);
if (path[strlen(path)-1] != '/') {
strcat(tpath,"/");
}
strcat(tpath,ent->d_name);
tbuffer[0] = '\0';
// Get file stat
statok = stat(tpath, &sb);
if (statok == 0) {
tm_info = localtime(&sb.st_mtime);
strftime(tbuffer, 80, "%d/%m/%Y %R", tm_info);
} else {
sprintf(tbuffer, " ");
}
if (ent->d_type == DT_REG) {
type = 'f';
nfiles++;
if (statok) {
strcpy(size, " ?");
} else {
total += sb.st_size;
if (sb.st_size < (1024*1024)) sprintf(size,"%8d", (int)sb.st_size);
else if ((sb.st_size/1024) < (1024*1024)) sprintf(size,"%6dKB", (int)(sb.st_size / 1024));
else sprintf(size,"%6dMB", (int)(sb.st_size / (1024 * 1024)));
}
} else {
type = 'd';
strcpy(size, " -");
}
printf("%c %s %s %s\r\n", type, size, tbuffer, ent->d_name);
}
printf("-----------------------------------\n");
if (total < (1024*1024)) printf(" %8d", (int)total);
else if ((total/1024) < (1024*1024)) printf(" %6dKB", (int)(total / 1024));
else printf(" %6dMB", (int)(total / (1024 * 1024)));
printf(" in %d file(s)\n", nfiles);
printf("-----------------------------------\n");
closedir(dir);
free(lpath);
}
typedef struct _file_browser_menu_args {
char type;
char path[512];
char label[512];
} file_browser_menu_args_t;
void find_parent_dir(char* path, char* parent) {
size_t last_separator = 0;
for (size_t index = 0; index < strlen(path); index++) {
if (path[index] == '/') last_separator = index;
}
strcpy(parent, path);
parent[last_separator] = '\0';
}
void file_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341, const char* initial_path) {
char path[512] = {0};
strncpy(path, initial_path, sizeof(path));
while (true) {
menu_t* menu = menu_alloc(path);
DIR* dir = opendir(path);
if (dir == NULL) {
ESP_LOGE(TAG, "Failed to open directory %s", path);
return;
}
struct dirent *ent;
file_browser_menu_args_t* pd_args = malloc(sizeof(file_browser_menu_args_t));
pd_args->type = 'd';
find_parent_dir(path, pd_args->path);
printf("Parent dir: %s\n", pd_args->path);
menu_insert_item(menu, "../", NULL, pd_args, -1);
while ((ent = readdir(dir)) != NULL) {
file_browser_menu_args_t* args = malloc(sizeof(file_browser_menu_args_t));
sprintf(args->path, path);
if (path[strlen(path)-1] != '/') {
strcat(args->path,"/");
}
strcat(args->path,ent->d_name);
if (ent->d_type == DT_REG) {
args->type = 'f';
} else {
args->type = 'd';
}
printf("%c %s %s\r\n", args->type, ent->d_name, args->path);
snprintf(args->label, sizeof(args->label), "%s%s", ent->d_name, (args->type == 'd') ? "/" : "");
menu_insert_item(menu, args->label, NULL, args, -1);
}
closedir(dir);
bool render = true;
bool exit = false;
file_browser_menu_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] install [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_BACK:
if (value) {
menuArgs = pd_args;
}
case RP2040_INPUT_BUTTON_ACCEPT:
case RP2040_INPUT_JOYSTICK_PRESS:
if (value) {
menuArgs = menu_get_callback_args(menu, menu_get_position(menu));
}
break;
case RP2040_INPUT_BUTTON_HOME:
if (value) exit = true;
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->type == 'd') {
strcpy(path, menuArgs->path);
break;
} else {
printf("File selected: %s\n", menuArgs->path);
appfs_store_app(pax_buffer, ili9341, menuArgs->path, menuArgs->label);
}
menuArgs = NULL;
render = true;
}
if (exit) {
break;
}
}
for (size_t index = 0; index < menu_get_length(menu); index++) {
free(menu_get_callback_args(menu, index));
}
menu_free(menu);
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,234 +1,570 @@
#include "fpga_test.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include "fpga.h"
#include "selftest.h"
#include <driver/gpio.h>
#include "hardware.h"
#include "ili9341.h"
#include "ice40.h"
#include "rp2040.h"
#include "hardware.h"
#include "fpga_test.h"
#include "pax_gfx.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";
esp_err_t load_file_into_psram(ICE40* ice40, FILE* fd) {
fseek(fd, 0, SEEK_SET);
const uint8_t write_cmd = 0x02;
uint32_t amount_read;
uint32_t position = 0;
uint8_t* tx_buffer = malloc(SPI_MAX_TRANSFER_SIZE);
if (tx_buffer == NULL) return ESP_FAIL;
/* SPI commands */
#define SPI_CMD_NOP1 0x00
#define SPI_CMD_SOC_MSG 0x10
#define SPI_CMD_REG_ACCESS 0xf0
#define SPI_CMD_LOOPBACK 0xf1
#define SPI_CMD_LCD_PASSTHROUGH 0xf2
#define SPI_CMD_BUTTON_REPORT 0xf4
#define SPI_CMD_IRQ_ACK 0xfd
#define SPI_CMD_RESP_ACK 0xfe
#define SPI_CMD_NOP2 0xff
while(1) {
tx_buffer[0] = write_cmd;
tx_buffer[1] = (position >> 16);
tx_buffer[2] = (position >> 8) & 0xFF;
tx_buffer[3] = position & 0xFF;
amount_read = fread(&tx_buffer[4], 1, SPI_MAX_TRANSFER_SIZE - 4, fd);
if (amount_read < 1) break;
ESP_LOGI(TAG, "Writing PSRAM @ %u (%u bytes)", position, amount_read);
esp_err_t res = ice40_transaction(ice40, tx_buffer, amount_read + 4, NULL, 0);
if (res != ESP_OK) {
ESP_LOGE(TAG, "Write transaction failed @ %u", position);
free(tx_buffer);
return res;
}
position += amount_read;
};
free(tx_buffer);
return ESP_OK;
}
/* Messages to self-test SoC */
#define SOC_CMD_PING 0x00
#define SOC_CMD_PING_PARAM 0xc0ffee
#define SOC_CMD_PING_RESP 0xcafebabe
esp_err_t load_buffer_into_psram(ICE40* ice40, uint8_t* buffer, uint32_t buffer_length) {
const uint8_t write_cmd = 0x02;
uint32_t position = 0;
uint8_t* tx_buffer = malloc(SPI_MAX_TRANSFER_SIZE);
if (tx_buffer == NULL) return ESP_FAIL;
while(1) {
tx_buffer[0] = write_cmd;
tx_buffer[1] = (position >> 16);
tx_buffer[2] = (position >> 8) & 0xFF;
tx_buffer[3] = position & 0xFF;
uint32_t length = buffer_length - position;
if (length > SPI_MAX_TRANSFER_SIZE - 4) length = SPI_MAX_TRANSFER_SIZE - 4;
memcpy(&tx_buffer[4], &buffer[position], length);
if (length == 0) break;
ESP_LOGI(TAG, "Writing PSRAM @ %u (%u bytes)", position, length);
esp_err_t res = ice40_transaction(ice40, tx_buffer, length + 4, NULL, 0);
if (res != ESP_OK) {
ESP_LOGE(TAG, "Write transaction failed @ %u", position);
free(tx_buffer);
return res;
}
position += length;
};
free(tx_buffer);
return ESP_OK;
}
#define SOC_CMD_RGB_STATE_SET 0x10
#define SOC_CMD_IRQN_SET 0x11
#define SOC_CMD_LCD_RGB_CYCLE_SET 0x12
#define SOC_CMD_PMOD_CYCLE_SET 0x13
#define SOC_CMD_LCD_PASSTHROUGH_SET 0x14
esp_err_t verify_file_in_psram(ICE40* ice40, FILE* fd) {
fseek(fd, 0, SEEK_SET);
const uint8_t read_cmd = 0x03;
uint32_t amount_read;
uint32_t position = 0;
uint8_t* tx_buffer = malloc(SPI_MAX_TRANSFER_SIZE);
if (tx_buffer == NULL) return ESP_FAIL;
memset(tx_buffer, 0, SPI_MAX_TRANSFER_SIZE);
uint8_t* verify_buffer = malloc(SPI_MAX_TRANSFER_SIZE);
if (verify_buffer == NULL) return ESP_FAIL;
uint8_t* rx_buffer = malloc(SPI_MAX_TRANSFER_SIZE);
if (rx_buffer == NULL) return ESP_FAIL;
#define SOC_CMD_PSRAM_TEST 0x20
#define SOC_CMD_UART_LOOPBACK_TEST 0x21
#define SOC_CMD_PMOD_OPEN_TEST 0x22
#define SOC_CMD_PMOD_PLUG_TEST 0x23
#define SOC_CMD_LCD_INIT_TEST 0x24
while(1) {
tx_buffer[0] = read_cmd;
tx_buffer[1] = (position >> 16);
tx_buffer[2] = (position >> 8) & 0xFF;
tx_buffer[3] = position & 0xFF;
amount_read = fread(&verify_buffer[4], 1, SPI_MAX_TRANSFER_SIZE - 4, fd);
if (amount_read < 1) break;
ESP_LOGI(TAG, "Reading PSRAM @ %u (%u bytes)", position, amount_read);
esp_err_t res = ice40_transaction(ice40, tx_buffer, amount_read + 4, rx_buffer, amount_read + 4);
if (res != ESP_OK) {
ESP_LOGE(TAG, "Read transaction failed @ %u", position);
free(tx_buffer);
return res;
}
position += amount_read;
ESP_LOGI(TAG, "Verifying PSRAM @ %u (%u bytes)", position, amount_read);
for (uint32_t i = 4; i < amount_read; i++) {
if (rx_buffer[i] != verify_buffer[i]) {
ESP_LOGE(TAG, "Verifying PSRAM @ %u failed: %02X != %02X", position + i, rx_buffer[i], verify_buffer[i]);
free(tx_buffer);
free(rx_buffer);
free(verify_buffer);
return ESP_FAIL;
}
}
};
free(tx_buffer);
free(rx_buffer);
free(verify_buffer);
ESP_LOGI(TAG, "PSRAM contents verified!");
return ESP_OK;
}
#define SOC_CMD_LCD_CHECK_MODE 0x30
bool test_spi(ICE40* ice40) {
#define SOC_RESP_OK 0x00000000
/* SoC commands */
static bool soc_message(ICE40* ice40, uint8_t cmd, uint32_t param, uint32_t *resp, TickType_t ticks_to_wait) {
esp_err_t res;
uint8_t data_tx[256];
uint8_t data_rx[128];
uint8_t data_tx[6];
uint8_t data_rx[6];
// Generate pseudo random sequence
data_tx[0] = 1;
for (int i = 1; i < 256; i++)
/* Default delay */
ticks_to_wait /= 10; /* We do 10 retries */
if (!ticks_to_wait)
ticks_to_wait = pdMS_TO_TICKS(50);
/* Prepare message */
data_tx[0] = SPI_CMD_SOC_MSG;
data_tx[1] = cmd;
data_tx[2] = (param >> 16) & 0xff;
data_tx[3] = (param >> 8) & 0xff;
data_tx[4] = (param ) & 0xff;
/* Send message to PicoRV */
res = ice40_send_turbo(ice40, data_tx, 5);
if (res != ESP_OK) {
ESP_LOGE(TAG, "SoC message TX failed");
return false;
}
/* Poll until we get a response */
data_tx[0] = SPI_CMD_RESP_ACK;
for (int i=0; i<10; i++) {
/* Poll */
res = ice40_transaction(ice40, data_tx, 6, data_rx, 6);
if (res != ESP_OK) {
ESP_LOGE(TAG, "SoC response RX failed");
return false;
}
/* Was response valid ? */
if (data_rx[1] & 0x80)
break;
/* Wait before retry */
vTaskDelay(ticks_to_wait);
}
if (!(data_rx[1] & 0x80)) {
ESP_LOGE(TAG, "SoC response RX timeout");
return false;
}
/* Report response */
if (resp) {
*resp = 0;
for (int i=0; i<4; i++)
*resp = (*resp << 8) | data_rx[2+i];
}
return true;
}
/* Test routines */
static bool test_bitstream_load(ICE40* ice40, uint32_t *rc) {
esp_err_t res;
res = ice40_load_bitstream(ice40, fpga_selftest_bin_start, fpga_selftest_bin_end - fpga_selftest_bin_start);
if (res != ESP_OK) {
*rc = res;
return false;
}
*rc = 0;
return true;
}
static bool test_spi_loopback_one(ICE40* ice40) {
esp_err_t res;
uint8_t data_tx[257];
uint8_t data_rx[258];
/* Generate pseudo random sequence */
data_tx[1] = 1;
for (int i = 2; i < 257; i++)
data_tx[i] = (data_tx[i-1] << 1) ^ ((data_tx[i-1] & 0x80) ? 0x1d : 0x00);
// Send first 128 byte at high speed
res = ice40_send_turbo(ice40, &data_tx[0], 128);
/* Send 256 bytes at high speed with echo command */
data_tx[0] = SPI_CMD_LOOPBACK;
res = ice40_send_turbo(ice40, data_tx, 257);
if (res != ESP_OK) {
ESP_LOGE(TAG, "Transaction 1 failed (Turbo TX)");
ESP_LOGE(TAG, "SPI loopback transaction 1 failed (Turbo TX)");
return false;
}
// Execute full duplex transaction with next 128 bytes
res = ice40_transaction(ice40, &data_tx[128], 128, data_rx, 128);
/* Execute full duplex transaction with next 128 bytes */
res = ice40_transaction(ice40, data_tx, 257, data_rx, 257);
if (res != ESP_OK) {
ESP_LOGE(TAG, "Transaction 2 failed (Full Duplex)");
ESP_LOGE(TAG, "SPI loopback transaction 2 failed (Full Duplex)");
return false;
}
// Validate RX data
if (memcmp(&data_rx[1], &data_tx[0], 127)) {
printf("Transaction 1->2 integrity fail:\n");
for (int i = 0; i < 128; i++)
/* Validate response present */
if ((data_rx[1] & 0x80) == 0) {
ESP_LOGE(TAG, "SPI loopback transaction 2 reports no response available\n");
return false;
}
/* Validate RX data (only 254 byte got read) */
if (memcmp(&data_rx[2], &data_tx[1], 254)) {
ESP_LOGE(TAG, "SPI loopback transaction 1->2 integrity fail:\n");
for (int i = 0; i < 254; i++)
printf("%02X%c", data_rx[i], ((i&0xf)==0xf) ? '\n' : ' ');
printf("\n");
return false;
}
// Receive half duplex
res = ice40_receive(ice40, data_rx, 128);
/* Read two responses and ack them */
for (int t = 0; t < 2; t++) {
/* Receive half duplex */
res = ice40_receive(ice40, data_rx, 258);
if (res != ESP_OK) {
ESP_LOGE(TAG, "Transaction 3 failed (Half Duplex RX)");
ESP_LOGE(TAG, "SPI loopback transaction 3.%d failed (Half Duplex RX)", t);
return false;
}
// Validate RX data
if (memcmp(&data_rx[1], &data_tx[128], 127)) {
printf("Transaction 2->3 integrity fail:\n");
for (int i = 0; i < 128; i++)
/* Short acknowledge command */
data_tx[0] = SPI_CMD_RESP_ACK;
res = ice40_send_turbo(ice40, data_tx, 1);
if (res != ESP_OK) {
ESP_LOGE(TAG, "SPI loopback transaction 4.%d failed (Turbo ACK)", t);
return false;
}
/* Validate response present */
if ((data_rx[1] & 0x80) == 0) {
ESP_LOGE(TAG, "SPI loopback transaction 3.%d reports no response available\n", t);
return false;
}
/* Validate RX data (only 254 byte got read) */
if (memcmp(&data_rx[2], &data_tx[1], 254)) {
ESP_LOGE(TAG, "SPI loopback transaction %d->3.%d integrity fail:\n", 1+t, t);
for (int i = 0; i < 254; i++)
printf("%02X%c", data_rx[i], ((i&0xf)==0xf) ? '\n' : ' ');
printf("\n");
return false;
}
}
/* Check there is no more responses pending */
data_tx[0] = SPI_CMD_NOP2;
res = ice40_transaction(ice40, data_tx, 2, data_rx, 2);
if (res != ESP_OK) {
ESP_LOGE(TAG, "SPI loopback transaction 5 failed (Full Duplex)");
return false;
}
if ((data_rx[1] & 0x80) != 0) {
ESP_LOGE(TAG, "SPI loopback transaction 5 reports response available\n");
return false;
}
return true;
}
void fpga_test(ILI9341* ili9341, ICE40* ice40, xQueueHandle buttonQueue) {
esp_err_t res;
bool reload_fpga = false;
bool load_old_bitstream = false;
do {
printf("Start FPGA test...\n");
reload_fpga = false;
printf("LCD deinit...\n");
ili9341_deinit(ili9341);
printf("FPGA load...\n");
if (load_old_bitstream) {
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) {
printf("Failed to load app bitstream into FPGA (%d)\n", res);
ice40_disable(ice40);
ili9341_init(ili9341);
return;
} else {
printf("Bitstream loaded succesfully!\n");
}
static bool test_spi_loopback(ICE40* ice40, uint32_t *rc) {
int i;
for (i = 0; i < 256; i++)
if (!test_spi(ice40))
break;
if (i == 256)
printf("SPI test success\n");
else
printf("SPI test failure at iteration %d\n", i);
bool waitForChoice = true;
while (waitForChoice) {
/* Run test 256 times */
for (i=0; i<256; i++) {
if (!test_spi_loopback_one(ice40))
break;
}
/* Failure ? */
if (i != 256) {
*rc = i + 1;
return false;
}
/* OK ! */
*rc = 0;
return true;
}
static bool test_soc_loopback(ICE40 *ice40, uint32_t *rc) {
/* Execute command */
if (!soc_message(ice40, SOC_CMD_PING, SOC_CMD_PING_PARAM, rc, 0)) {
*rc = -1;
return false;
}
/* Check response */
if (*rc != SOC_CMD_PING_RESP)
return false;
/* Success */
*rc = 0;
return true;
}
static bool test_uart_loopback(ICE40* ice40, uint32_t *rc) {
/* Enable loopback mode of RP2040 */
rp2040_set_fpga_loopback(get_rp2040(), true, true);
vTaskDelay(pdMS_TO_TICKS(10));
/* Execute command */
if (!soc_message(ice40, SOC_CMD_UART_LOOPBACK_TEST, 0, rc, 0)) {
*rc = -1;
return false;
}
/* Disable loopback mode of RP2040 */
rp2040_set_fpga_loopback(get_rp2040(), true, false);
/* Check response */
return *rc == SOC_RESP_OK;
}
static bool test_psram(ICE40* ice40, uint32_t *rc) {
/* Execute command */
if (!soc_message(ice40, SOC_CMD_PSRAM_TEST, 0, rc, pdMS_TO_TICKS(1000))) {
*rc = -1;
return false;
}
/* Check response */
return *rc == SOC_RESP_OK;
}
static bool test_irq_n(ICE40* ice40, uint32_t *rc) {
esp_err_t res;
/* Set pin as input */
res = gpio_set_direction(GPIO_INT_FPGA, GPIO_MODE_INPUT);
if (res != ESP_OK) {
*rc = 32;
return false;
}
/* Assert interrupt line */
if (!soc_message(ice40, SOC_CMD_IRQN_SET, 1, rc, 0)) {
*rc = -1;
return false;
}
if (*rc != SOC_RESP_OK)
return false;
/* Check level is 0 */
if (gpio_get_level(GPIO_INT_FPGA) != 0) {
*rc = 16;
return false;
}
/* Release interrupt line */
if (!soc_message(ice40, SOC_CMD_IRQN_SET, 0, rc, 0)) {
*rc = -1;
return false;
}
if (*rc != SOC_RESP_OK)
return false;
/* Check level is 1 */
if (gpio_get_level(GPIO_INT_FPGA) != 1) {
*rc = 16;
return false;
}
return true;
}
static bool test_lcd_mode(ICE40* ice40, uint32_t *rc) {
esp_err_t res;
bool ok;
/* Defaults */
ok = true;
*rc = 0;
/* Check state is 0 */
if (!soc_message(ice40, SOC_CMD_LCD_CHECK_MODE, 0, rc, 0)) {
*rc = 16;
return false;
}
if (*rc != SOC_RESP_OK)
return false;
/* Set LCD mode to 1 */
res = gpio_set_level(GPIO_LCD_MODE, 1);
if (res != ESP_OK) {
*rc = 32;
return false;
}
/* Check state is 1 */
if (!soc_message(ice40, SOC_CMD_LCD_CHECK_MODE, 1, rc, 0)) {
*rc = 17;
ok = false;
}
if (*rc != SOC_RESP_OK)
ok = false;
/* Set LCD mode back to 0 */
res = gpio_set_level(GPIO_LCD_MODE, 0);
if (res != ESP_OK) {
*rc = 33;
return false;
}
/* All good */
return ok;
}
static bool test_pmod_open(ICE40* ice40, uint32_t *rc) {
/* Execute command */
if (!soc_message(ice40, SOC_CMD_PMOD_OPEN_TEST, 0, rc, 0)) {
*rc = -1;
return false;
}
/* Check response */
return *rc == SOC_RESP_OK;
}
static bool test_pmod_plug(ICE40* ice40, uint32_t *rc) {
/* Execute command */
if (!soc_message(ice40, SOC_CMD_PMOD_PLUG_TEST, 0, rc, 0)) {
*rc = -1;
return false;
}
/* Check response */
return *rc == SOC_RESP_OK;
}
static bool test_lcd_init(ICE40* ice40, uint32_t *rc) {
/* Execute command */
if (!soc_message(ice40, SOC_CMD_LCD_INIT_TEST, 0, rc, 0)) {
*rc = -1;
return false;
}
/* Check response */
return *rc == SOC_RESP_OK;
}
typedef bool (*test_fn)(ICE40 *ice40, uint32_t *rc);
static bool wait_button(xQueueHandle buttonQueue) {
rp2040_input_message_t buttonMessage = {0};
printf("Waiting for button press...\n");
if (xQueueReceive(buttonQueue, &buttonMessage, portMAX_DELAY) == pdTRUE) {
printf("Button: %u, %u\n", buttonMessage.input, buttonMessage.state);
while (1) {
if (xQueueReceive(buttonQueue, &buttonMessage, 0) == pdTRUE) {
if (buttonMessage.state) {
switch(buttonMessage.input) {
case RP2040_INPUT_BUTTON_HOME:
case RP2040_INPUT_BUTTON_MENU:
waitForChoice = false;
break;
case RP2040_INPUT_BUTTON_BACK:
reload_fpga = true;
load_old_bitstream = true;
waitForChoice = false;
break;
return false;
case RP2040_INPUT_BUTTON_ACCEPT:
reload_fpga = true;
load_old_bitstream = false;
waitForChoice = false;
break;
return true;
default:
break;
}
}
} else {
vTaskDelay(pdMS_TO_TICKS(10));
}
}
ice40_disable(ice40);
ili9341_init(ili9341);
} while (reload_fpga);
}
static bool run_test(ICE40* ice40, pax_buf_t* pax_buffer, const pax_font_t *font, ILI9341* ili9341, int line,
const char *test_name, test_fn fn) {
bool rv;
uint32_t rc;
/* Test name */
pax_draw_text(pax_buffer, 0xffffffff, font, 18, 0, 20*line, test_name);
if (ili9341)
ili9341_write(ili9341, pax_buffer->buf);
/* Run the test */
rv = fn(ice40, &rc);
/* Display result */
if (!rv) {
/* Error */
char buf[10];
snprintf(buf, sizeof(buf), "%08x", rc);
pax_draw_text(pax_buffer, 0xffff0000, font, 18, 200, 20*line, buf);
} else {
/* OK ! */
pax_draw_text(pax_buffer, 0xff00ff00, font, 18, 200, 20*line, " OK");
}
if (ili9341)
ili9341_write(ili9341, pax_buffer->buf);
/* Pass through the 'OK' status */
return rv;
}
#define RUN_TEST(name, fn) do {\
ok &= run_test(ice40, pax_buffer, font, ili9341, line++, name, fn); \
} while (0)
#define RUN_TEST_MANDATORY(name, fn) do {\
if (!run_test(ice40, pax_buffer, font, ili9341, line++, name, fn)) { \
pax_draw_text(pax_buffer, 0xffff0000, font, 18, 0, 20*line, "Aborted"); \
ili9341_write(ili9341, pax_buffer->buf); \
ok = false; \
goto error; \
} \
} while (0)
#define RUN_TEST_BLIND(name, fn) do {\
ok &= run_test(ice40, pax_buffer, font, NULL, line++, name, fn); \
} while (0)
static void
run_all_tests(xQueueHandle buttonQueue, ICE40* ice40, pax_buf_t* pax_buffer, ILI9341* ili9341)
{
const pax_font_t *font;
int line = 0;
bool ok = true;
/* Screen init */
font = pax_get_font("sky mono");
pax_noclip(pax_buffer);
pax_background(pax_buffer, 0x8060f0);
ili9341_write(ili9341, pax_buffer->buf);
/* Run mandatory tests */
RUN_TEST_MANDATORY("Bitstream load", test_bitstream_load);
RUN_TEST_MANDATORY("SPI loopback", test_spi_loopback);
RUN_TEST_MANDATORY("SoC loopback", test_soc_loopback);
/* Set indicator to "in-progress" */
soc_message(ice40, SOC_CMD_RGB_STATE_SET, 1, NULL, 0);
/* Run non-interactive tests */
RUN_TEST("UART loopback", test_uart_loopback);
RUN_TEST("PSRAM", test_psram);
RUN_TEST("IRQ_n signal", test_irq_n);
RUN_TEST("LCD_MODE signal", test_lcd_mode);
RUN_TEST("PMOD open", test_pmod_open);
/* Show instructions for interactive test */
pax_draw_text(pax_buffer, 0xffc0c0c0, font, 9, 25, 20*line+ 0, "Insert PMOD plug");
pax_draw_text(pax_buffer, 0xffc0c0c0, font, 9, 25, 20*line+10, "Then press button for interactive test");
pax_draw_text(pax_buffer, 0xffc0c0c0, font, 9, 25, 20*line+20, " - Check LCD color bars");
pax_draw_text(pax_buffer, 0xffc0c0c0, font, 9, 25, 20*line+30, " - Then LCD & RGB led color cycling");
ili9341_write(ili9341, pax_buffer->buf);
/* Wait for button */
wait_button(buttonQueue);
/* Clear the instructions from buffer */
pax_draw_rect(pax_buffer, 0xff8060f0, 0, 20*line, 320, 240-20*line);
/* Handover LCD to FPGA */
ili9341_deinit(ili9341);
/* Run interactive tests */
RUN_TEST("PMOD plug", test_pmod_plug);
RUN_TEST("LCD init", test_lcd_init);
/* Wait a second (for user to see color bars) */
vTaskDelay(pdMS_TO_TICKS(1000));
/* Start LCD / RGB cycling */
soc_message(ice40, SOC_CMD_LCD_RGB_CYCLE_SET, 1, NULL, 0);
/* Wait for button */
wait_button(buttonQueue);
/* Stop LCD / RGB cycling */
soc_message(ice40, SOC_CMD_LCD_RGB_CYCLE_SET, 0, NULL, 0);
/* Take control of the LCD back and refresh screen */
ili9341_init(ili9341);
error:
/* Update indicator */
soc_message(ice40, SOC_CMD_RGB_STATE_SET, ok ? 2 : 3, NULL, 0);
/* Pass / Fail result on screen */
if (ok)
pax_draw_text(pax_buffer, 0xff00ff00, font, 36, 100, 20*line, "PASS");
else
pax_draw_text(pax_buffer, 0xffff0000, font, 36, 100, 20*line, "FAIL");
ili9341_write(ili9341, pax_buffer->buf);
/* Done, just wait for button */
wait_button(buttonQueue);
/* Cleanup */
ice40_disable(ice40);
return;
}
void fpga_test(xQueueHandle buttonQueue, ICE40* ice40, pax_buf_t* pax_buffer, ILI9341* ili9341) {
run_all_tests(buttonQueue, ice40, pax_buffer, ili9341);
}

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) {
pax_background(pax_buffer, 0xCCCCCC);
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) {

View file

@ -14,3 +14,4 @@
esp_err_t appfs_init(void);
uint8_t* load_file_to_ram(FILE* fd, size_t* fsize);
void appfs_boot_app(int fd);
void appfs_store_app(pax_buf_t* pax_buffer, ILI9341* ili9341, char* path, char* label);

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

@ -0,0 +1,9 @@
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "ili9341.h"
#include "pax_gfx.h"
void list_files_in_folder(const char* path);
void file_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341, const char* initial_path);

View file

@ -1,8 +1,13 @@
#pragma once
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "hardware.h"
#include "rp2040.h"
#include "ili9341.h"
#include "ice40.h"
#include "pax_gfx.h"
void fpga_test(ILI9341* ili9341, ICE40* ice40, xQueueHandle buttonQueue);
void fpga_test(xQueueHandle buttonQueue, ICE40* ice40, pax_buf_t* pax_buffer, ILI9341* ili9341);

View file

@ -40,7 +40,7 @@ size_t menu_get_position(menu_t* aMenu);
size_t menu_get_length(menu_t* aMenu);
void* menu_get_callback_args(menu_t* aMenu, size_t aPosition);
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
}

File diff suppressed because it is too large Load diff

8
main/include/uninstall.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 uninstall_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341);

View file

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

View file

@ -40,262 +40,23 @@
#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";
typedef enum action {
ACTION_NONE,
ACTION_APPFS,
ACTION_INSTALLER,
ACTION_SETTINGS,
ACTION_OTA,
ACTION_FPGA,
ACTION_FPGA_DL,
ACTION_RP2040_BL,
ACTION_WIFI_CONNECT,
ACTION_WIFI_SCAN,
ACTION_WIFI_MANUAL,
ACTION_WIFI_LIST,
ACTION_BACK,
ACTION_FILE_BROWSER,
ACTION_FILE_BROWSER_INT,
ACTION_UNINSTALL
} menu_action_t;
typedef struct _menu_args {
appfs_handle_t fd;
menu_action_t action;
} menu_args_t;
void appfs_store_app(pax_buf_t* pax_buffer, ILI9341* ili9341, char* path, char* label) {
graphics_task(pax_buffer, ili9341, NULL, "Installing app...");
esp_err_t res;
appfs_handle_t handle;
FILE* app_fd = fopen(path, "rb");
if (app_fd == NULL) {
graphics_task(pax_buffer, ili9341, NULL, "Failed to open file");
ESP_LOGE(TAG, "Failed to open file");
vTaskDelay(100 / portTICK_PERIOD_MS);
return;
}
size_t app_size;
uint8_t* app = load_file_to_ram(app_fd, &app_size);
if (app == NULL) {
graphics_task(pax_buffer, ili9341, NULL, "Failed to load app to RAM");
ESP_LOGE(TAG, "Failed to load application into RAM");
vTaskDelay(100 / portTICK_PERIOD_MS);
return;
}
ESP_LOGI(TAG, "Application size %d", app_size);
res = appfsCreateFile(label, app_size, &handle);
if (res != ESP_OK) {
graphics_task(pax_buffer, ili9341, NULL, "Failed to create on AppFS");
ESP_LOGE(TAG, "Failed to create file on AppFS (%d)", res);
vTaskDelay(100 / portTICK_PERIOD_MS);
free(app);
return;
}
res = appfsWrite(handle, 0, app, app_size);
if (res != ESP_OK) {
graphics_task(pax_buffer, ili9341, NULL, "Failed to write to AppFS");
ESP_LOGE(TAG, "Failed to write to file on AppFS (%d)", res);
vTaskDelay(100 / portTICK_PERIOD_MS);
free(app);
return;
}
free(app);
ESP_LOGI(TAG, "Application is now stored in AppFS");
graphics_task(pax_buffer, ili9341, NULL, "App installed!");
vTaskDelay(100 / portTICK_PERIOD_MS);
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;
menu_args_t* wifi_scan_args = malloc(sizeof(menu_args_t));
wifi_scan_args->action = ACTION_WIFI_SCAN;
menu_insert_item(menu, "Add by scan...", NULL, wifi_scan_args, -1);
menu_args_t* wifi_manual_args = malloc(sizeof(menu_args_t));
wifi_manual_args->action = ACTION_WIFI_MANUAL;
menu_insert_item(menu, "Add manually...", NULL, wifi_manual_args, -1);
menu_args_t* wifi_list_args = malloc(sizeof(menu_args_t));
wifi_list_args->action = ACTION_WIFI_LIST;
menu_insert_item(menu, "List known networks", NULL, wifi_list_args, -1);
menu_args_t* back_args = malloc(sizeof(menu_args_t));
back_args->action = ACTION_BACK;
menu_insert_item(menu, "< Back", NULL, back_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) {
*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 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,346 +67,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) {
ESP_LOGE(TAG, "Failed to open directory %s", path);
return;
}
struct dirent *ent;
char type;
char size[12];
char tpath[255];
char tbuffer[80];
struct stat sb;
struct tm *tm_info;
char *lpath = NULL;
int statok;
uint64_t total = 0;
int nfiles = 0;
printf("T Size Date/Time Name\n");
printf("-----------------------------------\n");
while ((ent = readdir(dir)) != NULL) {
sprintf(tpath, path);
if (path[strlen(path)-1] != '/') {
strcat(tpath,"/");
}
strcat(tpath,ent->d_name);
tbuffer[0] = '\0';
// Get file stat
statok = stat(tpath, &sb);
if (statok == 0) {
tm_info = localtime(&sb.st_mtime);
strftime(tbuffer, 80, "%d/%m/%Y %R", tm_info);
} else {
sprintf(tbuffer, " ");
}
if (ent->d_type == DT_REG) {
type = 'f';
nfiles++;
if (statok) {
strcpy(size, " ?");
} else {
total += sb.st_size;
if (sb.st_size < (1024*1024)) sprintf(size,"%8d", (int)sb.st_size);
else if ((sb.st_size/1024) < (1024*1024)) sprintf(size,"%6dKB", (int)(sb.st_size / 1024));
else sprintf(size,"%6dMB", (int)(sb.st_size / (1024 * 1024)));
}
} else {
type = 'd';
strcpy(size, " -");
}
printf("%c %s %s %s\r\n", type, size, tbuffer, ent->d_name);
}
printf("-----------------------------------\n");
if (total < (1024*1024)) printf(" %8d", (int)total);
else if ((total/1024) < (1024*1024)) printf(" %6dKB", (int)(total / 1024));
else printf(" %6dMB", (int)(total / (1024 * 1024)));
printf(" in %d file(s)\n", nfiles);
printf("-----------------------------------\n");
closedir(dir);
free(lpath);
}
typedef struct _uninstall_menu_args {
appfs_handle_t fd;
char name[512];
} uninstall_menu_args_t;
void uninstall_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341) {
appfs_handle_t appfs_fd = APPFS_INVALID_FD;
menu_t* menu = menu_alloc("Uninstall application");
while (1) {
appfs_fd = appfsNextEntry(appfs_fd);
if (appfs_fd == APPFS_INVALID_FD) break;
const char* name = NULL;
appfsEntryInfo(appfs_fd, &name, NULL);
uninstall_menu_args_t* args = malloc(sizeof(uninstall_menu_args_t));
if (args == NULL) {
ESP_LOGE(TAG, "Failed to malloc() menu args");
return;
}
args->fd = appfs_fd;
sprintf(args->name, name);
menu_insert_item(menu, name, NULL, (void*) args, -1);
}
uninstall_menu_args_t* menuArgs = NULL;
bool render = true;
bool exit = false;
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;
case RP2040_INPUT_BUTTON_HOME:
if (value) exit = true;
break;
default:
break;
}
}
if (render) {
graphics_task(pax_buffer, ili9341, menu, NULL);
render = false;
}
if (menuArgs != NULL) {
char message[1024];
sprintf(message, "Uninstalling %s...", menuArgs->name);
printf("%s\n", message);
graphics_task(pax_buffer, ili9341, menu, message);
appfsDeleteFile(menuArgs->name);
menuArgs = NULL;
break;
}
if (exit) {
break;
}
}
for (size_t index = 0; index < menu_get_length(menu); index++) {
free(menu_get_callback_args(menu, index));
}
menu_free(menu);
}
typedef struct _file_browser_menu_args {
char type;
char path[512];
char label[512];
} file_browser_menu_args_t;
void find_parent_dir(char* path, char* parent) {
size_t last_separator = 0;
for (size_t index = 0; index < strlen(path); index++) {
if (path[index] == '/') last_separator = index;
}
strcpy(parent, path);
parent[last_separator] = '\0';
}
void file_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341, const char* initial_path) {
char path[512] = {0};
strncpy(path, initial_path, sizeof(path));
while (true) {
menu_t* menu = menu_alloc(path);
DIR* dir = opendir(path);
if (dir == NULL) {
ESP_LOGE(TAG, "Failed to open directory %s", path);
return;
}
struct dirent *ent;
file_browser_menu_args_t* pd_args = malloc(sizeof(file_browser_menu_args_t));
pd_args->type = 'd';
find_parent_dir(path, pd_args->path);
printf("Parent dir: %s\n", pd_args->path);
menu_insert_item(menu, "../", NULL, pd_args, -1);
while ((ent = readdir(dir)) != NULL) {
file_browser_menu_args_t* args = malloc(sizeof(file_browser_menu_args_t));
sprintf(args->path, path);
if (path[strlen(path)-1] != '/') {
strcat(args->path,"/");
}
strcat(args->path,ent->d_name);
if (ent->d_type == DT_REG) {
args->type = 'f';
} else {
args->type = 'd';
}
printf("%c %s %s\r\n", args->type, ent->d_name, args->path);
snprintf(args->label, sizeof(args->label), "%s%s", ent->d_name, (args->type == 'd') ? "/" : "");
menu_insert_item(menu, args->label, NULL, args, -1);
}
closedir(dir);
bool render = true;
bool exit = false;
file_browser_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;
case RP2040_INPUT_BUTTON_HOME:
if (value) exit = true;
break;
default:
break;
}
}
if (render) {
graphics_task(pax_buffer, ili9341, menu, NULL);
render = false;
}
if (menuArgs != NULL) {
if (menuArgs->type == 'd') {
strcpy(path, menuArgs->path);
break;
} else {
printf("File selected: %s\n", menuArgs->path);
appfs_store_app(pax_buffer, ili9341, menuArgs->path, menuArgs->label);
}
menuArgs = NULL;
render = true;
}
if (exit) {
break;
}
}
for (size_t index = 0; index < menu_get_length(menu); index++) {
free(menu_get_callback_args(menu, index));
}
menu_free(menu);
}
}
void app_main(void) {
esp_err_t res;
@ -681,8 +102,9 @@ void app_main(void) {
esp_restart();
}
audio_init();
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 +129,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);
@ -719,6 +143,8 @@ void app_main(void) {
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");
display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "BNO055 sensor error", "Check I2C bus", "Remove SAO and try again");
@ -731,6 +157,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");
@ -743,6 +171,8 @@ void app_main(void) {
esp_restart();
}
display_boot_screen(pax_buffer, ili9341, "Initializing AppFS...");
/* Start AppFS */
res = appfs_init();
if (res != ESP_OK) {
@ -751,6 +181,8 @@ void app_main(void) {
esp_restart();
}
display_boot_screen(pax_buffer, ili9341, "Initializing NVS...");
/* Start NVS */
res = nvs_init();
if (res != ESP_OK) {
@ -759,6 +191,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");
@ -785,9 +219,10 @@ void app_main(void) {
bool sdcard_ready = (res == ESP_OK);
if (sdcard_ready) {
ESP_LOGI(TAG, "SD card filesystem mounted");
//list_files_in_folder("/sd");
}
play_bootsound();
/* Start LEDs */
ws2812_init(GPIO_LED_DATA);
uint8_t ledBuffer[15] = {50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0};
@ -798,107 +233,8 @@ 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) {
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) {
file_browser(rp2040->queue, pax_buffer, ili9341, "/internal");
} else if (menu_action == ACTION_UNINSTALL) {
uninstall_browser(rp2040->queue, pax_buffer, ili9341);
} else if (menu_action == ACTION_SETTINGS) {
while (true) {
menu_wifi_settings(rp2040->queue, pax_buffer, ili9341, &menu_action);
if (menu_action == ACTION_WIFI_MANUAL) {
nvs_handle_t handle;
nvs_open("system", NVS_READWRITE, &handle);
char ssid[33];
char password[33];
size_t requiredSize;
esp_err_t res = nvs_get_str(handle, "wifi.ssid", NULL, &requiredSize);
if (res != ESP_OK) {
strcpy(ssid, "");
strcpy(password, "");
} else if (requiredSize < sizeof(ssid)) {
res = nvs_get_str(handle, "wifi.ssid", ssid, &requiredSize);
if (res != ESP_OK) strcpy(ssid, "");
res = nvs_get_str(handle, "wifi.password", NULL, &requiredSize);
if (res != ESP_OK) {
strcpy(password, "");
} else if (requiredSize < sizeof(password)) {
res = nvs_get_str(handle, "wifi.password", password, &requiredSize);
if (res != ESP_OK) strcpy(password, "");
menu_start(rp2040->queue, pax_buffer, ili9341);
}
}
bool accepted = keyboard(rp2040->queue, pax_buffer, ili9341, 30, 30, pax_buffer->width - 60, pax_buffer->height - 60, "WiFi SSID", "Press HOME to exit", ssid, sizeof(ssid));
if (accepted) {
accepted = keyboard(rp2040->queue, pax_buffer, ili9341, 30, 30, pax_buffer->width - 60, pax_buffer->height - 60, "WiFi password", "Press HOME to exit", password, sizeof(password));
}
if (accepted) {
nvs_set_str(handle, "wifi.ssid", ssid);
nvs_set_str(handle, "wifi.password", password);
graphics_task(pax_buffer, ili9341, NULL, "WiFi settings stored");
} else {
graphics_task(pax_buffer, ili9341, NULL, "Canceled");
}
nvs_set_u8(handle, "wifi.use_ent", 0);
nvs_close(handle);
} else if (menu_action == ACTION_WIFI_LIST) {
nvs_handle_t handle;
nvs_open("system", NVS_READWRITE, &handle);
char ssid[33];
char password[33];
size_t requiredSize;
esp_err_t res = nvs_get_str(handle, "wifi.ssid", NULL, &requiredSize);
if (res != ESP_OK) {
strcpy(ssid, "");
} else if (requiredSize < sizeof(ssid)) {
res = nvs_get_str(handle, "wifi.ssid", ssid, &requiredSize);
if (res != ESP_OK) strcpy(ssid, "");
res = nvs_get_str(handle, "wifi.password", NULL, &requiredSize);
if (res != ESP_OK) {
strcpy(password, "");
} else if (requiredSize < sizeof(password)) {
res = nvs_get_str(handle, "wifi.password", password, &requiredSize);
if (res != ESP_OK) strcpy(password, "");
}
}
nvs_close(handle);
char buffer[300];
snprintf(buffer, sizeof(buffer), "SSID is %s\nPassword is %s", ssid, password);
graphics_task(pax_buffer, ili9341, NULL, buffer);
} else if (menu_action == ACTION_WIFI_SCAN) {
// Scan for WiFi networks.
wifi_scan(NULL);
} else {
break;
}
}
}
}
free(framebuffer);
}

View file

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

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

@ -0,0 +1,112 @@
#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"
#include "file_browser.h"
#include "fpga_test.h"
typedef enum action {
ACTION_NONE,
ACTION_BACK,
ACTION_FPGA_DL,
ACTION_FPGA_TEST,
ACTION_FILE_BROWSER,
ACTION_FILE_BROWSER_INT,
} 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);
menu_insert_item(menu, "FPGA selftest", NULL, (void*) ACTION_FPGA_TEST, -1);
menu_insert_item(menu, "File browser (SD card)", NULL, (void*) ACTION_FILE_BROWSER, -1);
menu_insert_item(menu, "File browser (internal)", NULL, (void*) ACTION_FILE_BROWSER_INT, -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_FPGA_TEST) {
fpga_test(buttonQueue, get_ice40(), pax_buffer, ili9341);
} else if (action == ACTION_FILE_BROWSER) {
file_browser(buttonQueue, pax_buffer, ili9341, "/sd");
} else if (action == ACTION_FILE_BROWSER_INT) {
file_browser(buttonQueue, pax_buffer, ili9341, "/internal");
} 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);

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

@ -0,0 +1,112 @@
#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");
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");
bool quit = false;
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) {
quit = true;
}
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;
}
if (quit) {
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);

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

@ -0,0 +1,122 @@
#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"
#include "uninstall.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_UNINSTALL) {
uninstall_browser(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);

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

@ -0,0 +1,201 @@
#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 "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 "graphics_wrapper.h"
static const char *TAG = "wifi menu";
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 wifi_show(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341);
void wifi_setup(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341, bool scan);
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) {
wifi_show(buttonQueue, pax_buffer, ili9341);
} else if (action == ACTION_SCAN) {
wifi_setup(buttonQueue, pax_buffer, ili9341, true);
} else if (action == ACTION_MANUAL) {
wifi_setup(buttonQueue, pax_buffer, ili9341, false);
} else if (action == ACTION_BACK) {
break;
}
render = true;
action = ACTION_NONE;
render_wifi_help(pax_buffer);
}
}
menu_free(menu);
}
void wifi_show(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341) {
nvs_handle_t handle;
nvs_open("system", NVS_READWRITE, &handle);
char ssid[33] = "<not set>";
char password[33] = "<not set>";
size_t requiredSize;
esp_err_t res = nvs_get_str(handle, "wifi.ssid", NULL, &requiredSize);
if ((res == ESP_OK) && (requiredSize < sizeof(ssid))) {
res = nvs_get_str(handle, "wifi.ssid", ssid, &requiredSize);
}
res = nvs_get_str(handle, "wifi.password", NULL, &requiredSize);
if ((res == ESP_OK) && (requiredSize < sizeof(password))) {
res = nvs_get_str(handle, "wifi.password", password, &requiredSize);
}
nvs_close(handle);
char buffer[300];
pax_noclip(pax_buffer);
pax_background(pax_buffer, 0xFFFFFF);
snprintf(buffer, sizeof(buffer), "SSID is %s", ssid);
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*0, buffer);
snprintf(buffer, sizeof(buffer), "Password is %s", password);
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*1, buffer);
ili9341_write(ili9341, pax_buffer->buf);
bool quit = false;
while (!quit) {
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_BUTTON_HOME:
case RP2040_INPUT_BUTTON_BACK:
case RP2040_INPUT_BUTTON_ACCEPT:
case RP2040_INPUT_JOYSTICK_PRESS:
case RP2040_INPUT_BUTTON_SELECT:
case RP2040_INPUT_BUTTON_START:
if (value) quit = true;
break;
default:
break;
}
}
}
}
void wifi_setup(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341, bool scan) {
nvs_handle_t handle;
nvs_open("system", NVS_READWRITE, &handle);
char ssid[33];
char password[33];
size_t requiredSize;
esp_err_t res = nvs_get_str(handle, "wifi.ssid", NULL, &requiredSize);
if (res != ESP_OK) {
strcpy(ssid, "");
strcpy(password, "");
} else if (requiredSize < sizeof(ssid)) {
res = nvs_get_str(handle, "wifi.ssid", ssid, &requiredSize);
if (res != ESP_OK) strcpy(ssid, "");
res = nvs_get_str(handle, "wifi.password", NULL, &requiredSize);
if (res != ESP_OK) {
strcpy(password, "");
} else if (requiredSize < sizeof(password)) {
res = nvs_get_str(handle, "wifi.password", password, &requiredSize);
if (res != ESP_OK) strcpy(password, "");
}
}
bool accepted = keyboard(buttonQueue, pax_buffer, ili9341, 30, 30, pax_buffer->width - 60, pax_buffer->height - 60, "WiFi SSID", "Press HOME to exit", ssid, sizeof(ssid));
if (accepted) {
accepted = keyboard(buttonQueue, pax_buffer, ili9341, 30, 30, pax_buffer->width - 60, pax_buffer->height - 60, "WiFi password", "Press HOME to exit", password, sizeof(password));
}
if (accepted) {
nvs_set_str(handle, "wifi.ssid", ssid);
nvs_set_str(handle, "wifi.password", password);
display_boot_screen(pax_buffer, ili9341, "WiFi settings stored");
}
nvs_set_u8(handle, "wifi.use_ent", 0);
nvs_close(handle);
}

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 "rp2040.h"
#include "rp2040bl.h"
#include "rp2040_firmware.h"
#include "system_wrapper.h"
#include "graphics_wrapper.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) {
size_t firmware_size = rp2040_firmware_bin_end - rp2040_firmware_bin_start;
char message[64];
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);
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
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;
while (true) {
if ((sizeof(mch2022_firmware_bin) - position) < txSize) {
txSize = sizeof(mch2022_firmware_bin) - position;
if ((firmware_size - position) < txSize) {
txSize = firmware_size - position;
}
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_background(pax_buffer, 0x325aa8);
@ -206,7 +209,7 @@ void rp2040_updater(RP2040* rp2040, pax_buf_t* pax_buffer, ILI9341* ili9341) {
uint32_t checkCrc = 0;
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);
totalCrc = crc32_le(totalCrc, txBuffer, write_size);
totalLength += write_size;

File diff suppressed because it is too large Load diff

119
main/uninstall.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 "uninstall.h"
static const char *TAG = "uninstaller";
typedef struct _uninstall_menu_args {
appfs_handle_t fd;
char name[512];
} uninstall_menu_args_t;
void uninstall_browser(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341) {
menu_t* menu = menu_alloc("Uninstall application");
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);
uninstall_menu_args_t* args = malloc(sizeof(uninstall_menu_args_t));
if (args == NULL) {
ESP_LOGE(TAG, "Failed to malloc() menu args");
return;
}
args->fd = appfs_fd;
sprintf(args->name, name);
menu_insert_item(menu, name, NULL, (void*) args, -1);
}
bool render = true;
uninstall_menu_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] uninstall app [B] back");
bool quit = false;
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) {
quit = true;
}
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) {
char message[1024];
sprintf(message, "Uninstalling %s...", menuArgs->name);
printf("%s\n", message);
display_boot_screen(pax_buffer, ili9341, message);
appfsDeleteFile(menuArgs->name);
menuArgs = NULL;
break;
}
if (quit) {
break;
}
}
for (size_t index = 0; index < menu_get_length(menu); index++) {
free(menu_get_callback_args(menu, index));
}
menu_free(menu);
}

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