1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-12-17     Wayne        The first version
9  */
10 #include <lvgl.h>
11 
12 #define LOG_TAG             "lvgl.disp"
13 #define DBG_ENABLE
14 #define DBG_SECTION_NAME   LOG_TAG
15 #define DBG_LEVEL DBG_ERROR
16 #define DBG_COLOR
17 #include <rtdbg.h>
18 
19 #if !defined(NU_PKG_LVGL_RENDERING_LAYER)
20     #define NU_PKG_LVGL_RENDERING_LAYER "lcd"
21 #endif
22 
23 /*A static or global variable to store the buffers*/
24 static lv_disp_draw_buf_t disp_buf;
25 static lv_disp_drv_t disp_drv;  /*Descriptor of a display driver*/
26 
27 static rt_device_t lcd_device = 0;
28 static struct rt_device_graphic_info info;
29 
lcd_fb_flush(lv_disp_drv_t * disp_drv,const lv_area_t * area,lv_color_t * color_p)30 static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
31 {
32     /* Rendering */
33     struct rt_device_rect_info rect;
34 
35     rect.x = area->x1;
36     rect.y = area->y1;
37     rect.width = area->x2 - area->x1 + 1;
38     rect.height = area->y2 - area->y1 + 1;
39 
40     rt_device_control(lcd_device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect);
41     lv_disp_flush_ready(disp_drv);
42 }
43 
lcd_perf_monitor(struct _lv_disp_drv_t * disp_drv,uint32_t time,uint32_t px)44 void lcd_perf_monitor(struct _lv_disp_drv_t *disp_drv, uint32_t time, uint32_t px)
45 {
46     rt_kprintf("Elapsed: %dms, Pixel: %d, Bytes:%d\n", time, px, px * sizeof(lv_color_t));
47 }
48 
lv_port_disp_init(void)49 void lv_port_disp_init(void)
50 {
51     rt_err_t result;
52     void *buf1 = RT_NULL;
53 
54     lcd_device = rt_device_find(NU_PKG_LVGL_RENDERING_LAYER);
55     if (lcd_device == 0)
56     {
57         LOG_E("error!");
58         return;
59     }
60 
61     /* get framebuffer address */
62     result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
63     if (result != RT_EOK && info.framebuffer == RT_NULL)
64     {
65         LOG_E("error!");
66         /* get device information failed */
67         return;
68     }
69 
70     RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
71               info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
72 
73     buf1 = (void *)info.framebuffer;
74     rt_kprintf("LVGL: Use one buffers - buf1@%08x, size: %d bytes\n", buf1, info.smem_len);
75 
76     /*Initialize `disp_buf` with the buffer(s).*/
77     lv_disp_draw_buf_init(&disp_buf, buf1, RT_NULL, info.smem_len / (info.bits_per_pixel / 8));
78 
79     result = rt_device_open(lcd_device, 0);
80     if (result != RT_EOK)
81     {
82         LOG_E("error!");
83         return;
84     }
85 
86     lv_disp_drv_init(&disp_drv); /*Basic initialization*/
87 
88     /*Set the resolution of the display*/
89     disp_drv.hor_res = info.width;
90     disp_drv.ver_res = info.height;
91 
92     /*Set a display buffer*/
93     disp_drv.draw_buf = &disp_buf;
94 
95     /*Write the internal buffer (draw_buf) to the display*/
96     disp_drv.flush_cb = lcd_fb_flush;
97 
98     /* Called after every refresh cycle to tell the rendering and flushing time + the number of flushed pixels */
99     //disp_drv.monitor_cb = lcd_perf_monitor;
100 
101     /*Finally register the driver*/
102     lv_disp_drv_register(&disp_drv);
103 }
104