mch2022-template-app/main/graphics_wrapper.c

158 lines
6 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
2022-04-18 00:10:44 +00:00
void render_message(pax_buf_t *aBuffer, 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(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);
}
esp_err_t graphics_task(pax_buf_t* pax_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);
}
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, framebuffer);
}
2022-04-18 00:10:44 +00:00
bool keyboard(xQueueHandle buttonQueue, pax_buf_t* aBuffer, ILI9341* ili9341, uint8_t* framebuffer, float aPosX, float aPosY, float aWidth, float aHeight, const char* aTitle, const char* aHint, char* aOutput, size_t aOutputSize) {
bool accepted = false;
pkb_ctx_t kb_ctx;
pkb_init(aBuffer, &kb_ctx, aOutput);
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;
kb_ctx.sel_text_col = selColor;
kb_ctx.sel_col = selColor;
kb_ctx.bg_col = bgColor;
kb_ctx.kb_font_size = 18;
float titleHeight = 20;
float hintHeight = 14;
pax_noclip(aBuffer);
pax_simple_rect(aBuffer, shadowColor, aPosX+5, aPosY+5, aWidth, aHeight);
pax_simple_rect(aBuffer, bgColor, aPosX, aPosY, aWidth, aHeight);
pax_outline_rect(aBuffer, borderColor, aPosX, aPosY, aWidth, aHeight);
pax_simple_rect(aBuffer, titleBgColor, aPosX, aPosY, aWidth, titleHeight);
pax_simple_line(aBuffer, titleColor, aPosX + 1, aPosY + titleHeight, aPosX + aWidth - 2, aPosY + titleHeight - 1);
pax_clip(aBuffer, aPosX + 1, aPosY + 1, aWidth - 2, titleHeight - 2);
pax_draw_text(aBuffer, titleColor, NULL, titleHeight - 2, aPosX + 1, aPosY + 1, aTitle);
pax_clip(aBuffer, aPosX + 1, aPosY + aHeight - hintHeight, aWidth - 2, hintHeight);
pax_draw_text(aBuffer, borderColor, NULL, hintHeight - 2, aPosX + 1, aPosY + aHeight - hintHeight, aHint);
pax_noclip(aBuffer);
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(aBuffer, &kb_ctx);
ili9341_write(ili9341, framebuffer);
}
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;
}