From patchwork Sun Sep 15 23:24:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 21272 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 1B9AAC324C for ; Sun, 15 Sep 2024 23:24:41 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 29B9763503; Mon, 16 Sep 2024 01:24:37 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="KmR+HLPo"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 31BEB634F5 for ; Mon, 16 Sep 2024 01:24:32 +0200 (CEST) Received: from pyrite.hamster-moth.ts.net (unknown [IPv6:2001:4bc9:a45:b0af:31d1:7a87:7f90:977b]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id DAE888BE; Mon, 16 Sep 2024 01:23:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1726442591; bh=bskHHRvbOOayvOrH/YHrhp0bUkWWGoRjOdpules77Qs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KmR+HLPo4gkXy2NyIltTC9vKquj7Z49590S+nvi+Fr6azM9HlFd1/9UI6HeEP9Pqk 62JpDelEPAvSuDYMk3PfSSzsOTORq1kZPQkAI9hZMWw12J+WwEQJWiNO/iVkxRDqTv 6Bp3fuciga9/uDU9NsTjHvyX4TCQcLHnKMVjuct0= From: Paul Elder To: libcamera-devel@lists.libcamera.org Cc: Paul Elder , stefan.klug@ideasonboard.com, Kieran Bingham , Laurent Pinchart , Umang Jain Subject: [PATCH v4 1/3] libcamera: controls: Add enum names and values map to ControlId Date: Mon, 16 Sep 2024 01:24:18 +0200 Message-Id: <20240915232420.2106705-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240915232420.2106705-1-paul.elder@ideasonboard.com> References: <20240915232420.2106705-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. 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 Reviewed-by: Umang Jain Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- Changes in v4: - improve commit message - remove forward map, as it won't be used - replace enumToString() with simply exposing the reverse map (as a const map reference) - simplify the code accordingly Changes in v3: - s/nameValueMap/enumStrMap/ - s/enumName/enumToString/ No change in v2 --- include/libcamera/controls.h | 14 ++++++++------ src/libcamera/control_ids.cpp.in | 4 +++- src/libcamera/controls.cpp | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index 7c2bb2872..96a774ccd 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,13 @@ 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 &enumerators() const { return reverseMap_; } private: LIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId) @@ -228,6 +228,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 +258,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..a46c431a1 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -389,7 +389,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 +417,12 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen * \return The control data type */ +/** + * \fn const std::map &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) * \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 * \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 Sun Sep 15 23:24:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 21273 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 7CAA3C324C for ; Sun, 15 Sep 2024 23:24:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 85CCD63505; Mon, 16 Sep 2024 01:24:39 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="vbGQ02dv"; 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 6865C634FB for ; Mon, 16 Sep 2024 01:24:32 +0200 (CEST) Received: from pyrite.hamster-moth.ts.net (unknown [IPv6:2001:4bc9:a45:b0af:31d1:7a87:7f90:977b]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 7045DD8B; Mon, 16 Sep 2024 01:23:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1726442591; bh=OvlJ5QVCC96KqOp5pULLo0ri03XnBzVKCaNHrCz1L/U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vbGQ02dvXw066B2QETFKUKGDU81aLf7EJtceCO2bhmsgD+vAX9vFz9xOj6DRhT2Kf fuhSdltgKWGv/tNUjvfP3GyPjO9Jpc08PyycHhQYtNAnQYOqrUnaRs6fL/hYpHR9lb +UQq5mNWQStmr54gw1/MS1iOBdwIm2ia4v/Wb65Q= From: Paul Elder To: libcamera-devel@lists.libcamera.org Cc: Paul Elder , stefan.klug@ideasonboard.com, Kieran Bingham , Laurent Pinchart , Umang Jain Subject: [PATCH v4 2/3] apps: cam: Print control enum values more nicely Date: Mon, 16 Sep 2024 01:24:19 +0200 Message-Id: <20240915232420.2106705-3-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240915232420.2106705-1-paul.elder@ideasonboard.com> References: <20240915232420.2106705-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. Example output (with a dummy AwbMode ControlInfo added to vimc): $ cam -c 1 --list-controls Using camera platform/vimc.0 Sensor B as cam0 Control: AwbMode: - AwbTungsten (2) - AwbFluorescent (3) - AwbDaylight (5) Control: Brightness: [-1.000000..1.000000] Control: Contrast: [0.000000..2.000000] Control: Saturation: [0.000000..2.000000] Signed-off-by: Paul Elder Reviewed-by: Umang Jain Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- Changes in v4: - use the newly exposed enumerators() (instead of enumToString()) - add example output to commit message Changes in v3: - s/enumName/enumToString/ No change in v2 --- src/apps/cam/camera_session.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/apps/cam/camera_session.cpp b/src/apps/cam/camera_session.cpp index 097dc4792..fc0923801 100644 --- a/src/apps/cam/camera_session.cpp +++ b/src/apps/cam/camera_session.cpp @@ -159,8 +159,23 @@ 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().empty()) { + 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(); + const auto &it = id->enumerators().find(val); + + std::cout << " - "; + if (it == id->enumerators().end()) + std::cout << "UNKNOWN"; + else + std::cout << it->second; + std::cout << " (" << val << ")" << std::endl; + } + } } } From patchwork Sun Sep 15 23:24:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 21274 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 0A4E4C3261 for ; Sun, 15 Sep 2024 23:24:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DF7B263500; Mon, 16 Sep 2024 01:24:40 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="MPXvHk0H"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BD43F634FC for ; Mon, 16 Sep 2024 01:24:32 +0200 (CEST) Received: from pyrite.hamster-moth.ts.net (unknown [IPv6:2001:4bc9:a45:b0af:31d1:7a87:7f90:977b]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id EA4A4D90; Mon, 16 Sep 2024 01:23:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1726442592; bh=jahOa5LnjgjhjjhoBNQxC+BykSS5NvVWy4340U1+JWs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MPXvHk0HEfJ80iU2Qdgx60yuWaydsgTPa+h6uF0aE4i4jm9mBP5KDaC/e3IqqTQzf IFaoEZYmNpN0Mkqjp5/7M6dybC/yybINab7AUVZmYDRklAqXYrdREkbWqKqzSjjtBA p3UdLhpQp4s1SC3QafqX7EHwM6PPP4siFSNePuNo= From: Paul Elder To: libcamera-devel@lists.libcamera.org Cc: Paul Elder , stefan.klug@ideasonboard.com, Kieran Bingham , Laurent Pinchart Subject: [PATCH v4 3/3] py: Add bindings for ControlId enum name Date: Mon, 16 Sep 2024 01:24:20 +0200 Message-Id: <20240915232420.2106705-4-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240915232420.2106705-1-paul.elder@ideasonboard.com> References: <20240915232420.2106705-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. Example usage: >>> cid libcamera.ControlId(16, AwbMode, ControlType.Integer32) >>> cid.enumerators() {0: 'AwbAuto', 1: 'AwbIncandescent', 2: 'AwbTungsten', 3: 'AwbFluorescent', 4: 'AwbIndoor', 5: 'AwbDaylight', 6: 'AwbCloudy', 7: 'AwbCustom'} >>> cinfo libcamera.ControlInfo([2..5]) >>> cinfo.values [2, 3, 5] >>> [cid.enumerators()[v] for v in cinfo.values] ['AwbTungsten', 'AwbFluorescent', 'AwbDaylight'] Signed-off-by: Paul Elder Reviewed-by: Kieran Bingham Reviewed-by: Stefan Klug Reviewed-by: Laurent Pinchart --- Changes in v4: - s/enum_str/enumerators/ - s/enumToString/enumerators/ - add example usage to commit message 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..983b76f6e 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("enumerators", &ControlId::enumerators); pyControlInfo .def_property_readonly("min", [](const ControlInfo &self) {