mch2022-template-app/main/main.c

374 lines
12 KiB
C
Raw Normal View History

#include <stdio.h>
2022-01-01 04:51:12 +00:00
#include <string.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
2022-04-08 21:15:57 +00:00
#include <freertos/queue.h>
#include <esp_system.h>
2022-01-24 21:32:33 +00:00
//#include <esp_spi_flash.h>
#include <esp_err.h>
#include <esp_log.h>
#include "hardware.h"
#include "managed_i2c.h"
#include "pax_gfx.h"
#include "sdcard.h"
#include "appfs.h"
2022-01-11 23:53:38 +00:00
#include "driver_framebuffer.h"
2022-01-06 00:12:28 +00:00
#include "esp_sleep.h"
#include "soc/rtc.h"
#include "soc/rtc_cntl_reg.h"
2022-01-24 21:32:33 +00:00
#include "rp2040.h"
2022-04-08 21:15:57 +00:00
#include "fpga_test.h"
#include "menu.h"
#include "button_message.h"
2022-02-04 00:04:51 +00:00
static const char *TAG = "main";
bool calibrate = true;
bool display_bno_value = false;
2022-01-01 04:51:12 +00:00
ILI9341* ili9341 = NULL;
2022-01-01 06:13:50 +00:00
ICE40* ice40 = NULL;
BNO055* bno055 = NULL;
2022-01-24 21:32:33 +00:00
RP2040* rp2040 = NULL;
2022-02-04 00:04:51 +00:00
uint8_t* framebuffer = NULL;
pax_buf_t* pax_buffer = NULL;
2022-04-08 21:15:57 +00:00
xQueueHandle buttonQueue;
typedef enum action {
ACTION_NONE,
2022-04-08 21:15:57 +00:00
ACTION_APPFS,
2022-02-04 00:04:51 +00:00
ACTION_INSTALLER,
ACTION_FPGA
} menu_action_t;
2022-04-08 21:15:57 +00:00
typedef struct _menu_args {
appfs_handle_t fd;
menu_action_t action;
2022-04-08 21:15:57 +00:00
} menu_args_t;
2022-01-23 00:27:22 +00:00
void button_handler(uint8_t pin, bool value) {
2022-04-08 21:15:57 +00:00
button_message_t message;
message.button = pin;
message.state = value;
xQueueSend(buttonQueue, &message, portMAX_DELAY);
}
void button_init() {
2022-01-11 23:53:38 +00:00
PCA9555* pca9555 = get_pca9555();
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_START, button_handler);
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_SELECT, button_handler);
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_MENU, button_handler);
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_HOME, button_handler);
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_JOY_LEFT, button_handler);
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_JOY_PRESS, button_handler);
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_JOY_DOWN, button_handler);
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_JOY_UP, button_handler);
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_JOY_RIGHT, button_handler);
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_BACK, button_handler);
pca9555_set_interrupt_handler(pca9555, PCA9555_PIN_BTN_ACCEPT, button_handler);
}
void restart() {
for (int i = 3; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();
}
2022-04-08 21:15:57 +00:00
void message_render(pax_buf_t *aBuffer, char* message, float aPosX, float aPosY, float aWidth, float aHeight) {
pax_col_t fgColor = 0xFFFF0000;
pax_col_t bgColor = 0xFFFFD4D4;
pax_clip(aBuffer, aPosX, aPosY, aWidth, aHeight);
pax_simple_rect(aBuffer, bgColor, aPosX, aPosY, aWidth, aHeight);
pax_outline_rect(aBuffer, fgColor, aPosX, aPosY, aWidth, aHeight);
pax_clip(aBuffer, aPosX + 1, aPosY + 1, aWidth - 2, aHeight - 2);
pax_draw_text(aBuffer, fgColor, NULL, 18, aPosX + 1, aPosY + 1, message);
pax_noclip(aBuffer);
}
2022-04-08 21:15:57 +00:00
esp_err_t graphics_task(pax_buf_t* buffer, ILI9341* ili9341, uint8_t* framebuffer, menu_t* menu, char* message) {
pax_background(pax_buffer, 0xCCCCCC);
if (menu != NULL) {
menu_render(pax_buffer, menu, 10, 10, 320-20, 240-20);
}
2022-04-08 21:15:57 +00:00
if (message != NULL) {
message_render(pax_buffer, message, 20, 110, 320-40, 20);
}
return ili9341_write(ili9341, framebuffer);
}
2022-02-04 00:04:51 +00:00
esp_err_t draw_message(char* message) {
pax_background(pax_buffer, 0xFFFFFF);
pax_draw_text(pax_buffer, pax_col_rgb(0,0,0), PAX_FONT_DEFAULT, 18, 0, 0, message);
2022-01-24 21:32:33 +00:00
return ili9341_write(ili9341, framebuffer);
}
2022-04-08 21:15:57 +00:00
esp_err_t appfs_init(void) {
return appfsInit(APPFS_PART_TYPE, APPFS_PART_SUBTYPE);
}
2022-01-06 00:12:28 +00:00
uint8_t* load_file_to_ram(FILE* fd, size_t* fsize) {
fseek(fd, 0, SEEK_END);
*fsize = ftell(fd);
fseek(fd, 0, SEEK_SET);
uint8_t* file = malloc(*fsize);
2022-01-06 00:12:28 +00:00
if (file == NULL) return NULL;
fread(file, *fsize, 1, fd);
return file;
}
2022-01-06 00:12:28 +00:00
void appfs_store_app(void) {
2022-04-08 21:15:57 +00:00
draw_message("Installing app...");
2022-01-06 00:12:28 +00:00
esp_err_t res;
appfs_handle_t handle;
FILE* app_fd = fopen("/sd/gnuboy.bin", "rb");
if (app_fd == NULL) {
2022-04-08 21:15:57 +00:00
draw_message("Failed to open gnuboy.bin");
2022-01-06 00:12:28 +00:00
ESP_LOGE(TAG, "Failed to open gnuboy.bin");
2022-04-08 21:15:57 +00:00
vTaskDelay(100 / portTICK_PERIOD_MS);
2022-01-06 00:12:28 +00:00
return;
}
size_t app_size;
uint8_t* app = load_file_to_ram(app_fd, &app_size);
if (app == NULL) {
2022-04-08 21:15:57 +00:00
draw_message("Failed to load app to RAM");
2022-01-06 00:12:28 +00:00
ESP_LOGE(TAG, "Failed to load application into RAM");
2022-04-08 21:15:57 +00:00
vTaskDelay(100 / portTICK_PERIOD_MS);
2022-01-06 00:12:28 +00:00
return;
}
2022-01-23 00:27:22 +00:00
ESP_LOGI(TAG, "Application size %d", app_size);
2022-01-06 00:12:28 +00:00
res = appfsCreateFile("gnuboy", app_size, &handle);
if (res != ESP_OK) {
2022-04-08 21:15:57 +00:00
draw_message("Failed to create on AppFS");
2022-01-06 00:12:28 +00:00
ESP_LOGE(TAG, "Failed to create file on AppFS (%d)", res);
2022-04-08 21:15:57 +00:00
vTaskDelay(100 / portTICK_PERIOD_MS);
2022-01-06 00:12:28 +00:00
free(app);
return;
}
2022-01-06 00:12:28 +00:00
res = appfsWrite(handle, 0, app, app_size);
if (res != ESP_OK) {
2022-04-08 21:15:57 +00:00
draw_message("Failed to write to AppFS");
2022-01-06 00:12:28 +00:00
ESP_LOGE(TAG, "Failed to write to file on AppFS (%d)", res);
2022-04-08 21:15:57 +00:00
vTaskDelay(100 / portTICK_PERIOD_MS);
2022-01-06 00:12:28 +00:00
free(app);
return;
}
free(app);
ESP_LOGI(TAG, "Application is now stored in AppFS");
2022-04-08 21:15:57 +00:00
draw_message("App installed!");
vTaskDelay(100 / portTICK_PERIOD_MS);
2022-01-06 00:12:28 +00:00
return;
}
void appfs_boot_app(int fd) {
if (fd<0 || fd>255) {
REG_WRITE(RTC_CNTL_STORE0_REG, 0);
} else {
REG_WRITE(RTC_CNTL_STORE0_REG, 0xA5000000|fd);
}
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
esp_sleep_enable_timer_wakeup(10);
esp_deep_sleep_start();
}
void appfs_test(bool sdcard_ready) {
appfs_handle_t fd = appfsOpen("gnuboy");
if (fd < 0) {
ESP_LOGW(TAG, "gnuboy not found in appfs");
2022-04-08 21:15:57 +00:00
draw_message("gnuboy not found in fs!");
/*if (sdcard_ready) {
2022-01-06 00:12:28 +00:00
appfs_store_app();
appfs_test(false); // Recursive, but who cares :D
2022-04-08 21:15:57 +00:00
}*/
2022-01-06 00:12:28 +00:00
} else {
2022-04-08 21:15:57 +00:00
draw_message("Booting gnuboy...");
2022-01-23 00:27:22 +00:00
ESP_LOGE(TAG, "booting gnuboy from appfs (%d)", fd);
2022-01-06 00:12:28 +00:00
appfs_boot_app(fd);
}
}
void app_main(void) {
esp_err_t res;
2022-04-08 21:15:57 +00:00
buttonQueue = xQueueCreate(10, sizeof(button_message_t));
if (buttonQueue == NULL) {
ESP_LOGE(TAG, "Failed to allocate queue");
restart();
}
2022-02-04 00:04:51 +00:00
framebuffer = heap_caps_malloc(ILI9341_BUFFER_SIZE, MALLOC_CAP_8BIT);
if (framebuffer == NULL) {
ESP_LOGE(TAG, "Failed to allocate framebuffer");
restart();
}
memset(framebuffer, 0, ILI9341_BUFFER_SIZE);
2022-02-04 00:04:51 +00:00
pax_buffer = malloc(sizeof(pax_buf_t));
if (framebuffer == NULL) {
ESP_LOGE(TAG, "Failed to allocate pax buffer");
restart();
}
memset(pax_buffer, 0, sizeof(pax_buf_t));
pax_buf_init(pax_buffer, framebuffer, ILI9341_WIDTH, ILI9341_HEIGHT, PAX_BUF_16_565RGB);
2022-01-11 23:53:38 +00:00
driver_framebuffer_init(framebuffer);
2022-01-24 21:32:33 +00:00
res = board_init();
2022-01-23 00:27:22 +00:00
if (res != ESP_OK) {
printf("Failed to initialize hardware!\n");
restart();
}
ili9341 = get_ili9341();
ice40 = get_ice40();
bno055 = get_bno055();
2022-01-24 21:32:33 +00:00
rp2040 = get_rp2040();
2022-04-08 21:15:57 +00:00
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Button init...");
button_init();
2022-02-04 00:04:51 +00:00
rp2040_set_led_mode(rp2040, true, true);
2022-04-08 21:15:57 +00:00
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "AppFS init...");
2022-01-06 00:12:28 +00:00
res = appfs_init();
if (res != ESP_OK) {
ESP_LOGE(TAG, "AppFS init failed: %d", res);
return;
}
ESP_LOGI(TAG, "AppFS initialized");
2022-04-08 21:15:57 +00:00
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Mount SD card...");
2022-01-06 00:12:28 +00:00
res = mount_sd(SD_CMD, SD_CLK, SD_D0, SD_PWR, "/sd", false, 5);
bool sdcard_ready = (res == ESP_OK);
2022-04-08 21:15:57 +00:00
if (sdcard_ready) {
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "SD card mounted");
2022-02-04 00:04:51 +00:00
} else {
2022-04-08 21:15:57 +00:00
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "No SD card");
}
2022-04-08 21:15:57 +00:00
menu_t* menu = menu_alloc("Launcher");
2022-04-08 21:15:57 +00:00
appfs_handle_t current_fd = APPFS_INVALID_FD;
while (1) {
2022-04-08 21:15:57 +00:00
current_fd = appfsNextEntry(current_fd);
if (current_fd == APPFS_INVALID_FD) break;
2022-04-08 21:15:57 +00:00
const char* name = NULL;
appfsEntryInfo(current_fd, &name, NULL);
menu_args_t* args = malloc(sizeof(menu_args_t));
args->fd = current_fd;
args->action = ACTION_APPFS;
menu_insert_item(menu, name, NULL, (void*) 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* install_args = malloc(sizeof(menu_args_t));
install_args->action = ACTION_INSTALLER;
menu_insert_item(menu, "Install app...", NULL, install_args, -1);
bool render = true;
menu_args_t* menuAction = NULL;
while (1) {
button_message_t buttonMessage = {0};
if (xQueueReceive(buttonQueue, &buttonMessage, 16 / portTICK_PERIOD_MS) == pdTRUE) {
uint8_t pin = buttonMessage.button;
bool value = buttonMessage.state;
switch(pin) {
case PCA9555_PIN_BTN_JOY_LEFT:
printf("Joystick horizontal %s\n", value ? "left" : "center");
break;
case PCA9555_PIN_BTN_JOY_PRESS:
printf("Joystick %s\n", value ? "pressed" : "released");
break;
case PCA9555_PIN_BTN_JOY_DOWN:
printf("Joystick vertical %s\n", value ? "down" : "center");
if (value) {
menu_navigate_next(menu);
render = true;
}
break;
case PCA9555_PIN_BTN_JOY_UP:
printf("Joystick vertical %s\n", value ? "up" : "center");
if (value) {
menu_navigate_previous(menu);
render = true;
}
break;
case PCA9555_PIN_BTN_JOY_RIGHT:
printf("Joystick horizontal %s\n", value ? "right" : "center");
break;
case PCA9555_PIN_BTN_HOME:
printf("Home button %s\n", value ? "pressed" : "released");
break;
case PCA9555_PIN_BTN_MENU:
printf("Menu button %s\n", value ? "pressed" : "released");
//if (value) reset_to_menu = true;
break;
case PCA9555_PIN_BTN_START: {
printf("Start button %s\n", value ? "pressed" : "released");
break;
}
case PCA9555_PIN_BTN_SELECT: {
printf("Select button %s\n", value ? "pressed" : "released");
break;
}
case PCA9555_PIN_BTN_BACK:
printf("Back button %s\n", value ? "pressed" : "released");
break;
case PCA9555_PIN_BTN_ACCEPT:
printf("Accept button %s\n", value ? "pressed" : "released");
if (value) {
menuAction = menu_get_callback_args(menu, menu_get_position(menu));
printf("Position: %u\n", menu_get_position(menu));
}
break;
default:
printf("Unknown button %d %s\n", pin, value ? "pressed" : "released");
}
}
2022-04-08 21:15:57 +00:00
if (render) {
graphics_task(pax_buffer, ili9341, framebuffer, menu, NULL);
render = false;
}
2022-04-08 21:15:57 +00:00
if (menuAction != NULL) {
graphics_task(pax_buffer, ili9341, framebuffer, menu, "Please wait...");
if (menuAction->action == ACTION_APPFS) {
appfs_boot_app(menuAction->fd);
} else if (menuAction->action == ACTION_FPGA) {
graphics_task(pax_buffer, ili9341, framebuffer, menu, "FPGA TEST");
fpga_test(ili9341, ice40, buttonQueue);
}else if (menuAction->action == ACTION_INSTALLER) {
graphics_task(pax_buffer, ili9341, framebuffer, menu, "INSTALLER");
appfs_store_app();
}
2022-04-08 21:15:57 +00:00
menuAction = NULL;
render = true;
2022-01-01 06:13:50 +00:00
}
}
2022-04-08 21:15:57 +00:00
free(framebuffer);
}