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