Files
ESP32S3_vt/components/cam_wrapper/include/cam_wrapper.h
T
Horldsence 4f893ce140 ESP32-S3 摄像头图传:固件 + Slint 客户端 + 共享协议
两块 ESP32-S3-WROOM-1-N16R8 图传板(DVP 接口接 OV2640/OV3660/OV5640),
向队伍客户端与裁判终端推 UDP 图传。固件是跑在 ESP-IDF 上的 Rust,用官方
esp32-camera 组件采集;客户端是 Rust + Slint 桌面程序。

三个 crate 各自独立构建:
  .            firmware,交叉编译到 Xtensa
  protocol/    线路格式与帧重组,两端共用,无 ESP-IDF 依赖,有单元测试
  client/      Slint 桌面客户端

视频走 UDP 而非 TCP:实测传感器产出 22 fps,TCP MJPEG 只送出 1.9 fps,
且只占用链路能力的 1%——链路是空的,帧堵在队头阻塞后面等一个实时画面
等不起的重传计时器。改为独立数据报、不重传、缺片丢整帧后达到 20.8 fps。

发现是双向的:板子每 2 秒广播 Hello,客户端广播 Subscribe,板子据此定向
推流。板子身份由 MAC 派生,重启不变,所以客户端可以把绑定存进配置。

裁判端同时收两块板子(每个发送方一个重组器),队伍端绑定其中一块并只显示
自己那一边的血量。装甲板接口已预留(ArmorSource trait + ARMOR_PORT),
当前无数据源,界面显示占位符。

vendor 了 esp32-camera 2.1.7:组件注册表的版本求解器找不到它,且需要打一处
补丁(见 components/esp32-camera/target/esp32s3/ll_cam.c)。

.gitignore 三条 target/ 都锚定在根目录,因为 protocol/ 与 client/ 各自也是
独立 crate;而 components/esp32-camera/target/ 是上游的按芯片分目录,里面是
源码,必须保留。
2026-10-05 18:22:19 +08:00

134 lines
4.5 KiB
C

