CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
host_api_display.cpp
Go to the documentation of this file.
1
9
12#include "cdc_hal/IDisplay.h"
14#include "host_str_conv.h"
15#include <goodisplay/gdey029T94.h>
16#include <string>
17
18extern "C" void* plg_get_active_plugin(void);
19
20namespace {
21
24}
25
26bool allowed() {
27 auto* p = active();
28 return p && p->manifest().capabilities.display_lowlevel;
29}
30
32
33} // namespace
34
35extern "C" {
36
37uint16_t host_display_width(void)
38{
39 auto* d = disp();
40 return (allowed() && d) ? d->getWidth() : 0;
41}
42
43uint16_t host_display_height(void)
44{
45 auto* d = disp();
46 return (allowed() && d) ? d->getHeight() : 0;
47}
48
50{
51 if (!allowed()) return HOST_ERR_NO_CAPABILITY;
52 auto* d = disp();
53 if (!d) return HOST_ERR_GENERIC;
54 d->clear();
55 return HOST_OK;
56}
57
58int host_display_draw_pixel(int16_t x, int16_t y, uint16_t color)
59{
60 if (!allowed()) return HOST_ERR_NO_CAPABILITY;
61 auto* d = disp();
62 if (!d) return HOST_ERR_GENERIC;
63 d->drawPixel(x, y, color);
64 return HOST_OK;
65}
66
67int host_display_draw_line(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
68{
69 if (!allowed()) return HOST_ERR_NO_CAPABILITY;
70 auto* d = disp();
71 if (!d) return HOST_ERR_GENERIC;
72 d->drawLine(x0, y0, x1, y1, color);
73 return HOST_OK;
74}
75
76int host_display_draw_rect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
77{
78 if (!allowed()) return HOST_ERR_NO_CAPABILITY;
79 auto* d = disp();
80 if (!d) return HOST_ERR_GENERIC;
81 d->drawRect(x, y, w, h, color);
82 return HOST_OK;
83}
84
85int host_display_fill_rect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
86{
87 if (!allowed()) return HOST_ERR_NO_CAPABILITY;
88 auto* d = disp();
89 if (!d) return HOST_ERR_GENERIC;
90 d->fillRect(x, y, w, h, color);
91 return HOST_OK;
92}
93
94int host_display_draw_text(int16_t x, int16_t y, const char* text, uint8_t size, uint16_t color)
95{
96 if (!allowed()) return HOST_ERR_NO_CAPABILITY;
97 if (!text) return HOST_ERR_INVALID_ARG;
98 auto* d = disp();
99 if (!d) return HOST_ERR_GENERIC;
100 auto* gfx = static_cast<Gdey029T94*>(d->getNativeHandle());
101 if (!gfx) return HOST_ERR_GENERIC;
102 d->setTextSize(size ? size : 1);
103 d->setTextColor(color);
104 d->setCursor(x, y);
106 return HOST_OK;
107}
108
109// refresh_mode: 0 = FULL, 1 = PARTIAL, 2 = FAST (single-flash waveform).
110// Plugins choose per flush; explicit FULL/FAST also reset the HAL ghost
111// escalation counters, so high-churn apps (e.g. games) can keep their own
112// panel hygiene. Unknown values map to FULL for backward compatibility.
113int host_display_flush(uint8_t refresh_mode)
114{
115 if (!allowed()) return HOST_ERR_NO_CAPABILITY;
116 auto* d = disp();
117 if (!d) return HOST_ERR_GENERIC;
119 if (refresh_mode == 1) mode = cdc::hal::RefreshMode::PARTIAL;
120 else if (refresh_mode == 2) mode = cdc::hal::RefreshMode::FAST;
121 d->flush(mode);
122 return HOST_OK;
123}
124
126{
127 auto* d = disp();
128 return (allowed() && d) ? d->isBusy() : false;
129}
130
131// Backlight control is not framebuffer access, so - like host_ui_wink - it is
132// NOT gated on the display_lowlevel capability. The value is applied live and
133// is NOT persisted to NVS (the firmware persists only via saveBacklight()).
134int host_display_set_backlight(uint16_t level)
135{
136 auto* d = disp();
137 if (!d) return HOST_ERR_GENERIC;
138 d->setBacklight(level); // clamped to the panel maximum inside the HAL
139 return HOST_OK;
140}
141
143{
144 auto* d = disp();
145 return d ? d->getBacklight() : 0;
146}
147
148} // extern "C"
Owned WAMR module instance + per-plugin state.
int host_display_draw_rect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
Draw a rectangle outline.
bool host_display_is_busy(void)
True while the panel is processing a previous refresh.
int host_display_draw_pixel(int16_t x, int16_t y, uint16_t color)
Set a single pixel.
uint16_t host_display_get_backlight(void)
Current backlight level (0 when no display is available).
int host_display_draw_line(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
Draw a line between two points.
uint16_t host_display_width(void)
Display width in pixels.
int host_display_set_backlight(uint16_t level)
Set the display backlight level (0 = off .. panel maximum, e.g. 1023). Applied live; NOT persisted to...
int host_display_flush(uint8_t refresh_mode)
Push the framebuffer to the panel using the given refresh mode.
int host_display_fill_rect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
Draw a filled rectangle.
int host_display_draw_text(int16_t x, int16_t y, const char *text, uint8_t size, uint16_t color)
Draw text using the default GFX font.
uint16_t host_display_height(void)
Display height in pixels.
int host_display_clear(void)
Clear the framebuffer to background.
CDC Badge OS plugin host API - canonical C ABI contract.
#define HOST_ERR_NO_CAPABILITY
Definition host_api.h:40
#define HOST_OK
Definition host_api.h:37
#define HOST_ERR_INVALID_ARG
Definition host_api.h:39
#define HOST_ERR_GENERIC
Definition host_api.h:38
void * plg_get_active_plugin(void)
Internal UTF-8 <-> CP437 helpers for the plugin host API boundary.
IDisplay * getDisplayInstance()
Returns lazily created singleton display instance.
std::string toDisplay(const char *utf8)
Decode a UTF-8 (with optional HTML entities) string into CP437 bytes.
void printText(Adafruit_GFX *gfx, const char *text)
Draws CP437 text with the built-in 6x8 glyph font, byte-for-byte.