[libcamera-devel,19/31] libcamera: controls: Add support for float controls

Message ID 20200229164254.23604-20-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • libcamera: Add support for array controls
Related show

Commit Message

Laurent Pinchart Feb. 29, 2020, 4:42 p.m. UTC
From: Jacopo Mondi <jacopo@jmondi.org>

Add support for float values in Control<> and ControlValue classes.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 include/libcamera/controls.h         |  6 ++++++
 src/libcamera/control_serializer.cpp | 12 ++++++++++++
 src/libcamera/controls.cpp           | 14 +++++++++++---
 3 files changed, 29 insertions(+), 3 deletions(-)

Comments

Kieran Bingham March 3, 2020, 12:41 a.m. UTC | #1
On 29/02/2020 16:42, Laurent Pinchart wrote:
> From: Jacopo Mondi <jacopo@jmondi.org>
> 
> Add support for float values in Control<> and ControlValue classes.
> 

I like how clear it's become to add new types

> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  include/libcamera/controls.h         |  6 ++++++
>  src/libcamera/control_serializer.cpp | 12 ++++++++++++
>  src/libcamera/controls.cpp           | 14 +++++++++++---
>  3 files changed, 29 insertions(+), 3 deletions(-)
> 
> diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h
> index f99c90e934c1..97a662e3f4a6 100644
> --- a/include/libcamera/controls.h
> +++ b/include/libcamera/controls.h
> @@ -24,6 +24,7 @@ enum ControlType {
>  	ControlTypeBool,
>  	ControlTypeInteger32,
>  	ControlTypeInteger64,
> +	ControlTypeFloat,
>  };
>  
>  namespace details {
> @@ -52,6 +53,11 @@ struct control_type<int64_t> {
>  	static constexpr ControlType value = ControlTypeInteger64;
>  };
>  
> +template<>
> +struct control_type<float> {
> +	static constexpr ControlType value = ControlTypeFloat;
> +};
> +
>  template<typename T, std::size_t N>
>  struct control_type<Span<T, N>> : public control_type<std::remove_cv_t<T>> {
>  };
> diff --git a/src/libcamera/control_serializer.cpp b/src/libcamera/control_serializer.cpp
> index 2b66ab978f81..5feaaa965cc5 100644
> --- a/src/libcamera/control_serializer.cpp
> +++ b/src/libcamera/control_serializer.cpp
> @@ -165,6 +165,12 @@ void ControlSerializer::store(const ControlValue &value,
>  		break;
>  	}
>  
> +	case ControlTypeFloat: {
> +		float data = value.get<float>();
> +		buffer.write(&data);
> +		break;
> +	}
> +
>  	default:
>  		break;
>  	}
> @@ -337,6 +343,12 @@ ControlValue ControlSerializer::load<ControlValue>(ControlType type,
>  		return ControlValue(value);
>  	}
>  
> +	case ControlTypeFloat: {
> +		float value;
> +		b.read(&value);
> +		return ControlValue(value);
> +	}
> +
>  	default:
>  		return ControlValue();
>  	}
> diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp
> index 829eea6f4240..46469dd8b98b 100644
> --- a/src/libcamera/controls.cpp
> +++ b/src/libcamera/controls.cpp
> @@ -55,6 +55,7 @@ static constexpr size_t ControlValueSize[] = {
>  	[ControlTypeBool]		= sizeof(bool),
>  	[ControlTypeInteger32]		= sizeof(int32_t),
>  	[ControlTypeInteger64]		= sizeof(int64_t),
> +	[ControlTypeFloat]		= sizeof(float),
>  };
>  
>  } /* namespace */
> @@ -70,6 +71,8 @@ static constexpr size_t ControlValueSize[] = {
>   * The control stores a 32-bit integer value
>   * \var ControlTypeInteger64
>   * The control stores a 64-bit integer value
> + * \var ControlTypeFloat
> + * The control stores a 32-bit floating point value
>   */
>  
>  /**
> @@ -209,6 +212,11 @@ std::string ControlValue::toString() const
>  			str += std::to_string(*value);
>  			break;
>  		}
> +		case ControlTypeFloat: {
> +			const float *value = reinterpret_cast<const float *>(data);
> +			str += std::to_string(*value);
> +			break;
> +		}
>  		case ControlTypeNone:
>  			break;
>  		}
> @@ -378,9 +386,9 @@ void ControlValue::set(ControlType type, bool isArray, const void *data,
>   * instead of Control.
>   *
>   * Controls of any type can be defined through template specialisation, but
> - * libcamera only supports the bool, int32_t and int64_t types natively (this
> - * includes types that are equivalent to the supported types, such as int and
> - * long int).
> + * libcamera only supports the bool, int32_t, int64_t and float types natively
> + * (this includes types that are equivalent to the supported types, such as int
> + * and long int).
>   *
>   * Controls IDs shall be unique. While nothing prevents multiple instances of
>   * the Control class to be created with the same ID for the same object, doing
>

Patch

diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h
index f99c90e934c1..97a662e3f4a6 100644
--- a/include/libcamera/controls.h
+++ b/include/libcamera/controls.h
@@ -24,6 +24,7 @@  enum ControlType {
 	ControlTypeBool,
 	ControlTypeInteger32,
 	ControlTypeInteger64,
+	ControlTypeFloat,
 };
 
 namespace details {
@@ -52,6 +53,11 @@  struct control_type<int64_t> {
 	static constexpr ControlType value = ControlTypeInteger64;
 };
 
+template<>
+struct control_type<float> {
+	static constexpr ControlType value = ControlTypeFloat;
+};
+
 template<typename T, std::size_t N>
 struct control_type<Span<T, N>> : public control_type<std::remove_cv_t<T>> {
 };
diff --git a/src/libcamera/control_serializer.cpp b/src/libcamera/control_serializer.cpp
index 2b66ab978f81..5feaaa965cc5 100644
--- a/src/libcamera/control_serializer.cpp
+++ b/src/libcamera/control_serializer.cpp
@@ -165,6 +165,12 @@  void ControlSerializer::store(const ControlValue &value,
 		break;
 	}
 
+	case ControlTypeFloat: {
+		float data = value.get<float>();
+		buffer.write(&data);
+		break;
+	}
+
 	default:
 		break;
 	}
@@ -337,6 +343,12 @@  ControlValue ControlSerializer::load<ControlValue>(ControlType type,
 		return ControlValue(value);
 	}
 
+	case ControlTypeFloat: {
+		float value;
+		b.read(&value);
+		return ControlValue(value);
+	}
+
 	default:
 		return ControlValue();
 	}
diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp
index 829eea6f4240..46469dd8b98b 100644
--- a/src/libcamera/controls.cpp
+++ b/src/libcamera/controls.cpp
@@ -55,6 +55,7 @@  static constexpr size_t ControlValueSize[] = {
 	[ControlTypeBool]		= sizeof(bool),
 	[ControlTypeInteger32]		= sizeof(int32_t),
 	[ControlTypeInteger64]		= sizeof(int64_t),
+	[ControlTypeFloat]		= sizeof(float),
 };
 
 } /* namespace */
@@ -70,6 +71,8 @@  static constexpr size_t ControlValueSize[] = {
  * The control stores a 32-bit integer value
  * \var ControlTypeInteger64
  * The control stores a 64-bit integer value
+ * \var ControlTypeFloat
+ * The control stores a 32-bit floating point value
  */
 
 /**
@@ -209,6 +212,11 @@  std::string ControlValue::toString() const
 			str += std::to_string(*value);
 			break;
 		}
+		case ControlTypeFloat: {
+			const float *value = reinterpret_cast<const float *>(data);
+			str += std::to_string(*value);
+			break;
+		}
 		case ControlTypeNone:
 			break;
 		}
@@ -378,9 +386,9 @@  void ControlValue::set(ControlType type, bool isArray, const void *data,
  * instead of Control.
  *
  * Controls of any type can be defined through template specialisation, but
- * libcamera only supports the bool, int32_t and int64_t types natively (this
- * includes types that are equivalent to the supported types, such as int and
- * long int).
+ * libcamera only supports the bool, int32_t, int64_t and float types natively
+ * (this includes types that are equivalent to the supported types, such as int
+ * and long int).
  *
  * Controls IDs shall be unique. While nothing prevents multiple instances of
  * the Control class to be created with the same ID for the same object, doing