mch2022-talktopics/main/menus/launcher.c

135 lines
4.1 KiB
C
Raw Normal View History

2022-05-30 21:56:23 +00:00
#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"
2022-06-03 02:23:00 +00:00
#include "pax_codecs.h"
2022-05-30 21:56:23 +00:00
#include "menu.h"
#include "rp2040.h"
#include "appfs_wrapper.h"
2022-06-03 02:23:00 +00:00
extern const uint8_t apps_png_start[] asm("_binary_apps_png_start");
extern const uint8_t apps_png_end[] asm("_binary_apps_png_end");
2022-05-30 21:56:23 +00:00
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", 34, 18);
2022-06-03 02:23:00 +00:00
menu->fgColor = 0xFF000000;
menu->bgColor = 0xFFFFFFFF;
menu->bgTextColor = 0xFFFFFFFF;
menu->selectedItemColor = 0xFFfa448c;
menu->borderColor = 0xFF491d88;
menu->titleColor = 0xFFfa448c;
menu->titleBgColor = 0xFF491d88;
menu->scrollbarBgColor = 0xFFCCCCCC;
menu->scrollbarFgColor = 0xFF555555;
pax_buf_t icon_apps;
pax_decode_png_buf(&icon_apps, (void*) apps_png_start, apps_png_end - apps_png_start, PAX_BUF_32_8888ARGB, 0);
menu_set_icon(menu, &icon_apps);
2022-06-02 10:39:19 +00:00
const pax_font_t *font = pax_get_font("saira regular");
2022-05-30 21:56:23 +00:00
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);
2022-06-03 02:23:00 +00:00
pax_draw_text(pax_buffer, 0xFF000000, font, 18, 5, 240 - 18, "[A] start app [B] back");
2022-05-30 21:56:23 +00:00
bool quit = false;
2022-05-30 21:56:23 +00:00
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;
2022-05-30 21:56:23 +00:00
}
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) {
2022-06-03 02:23:00 +00:00
menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF491d88);
2022-05-30 21:56:23 +00:00
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;
}
2022-05-30 21:56:23 +00:00
}
for (size_t index = 0; index < menu_get_length(menu); index++) {
free(menu_get_callback_args(menu, index));
}
menu_free(menu);
2022-06-03 02:23:00 +00:00
pax_buf_destroy(&icon_apps);
2022-05-30 21:56:23 +00:00
}