From patchwork Wed Sep 11 09:35:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 21229 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id AF879C324C for ; Wed, 11 Sep 2024 09:36:23 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 87CC363502; Wed, 11 Sep 2024 11:36:21 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="NNS1I/OO"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id F329C634F8 for ; Wed, 11 Sep 2024 11:36:17 +0200 (CEST) Received: from pyrite.hamster-moth.ts.net (213-229-8-243.static.upcbusiness.at [213.229.8.243]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 368035B3; Wed, 11 Sep 2024 11:35:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1726047300; bh=Merarit6R7O+1+scQXD8fxtOfsoykGcNhExOBQXwP+c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NNS1I/OOjB7Rqa0HPx5ea6itvd/EC6jWqNW2yF2Vvi4GnQHONEI57R0XcyI7KdRFA ulDhfnVoYQYwh5q0+DZsLB8lUVbTiuVuVMwx6HclLp4WDO3DkDgUQy0AHIQPbXrVCD R5kb/qKZvNo2sx6jg+aXn7E8pYj4qxTOJmxCvvHM= From: Paul Elder To: libcamera-devel@lists.libcamera.org Cc: Paul Elder , stefan.klug@ideasonboard.com, Umang Jain Subject: [PATCH v3 1/3] libcamera: controls: Add enum names and values map to ControlId Date: Wed, 11 Sep 2024 11:35:58 +0200 Message-Id: <20240911093600.671979-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240911093600.671979-1-paul.elder@ideasonboard.com> References: <20240911093600.671979-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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. The main map is name -> value, as this was necessary for mapping names from tuning files to enum values. We reuse this generated code to reduce complexity, and generate a reverse map from it. Signed-off-by: Paul Elder Reviewed-by: Umang Jain --- Changes in v3: - s/nameValueMap/enumStrMap/ - s/enumName/enumToString/ No change in v2 --- include/libcamera/controls.h | 15 +++++++++------ src/libcamera/control_ids.cpp.in | 4 +++- src/libcamera/controls.cpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index 7c2bb2872..8959dfcc2 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include #include @@ -213,14 +214,14 @@ private: class ControlId { public: - ControlId(unsigned int id, const std::string &name, ControlType type) - : id_(id), name_(name), type_(type) - { - } + ControlId(unsigned int id, const std::string &name, ControlType type, + const std::map &enumStrMap = {}); unsigned int id() const { return id_; } const std::string &name() const { return name_; } ControlType type() const { return type_; } + const std::map &enumStrMap() const { return enumStrMap_; } + const std::string enumToString(int32_t value) const; private: LIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId) @@ -228,6 +229,8 @@ private: unsigned int id_; std::string name_; ControlType type_; + std::map enumStrMap_; + std::map reverseMap_; }; static inline bool operator==(unsigned int lhs, const ControlId &rhs) @@ -256,8 +259,8 @@ class Control : public ControlId public: using type = T; - Control(unsigned int id, const char *name) - : ControlId(id, name, details::control_type>::value) + Control(unsigned int id, const char *name, const std::map &enumStrMap = {}) + : ControlId(id, name, details::control_type>::value, enumStrMap) { } diff --git a/src/libcamera/control_ids.cpp.in b/src/libcamera/control_ids.cpp.in index 05c8fb385..3a2049311 100644 --- a/src/libcamera/control_ids.cpp.in +++ b/src/libcamera/control_ids.cpp.in @@ -89,8 +89,10 @@ extern const std::map {{ctrl.name}}NameValueMap = { { "{{enum.name}}", {{enum.name}} }, {%- 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}}"); +{% endif -%} {%- endfor %} {% if vendor != 'libcamera' %} diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index dba744042..0f83139fb 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -7,6 +7,8 @@ #include +#include +#include #include #include #include @@ -389,7 +391,15 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen * \param[in] id The control numerical ID * \param[in] name The control name * \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 &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 @@ -409,6 +419,24 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen * \return The control data type */ +/** + * \brief Retrieve the name of an enum value + * \return The name of the enum value + */ +const std::string ControlId::enumToString(int32_t value) const +{ + if (reverseMap_.find(value) != reverseMap_.end()) + return reverseMap_.at(value); + + return "UNKNOWN"; +} + +/** + * \fn std::map ControlId::enumStrMap() const + * \brief Retrieve the map from enum names to enum values (if applicable) + * \return The map from enum names to enum values + */ + /** * \fn bool operator==(unsigned int lhs, const ControlId &rhs) * \brief Compare a ControlId with a control numerical ID @@ -459,6 +487,7 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen * \brief Construct a Control instance * \param[in] id The control numerical ID * \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. */ From patchwork Wed Sep 11 09:35:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 21230 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 9A574C324C for ; Wed, 11 Sep 2024 09:36:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4A075634FD; Wed, 11 Sep 2024 11:36:23 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Is5W4NNL"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1AB6B634F9 for ; Wed, 11 Sep 2024 11:36:18 +0200 (CEST) Received: from pyrite.hamster-moth.ts.net (213-229-8-243.static.upcbusiness.at [213.229.8.243]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 843CABEB; Wed, 11 Sep 2024 11:35:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1726047300; bh=c6svChRR/G6yEUvhfG8OKLKYhvx8tqXtO3PGOJJc0V4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Is5W4NNL3nLfuAySdpDb8xZ+YA6zukbIwm96VxXi2/SZYLCQOaOAZmQaGqrS/FMAE X/JKW1Jhd0lC/mUsTcOwBaH/2je9G/JdBQ+rhHOxdcZ4Qf1UpTOxu2UH8ukLqYjL3A mXaQVDRgCiEokpOvC2S1ssmH5Hltvzz042PBl1aQ= From: Paul Elder To: libcamera-devel@lists.libcamera.org Cc: Paul Elder , stefan.klug@ideasonboard.com, Umang Jain Subject: [PATCH v3 2/3] apps: cam: Print control enum values more nicely Date: Wed, 11 Sep 2024 11:35:59 +0200 Message-Id: <20240911093600.671979-3-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240911093600.671979-1-paul.elder@ideasonboard.com> References: <20240911093600.671979-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Now that enum names can be obtained from ControlId, use that information to print out the list of supported enum values in --list-controls. Signed-off-by: Paul Elder Reviewed-by: Umang Jain --- Changes in v3: - s/enumName/enumToString/ No change in v2 --- src/apps/cam/camera_session.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/apps/cam/camera_session.cpp b/src/apps/cam/camera_session.cpp index 097dc4792..37de6c9f3 100644 --- a/src/apps/cam/camera_session.cpp +++ b/src/apps/cam/camera_session.cpp @@ -159,8 +159,16 @@ CameraSession::~CameraSession() void CameraSession::listControls() const { for (const auto &[id, info] : camera_->controls()) { - std::cout << "Control: " << id->name() << ": " - << info.toString() << std::endl; + if (info.values().size() == 0) { + std::cout << "Control: " << id->name() << ": " + << info.toString() << std::endl; + } else { + std::cout << "Control: " << id->name() << ": " << std::endl; + for (const auto &value : info.values()) { + int32_t val = value.get(); + std::cout << " - " << id->enumToString(val) << " (" << val << ")" << std::endl; + } + } } } From patchwork Wed Sep 11 09:36:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 21231 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 698FFC324C for ; Wed, 11 Sep 2024 09:36:28 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 14C0163500; Wed, 11 Sep 2024 11:36:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="QMYGHdwG"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5DF59634FC for ; Wed, 11 Sep 2024 11:36:18 +0200 (CEST) Received: from pyrite.hamster-moth.ts.net (213-229-8-243.static.upcbusiness.at [213.229.8.243]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D44A111D6; Wed, 11 Sep 2024 11:35:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1726047301; bh=6ZXWUodmfH/b6r8gKECpJITCPI5z60rFpay3/WIP/l8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QMYGHdwGwMYkDiO3Bhktb89p/TasoEwOpDv1LKjI3FXo8kC93mZfISKRofaBLnmfi ntI6kdHaPooM5MTrpcSluhf5vOfdgKz+BWXMO4hzhf5VDTkQA49vpWUSBi8ViadHWN fCaduWPo8zpkRDudbNAYl4KvHRKx2uDLmQ3iRRIQ= From: Paul Elder To: libcamera-devel@lists.libcamera.org Cc: Paul Elder , stefan.klug@ideasonboard.com, Kieran Bingham Subject: [PATCH v3 3/3] py: Add bindings for ControlId enum name Date: Wed, 11 Sep 2024 11:36:00 +0200 Message-Id: <20240911093600.671979-4-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240911093600.671979-1-paul.elder@ideasonboard.com> References: <20240911093600.671979-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add python bindings for querying enum value names from a ControlId. Signed-off-by: Paul Elder Reviewed-by: Kieran Bingham --- Changes in v3: - s/enumName/enumToString/ - s/enum_name/enum_str/ New in v2 --- src/py/libcamera/py_main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/py/libcamera/py_main.cpp b/src/py/libcamera/py_main.cpp index ab33f38a8..f2897b11c 100644 --- a/src/py/libcamera/py_main.cpp +++ b/src/py/libcamera/py_main.cpp @@ -404,7 +404,8 @@ PYBIND11_MODULE(_libcamera, m) .def("__repr__", [](const ControlId &self) { return py::str("libcamera.ControlId({}, {}, {})") .format(self.id(), self.name(), self.type()); - }); + }) + .def("enum_str", &ControlId::enumToString); pyControlInfo .def_property_readonly("min", [](const ControlInfo &self) {