From 2efcd61c3b2414a4b95b4e4ffec09a0ca0054455 Mon Sep 17 00:00:00 2001 From: Programmers3539 Date: Sun, 17 Dec 2023 12:30:27 -0800 Subject: [PATCH] RPI 5 Support (#7) --- CMakeLists.txt | 2 +- camera_grabber.cpp | 5 ++--- camera_model.cpp | 1 + camera_model.h | 1 + camera_runner.cpp | 26 ++++++++++++++++++++------ libcamera_jni.cpp | 2 +- 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f913fb5..73a38f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.25.1) project(libcamera_meme) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) diff --git a/camera_grabber.cpp b/camera_grabber.cpp index d34f7a1..bdd5695 100644 --- a/camera_grabber.cpp +++ b/camera_grabber.cpp @@ -45,10 +45,9 @@ CameraGrabber::CameraGrabber(std::shared_ptr camera, printf("Rotation = %i\n", rotation); if (rotation == 180) { - using namespace libcamera; - config->transform = Transform::HFlip * Transform::VFlip * libcamera::Transform::Identity; + config->orientation = libcamera::Orientation::Rotate180; } else { - config->transform = libcamera::Transform::Identity; + config->orientation = libcamera::Orientation::Rotate0; } if (config->validate() == libcamera::CameraConfiguration::Invalid) { diff --git a/camera_model.cpp b/camera_model.cpp index 692e669..938b6f0 100644 --- a/camera_model.cpp +++ b/camera_model.cpp @@ -6,6 +6,7 @@ CameraModel stringToModel(const std::string& model) { const char* famname = model.c_str(); if (!strcmp(famname, "ov5647")) return OV5647; else if (!strcmp(famname, "imx219")) return IMX219; + else if (!strcmp(famname, "imx708")) return IMX708; else if (!strcmp(famname, "imx477")) return IMX477; else if (!strcmp(famname, "ov9281")) return OV9281; else if (!strcmp(famname, "ov7251")) return OV7251; diff --git a/camera_model.h b/camera_model.h index f2fcf8e..1822b22 100644 --- a/camera_model.h +++ b/camera_model.h @@ -6,6 +6,7 @@ enum CameraModel { Disconnected = 0, OV5647, // Picam v1 IMX219, // Picam v2 + IMX708, // Picam v3 IMX477, // Picam HQ OV9281, OV7251, diff --git a/camera_runner.cpp b/camera_runner.cpp index 7509f2b..43bfbe1 100644 --- a/camera_runner.cpp +++ b/camera_runner.cpp @@ -19,6 +19,9 @@ using latch = Latch; #include #include +#include +#include + using namespace std::chrono; using namespace std::chrono_literals; @@ -84,11 +87,6 @@ void CameraRunner::start() { .at(grabber.streamConfiguration().stream()) ->planes(); - for (int i = 0; i < 3; i++) { - // std::cout << "Plane " << (i + 1) << " has fd " << planes[i].fd.get() << " with offset " << planes[i].offset << std::endl; - // std::cout << "Plane " << (i + 1) << " has fd " << planes[i].fd.get() << " with offset " << planes[i].offset << " and pitch " << static_cast(stride / 2) << std::endl; - } - std::array yuv_data{{ {planes[0].fd.get(), static_cast(planes[0].offset), static_cast(stride)}, @@ -107,7 +105,6 @@ void CameraRunner::start() { rangeFromColorspace(colorspace), type); - if (out != 0) { /* From libcamera docs: @@ -177,6 +174,15 @@ void CameraRunner::start() { auto input_ptr = mmaped.at(data.fd); int bound = m_width * m_height; + + { + struct dma_buf_sync dma_sync {}; + dma_sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW; + int ret = ::ioctl(data.fd, DMA_BUF_IOCTL_SYNC, &dma_sync); + if (ret) + throw std::runtime_error("failed to start DMA buf sync"); + } + if (m_copyInput) { for (int i = 0; i < bound; i++) { std::memcpy(color_out_buf + i * 3, input_ptr + i * 4, 3); @@ -188,6 +194,14 @@ void CameraRunner::start() { processed_out_buf[i] = input_ptr[i * 4 + 3]; } } + + { + struct dma_buf_sync dma_sync {}; + dma_sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW; + int ret = ::ioctl(data.fd, DMA_BUF_IOCTL_SYNC, &dma_sync); + if (ret) + throw std::runtime_error("failed to start DMA buf sync"); + } m_thresholder.returnBuffer(data.fd); outgoing.set(std::move(mat_pair)); diff --git a/libcamera_jni.cpp b/libcamera_jni.cpp index b360919..81a9e34 100644 --- a/libcamera_jni.cpp +++ b/libcamera_jni.cpp @@ -62,7 +62,7 @@ Java_org_photonvision_raspi_LibCameraJNI_createCamera(JNIEnv *env, jclass, JNIEXPORT jint Java_org_photonvision_raspi_LibCameraJNI_getSensorModelRaw( JNIEnv *env, jclass clazz) { - bool runner_exists = runner > 0; + bool runner_exists = runner != 0; if (!runner_exists) { Java_org_photonvision_raspi_LibCameraJNI_createCamera(env, clazz, 320, 240, 30);