File size: 6,131 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
222
223
224
225
226
227
/* Copyright (c) 2026 Eoin Jordan
 * SPDX-License-Identifier: Apache-2.0
 *
 * OV2640 capture on the XIAO ESP32S3 Sense through Zephyr's video API
 * (LCD_CAM DVP receiver + ov2640 driver, both in the board devicetree).
 *
 * The colour path of the camera code in executorch-zephyr-xiao-esp32s3.
 */

#include "camera.h"

#include <errno.h>
#include <string.h>
#include <zephyr/device.h>
#include <zephyr/drivers/video.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(app_camera, CONFIG_LOG_DEFAULT_LEVEL);

#if !DT_HAS_CHOSEN(zephyr_camera)
#error "No zephyr,camera chosen in devicetree. Build for xiao_esp32s3/esp32s3/procpu/sense."
#endif

#define CAM_WIDTH  160
#define CAM_HEIGHT 120

static const struct device *const cam = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
static struct video_format fmt = {.type = VIDEO_BUF_TYPE_OUTPUT};

static void grab_thread(void *a, void *b, void *c);
K_THREAD_STACK_DECLARE(grab_stack, 2048);
static struct k_thread grab_data;

int app_camera_init(void)
{
	struct video_caps caps = {.type = VIDEO_BUF_TYPE_OUTPUT};
	int ret;

	if (!device_is_ready(cam)) {
		LOG_ERR("%s not ready", cam->name);
		return -ENODEV;
	}

	ret = video_get_caps(cam, &caps);
	if (ret < 0) {
		LOG_ERR("video_get_caps: %d", ret);
		return ret;
	}

	ret = video_get_format(cam, &fmt);
	if (ret < 0) {
		LOG_ERR("video_get_format: %d", ret);
		return ret;
	}
	fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
	fmt.width = CAM_WIDTH;
	fmt.height = CAM_HEIGHT;

	ret = video_set_compose_format(cam, &fmt);
	if (ret < 0) {
		LOG_ERR("video_set_compose_format: %d", ret);
		return ret;
	}
	/* Read back pitch/size as the driver computed them. */
	ret = video_get_format(cam, &fmt);
	if (ret < 0) {
		return ret;
	}
	LOG_INF("camera %s: %s %ux%u pitch=%u size=%u", cam->name,
		VIDEO_FOURCC_TO_STR(fmt.pixelformat), fmt.width, fmt.height, fmt.pitch,
		fmt.size);

	if (caps.min_vbuf_count > CONFIG_VIDEO_BUFFER_POOL_NUM_MAX) {
		LOG_ERR("driver needs %u buffers, pool has %u", caps.min_vbuf_count,
			CONFIG_VIDEO_BUFFER_POOL_NUM_MAX);
		return -EINVAL;
	}

	for (int i = 0; i < CONFIG_VIDEO_BUFFER_POOL_NUM_MAX; i++) {
		struct video_buffer *vbuf = video_buffer_aligned_alloc(
			fmt.size, CONFIG_VIDEO_BUFFER_POOL_ALIGN, K_NO_WAIT);

		if (vbuf == NULL) {
			LOG_ERR("video buffer alloc failed (raise CONFIG_VIDEO_BUFFER_POOL_HEAP_SIZE)");
			return -ENOMEM;
		}
		vbuf->type = VIDEO_BUF_TYPE_OUTPUT;
		ret = video_enqueue(cam, vbuf);
		if (ret < 0) {
			LOG_ERR("video_enqueue: %d", ret);
			return ret;
		}
	}

	ret = video_stream_start(cam, VIDEO_BUF_TYPE_OUTPUT);
	if (ret < 0) {
		LOG_ERR("video_stream_start: %d", ret);
		return ret;
	}
	LOG_INF("camera streaming");
	k_thread_create(&grab_data, grab_stack, K_THREAD_STACK_SIZEOF(grab_stack), grab_thread, NULL, NULL, NULL,
			K_PRIO_COOP(2), 0, K_NO_WAIT); /* above main: it must run during an inference */
	k_thread_name_set(&grab_data, "camera");
	return 0;
}

/* RGB565 pixel at p as a host-order value. */
static inline uint16_t rgb565_at(const uint8_t *p)
{
	if (IS_ENABLED(CONFIG_APP_CAMERA_SWAP_BYTES)) {
		return (uint16_t)((p[0] << 8) | p[1]);
	}
	return (uint16_t)(p[0] | (p[1] << 8));
}

