26
26
#include < zephyr/drivers/display.h>
27
27
28
28
29
+ #if __has_include ("lvgl.h")
30
+ #include " lvgl.h"
31
+ #endif
32
+
33
+ /* Private function prototypes -----------------------------------------------*/
34
+ #if __has_include ("lvgl.h")
35
+ #if defined(__MBED__)
36
+ #include " mbed.h"
37
+ #endif
38
+ #if (LVGL_VERSION_MAJOR == 9)
39
+ void lvgl_displayFlushing (lv_display_t * display, const lv_area_t * area, unsigned char * px_map);
40
+ #if defined(__MBED__)
41
+ static void inc_thd () {
42
+ while (1 ) {
43
+ lv_tick_inc (16 );
44
+ delay (16 );
45
+ }
46
+ }
47
+ static rtos::Thread lvgl_inc_thd;
48
+ #endif
49
+ #else
50
+ void lvgl_displayFlushing (lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * color_p);
51
+ #endif
52
+
53
+
29
54
#ifdef HAS_ARDUINOGRAPHICS
30
55
Display::Display (int width, int height) : ArduinoGraphics(width, height), gdev(NULL )
31
56
#else
@@ -128,7 +153,9 @@ bool Display::begin(DisplayPixelFormat pixformat) {
128
153
_width = capabilities.x_resolution ;
129
154
#endif
130
155
131
- #ifdef HAS_ARDUINOGRAPHICS
156
+ #if defined( HAS_ARDUINOGRAPHICS) || __has_include ("lvgl.h")
157
+ printk (" setting up buffer....\n " );
158
+ setBlanking (false );
132
159
#ifdef CONFIG_SHARED_MULTI_HEAP
133
160
void * ptrFB = getFrameBuffer ();
134
161
if (ptrFB == nullptr ){
@@ -142,21 +169,85 @@ bool Display::begin(DisplayPixelFormat pixformat) {
142
169
SDRAM.begin ();
143
170
buffer = (uint16_t *)SDRAM.malloc (this ->width () * this -> height () * sizeof (uint16_t ));
144
171
#endif
172
+ setBlanking (true );
173
+
145
174
sizeof_framebuffer = width () * height () * sizeof (uint16_t );
146
175
setFrameDesc (width (), height (), width (), sizeof_framebuffer);
147
176
Serial.print (" Buffer: 0x" ); Serial.println ((uint32_t )buffer, HEX);
148
177
149
178
// turn on the display backlight
150
179
pinMode (74 , OUTPUT);
151
180
digitalWrite (74 , HIGH);
152
-
181
+ #endif
182
+
183
+ #ifdef HAS_ARDUINOGRAPHICS
153
184
if (!ArduinoGraphics::begin ()) {
154
185
return 1 ; /* Unknown err */
155
186
}
156
187
157
188
textFont (Font_5x7);
158
189
#endif // arduinoGraphics
159
190
191
+
192
+ #if __has_include("lvgl.h")
193
+ printk (" initializing LVGL!!!!\n " );
194
+ /* Initiliaze LVGL library */
195
+ lv_init ();
196
+
197
+
198
+ #if (LVGL_VERSION_MAJOR == 9)
199
+ /* Create a draw buffer */
200
+ static lv_color_t * buf1 = (lv_color_t *)malloc ((width () * height () / 10 )); /* Declare a buffer for 1/10 screen size */
201
+ if (buf1 == NULL ) {
202
+ return 2 ; /* Insuff memory err */
203
+ }
204
+
205
+ lv_display_t *display;
206
+ if (_rotated) {
207
+ display = lv_display_create (height (), width ());
208
+ lv_display_set_rotation (display, LV_DISPLAY_ROTATION_270);
209
+ // display->sw_rotate = 1;
210
+ } else {
211
+ display = lv_display_create (width (), height ());
212
+ }
213
+ lv_display_set_buffers (display, buf1, NULL , width () * height () / 10 , LV_DISPLAY_RENDER_MODE_PARTIAL); /* Initialize the display buffer.*/
214
+ lv_display_set_flush_cb (display, lvgl_displayFlushing);
215
+
216
+ lvgl_inc_thd.start (inc_thd);
217
+
218
+ printk (" LVGL Initialized\n " );
219
+
220
+ #else // LVGL_VERSION_MAJOR
221
+
222
+ /* Create a draw buffer */
223
+ static lv_disp_draw_buf_t draw_buf;
224
+ static lv_color_t * buf1;
225
+ buf1 = (lv_color_t *)malloc ((width () * height () / 10 ) * sizeof (lv_color_t )); /* Declare a buffer for 1/10 screen size */
226
+ if (buf1 == NULL ) {
227
+ return 2 ; /* Insuff memory err */
228
+ }
229
+ lv_disp_draw_buf_init (&draw_buf, buf1, NULL , width () * height () / 10 ); /* Initialize the display buffer. */
230
+
231
+ /* Initialize display features for LVGL library */
232
+ static lv_disp_drv_t disp_drv; /* Descriptor of a display driver */
233
+ lv_disp_drv_init (&disp_drv); /* Basic initialization */
234
+ disp_drv.flush_cb = lvgl_displayFlushing; /* Set your driver function */
235
+ disp_drv.draw_buf = &draw_buf; /* Assign the buffer to the display */
236
+ if (_rotated) {
237
+ disp_drv.hor_res = height (); /* Set the horizontal resolution of the display */
238
+ disp_drv.ver_res = width (); /* Set the vertical resolution of the display */
239
+ disp_drv.rotated = LV_DISP_ROT_270;
240
+ } else {
241
+ disp_drv.hor_res = width (); /* Set the horizontal resolution of the display */
242
+ disp_drv.ver_res = height (); /* Set the vertical resolution of the display */
243
+ disp_drv.rotated = LV_DISP_ROT_NONE;
244
+ }
245
+ disp_drv.sw_rotate = 1 ;
246
+ lv_disp_drv_register (&disp_drv); /* Finally register the driver */
247
+ #endif
248
+ #endif
249
+
250
+
160
251
return true ;
161
252
}
162
253
@@ -284,4 +375,66 @@ void Display::lcdClear(uint16_t Color) {
284
375
}
285
376
#endif // HAS_ARDUINOGRAPHICS
286
377
287
- #endif // __ZEPHYR__
378
+ #if __has_include("lvgl.h")
379
+ #if (LVGL_VERSION_MAJOR == 9)
380
+ static uint8_t * rotated_buf = nullptr ;
381
+ void lvgl_displayFlushing (lv_display_t * disp, const lv_area_t * area, unsigned char * px_map) {
382
+ uint32_t w = lv_area_get_width (area);
383
+ uint32_t h = lv_area_get_height (area);
384
+ lv_area_t * area_in_use = (lv_area_t *)area;
385
+
386
+ // TODO: find a smart way to tackle sw rotation
387
+ lv_display_rotation_t rotation = lv_display_get_rotation (disp);
388
+ lv_area_t rotated_area;
389
+ if (rotation != LV_DISPLAY_ROTATION_0) {
390
+ rotated_buf = (uint8_t *)realloc (rotated_buf, w * h * 4 );
391
+ lv_color_format_t cf = lv_display_get_color_format (disp);
392
+ #if (LVGL_VERSION_MINOR < 2)
393
+ rotation = LV_DISPLAY_ROTATION_90; // bugfix: force 90 degree rotation for lvgl 9.1 end earlier
394
+ #endif
395
+ lv_draw_sw_rotate (px_map, rotated_buf,
396
+ w, h, lv_draw_buf_width_to_stride (w, cf),
397
+ lv_draw_buf_width_to_stride (h, cf),
398
+ rotation, cf);
399
+
400
+ rotated_area.x1 = lv_display_get_vertical_resolution (disp) - area->y2 - 1 ;
401
+ rotated_area.y1 = area->x1 ;
402
+ // rotated_area.y2 = dsi_getDisplayYSize() - area->x1 - 1;
403
+ rotated_area.x2 = rotated_area.x1 + h - 1 ;
404
+ rotated_area.y2 = rotated_area.y1 + w + 1 ;
405
+
406
+ area_in_use = &rotated_area;
407
+ px_map = rotated_buf;
408
+ auto temp = w;
409
+ w = h;
410
+ h = temp;
411
+ }
412
+
413
+ #if defined(__MBED__)
414
+ uint32_t offsetPos = (area_in_use->x1 + (dsi_getDisplayXSize () * area_in_use->y1 )) * sizeof (uint16_t );
415
+ dsi_lcdDrawImage ((void *) px_map, (void *)(dsi_getActiveFrameBuffer () + offsetPos), w, h, DMA2D_INPUT_RGB565);
416
+ #else
417
+ uint32_t offsetPos = (area_in_use->x1 + (getDisplayXSize () * area_in_use->y1 )) * sizeof (uint16_t );
418
+ // dsi_lcdDrawImage((void *) px_map, (void *)(dsi_getActiveFrameBuffer() + offsetPos), w, h, DMA2D_INPUT_RGB565);
419
+ memcpy (px_map, buffer + offest, w * h);
420
+ write8 (0 , 0 , buffer);
421
+ #endif
422
+ lv_display_flush_ready (disp); /* Indicate you are ready with the flushing*/
423
+ }
424
+ #else
425
+ void lvgl_displayFlushing (lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * color_p) {
426
+ uint32_t width = lv_area_get_width (area);
427
+ uint32_t height = lv_area_get_height (area);
428
+ uint32_t offsetPos = (area->x1 + (dsi_getDisplayXSize () * area->y1 )) * sizeof (uint16_t );
429
+ #if defined(__MBED__)
430
+ dsi_lcdDrawImage ((void *) color_p, (void *)(dsi_getActiveFrameBuffer () + offsetPos), width, height, DMA2D_INPUT_RGB565);
431
+ #else
432
+ memcpy (color_p, buffer + offest, w * h);
433
+ write8 (0 , 0 , buffer);
434
+ #endif
435
+ lv_disp_flush_ready (disp); /* Indicate you are ready with the flushing*/
436
+ }
437
+ #endif
438
+ #endif
439
+
440
+ #endif // __ZEPHYR__
0 commit comments