[libcamera-devel,6/6,HACK] ipu3: Set and get a few sensor controls

Message ID 20190602130435.18780-7-jacopo@jmondi.org
State Superseded
Delegated to: Jacopo Mondi
Headers show
Series
  • libcamera: v4l2_controls: Add support for V4L2 controls
Related show

Commit Message

Jacopo Mondi June 2, 2019, 1:04 p.m. UTC
Not to merge patch to demonstrate how to set controls on the image
sensor device.

Not-Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 src/libcamera/pipeline/ipu3/ipu3.cpp | 86 ++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)

Patch

diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp
index 0a50b6159782..7854e5140de8 100644
--- a/src/libcamera/pipeline/ipu3/ipu3.cpp
+++ b/src/libcamera/pipeline/ipu3/ipu3.cpp
@@ -689,6 +689,92 @@  int PipelineHandlerIPU3::start(Camera *camera)
 	ImgUDevice *imgu = data->imgu_;
 	int ret;
 
+	/* --- Get control values --- */
+	std::vector<unsigned int> controlIds = {
+		V4L2_CID_EXPOSURE, V4L2_CID_ANALOGUE_GAIN,
+	};
+	std::vector<V4L2Control *> controls =
+		cio2->sensor_->dev()->getControls(controlIds);
+	if (controls.empty()) {
+		LOG(Error) << "Failed to get control values";
+		return -EINVAL;
+	}
+
+	for (V4L2Control *ctrl : controls) {
+		unsigned int id = ctrl->id();
+
+		switch(ctrl->type()) {
+		case V4L2_CTRL_TYPE_INTEGER:
+		case V4L2_CTRL_TYPE_BOOLEAN:
+		{
+			V4L2IntControl *c = static_cast<V4L2IntControl *>(ctrl);
+			uint32_t val = c->value();
+			LOG(Error) << "Control : " << id
+				   << " - value: " << val;
+		}
+			break;
+		case V4L2_CTRL_TYPE_INTEGER64:
+		{
+			V4L2Int64Control *c = static_cast<V4L2Int64Control *>(ctrl);
+			uint64_t val = c->value();
+			LOG(Error) << "Control : " << id
+				   << " - value: " << val;
+		}
+			break;
+		default:
+			LOG(Error) << "Unsupported type: " << ctrl->type();
+			return -EINVAL;
+		}
+	}
+
+	/* --- Set control values --- */
+	V4L2IntControl exposureControl(V4L2_CID_EXPOSURE, 2046);
+	V4L2IntControl gainControl(V4L2_CID_ANALOGUE_GAIN, 1024);
+	std::vector<V4L2Control *> sensorControls = {
+		&exposureControl, &gainControl,
+	};
+
+	ret = cio2->sensor_->dev()->setControls(sensorControls);
+	if (ret) {
+		LOG(IPU3, Error) << "Failed to set controls";
+		return ret;
+	}
+
+	/* --- Get control values back again and verify they have changed --- */
+	std::vector<V4L2Control *> newcontrols =
+		cio2->sensor_->dev()->getControls(controlIds);
+	if (newcontrols.empty()) {
+		LOG(Error) << "Failed to get control values";
+		return -EINVAL;
+	}
+
+	for (V4L2Control *ctrl : newcontrols) {
+		unsigned int id = ctrl->id();
+
+		switch(ctrl->type()) {
+		case V4L2_CTRL_TYPE_INTEGER:
+		case V4L2_CTRL_TYPE_BOOLEAN:
+		{
+			V4L2IntControl *c = static_cast<V4L2IntControl *>(ctrl);
+			uint32_t val = c->value();
+			LOG(Error) << "Control : " << id
+				   << " - value: " << val;
+		}
+			break;
+		case V4L2_CTRL_TYPE_INTEGER64:
+		{
+			V4L2Int64Control *c = static_cast<V4L2Int64Control *>(ctrl);
+			uint64_t val = c->value();
+			LOG(Error) << "Control : " << id
+				   << " - value: " << val;
+		}
+			break;
+		default:
+			LOG(Error) << "Unsupported type: " << ctrl->type();
+			return -EINVAL;
+		}
+	}
+
 	/*
 	 * Start the ImgU video devices, buffers will be queued to the
 	 * ImgU output and viewfinder when requests will be queued.