forked from arduino/ArduinoCore-zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.cpp
170 lines (144 loc) · 4.59 KB
/
camera.cpp
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
/*
* Copyright 2025 Arduino SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Camera driver.
*/
#include "Arduino.h"
#include "camera.h"
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/video.h>
#include <zephyr/drivers/video-controls.h>
FrameBuffer::FrameBuffer() : vbuf(NULL) {
}
uint32_t FrameBuffer::getBufferSize() {
if (this->vbuf) {
return this->vbuf->bytesused;
}
}
uint8_t* FrameBuffer::getBuffer() {
if (this->vbuf) {
return this->vbuf->buffer;
}
}
Camera::Camera() : vdev(NULL), byte_swap(false), yuv_to_gray(false) {
for (size_t i = 0; i < ARRAY_SIZE(this->vbuf); i++) {
this->vbuf[i] = NULL;
}
}
bool Camera::begin(uint32_t width, uint32_t height, uint32_t pixformat, bool byte_swap) {
#if DT_HAS_CHOSEN(zephyr_camera)
this->vdev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
#endif
if (!this->vdev || !device_is_ready(this->vdev)) {
return false;
}
switch (pixformat) {
case CAMERA_RGB565:
this->byte_swap = byte_swap;
pixformat = VIDEO_PIX_FMT_RGB565;
break;
case CAMERA_GRAYSCALE:
// There's no support for mono sensors.
this->yuv_to_gray = true;
pixformat = VIDEO_PIX_FMT_YUYV;
break;
default:
break;
}
// Get capabilities
struct video_caps caps = {0};
if (video_get_caps(this->vdev, VIDEO_EP_OUT, &caps)) {
return false;
}
for (size_t i=0; caps.format_caps[i].pixelformat != NULL; i++) {
const struct video_format_cap *fcap = &caps.format_caps[i];
if (fcap->width_min == width &&
fcap->height_min == height &&
fcap->pixelformat == pixformat) {
break;
}
if (caps.format_caps[i+1].pixelformat == NULL) {
Serial.println("The specified format is not supported");
return false;
}
}
// Set format.
static struct video_format fmt = {
.pixelformat = pixformat,
.width = width,
.height = height,
.pitch = width * 2,
};
if (video_set_format(this->vdev, VIDEO_EP_OUT, &fmt)) {
Serial.println("Failed to set video format");
return false;
}
// Allocate video buffers.
for (size_t i = 0; i < ARRAY_SIZE(this->vbuf); i++) {
this->vbuf[i] = video_buffer_aligned_alloc(fmt.pitch * fmt.height,
CONFIG_VIDEO_BUFFER_POOL_ALIGN,
K_FOREVER);
if (this->vbuf[i] == NULL) {
Serial.println("Failed to allocate video buffers");
return false;
}
video_enqueue(this->vdev, VIDEO_EP_OUT, this->vbuf[i]);
}
// Start video capture
if (video_stream_start(this->vdev)) {
Serial.println("Failed to start capture");
return false;
}
return true;
}
bool Camera::grabFrame(FrameBuffer &fb, uint32_t timeout) {
if (this->vdev == NULL) {
return false;
}
if (video_dequeue(this->vdev, VIDEO_EP_OUT, &fb.vbuf, K_MSEC(timeout))) {
return false;
}
if (this->byte_swap) {
uint16_t *pixels = (uint16_t *) fb.vbuf->buffer;
for (size_t i=0; i<fb.vbuf->bytesused / 2; i++) {
pixels[i] = __REVSH(pixels[i]);
}
}
if (this->yuv_to_gray) {
uint8_t *pixels = (uint8_t *) fb.vbuf->buffer;
for (size_t i=0; i<fb.vbuf->bytesused / 2; i++) {
pixels[i] = pixels[i*2];
}
fb.vbuf->bytesused /= 2;
}
return true;
}
bool Camera::releaseFrame(FrameBuffer &fb) {
if (this->vdev == NULL) {
return false;
}
if (video_enqueue(this->vdev, VIDEO_EP_OUT, fb.vbuf)) {
return false;
}
return true;
}
bool Camera::setVerticalFlip(bool flip_enable) {
return video_set_ctrl(this->vdev, VIDEO_CID_VFLIP, (void *) flip_enable) == 0;
}
bool Camera::setHorizontalMirror(bool mirror_enable) {
return video_set_ctrl(this->vdev, VIDEO_CID_HFLIP, (void *) mirror_enable) == 0;
}