mch2022-talktopics/main/graphics_wrapper.c

162 lines
6.1 KiB
C
Raw Normal View History

2022-04-18 00:10:44 +00:00
#include <string.h>
2022-04-15 23:16:36 +00:00
#include "graphics_wrapper.h"
2022-04-18 00:10:44 +00:00
#include "hardware.h"
#include "pax_keyboard.h"
2022-04-23 02:00:55 +00:00
#include "rp2040.h"
2022-04-15 23:16:36 +00:00
void render_message(pax_buf_t *pax_buffer, char* message, float aPosX, float aPosY, float aWidth, float aHeight) {
2022-04-15 23:16:36 +00:00
pax_col_t fgColor = 0xFFFF0000;
pax_col_t bgColor = 0xFFFFD4D4;
pax_clip(pax_buffer, aPosX, aPosY, aWidth, aHeight);
pax_simple_rect(pax_buffer, bgColor, aPosX, aPosY, aWidth, aHeight);
pax_outline_rect(pax_buffer, fgColor, aPosX, aPosY, aWidth, aHeight);
pax_clip(pax_buffer, aPosX + 1, aPosY + 1, aWidth - 2, aHeight - 2);
pax_draw_text(pax_buffer, fgColor, NULL, 18, aPosX + 1, aPosY + 1, message);
pax_noclip(pax_buffer);
2022-04-15 23:16:36 +00:00
}
esp_err_t graphics_task(pax_buf_t* pax_buffer, ILI9341* ili9341, menu_t* menu, char* message) {
2022-04-15 23:16:36 +00:00
pax_background(pax_buffer, 0xCCCCCC);
if (menu != NULL) {
2022-05-30 21:56:23 +00:00
menu_render(pax_buffer, menu, 10, 10, 320-20, 240-20, 0xFF000000);
2022-04-15 23:16:36 +00:00
}
if (message != NULL) {
2022-04-18 00:10:44 +00:00
render_message(pax_buffer, message, 20, 110, 320-40, 20);
2022-04-15 23:16:36 +00:00
}
return ili9341_write(ili9341, pax_buffer->buf);
2022-04-15 23:16:36 +00:00
}
2022-04-18 00:10:44 +00:00
bool keyboard(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341, float aPosX, float aPosY, float aWidth, float aHeight, const char* aTitle, const char* aHint, char* aOutput, size_t aOutputSize) {
2022-06-02 11:10:56 +00:00
const pax_font_t *font = pax_get_font("saira regular");
2022-04-18 00:10:44 +00:00
bool accepted = false;
pkb_ctx_t kb_ctx;
2022-06-02 11:31:24 +00:00
pkb_init(pax_buffer, &kb_ctx, 32);
2022-06-02 11:10:56 +00:00
pkb_set_content(&kb_ctx, aOutput);
kb_ctx.kb_font = font;
kb_ctx.text_font = font;
2022-04-18 00:10:44 +00:00
pax_col_t fgColor = 0xFF000000;
pax_col_t bgColor = 0xFFFFFFFF;
pax_col_t shadowColor = 0xFFC0C3C8;
pax_col_t borderColor = 0xFF0000AA;
pax_col_t titleBgColor = 0xFF080764;
pax_col_t titleColor = 0xFFFFFFFF;
pax_col_t selColor = 0xff007fff;
kb_ctx.text_col = borderColor;
2022-06-02 11:10:56 +00:00
kb_ctx.sel_text_col = bgColor;
2022-04-18 00:10:44 +00:00
kb_ctx.sel_col = selColor;
kb_ctx.bg_col = bgColor;
kb_ctx.kb_font_size = 18;
float titleHeight = 20;
float hintHeight = 14;
pax_noclip(pax_buffer);
pax_simple_rect(pax_buffer, shadowColor, aPosX+5, aPosY+5, aWidth, aHeight);
pax_simple_rect(pax_buffer, bgColor, aPosX, aPosY, aWidth, aHeight);
pax_outline_rect(pax_buffer, borderColor, aPosX, aPosY, aWidth, aHeight);
pax_simple_rect(pax_buffer, titleBgColor, aPosX, aPosY, aWidth, titleHeight);
pax_simple_line(pax_buffer, titleColor, aPosX + 1, aPosY + titleHeight, aPosX + aWidth - 2, aPosY + titleHeight - 1);
pax_clip(pax_buffer, aPosX + 1, aPosY + 1, aWidth - 2, titleHeight - 2);
2022-06-02 11:10:56 +00:00
pax_draw_text(pax_buffer, titleColor, font, titleHeight - 2, aPosX + 1, aPosY + 1, aTitle);
pax_clip(pax_buffer, aPosX + 1, aPosY + aHeight - hintHeight, aWidth - 2, hintHeight);
2022-06-02 11:10:56 +00:00
pax_draw_text(pax_buffer, borderColor, font, hintHeight - 2, aPosX + 1, aPosY + aHeight - hintHeight, aHint);
pax_noclip(pax_buffer);
2022-04-18 00:10:44 +00:00
kb_ctx.x = aPosX + 1;
kb_ctx.y = aPosY + titleHeight + 1 ;
kb_ctx.width = aWidth - 2;
kb_ctx.height = aHeight - 3 - titleHeight - hintHeight;
bool running = true;
while (running) {
2022-04-23 02:00:55 +00:00
rp2040_input_message_t buttonMessage = {0};
2022-04-18 00:10:44 +00:00
if (xQueueReceive(buttonQueue, &buttonMessage, 16 / portTICK_PERIOD_MS) == pdTRUE) {
2022-04-23 02:00:55 +00:00
uint8_t pin = buttonMessage.input;
2022-04-18 00:10:44 +00:00
bool value = buttonMessage.state;
switch(pin) {
2022-04-23 02:00:55 +00:00
case RP2040_INPUT_JOYSTICK_DOWN:
2022-04-18 00:10:44 +00:00
if (value) {
pkb_press(&kb_ctx, PKB_DOWN);
} else {
pkb_release(&kb_ctx, PKB_DOWN);
}
break;
2022-04-23 02:00:55 +00:00
case RP2040_INPUT_JOYSTICK_UP:
2022-04-18 00:10:44 +00:00
if (value) {
pkb_press(&kb_ctx, PKB_UP);
} else {
pkb_release(&kb_ctx, PKB_UP);
}
break;
2022-04-23 02:00:55 +00:00
case RP2040_INPUT_JOYSTICK_LEFT:
2022-04-18 00:10:44 +00:00
if (value) {
pkb_press(&kb_ctx, PKB_LEFT);
} else {
pkb_release(&kb_ctx, PKB_LEFT);
}
break;
2022-04-23 02:00:55 +00:00
case RP2040_INPUT_JOYSTICK_RIGHT:
2022-04-18 00:10:44 +00:00
if (value) {
pkb_press(&kb_ctx, PKB_RIGHT);
} else {
pkb_release(&kb_ctx, PKB_RIGHT);
}
break;
2022-04-23 02:00:55 +00:00
case RP2040_INPUT_JOYSTICK_PRESS:
2022-04-18 00:10:44 +00:00
if (value) {
pkb_press(&kb_ctx, PKB_SHIFT);
} else {
pkb_release(&kb_ctx, PKB_SHIFT);
}
break;
2022-04-23 02:00:55 +00:00
case RP2040_INPUT_BUTTON_ACCEPT:
2022-04-18 00:10:44 +00:00
if (value) {
pkb_press(&kb_ctx, PKB_CHARSELECT);
} else {
pkb_release(&kb_ctx, PKB_CHARSELECT);
}
break;
2022-04-23 02:00:55 +00:00
case RP2040_INPUT_BUTTON_BACK:
2022-04-18 00:10:44 +00:00
if (value) {
pkb_press(&kb_ctx, PKB_DELETE_BEFORE);
} else {
pkb_release(&kb_ctx, PKB_DELETE_BEFORE);
}
break;
2022-04-23 02:00:55 +00:00
case RP2040_INPUT_BUTTON_SELECT:
2022-04-18 00:10:44 +00:00
if (value) {
pkb_press(&kb_ctx, PKB_MODESELECT);
} else {
pkb_release(&kb_ctx, PKB_MODESELECT);
}
break;
2022-04-23 02:00:55 +00:00
case RP2040_INPUT_BUTTON_HOME:
2022-04-18 00:10:44 +00:00
if (value) {
running = false;
}
break;
default:
break;
}
}
pkb_loop(&kb_ctx);
if (kb_ctx.dirty) {
pkb_redraw(pax_buffer, &kb_ctx);
ili9341_write(ili9341, pax_buffer->buf);
2022-04-18 00:10:44 +00:00
}
if (kb_ctx.input_accepted) {
memset(aOutput, 0, aOutputSize);
strncpy(aOutput, kb_ctx.content, aOutputSize - 1);
running = false;
accepted = true;
}
}
pkb_destroy(&kb_ctx);
return accepted;
}