pose-micro / firmware /src /camera.c
eoinedge's picture
PoseMicro from GitHub run 3
ce7fc72 verified
Raw History Blame Contribute Delete
6.13 kB
/* 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;
}