|
| 1 | +/* |
| 2 | + * Copyright 2025 Arduino SA |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * Camera driver. |
| 18 | + */ |
| 19 | +#include "Arduino.h" |
| 20 | +#include "camera.h" |
| 21 | + |
| 22 | +#include <zephyr/kernel.h> |
| 23 | +#include <zephyr/device.h> |
| 24 | +#include <zephyr/drivers/video.h> |
| 25 | +#include <zephyr/drivers/video-controls.h> |
| 26 | + |
| 27 | +FrameBuffer::FrameBuffer() : vbuf(NULL) { |
| 28 | + |
| 29 | +} |
| 30 | + |
| 31 | +uint32_t FrameBuffer::getBufferSize() { |
| 32 | + if (this->vbuf) { |
| 33 | + return this->vbuf->bytesused; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +uint8_t* FrameBuffer::getBuffer() { |
| 38 | + if (this->vbuf) { |
| 39 | + return this->vbuf->buffer; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +Camera::Camera() : vdev(NULL), byte_swap(false), yuv_to_gray(false) { |
| 44 | + for (size_t i = 0; i < ARRAY_SIZE(this->vbuf); i++) { |
| 45 | + this->vbuf[i] = NULL; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +bool Camera::begin(uint32_t width, uint32_t height, uint32_t pixformat, bool byte_swap) { |
| 50 | + #if DT_HAS_CHOSEN(zephyr_camera) |
| 51 | + this->vdev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera)); |
| 52 | + #endif |
| 53 | + |
| 54 | + if (!this->vdev || !device_is_ready(this->vdev)) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + switch (pixformat) { |
| 59 | + case CAMERA_RGB565: |
| 60 | + this->byte_swap = byte_swap; |
| 61 | + pixformat = VIDEO_PIX_FMT_RGB565; |
| 62 | + break; |
| 63 | + case CAMERA_GRAYSCALE: |
| 64 | + // There's no support for mono sensors. |
| 65 | + this->yuv_to_gray = true; |
| 66 | + pixformat = VIDEO_PIX_FMT_YUYV; |
| 67 | + break; |
| 68 | + default: |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + // Get capabilities |
| 73 | + struct video_caps caps = {0}; |
| 74 | + if (video_get_caps(this->vdev, VIDEO_EP_OUT, &caps)) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + for (size_t i=0; caps.format_caps[i].pixelformat != NULL; i++) { |
| 79 | + const struct video_format_cap *fcap = &caps.format_caps[i]; |
| 80 | + if (fcap->width_min == width && |
| 81 | + fcap->height_min == height && |
| 82 | + fcap->pixelformat == pixformat) { |
| 83 | + break; |
| 84 | + } |
| 85 | + if (caps.format_caps[i+1].pixelformat == NULL) { |
| 86 | + Serial.println("The specified format is not supported"); |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Set format. |
| 92 | + static struct video_format fmt = { |
| 93 | + .pixelformat = pixformat, |
| 94 | + .width = width, |
| 95 | + .height = height, |
| 96 | + .pitch = width * 2, |
| 97 | + }; |
| 98 | + |
| 99 | + if (video_set_format(this->vdev, VIDEO_EP_OUT, &fmt)) { |
| 100 | + Serial.println("Failed to set video format"); |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + // Allocate video buffers. |
| 105 | + for (size_t i = 0; i < ARRAY_SIZE(this->vbuf); i++) { |
| 106 | + this->vbuf[i] = video_buffer_aligned_alloc(fmt.pitch * fmt.height, |
| 107 | + CONFIG_VIDEO_BUFFER_POOL_ALIGN, |
| 108 | + K_FOREVER); |
| 109 | + if (this->vbuf[i] == NULL) { |
| 110 | + Serial.println("Failed to allocate video buffers"); |
| 111 | + return false; |
| 112 | + } |
| 113 | + video_enqueue(this->vdev, VIDEO_EP_OUT, this->vbuf[i]); |
| 114 | + } |
| 115 | + |
| 116 | + // Start video capture |
| 117 | + if (video_stream_start(this->vdev)) { |
| 118 | + Serial.println("Failed to start capture"); |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + return true; |
| 123 | +} |
| 124 | + |
| 125 | +bool Camera::grabFrame(FrameBuffer &fb, uint32_t timeout) { |
| 126 | + if (this->vdev == NULL) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + if (video_dequeue(this->vdev, VIDEO_EP_OUT, &fb.vbuf, K_MSEC(timeout))) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + if (this->byte_swap) { |
| 135 | + uint16_t *pixels = (uint16_t *) fb.vbuf->buffer; |
| 136 | + for (size_t i=0; i<fb.vbuf->bytesused / 2; i++) { |
| 137 | + pixels[i] = __REVSH(pixels[i]); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if (this->yuv_to_gray) { |
| 142 | + uint8_t *pixels = (uint8_t *) fb.vbuf->buffer; |
| 143 | + for (size_t i=0; i<fb.vbuf->bytesused / 2; i++) { |
| 144 | + pixels[i] = pixels[i*2]; |
| 145 | + } |
| 146 | + fb.vbuf->bytesused /= 2; |
| 147 | + } |
| 148 | + |
| 149 | + return true; |
| 150 | +} |
| 151 | + |
| 152 | +bool Camera::releaseFrame(FrameBuffer &fb) { |
| 153 | + if (this->vdev == NULL) { |
| 154 | + return false; |
| 155 | + } |
| 156 | + |
| 157 | + if (video_enqueue(this->vdev, VIDEO_EP_OUT, fb.vbuf)) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + return true; |
| 162 | +} |
| 163 | + |
| 164 | +bool Camera::setVerticalFlip(bool flip_enable) { |
| 165 | + return video_set_ctrl(this->vdev, VIDEO_CID_VFLIP, (void *) flip_enable) == 0; |
| 166 | +} |
| 167 | + |
| 168 | +bool Camera::setHorizontalMirror(bool mirror_enable) { |
| 169 | + return video_set_ctrl(this->vdev, VIDEO_CID_HFLIP, (void *) mirror_enable) == 0; |
| 170 | +} |
0 commit comments