/* Union-free wrapper around the esp32-camera API.
*
* `camera_config_t` in esp_camera.h uses anonymous unions for the deprecated
* SCCB pin names, which bindgen renders as unsafe union fields that are
* awkward to build from Rust. This wrapper takes a plain struct and fills in
* the unions on the C side, so the generated bindings stay trivial to use.
*
* The enum-valued config fields are declared as local enums whose values equal
* the esp32-camera ones (`pixformat_t`, `framesize_t`, `camera_fb_location_t`,
* `camera_grab_mode_t`). That keeps the numbers defined in exactly one place -
* the esp32-camera headers - while still giving Rust named constants to use
* instead of bare magic numbers.
*/
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "esp_camera.h"
#include "esp_err.h"
/* Defined by esp32-camera's cam_hal.c but declared only in its private header,
* which components outside esp32-camera cannot include. Redeclared here so the
* wrapper can set the VSYNC polarity before esp_camera_init() and read back the
* clock the driver used. */
void cam_set_vsync_invert(bool invert);
bool cam_get_vsync_invert(void);
void cam_set_xclk_hz(int hz);
int cam_get_xclk_hz(void);
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
CAM_WRAPPER_PIXFORMAT_JPEG = PIXFORMAT_JPEG,
CAM_WRAPPER_PIXFORMAT_RGB565 = PIXFORMAT_RGB565,
} cam_wrapper_pixel_format_t;
typedef enum {
CAM_WRAPPER_FRAMESIZE_QQVGA = FRAMESIZE_QQVGA,
CAM_WRAPPER_FRAMESIZE_QVGA = FRAMESIZE_QVGA,
CAM_WRAPPER_FRAMESIZE_CIF = FRAMESIZE_CIF,
CAM_WRAPPER_FRAMESIZE_HVGA = FRAMESIZE_HVGA,
CAM_WRAPPER_FRAMESIZE_VGA = FRAMESIZE_VGA,
CAM_WRAPPER_FRAMESIZE_SVGA = FRAMESIZE_SVGA,
CAM_WRAPPER_FRAMESIZE_XGA = FRAMESIZE_XGA,
CAM_WRAPPER_FRAMESIZE_SXGA = FRAMESIZE_SXGA,
CAM_WRAPPER_FRAMESIZE_UXGA = FRAMESIZE_UXGA,
} cam_wrapper_framesize_t;
typedef enum {
CAM_WRAPPER_FB_IN_PSRAM = CAMERA_FB_IN_PSRAM,
CAM_WRAPPER_FB_IN_DRAM = CAMERA_FB_IN_DRAM,
} cam_wrapper_fb_location_t;
typedef enum {
CAM_WRAPPER_GRAB_WHEN_EMPTY = CAMERA_GRAB_WHEN_EMPTY,
CAM_WRAPPER_GRAB_LATEST = CAMERA_GRAB_LATEST,
} cam_wrapper_grab_mode_t;
/* Pin map of the camera connector. Use -1 for signals that are not wired. */
typedef struct {
int pin_pwdn;
int pin_reset;
int pin_xclk;
int pin_sccb_sda;
int pin_sccb_scl;
int pin_d[8]; /* index 0 -> pin_d0 .. index 7 -> pin_d7 */
int pin_vsync;
int pin_href;
int pin_pclk;
int xclk_freq_hz;
int ledc_timer;
int ledc_channel;
cam_wrapper_pixel_format_t pixel_format;
cam_wrapper_framesize_t frame_size;
int jpeg_quality;
size_t fb_count;
cam_wrapper_fb_location_t fb_location;
cam_wrapper_grab_mode_t grab_mode;
int sccb_i2c_port;
/* VSYNC polarity. 0 = active-high, 1 = active-low (esp32-camera's default,
* correct for most DVP sensors). Getting this wrong does not fail: the
* driver latches frames on the wrong edge and the result is a valid but
* very low frame rate, so it is worth being able to flip it. */
int vsync_invert;
} cam_wrapper_config_t;
/* Sensor hint. esp32-camera probes the sensor over SCCB and binds a driver by
* its PID/ID, so AUTO is normally all that is needed; the others exist only to
* disambiguate when the probe itself is unreliable. */
typedef enum {
CAM_WRAPPER_SENSOR_AUTO = 0,
CAM_WRAPPER_SENSOR_OV2640 = 1,
CAM_WRAPPER_SENSOR_OV3660 = 2,
CAM_WRAPPER_SENSOR_OV5640 = 3,
} cam_wrapper_sensor_t;
/* A single captured frame. `data` points into memory owned by the driver and
* stays valid only until cam_wrapper_fb_release() is called on this struct.
* The struct is caller-owned so it can live on the handler's stack. */
typedef struct {
uint8_t *data;
size_t len;
size_t width;
size_t height;
int format;
void *handle; /* internal driver frame; do not touch */
} cam_wrapper_fb_t;
esp_err_t cam_wrapper_init(const cam_wrapper_config_t *config, cam_wrapper_sensor_t sensor);
/* Acquire one frame into `out`. Returns ESP_OK on success, ESP_FAIL if the
* driver could not hand out a frame (e.g. timeout or buffer starvation). */
esp_err_t cam_wrapper_fb_get(cam_wrapper_fb_t *out, cam_wrapper_sensor_t sensor);
void cam_wrapper_fb_release(cam_wrapper_fb_t *fb);
esp_err_t cam_wrapper_reconfigure(const cam_wrapper_config_t *config);
esp_err_t cam_wrapper_deinit(void);
/* Name of the sensor the driver detected, or "unknown". Never NULL. */
const char *cam_wrapper_sensor_name(void);
#ifdef __cplusplus
}
#endif