1
2 /**
3 * @file hal_disp.c
4 *
5 * @description HAL layer for display driver
6 *
7 */
8
9 /*********************
10 * INCLUDES
11 *********************/
12 #include <stdint.h>
13 #include <stddef.h>
14 #include "lv_hal.h"
15 #include "../lv_misc/lv_mem.h"
16 #include "../lv_core/lv_obj.h"
17 #include "../lv_core/lv_refr.h"
18 #include "../lv_misc/lv_gc.h"
19
20 #if defined(LV_GC_INCLUDE)
21 #include LV_GC_INCLUDE
22 #endif /* LV_ENABLE_GC */
23
24 /*********************
25 * DEFINES
26 *********************/
27
28 /**********************
29 * TYPEDEFS
30 **********************/
31
32 /**********************
33 * STATIC PROTOTYPES
34 **********************/
35
36 /**********************
37 * STATIC VARIABLES
38 **********************/
39 static lv_disp_t * disp_def;
40
41 /**********************
42 * MACROS
43 **********************/
44
45 /**********************
46 * GLOBAL FUNCTIONS
47 **********************/
48
49 /**
50 * Initialize a display driver with default values.
51 * It is used to surly have known values in the fields ant not memory junk.
52 * After it you can set the fields.
53 * @param driver pointer to driver variable to initialize
54 */
lv_disp_drv_init(lv_disp_drv_t * driver)55 void lv_disp_drv_init(lv_disp_drv_t * driver)
56 {
57 memset(driver, 0, sizeof(lv_disp_drv_t));
58
59 driver->flush_cb = NULL;
60 driver->hor_res = LV_HOR_RES_MAX;
61 driver->ver_res = LV_VER_RES_MAX;
62 driver->buffer = NULL;
63 driver->rotated = 0;
64 driver->color_chroma_key = LV_COLOR_TRANSP;
65
66 #if LV_ANTIALIAS
67 driver->antialiasing = true;
68 #endif
69
70 #if LV_COLOR_SCREEN_TRANSP
71 driver->screen_transp = 1;
72 #endif
73
74 #if LV_USE_GPU
75 driver->gpu_blend_cb = NULL;
76 driver->gpu_fill_cb = NULL;
77 #endif
78
79 #if LV_USE_USER_DATA
80 driver->user_data = NULL;
81 #endif
82
83 driver->set_px_cb = NULL;
84 }
85
86 /**
87 * Initialize a display buffer
88 * @param disp_buf pointer `lv_disp_buf_t` variable to initialize
89 * @param buf1 A buffer to be used by LittlevGL to draw the image.
90 * Always has to specified and can't be NULL.
91 * Can be an array allocated by the user. E.g. `static lv_color_t disp_buf1[1024 * 10]`
92 * Or a memory address e.g. in external SRAM
93 * @param buf2 Optionally specify a second buffer to make image rendering and image flushing
94 * (sending to the display) parallel.
95 * In the `disp_drv->flush` you should use DMA or similar hardware to send
96 * the image to the display in the background.
97 * It lets LittlevGL to render next frame into the other buffer while previous is being
98 * sent. Set to `NULL` if unused.
99 * @param size_in_px_cnt size of the `buf1` and `buf2` in pixel count.
100 */
lv_disp_buf_init(lv_disp_buf_t * disp_buf,void * buf1,void * buf2,uint32_t size_in_px_cnt)101 void lv_disp_buf_init(lv_disp_buf_t * disp_buf, void * buf1, void * buf2, uint32_t size_in_px_cnt)
102 {
103 memset(disp_buf, 0, sizeof(lv_disp_buf_t));
104
105 disp_buf->buf1 = buf1;
106 disp_buf->buf2 = buf2;
107 disp_buf->buf_act = disp_buf->buf1;
108 disp_buf->size = size_in_px_cnt;
109 }
110
111 /**
112 * Register an initialized display driver.
113 * Automatically set the first display as active.
114 * @param driver pointer to an initialized 'lv_disp_drv_t' variable (can be local variable)
115 * @return pointer to the new display or NULL on error
116 */
lv_disp_drv_register(lv_disp_drv_t * driver)117 lv_disp_t * lv_disp_drv_register(lv_disp_drv_t * driver)
118 {
119 lv_disp_t * disp = lv_ll_ins_head(&LV_GC_ROOT(_lv_disp_ll));
120 if(!disp) {
121 lv_mem_assert(disp);
122 return NULL;
123 }
124
125 memcpy(&disp->driver, driver, sizeof(lv_disp_drv_t));
126 memset(&disp->inv_area_joined, 0, sizeof(disp->inv_area_joined));
127 memset(&disp->inv_areas, 0, sizeof(disp->inv_areas));
128 lv_ll_init(&disp->scr_ll, sizeof(lv_obj_t));
129
130 if(disp_def == NULL) disp_def = disp;
131
132 lv_disp_t * disp_def_tmp = disp_def;
133 disp_def = disp; /*Temporarily change the default screen to create the default screens on the
134 new display*/
135
136 disp->inv_p = 0;
137
138 disp->act_scr = lv_obj_create(NULL, NULL); /*Create a default screen on the display*/
139 disp->top_layer = lv_obj_create(NULL, NULL); /*Create top layer on the display*/
140 disp->sys_layer = lv_obj_create(NULL, NULL); /*Create top layer on the display*/
141 lv_obj_set_style(disp->top_layer, &lv_style_transp);
142 lv_obj_set_style(disp->sys_layer, &lv_style_transp);
143
144 lv_obj_invalidate(disp->act_scr);
145
146 disp_def = disp_def_tmp; /*Revert the default display*/
147
148 /*Create a refresh task*/
149 disp->refr_task = lv_task_create(lv_disp_refr_task, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, disp);
150 lv_mem_assert(disp->refr_task);
151 if(disp->refr_task == NULL) return NULL;
152
153 lv_task_ready(disp->refr_task); /*Be sure the screen will be refreshed immediately on start up*/
154
155 return disp;
156 }
157
158 /**
159 * Update the driver in run time.
160 * @param disp pointer to a display. (return value of `lv_disp_drv_register`)
161 * @param new_drv pointer to the new driver
162 */
lv_disp_drv_update(lv_disp_t * disp,lv_disp_drv_t * new_drv)163 void lv_disp_drv_update(lv_disp_t * disp, lv_disp_drv_t * new_drv)
164 {
165 memcpy(&disp->driver, new_drv, sizeof(lv_disp_drv_t));
166
167 lv_obj_t * scr;
168 LV_LL_READ(disp->scr_ll, scr)
169 {
170 lv_obj_set_size(scr, lv_disp_get_hor_res(disp), lv_disp_get_ver_res(disp));
171 }
172 }
173
174 /**
175 * Remove a display
176 * @param disp pointer to display
177 */
lv_disp_remove(lv_disp_t * disp)178 void lv_disp_remove(lv_disp_t * disp)
179 {
180 bool was_default = false;
181 if(disp == lv_disp_get_default()) was_default = true;
182
183 /*Detach the input devices */
184 lv_indev_t * indev;
185 indev = lv_indev_get_next(NULL);
186 while(indev) {
187 if(indev->driver.disp == disp) {
188 indev->driver.disp = NULL;
189 }
190 indev = lv_indev_get_next(indev);
191 }
192
193 lv_ll_rem(&LV_GC_ROOT(_lv_disp_ll), disp);
194 lv_mem_free(disp);
195
196 if(was_default) lv_disp_set_default(lv_ll_get_head(&LV_GC_ROOT(_lv_disp_ll)));
197 }
198
199 /**
200 * Set a default screen. The new screens will be created on it by default.
201 * @param disp pointer to a display
202 */
lv_disp_set_default(lv_disp_t * disp)203 void lv_disp_set_default(lv_disp_t * disp)
204 {
205 disp_def = disp;
206 }
207
208 /**
209 * Get the default display
210 * @return pointer to the default display
211 */
lv_disp_get_default(void)212 lv_disp_t * lv_disp_get_default(void)
213 {
214 return disp_def;
215 }
216
217 /**
218 * Get the horizontal resolution of a display
219 * @param disp pointer to a display (NULL to use the default display)
220 * @return the horizontal resolution of the display
221 */
lv_disp_get_hor_res(lv_disp_t * disp)222 lv_coord_t lv_disp_get_hor_res(lv_disp_t * disp)
223 {
224 if(disp == NULL) disp = lv_disp_get_default();
225
226 if(disp == NULL)
227 return LV_HOR_RES_MAX;
228 else
229 return disp->driver.rotated == 0 ? disp->driver.hor_res : disp->driver.ver_res;
230 }
231
232 /**
233 * Get the vertical resolution of a display
234 * @param disp pointer to a display (NULL to use the default display)
235 * @return the vertical resolution of the display
236 */
lv_disp_get_ver_res(lv_disp_t * disp)237 lv_coord_t lv_disp_get_ver_res(lv_disp_t * disp)
238 {
239 if(disp == NULL) disp = lv_disp_get_default();
240
241 if(disp == NULL)
242 return LV_VER_RES_MAX;
243 else
244 return disp->driver.rotated == 0 ? disp->driver.ver_res : disp->driver.hor_res;
245 }
246
247 /**
248 * Get if anti-aliasing is enabled for a display or not
249 * @param disp pointer to a display (NULL to use the default display)
250 * @return true: anti-aliasing is enabled; false: disabled
251 */
lv_disp_get_antialiasing(lv_disp_t * disp)252 bool lv_disp_get_antialiasing(lv_disp_t * disp)
253 {
254 #if LV_ANTIALIAS == 0
255 return false;
256 #else
257 if(disp == NULL) disp = lv_disp_get_default();
258 if(disp == NULL) return false;
259
260 return disp->driver.antialiasing ? true : false;
261 #endif
262 }
263
264 /**
265 * Call in the display driver's `flush_cb` function when the flushing is finished
266 * @param disp_drv pointer to display driver in `flush_cb` where this function is called
267 */
lv_disp_flush_ready(lv_disp_drv_t * disp_drv)268 LV_ATTRIBUTE_FLUSH_READY void lv_disp_flush_ready(lv_disp_drv_t * disp_drv)
269 {
270 disp_drv->buffer->flushing = 0;
271
272 /*If the screen is transparent initialize it when the flushing is ready*/
273 #if LV_COLOR_SCREEN_TRANSP
274 if(disp_drv->screen_transp) {
275 memset(disp_drv->buffer->buf_act, 0x00, disp_drv->buffer->size * sizeof(lv_color32_t));
276 }
277 #endif
278 }
279
280 /**
281 * Get the next display.
282 * @param disp pointer to the current display. NULL to initialize.
283 * @return the next display or NULL if no more. Give the first display when the parameter is NULL
284 */
lv_disp_get_next(lv_disp_t * disp)285 lv_disp_t * lv_disp_get_next(lv_disp_t * disp)
286 {
287 if(disp == NULL)
288 return lv_ll_get_head(&LV_GC_ROOT(_lv_disp_ll));
289 else
290 return lv_ll_get_next(&LV_GC_ROOT(_lv_disp_ll), disp);
291 }
292
293 /**
294 * Get the internal buffer of a display
295 * @param disp pointer to a display
296 * @return pointer to the internal buffers
297 */
lv_disp_get_buf(lv_disp_t * disp)298 lv_disp_buf_t * lv_disp_get_buf(lv_disp_t * disp)
299 {
300 return disp->driver.buffer;
301 }
302
303 /**
304 * Get the number of areas in the buffer
305 * @return number of invalid areas
306 */
lv_disp_get_inv_buf_size(lv_disp_t * disp)307 uint16_t lv_disp_get_inv_buf_size(lv_disp_t * disp)
308 {
309 return disp->inv_p;
310 }
311
312 /**
313 * Pop (delete) the last 'num' invalidated areas from the buffer
314 * @param num number of areas to delete
315 */
lv_disp_pop_from_inv_buf(lv_disp_t * disp,uint16_t num)316 void lv_disp_pop_from_inv_buf(lv_disp_t * disp, uint16_t num)
317 {
318
319 if(disp->inv_p < num)
320 disp->inv_p = 0;
321 else
322 disp->inv_p -= num;
323 }
324
325 /**
326 * Check the driver configuration if it's double buffered (both `buf1` and `buf2` are set)
327 * @param disp pointer to to display to check
328 * @return true: double buffered; false: not double buffered
329 */
lv_disp_is_double_buf(lv_disp_t * disp)330 bool lv_disp_is_double_buf(lv_disp_t * disp)
331 {
332 if(disp->driver.buffer->buf1 && disp->driver.buffer->buf2)
333 return true;
334 else
335 return false;
336 }
337
338 /**
339 * Check the driver configuration if it's TRUE double buffered (both `buf1` and `buf2` are set and
340 * `size` is screen sized)
341 * @param disp pointer to to display to check
342 * @return true: double buffered; false: not double buffered
343 */
lv_disp_is_true_double_buf(lv_disp_t * disp)344 bool lv_disp_is_true_double_buf(lv_disp_t * disp)
345 {
346 uint32_t scr_size = disp->driver.hor_res * disp->driver.ver_res;
347
348 if(lv_disp_is_double_buf(disp) && disp->driver.buffer->size == scr_size) {
349 return true;
350 } else {
351 return false;
352 }
353 }
354
355 /**********************
356 * STATIC FUNCTIONS
357 **********************/
358