4#include "TinyGPU/Color/GammaTable.h"
6#include "TinyGPU/Input/TouchDriver.h"
7#include "TinyGPU/ThreeD/Vector.h"
11
12
13
14
15
16
17
18
19template <
typename RGB_T =
RGB565>
21 static_assert(
sizeof(RGB_T) == 2,
22 "LVGLDriver currently hardcodes LV_COLOR_FORMAT_RGB565 (a "
23 "16-bit format) for LVGL's own rendering, so RGB_T must also "
24 "be a 16-bit pixel type.");
27 LVGLDriver(DisplayDriver<RGB_T>& driver, size_t x, size_t y,
28 size_t bufferSize = 0) {
44 lv_tick_set_cb(my_tick);
49 disp = lv_display_create(disp_x, disp_y);
55 lv_display_set_color_format(disp, LV_COLOR_FORMAT_RGB565);
58 buf_1.resize(dispBufferSize);
61 lv_display_set_buffers(disp, buf_1.data(),
nullptr, dispBufferSize,
62 LV_DISPLAY_RENDER_MODE_PARTIAL);
68 lv_display_set_user_data(disp,
this);
69 lv_display_set_flush_cb(disp, &LVGLDriver::display);
81 if (!touch_driver->hasCalibration()) {
83 "LVGLDriver: touch driver has no calibration set. If it reports "
84 "raw ADC counts (e.g. a resistive XPT2046 panel) rather than "
85 "screen-pixel coordinates, touch input will be wrong until you "
86 "call touchDriver.setCalibration(...) before setTouchDriver().");
88 touch_driver->begin();
97 lv_display_delete(disp);
100 lv_tick_set_cb(
nullptr);
106 static void display(lv_display_t* disp,
const lv_area_t* area,
108 assert(disp !=
nullptr);
109 const size_t w =
static_cast<size_t>(area->x2 - area->x1 + 1);
110 const size_t h =
static_cast<size_t>(area->y2 - area->y1 + 1);
112 LVGLDriver* p_driver =
113 static_cast<LVGLDriver*>(lv_display_get_user_data(disp));
120 convertBuffer(px_map, w * h);
125 static Font5x7<RGB_T> font;
126 SurfaceWithExternalBuffer<RGB_T> surface(w, h, font);
127 surface.setExternalBuffer(px_map, w * h *
sizeof(RGB_T));
128 surface.resizeBuffer(w, h);
130 p_driver->getDriver().writeData(surface, area->x1, area->y1);
132 lv_display_flush_ready(disp);
176 static void setGamma(
float gammaR,
float gammaG,
float gammaB) {
177 gammaR_().setGamma(gammaR);
178 gammaG_().setGamma(gammaG);
179 gammaB_().setGamma(gammaB);
183 Vector<uint8_t> buf_1;
192 static uint32_t
my_tick(
void) {
return millis(); }
195 static GammaTable table;
199 static GammaTable table;
203 static GammaTable table;
215 const bool sameFormat = std::is_same<RGB_T, RGB565>::value;
216 const bool needsGamma = (
gammaR_().gamma() != 1.0f) ||
219 uint16_t* pixels =
reinterpret_cast<uint16_t*>(px_map);
220 if (sameFormat && !needsGamma) {
224 for (size_t i = 0; i < pixelCount; ++i) {
225 pixels[i] = RGB565::swapBytes(pixels[i]);
230 const GammaTable& gr = gammaR_();
231 const GammaTable& gg = gammaG_();
232 const GammaTable& gb = gammaB_();
233 for (size_t i = 0; i < pixelCount; ++i) {
236 const RGB565 src(RGB565::swapBytes(pixels[i]));
237 uint8_t r = src.getRed();
238 uint8_t g = src.getGreen();
239 uint8_t b = src.getBlue();
245 const RGB_T dst = RGB_T::fromRGB(r, g, b);
246 pixels[i] = dst.getValue();
251
252
254 if (!disp)
return nullptr;
256 lv_indev_t* indev = lv_indev_create();
257 if (!indev)
return nullptr;
259 lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
260 lv_indev_set_display(indev, disp);
261 lv_indev_set_user_data(indev, &touchDriver());
271 lv_indev_set_read_cb(indev, [](lv_indev_t* indev, lv_indev_data_t* data) {
273 static_cast<TouchDriver*>(lv_indev_get_user_data(indev));
275 if (touch && touch->isTouched() && touch->getPoint(p)) {
278 data->state = LV_INDEV_STATE_PRESSED;
280 data->state = LV_INDEV_STATE_RELEASED;
290 for (size_t i = 0; i < len; ++i) {
292 snprintf(lineHeader,
sizeof(lineHeader),
"\n%06u: ", (
unsigned)i);
293 Serial.print(lineHeader);
295 snprintf(byteStr,
sizeof(byteStr),
"%02X ", data[i]);
296 Serial.print(byteStr);
LVGLDriver is a helper class to initialize LVGL v9 with a TinyGPU Output driver. It sets up the displ...
Definition: LVGLDriver.h:20
static GammaTable & gammaG_()
Definition: LVGLDriver.h:198
TouchDriver * touch_driver
Definition: LVGLDriver.h:185
void dumpBuffer(const uint8_t *data, size_t len)
Definition: LVGLDriver.h:287
DisplayDriver< RGB_T > & getDriver()
Definition: LVGLDriver.h:158
size_t disp_y
Definition: LVGLDriver.h:188
DisplayDriver< RGB_T > * driver
Definition: LVGLDriver.h:184
static void convertBuffer(uint8_t *px_map, size_t pixelCount)
Definition: LVGLDriver.h:214
static void setGamma(float gammaR, float gammaG, float gammaB)
Definition: LVGLDriver.h:176
static GammaTable & gammaB_()
Definition: LVGLDriver.h:202
bool begin()
Definition: LVGLDriver.h:38
void setTouchDriver(TouchDriver &touch)
Definition: LVGLDriver.h:146
bool hasTouchDriver() const
Checks if a touch driver has been set for this LVGLDriver.
Definition: LVGLDriver.h:149
lv_display_t * disp
Definition: LVGLDriver.h:186
static GammaTable & gammaR_()
Definition: LVGLDriver.h:194
size_t dispBufferSize
Definition: LVGLDriver.h:189
void delay(uint32_t ms)
Definition: LVGLDriver.h:135
size_t disp_x
Definition: LVGLDriver.h:187
void end()
Definition: LVGLDriver.h:95
TouchDriver & touchDriver()
Definition: LVGLDriver.h:153
lv_indev_t * setupTouch()
Registers a touch driver with this LVGL display instance.
Definition: LVGLDriver.h:253
static void display(lv_display_t *disp, const lv_area_t *area, uint8_t *px_map)
Definition: LVGLDriver.h:106
static uint32_t my_tick(void)
Definition: LVGLDriver.h:192