1 /*
2  * Copyright (c) 2019 Jan Van Winkel <jan.van_winkel@dxplore.eu>
3  *
4  * Based on ST7789V sample:
5  * Copyright (c) 2019 Marc Reilly
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <zephyr/logging/log.h>
11 LOG_MODULE_REGISTER(sample, LOG_LEVEL_INF);
12 
13 #include <zephyr/kernel.h>
14 #include <zephyr/device.h>
15 #include <zephyr/drivers/display.h>
16 
17 #ifdef CONFIG_ARCH_POSIX
18 #include "posix_board_if.h"
19 #endif
20 
21 enum corner {
22 	TOP_LEFT,
23 	TOP_RIGHT,
24 	BOTTOM_RIGHT,
25 	BOTTOM_LEFT
26 };
27 
28 typedef void (*fill_buffer)(enum corner corner, uint8_t grey, uint8_t *buf,
29 			    size_t buf_size);
30 
31 
32 #ifdef CONFIG_ARCH_POSIX
posix_exit_main(int exit_code)33 static void posix_exit_main(int exit_code)
34 {
35 #if CONFIG_TEST
36 	if (exit_code == 0) {
37 		LOG_INF("PROJECT EXECUTION SUCCESSFUL");
38 	} else {
39 		LOG_INF("PROJECT EXECUTION FAILED");
40 	}
41 #endif
42 	posix_exit(exit_code);
43 }
44 #endif
45 
fill_buffer_argb8888(enum corner corner,uint8_t grey,uint8_t * buf,size_t buf_size)46 static void fill_buffer_argb8888(enum corner corner, uint8_t grey, uint8_t *buf,
47 				 size_t buf_size)
48 {
49 	uint32_t color = 0;
50 
51 	switch (corner) {
52 	case TOP_LEFT:
53 		color = 0xFFFF0000u;
54 		break;
55 	case TOP_RIGHT:
56 		color = 0xFF00FF00u;
57 		break;
58 	case BOTTOM_RIGHT:
59 		color = 0xFF0000FFu;
60 		break;
61 	case BOTTOM_LEFT:
62 		color = 0xFF000000u | grey << 16 | grey << 8 | grey;
63 		break;
64 	}
65 
66 	for (size_t idx = 0; idx < buf_size; idx += 4) {
67 		*((uint32_t *)(buf + idx)) = color;
68 	}
69 }
70 
fill_buffer_rgb888(enum corner corner,uint8_t grey,uint8_t * buf,size_t buf_size)71 static void fill_buffer_rgb888(enum corner corner, uint8_t grey, uint8_t *buf,
72 			       size_t buf_size)
73 {
74 	uint32_t color = 0;
75 
76 	switch (corner) {
77 	case TOP_LEFT:
78 		color = 0x00FF0000u;
79 		break;
80 	case TOP_RIGHT:
81 		color = 0x0000FF00u;
82 		break;
83 	case BOTTOM_RIGHT:
84 		color = 0x000000FFu;
85 		break;
86 	case BOTTOM_LEFT:
87 		color = grey << 16 | grey << 8 | grey;
88 		break;
89 	}
90 
91 	for (size_t idx = 0; idx < buf_size; idx += 3) {
92 		*(buf + idx + 0) = color >> 16;
93 		*(buf + idx + 1) = color >> 8;
94 		*(buf + idx + 2) = color >> 0;
95 	}
96 }
97 
get_rgb565_color(enum corner corner,uint8_t grey)98 static uint16_t get_rgb565_color(enum corner corner, uint8_t grey)
99 {
100 	uint16_t color = 0;
101 	uint16_t grey_5bit;
102 
103 	switch (corner) {
104 	case TOP_LEFT:
105 		color = 0xF800u;
106 		break;
107 	case TOP_RIGHT:
108 		color = 0x07E0u;
109 		break;
110 	case BOTTOM_RIGHT:
111 		color = 0x001Fu;
112 		break;
113 	case BOTTOM_LEFT:
114 		grey_5bit = grey & 0x1Fu;
115 		/* shift the green an extra bit, it has 6 bits */
116 		color = grey_5bit << 11 | grey_5bit << (5 + 1) | grey_5bit;
117 		break;
118 	}
119 	return color;
120 }
121 
fill_buffer_rgb565(enum corner corner,uint8_t grey,uint8_t * buf,size_t buf_size)122 static void fill_buffer_rgb565(enum corner corner, uint8_t grey, uint8_t *buf,
123 			       size_t buf_size)
124 {
125 	uint16_t color = get_rgb565_color(corner, grey);
126 
127 	for (size_t idx = 0; idx < buf_size; idx += 2) {
128 		*(buf + idx + 0) = (color >> 8) & 0xFFu;
129 		*(buf + idx + 1) = (color >> 0) & 0xFFu;
130 	}
131 }
132 
fill_buffer_bgr565(enum corner corner,uint8_t grey,uint8_t * buf,size_t buf_size)133 static void fill_buffer_bgr565(enum corner corner, uint8_t grey, uint8_t *buf,
134 			       size_t buf_size)
135 {
136 	uint16_t color = get_rgb565_color(corner, grey);
137 
138 	for (size_t idx = 0; idx < buf_size; idx += 2) {
139 		*(uint16_t *)(buf + idx) = color;
140 	}
141 }
142 
fill_buffer_mono(enum corner corner,uint8_t grey,uint8_t black,uint8_t white,uint8_t * buf,size_t buf_size)143 static void fill_buffer_mono(enum corner corner, uint8_t grey,
144 			     uint8_t black, uint8_t white,
145 			     uint8_t *buf, size_t buf_size)
146 {
147 	uint16_t color;
148 
149 	switch (corner) {
150 	case BOTTOM_LEFT:
151 		color = (grey & 0x01u) ? white : black;
152 		break;
153 	default:
154 		color = black;
155 		break;
156 	}
157 
158 	memset(buf, color, buf_size);
159 }
160 
fill_buffer_mono01(enum corner corner,uint8_t grey,uint8_t * buf,size_t buf_size)161 static inline void fill_buffer_mono01(enum corner corner, uint8_t grey,
162 				      uint8_t *buf, size_t buf_size)
163 {
164 	fill_buffer_mono(corner, grey, 0x00u, 0xFFu, buf, buf_size);
165 }
166 
fill_buffer_mono10(enum corner corner,uint8_t grey,uint8_t * buf,size_t buf_size)167 static inline void fill_buffer_mono10(enum corner corner, uint8_t grey,
168 				      uint8_t *buf, size_t buf_size)
169 {
170 	fill_buffer_mono(corner, grey, 0xFFu, 0x00u, buf, buf_size);
171 }
172 
main(void)173 int main(void)
174 {
175 	size_t x;
176 	size_t y;
177 	size_t rect_w;
178 	size_t rect_h;
179 	size_t h_step;
180 	size_t scale;
181 	size_t grey_count;
182 	uint8_t bg_color;
183 	uint8_t *buf;
184 	int32_t grey_scale_sleep;
185 	const struct device *display_dev;
186 	struct display_capabilities capabilities;
187 	struct display_buffer_descriptor buf_desc;
188 	size_t buf_size = 0;
189 	fill_buffer fill_buffer_fnc = NULL;
190 
191 	display_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
192 	if (!device_is_ready(display_dev)) {
193 		LOG_ERR("Device %s not found. Aborting sample.",
194 			display_dev->name);
195 #ifdef CONFIG_ARCH_POSIX
196 		posix_exit_main(1);
197 #else
198 		return 0;
199 #endif
200 	}
201 
202 	LOG_INF("Display sample for %s", display_dev->name);
203 	display_get_capabilities(display_dev, &capabilities);
204 
205 	if (capabilities.screen_info & SCREEN_INFO_MONO_VTILED) {
206 		rect_w = 16;
207 		rect_h = 8;
208 	} else {
209 		rect_w = 2;
210 		rect_h = 1;
211 	}
212 
213 	if ((capabilities.x_resolution < 3 * rect_w) ||
214 	    (capabilities.y_resolution < 3 * rect_h) ||
215 	    (capabilities.x_resolution < 8 * rect_h)) {
216 		rect_w = capabilities.x_resolution * 40 / 100;
217 		rect_h = capabilities.y_resolution * 40 / 100;
218 		h_step = capabilities.y_resolution * 20 / 100;
219 		scale = 1;
220 	} else {
221 		h_step = rect_h;
222 		scale = (capabilities.x_resolution / 8) / rect_h;
223 	}
224 
225 	rect_w *= scale;
226 	rect_h *= scale;
227 
228 	if (capabilities.screen_info & SCREEN_INFO_EPD) {
229 		grey_scale_sleep = 10000;
230 	} else {
231 		grey_scale_sleep = 100;
232 	}
233 
234 	buf_size = rect_w * rect_h;
235 
236 	if (buf_size < (capabilities.x_resolution * h_step)) {
237 		buf_size = capabilities.x_resolution * h_step;
238 	}
239 
240 	switch (capabilities.current_pixel_format) {
241 	case PIXEL_FORMAT_ARGB_8888:
242 		bg_color = 0x00u;
243 		fill_buffer_fnc = fill_buffer_argb8888;
244 		buf_size *= 4;
245 		break;
246 	case PIXEL_FORMAT_RGB_888:
247 		bg_color = 0xFFu;
248 		fill_buffer_fnc = fill_buffer_rgb888;
249 		buf_size *= 3;
250 		break;
251 	case PIXEL_FORMAT_RGB_565:
252 		bg_color = 0xFFu;
253 		fill_buffer_fnc = fill_buffer_rgb565;
254 		buf_size *= 2;
255 		break;
256 	case PIXEL_FORMAT_BGR_565:
257 		bg_color = 0xFFu;
258 		fill_buffer_fnc = fill_buffer_bgr565;
259 		buf_size *= 2;
260 		break;
261 	case PIXEL_FORMAT_MONO01:
262 		bg_color = 0xFFu;
263 		fill_buffer_fnc = fill_buffer_mono01;
264 		buf_size = DIV_ROUND_UP(DIV_ROUND_UP(
265 			buf_size, NUM_BITS(uint8_t)), sizeof(uint8_t));
266 		break;
267 	case PIXEL_FORMAT_MONO10:
268 		bg_color = 0x00u;
269 		fill_buffer_fnc = fill_buffer_mono10;
270 		buf_size = DIV_ROUND_UP(DIV_ROUND_UP(
271 			buf_size, NUM_BITS(uint8_t)), sizeof(uint8_t));
272 		break;
273 	default:
274 		LOG_ERR("Unsupported pixel format. Aborting sample.");
275 #ifdef CONFIG_ARCH_POSIX
276 		posix_exit_main(1);
277 #else
278 		return 0;
279 #endif
280 	}
281 
282 	buf = k_malloc(buf_size);
283 
284 	if (buf == NULL) {
285 		LOG_ERR("Could not allocate memory. Aborting sample.");
286 #ifdef CONFIG_ARCH_POSIX
287 		posix_exit_main(1);
288 #else
289 		return 0;
290 #endif
291 	}
292 
293 	(void)memset(buf, bg_color, buf_size);
294 
295 	buf_desc.buf_size = buf_size;
296 	buf_desc.pitch = capabilities.x_resolution;
297 	buf_desc.width = capabilities.x_resolution;
298 	buf_desc.height = h_step;
299 
300 	/*
301 	 * The following writes will only render parts of the image,
302 	 * so turn this option on.
303 	 * This allows double-buffered displays to hold the pixels
304 	 * back until the image is complete.
305 	 */
306 	buf_desc.frame_incomplete = true;
307 
308 	for (int idx = 0; idx < capabilities.y_resolution; idx += h_step) {
309 		/*
310 		 * Tweaking the height value not to draw outside of the display.
311 		 * It is required when using a monochrome display whose vertical
312 		 * resolution can not be divided by 8.
313 		 */
314 		if ((capabilities.y_resolution - idx) < h_step) {
315 			buf_desc.height = (capabilities.y_resolution - idx);
316 		}
317 		display_write(display_dev, 0, idx, &buf_desc, buf);
318 	}
319 
320 	buf_desc.pitch = rect_w;
321 	buf_desc.width = rect_w;
322 	buf_desc.height = rect_h;
323 
324 	fill_buffer_fnc(TOP_LEFT, 0, buf, buf_size);
325 	x = 0;
326 	y = 0;
327 	display_write(display_dev, x, y, &buf_desc, buf);
328 
329 	fill_buffer_fnc(TOP_RIGHT, 0, buf, buf_size);
330 	x = capabilities.x_resolution - rect_w;
331 	y = 0;
332 	display_write(display_dev, x, y, &buf_desc, buf);
333 
334 	/*
335 	 * This is the last write of the frame, so turn this off.
336 	 * Double-buffered displays will now present the new image
337 	 * to the user.
338 	 */
339 	buf_desc.frame_incomplete = false;
340 
341 	fill_buffer_fnc(BOTTOM_RIGHT, 0, buf, buf_size);
342 	x = capabilities.x_resolution - rect_w;
343 	y = capabilities.y_resolution - rect_h;
344 	display_write(display_dev, x, y, &buf_desc, buf);
345 
346 	display_blanking_off(display_dev);
347 
348 	grey_count = 0;
349 	x = 0;
350 	y = capabilities.y_resolution - rect_h;
351 
352 	LOG_INF("Display starts");
353 	while (1) {
354 		fill_buffer_fnc(BOTTOM_LEFT, grey_count, buf, buf_size);
355 		display_write(display_dev, x, y, &buf_desc, buf);
356 		++grey_count;
357 		k_msleep(grey_scale_sleep);
358 #if CONFIG_TEST
359 		if (grey_count >= 30) {
360 			LOG_INF("Display sample test mode done %s", display_dev->name);
361 			break;
362 		}
363 #endif
364 	}
365 
366 #ifdef CONFIG_ARCH_POSIX
367 	posix_exit_main(0);
368 #endif
369 	return 0;
370 }
371