[libcamera-devel] [RFC PATCH v2 2/5] libcamera: camera_sensor: Enable to set a test pattern mode

Hirokazu Honda hiroh at chromium.org
Tue Jun 22 04:36:51 CEST 2021


Provides a function to set the camera sensor a test pattern mode.

Signed-off-by: Hirokazu Honda <hiroh at chromium.org>
---
 include/libcamera/internal/camera_sensor.h |  1 +
 src/libcamera/camera_sensor.cpp            | 39 ++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h
index e133ebf4..8b9f84c9 100644
--- a/include/libcamera/internal/camera_sensor.h
+++ b/include/libcamera/internal/camera_sensor.h
@@ -43,6 +43,7 @@ public:
 	{
 		return testPatternModes_;
 	}
+	int setTestPatternMode(uint8_t testPatternMode);
 
 	V4L2SubdeviceFormat getFormat(const std::vector<unsigned int> &mbusCodes,
 				      const Size &size) const;
diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp
index 70bbd97a..ce8ff274 100644
--- a/src/libcamera/camera_sensor.cpp
+++ b/src/libcamera/camera_sensor.cpp
@@ -507,6 +507,45 @@ Size CameraSensor::resolution() const
  * \return The list of test pattern modes
  */
 
+/**
+ * \brief Set the camera sensor a specified controls::TestPatternMode
+ *
+ * \return 0 on success or a negative error code otherwise
+ */
+int CameraSensor::setTestPatternMode(uint8_t testPatternMode)
+{
+	if (std::find(testPatternModes_.begin(), testPatternModes_.end(),
+		      testPatternMode) == testPatternModes_.end()) {
+		LOG(CameraSensor, Error) << "Unsupported test pattern mode: "
+					 << testPatternMode;
+		return -EINVAL;
+	}
+
+	const CameraSensorProperties *props = CameraSensorProperties::get(model_);
+	if (!props)
+		return -EINVAL;
+
+	auto it = props->testPatternModes.find(testPatternMode);
+	ASSERT(it != props->testPatternModes.end());
+	const uint8_t index = it->second;
+
+	ControlList ctrls = getControls({ V4L2_CID_TEST_PATTERN });
+	if (ctrls.empty()) {
+		LOG(CameraSensor, Error)
+			<< "Failed to retrieve test pattern control";
+		return -EINVAL;
+	}
+
+	ControlValue value = ctrls.get(V4L2_CID_TEST_PATTERN);
+	if (value.get<int32_t>() == index)
+		return 0;
+
+	value.set<int32_t>(index);
+	ctrls.set(V4L2_CID_TEST_PATTERN, value);
+
+	return setControls(&ctrls);
+}
+
 /**
  * \brief Retrieve the best sensor format for a desired output
  * \param[in] mbusCodes The list of acceptable media bus codes
-- 
2.32.0.288.g62a8d224e6-goog



More information about the libcamera-devel mailing list