/* Where output pixel (ox, oy) of a side x side picture is stored: turned half
 * a turn when the camera board is mounted upside down.
 */
static inline uint32_t out_index(uint32_t ox, uint32_t oy, uint32_t side)
{
	if (IS_ENABLED(CONFIG_APP_CAMERA_ROTATE_180)) {
		return (side - 1 - oy) * side + (side - 1 - ox);
	}
	return oy * side + ox;
}

/* The newest frame as the pose model wants it: the centre square in colour,
 * box-reduced to 96 x 96. A thread keeps it fresh so the driver always gets
 * its buffers back at once: with two buffers it drops a frame, and then stops
 * delivering, whenever a buffer is held for longer than a frame period, and
 * one inference takes several.
 */
static uint16_t latest_rgb[APP_RGB_SIDE * APP_RGB_SIDE];

static void reduce_rgb(const uint8_t *frame, uint16_t *out)
{
	const uint32_t w = fmt.width;
	const uint32_t h = fmt.height;
	const uint32_t side = (w < h) ? w : h;
	const uint32_t x0 = (w - side) / 2;
	const uint32_t y0 = (h - side) / 2;

	for (uint32_t oy = 0; oy < APP_RGB_SIDE; oy++) {
		uint32_t ya = y0 + (oy * side) / APP_RGB_SIDE;
		uint32_t yb = y0 + ((oy + 1) * side) / APP_RGB_SIDE;

		for (uint32_t ox = 0; ox < APP_RGB_SIDE; ox++) {
			uint32_t xa = x0 + (ox * side) / APP_RGB_SIDE;
			uint32_t xb = x0 + ((ox + 1) * side) / APP_RGB_SIDE;
			uint32_t r = 0, g = 0, b = 0, n = 0;

			for (uint32_t y = ya; y < yb; y++) {
				const uint8_t *row = frame + y * fmt.pitch;

				for (uint32_t x = xa; x < xb; x++) {
					uint16_t v = rgb565_at(row + 2 * x);

					r += (v >> 11) & 0x1f;
					g += (v >> 5) & 0x3f;
					b += v & 0x1f;
					n++;
				}
			}
			if (n == 0) {
				n = 1;
			}
			out[out_index(ox, oy, APP_RGB_SIDE)] =
				(uint16_t)(((r / n) << 11) | ((g / n) << 5) | (b / n));
		}
	}
}

static K_MUTEX_DEFINE(latest_lock);
static K_SEM_DEFINE(latest_ready, 0, 1);

static int grab(void)
{
	struct video_buffer *vbuf = NULL;
	struct video_buffer tmp = {.type = VIDEO_BUF_TYPE_OUTPUT};
	int ret;

	vbuf = &tmp;
	ret = video_dequeue(cam, &vbuf, K_FOREVER);
	if (ret < 0) {
		LOG_ERR("video_dequeue: %d", ret);
		return ret;
	}

	/* Reduced straight into the shared picture: a second copy would cost
	 * 18 KiB, and the reader only waits a few milliseconds. */
	k_mutex_lock(&latest_lock, K_FOREVER);
	reduce_rgb(vbuf->buffer, latest_rgb);
	k_mutex_unlock(&latest_lock);
	ret = video_enqueue(cam, vbuf);
	if (ret < 0) {
		LOG_ERR("video_enqueue: %d", ret);
		return ret;
	}
	k_sem_give(&latest_ready);
	return 0;
}

static void grab_thread(void *a, void *b, void *c)
{
	ARG_UNUSED(a);
	ARG_UNUSED(b);
	ARG_UNUSED(c);

	while (grab() == 0) {
	}
}

K_THREAD_STACK_DEFINE(grab_stack, 2048);

int app_camera_capture_rgb565(uint16_t *out)
{
	if (k_sem_take(&latest_ready, K_MSEC(2000)) != 0) {
		LOG_ERR("no frame from the camera");
		return -EIO;
	}
	k_mutex_lock(&latest_lock, K_FOREVER);
	memcpy(out, latest_rgb, sizeof(latest_rgb));
	k_mutex_unlock(&latest_lock);
	return 0;
}