File size: 5,305 Bytes
ce7fc72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* Copyright (c) 2026 Eoin Jordan
 * SPDX-License-Identifier: Apache-2.0
 */
#include "screen.h"

#include <errno.h>
#include <string.h>

#include <zephyr/device.h>
#include <zephyr/drivers/display.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>

#include "font6x8.h"

#define BAND_ROWS CONFIG_APP_SCREEN_BAND_ROWS

static const struct device *const disp = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
static uint16_t band[SCREEN_W * BAND_ROWS];
static int band_y0;
static bool ready;
static bool dump_next;

/* RGB565 as the panel reads it. The GC9A01 takes the high byte first and
 * the driver sends the buffer unchanged, so on a little-endian CPU the two
 * bytes are swapped here. */
static inline uint16_t pack(uint16_t c)
{
	return IS_ENABLED(CONFIG_APP_SCREEN_SWAP_BYTES) ? (uint16_t)((c << 8) | (c >> 8)) : c;
}

/* The band as text: one line a row, four hex digits a pixel. */
static void dump_band(int rows)
{
	static const char hex[] = "0123456789abcdef";
	static char line[SCREEN_W * 4 + 1];

	for (int y = 0; y < rows; y++) {
		for (int x = 0; x < SCREEN_W; x++) {
			uint16_t c = pack(band[y * SCREEN_W + x]); /* pack() undoes itself */

			line[4 * x] = hex[c >> 12];
			line[4 * x + 1] = hex[(c >> 8) & 0xf];
			line[4 * x + 2] = hex[(c >> 4) & 0xf];
			line[4 * x + 3] = hex[c & 0xf];
		}
		line[SCREEN_W * 4] = '\0';
		printk("%s\n", line);
	}
}

static inline void put(int x, int y, uint16_t packed)
{
	y -= band_y0;
	if (x >= 0 && x < SCREEN_W && y >= 0 && y < BAND_ROWS) {
		band[y * SCREEN_W + x] = packed;
	}
}

int screen_init(void)
{
	struct display_capabilities caps;
	int ret;

	if (!device_is_ready(disp)) {
		printk("screen: display not ready\n");
		return -ENODEV;
	}
	display_get_capabilities(disp, &caps);
	printk("screen: %s %ux%u, pixel format 0x%x\n", disp->name, caps.x_resolution,
	       caps.y_resolution, (unsigned int)caps.current_pixel_format);
	if (caps.x_resolution != SCREEN_W || caps.y_resolution != SCREEN_H) {
		printk("screen: written for %dx%d\n", SCREEN_W, SCREEN_H);
		return -ENOTSUP;
	}
	if (caps.current_pixel_format != PIXEL_FORMAT_RGB_565) {
		ret = display_set_pixel_format(disp, PIXEL_FORMAT_RGB_565);
		if (ret != 0) {
			printk("screen: no 16-bit colour mode (%d)\n", ret);
			return ret;
		}
	}
	ready = true;
	screen_render(NULL, NULL);
	return display_blanking_off(disp);
}

void screen_render(screen_draw_fn draw, void *ctx)
{
	struct display_buffer_descriptor desc = {
		.width = SCREEN_W,
		.pitch = SCREEN_W,
	};

	if (!ready) {
		return;
	}
	if (dump_next) {
		printk("SCR %d %d rgb565\n", SCREEN_W, SCREEN_H);
	}
	for (band_y0 = 0; band_y0 < SCREEN_H; band_y0 += BAND_ROWS) {
		int rows = MIN(BAND_ROWS, SCREEN_H - band_y0);
		int ret;

		memset(band, 0, sizeof(band));
		if (draw != NULL) {
			draw(ctx);
		}
		if (dump_next) {
			dump_band(rows);
		}
		desc.height = rows;
		desc.buf_size = (uint32_t)rows * SCREEN_W * sizeof(uint16_t);
		ret = display_write(disp, 0, band_y0, &desc, band);
		if (ret != 0) {
			printk("screen: display_write %d\n", ret);
			break;
		}
	}
	if (dump_next) {
		printk("END SCR\n");
		dump_next = false;
	}
}

void screen_dump_next(void)
{
	dump_next = true;
}

void screen_fill(int x, int y, int w, int h, uint16_t color)
{
	uint16_t c = pack(color);

	for (int iy = MAX(y, band_y0); iy < MIN(y + h, band_y0 + BAND_ROWS); iy++) {
		for (int ix = x; ix < x + w; ix++) {
			put(ix, iy, c);
		}
	}
}

void screen_text(int x, int y, const char *s, int scale, uint16_t color)
{
	uint16_t c = pack(color);

	if (y + SCREEN_FONT_H * scale <= band_y0 || y >= band_y0 + BAND_ROWS) {
		return;
	}
	for (; *s != '\0'; s++, x += SCREEN_FONT_W * scale) {
		unsigned int ch = (unsigned char)*s;

		if (ch < FONT_FIRST || ch > FONT_LAST) {
			ch = '?';
		}
		const uint8_t *glyph = kFont6x8[ch - FONT_FIRST];

		for (int cx = 0; cx < FONT_W; cx++) {
			for (int cy = 0; cy < FONT_H; cy++) {
				if (((glyph[cx] >> cy) & 1) == 0) {
					continue;
				}
				for (int sy = 0; sy < scale; sy++) {
					for (int sx = 0; sx < scale; sx++) {
						put(x + cx * scale + sx, y + cy * scale + sy, c);
					}
				}
			}
		}
	}
}

void screen_rgb565(int x, int y, const uint16_t *img, int w, int h, int scale)
{
	if (y + h * scale <= band_y0 || y >= band_y0 + BAND_ROWS) {
		return;
	}
	for (int iy = 0; iy < h; iy++) {
		int py = y + iy * scale;

		if (py + scale <= band_y0 || py >= band_y0 + BAND_ROWS) {
			continue;
		}
		for (int ix = 0; ix < w; ix++) {
			uint16_t c = pack(img[iy * w + ix]);

			for (int sy = 0; sy < scale; sy++) {
				for (int sx = 0; sx < scale; sx++) {
					put(x + ix * scale + sx, py + sy, c);
				}
			}
		}
	}
}

void screen_frame(int x, int y, int w, int h, int t, uint16_t color)
{
	screen_fill(x, y, w, t, color);
	screen_fill(x, y + h - t, w, t, color);
	screen_fill(x, y, t, h, color);
	screen_fill(x + w - t, y, t, h, color);
}

void screen_gray(int x, int y, const uint8_t *img, int w, int h, int scale)
{
	if (y + h * scale <= band_y0 || y >= band_y0 + BAND_ROWS) {
		return;
	}
	for (int iy = 0; iy < h; iy++) {
		for (int ix = 0; ix < w; ix++) {
			uint8_t v = img[iy * w + ix];
			uint16_t c = pack(RGB565(v, v, v));

			for (int sy = 0; sy < scale; sy++) {
				for (int sx = 0; sx < scale; sx++) {
					put(x + ix * scale + sx, y + iy * scale + sy, c);
				}
			}
		}
	}
}