[libcamera-devel] [PATCH v7 08/13] py: support setting array-controls

Tomi Valkeinen tomi.valkeinen at ideasonboard.com
Thu May 5 12:40:59 CEST 2022


Improve PyToControlValue() to convert python lists and tuples to a
ControlValue with a Span<> value.

Original version from David Plowman <david.plowman at raspberrypi.com>.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen at ideasonboard.com>
---
 src/py/libcamera/pymain.cpp | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/py/libcamera/pymain.cpp b/src/py/libcamera/pymain.cpp
index 81d48a20..db9d90ab 100644
--- a/src/py/libcamera/pymain.cpp
+++ b/src/py/libcamera/pymain.cpp
@@ -75,19 +75,30 @@ static py::object ControlValueToPy(const ControlValue &cv)
 	}
 }
 
+template<typename T>
+static ControlValue ControlValueMaybeArray(const py::object &ob)
+{
+	if (py::isinstance<py::list>(ob) || py::isinstance<py::tuple>(ob)) {
+		std::vector<T> vec = ob.cast<std::vector<T>>();
+		return ControlValue(Span<const T>(vec));
+	}
+
+	return ControlValue(ob.cast<T>());
+}
+
 static ControlValue PyToControlValue(const py::object &ob, ControlType type)
 {
 	switch (type) {
 	case ControlTypeBool:
 		return ControlValue(ob.cast<bool>());
 	case ControlTypeByte:
-		return ControlValue(ob.cast<uint8_t>());
+		return ControlValueMaybeArray<uint8_t>(ob);
 	case ControlTypeInteger32:
-		return ControlValue(ob.cast<int32_t>());
+		return ControlValueMaybeArray<int32_t>(ob);
 	case ControlTypeInteger64:
-		return ControlValue(ob.cast<int64_t>());
+		return ControlValueMaybeArray<int64_t>(ob);
 	case ControlTypeFloat:
-		return ControlValue(ob.cast<float>());
+		return ControlValueMaybeArray<float>(ob);
 	case ControlTypeString:
 		return ControlValue(ob.cast<string>());
 	case ControlTypeRectangle:
-- 
2.34.1



More information about the libcamera-devel mailing list