[libcamera-devel,v7,08/13] py: support setting array-controls
diff mbox series

Message ID 20220505104104.70841-9-tomi.valkeinen@ideasonboard.com
State Superseded
Headers show
Series
  • Python bindings
Related show

Commit Message

Tomi Valkeinen May 5, 2022, 10:40 a.m. UTC
Improve PyToControlValue() to convert python lists and tuples to a
ControlValue with a Span<> value.

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

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

Comments

Laurent Pinchart May 5, 2022, 5:55 p.m. UTC | #1
Hi Tomi,

Thank you for the patch.

On Thu, May 05, 2022 at 01:40:59PM +0300, Tomi Valkeinen wrote:
> Improve PyToControlValue() to convert python lists and tuples to a
> ControlValue with a Span<> value.
> 
> Original version from David Plowman <david.plowman@raspberrypi.com>.

Assuming the original version has David's SoB, please keep it here. I
would also keep David as the author of the patch.

Same for the remaining patches in the series.

> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>

Reviewed-by: Laurent Pinchart <laurent.pinchart@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:

Patch
diff mbox series

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: