libcamera: controls: Add enum names and values map to ControlId

Add to ControlId information about the names and values of enum, in the
event that the ControlId is an enum type. This allows applications to
query the ControlId for the names of the enum values, so that they can
be displayed on a UI, for example. Without this, it was necessary to use
macros of NameOfControlNameValueMap, which is difficult to use and is
very inflexible.

There already exists a map from name -> value in generated code. Reuse
this and pass it to the ControlId constructor, which in turn generates
the reverse map. The reverse map is then exposed to applications.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Paul Elder 2024-09-16 01:24:18 +02:00 committed by Laurent Pinchart
parent 44b49af7a0
commit 9bbccb97fa
3 changed files with 26 additions and 7 deletions

View file

@ -8,6 +8,7 @@
#pragma once #pragma once
#include <assert.h> #include <assert.h>
#include <map>
#include <optional> #include <optional>
#include <set> #include <set>
#include <stdint.h> #include <stdint.h>
@ -213,14 +214,13 @@ private:
class ControlId class ControlId
{ {
public: public:
ControlId(unsigned int id, const std::string &name, ControlType type) ControlId(unsigned int id, const std::string &name, ControlType type,
: id_(id), name_(name), type_(type) const std::map<std::string, int32_t> &enumStrMap = {});
{
}
unsigned int id() const { return id_; } unsigned int id() const { return id_; }
const std::string &name() const { return name_; } const std::string &name() const { return name_; }
ControlType type() const { return type_; } ControlType type() const { return type_; }
const std::map<int32_t, std::string> &enumerators() const { return reverseMap_; }
private: private:
LIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId) LIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId)
@ -228,6 +228,8 @@ private:
unsigned int id_; unsigned int id_;
std::string name_; std::string name_;
ControlType type_; ControlType type_;
std::map<std::string, int32_t> enumStrMap_;
std::map<int32_t, std::string> reverseMap_;
}; };
static inline bool operator==(unsigned int lhs, const ControlId &rhs) static inline bool operator==(unsigned int lhs, const ControlId &rhs)
@ -256,8 +258,8 @@ class Control : public ControlId
public: public:
using type = T; using type = T;
Control(unsigned int id, const char *name) Control(unsigned int id, const char *name, const std::map<std::string, int32_t> &enumStrMap = {})
: ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value) : ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value, enumStrMap)
{ {
} }

View file

@ -89,8 +89,10 @@ extern const std::map<std::string, {{ctrl.type}}> {{ctrl.name}}NameValueMap = {
{ "{{enum.name}}", {{enum.name}} }, { "{{enum.name}}", {{enum.name}} },
{%- endfor %} {%- endfor %}
}; };
{% endif -%} extern const Control<{{ctrl.type}}> {{ctrl.name}}({{ctrl.name|snake_case|upper}}, "{{ctrl.name}}", {{ctrl.name}}NameValueMap);
{% else -%}
extern const Control<{{ctrl.type}}> {{ctrl.name}}({{ctrl.name|snake_case|upper}}, "{{ctrl.name}}"); extern const Control<{{ctrl.type}}> {{ctrl.name}}({{ctrl.name|snake_case|upper}}, "{{ctrl.name}}");
{% endif -%}
{%- endfor %} {%- endfor %}
{% if vendor != 'libcamera' %} {% if vendor != 'libcamera' %}

View file

@ -389,7 +389,15 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen
* \param[in] id The control numerical ID * \param[in] id The control numerical ID
* \param[in] name The control name * \param[in] name The control name
* \param[in] type The control data type * \param[in] type The control data type
* \param[in] enumStrMap The map from enum names to values (optional)
*/ */
ControlId::ControlId(unsigned int id, const std::string &name, ControlType type,
const std::map<std::string, int32_t> &enumStrMap)
: id_(id), name_(name), type_(type), enumStrMap_(enumStrMap)
{
for (const auto &pair : enumStrMap_)
reverseMap_[pair.second] = pair.first;
}
/** /**
* \fn unsigned int ControlId::id() const * \fn unsigned int ControlId::id() const
@ -409,6 +417,12 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen
* \return The control data type * \return The control data type
*/ */
/**
* \fn const std::map<int32_t, std::string> &ControlId::enumerators() const
* \brief Retrieve the map of enum values to enum names
* \return The map of enum values to enum names
*/
/** /**
* \fn bool operator==(unsigned int lhs, const ControlId &rhs) * \fn bool operator==(unsigned int lhs, const ControlId &rhs)
* \brief Compare a ControlId with a control numerical ID * \brief Compare a ControlId with a control numerical ID
@ -459,6 +473,7 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen
* \brief Construct a Control instance * \brief Construct a Control instance
* \param[in] id The control numerical ID * \param[in] id The control numerical ID
* \param[in] name The control name * \param[in] name The control name
* \param[in] enumStrMap The map from enum names to values (optional)
* *
* The control data type is automatically deduced from the template type T. * The control data type is automatically deduced from the template type T.
*/ */