[libcamera-devel] [RFC PATCH v4 01/21] controls: Add boolean constructor for ControlInfo
Paul Elder
paul.elder at ideasonboard.com
Fri Jul 16 12:56:11 CEST 2021
It would be convenient to be able to iterate over available boolean
values, for example for controls that designate if some function can be
enabled/disabled. The current min/max/def constructor is insufficient,
as .values() is empty, so the values cannot be easily iterated over, and
creating a Span of booleans does not work for the values constructor.
Add a new constructor to ControlInfo that takes a set of booleans (to
allow specifiying one or two available values), and a default. The
default value is not optional, as it doesn't make sense to have a silent
default for boolean values.
Update the ControlInfo test accordingly.
Signed-off-by: Paul Elder <paul.elder at ideasonboard.com>
---
Changes in v2:
- use set instead of span of bools
- add assertion to make sure that the default is a valid value
- update the test
---
include/libcamera/controls.h | 2 ++
src/libcamera/controls.cpp | 28 ++++++++++++++++++++++++++++
test/controls/control_info.cpp | 33 +++++++++++++++++++++++++++++++++
3 files changed, 63 insertions(+)
diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h
index 1bc958a4..707dc335 100644
--- a/include/libcamera/controls.h
+++ b/include/libcamera/controls.h
@@ -9,6 +9,7 @@
#define __LIBCAMERA_CONTROLS_H__
#include <assert.h>
+#include <set>
#include <stdint.h>
#include <string>
#include <unordered_map>
@@ -272,6 +273,7 @@ public:
const ControlValue &def = 0);
explicit ControlInfo(Span<const ControlValue> values,
const ControlValue &def = {});
+ explicit ControlInfo(std::set<bool> values, bool def);
const ControlValue &min() const { return min_; }
const ControlValue &max() const { return max_; }
diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp
index 34317fa0..f6351c01 100644
--- a/src/libcamera/controls.cpp
+++ b/src/libcamera/controls.cpp
@@ -514,6 +514,34 @@ ControlInfo::ControlInfo(Span<const ControlValue> values,
values_.push_back(value);
}
+/**
+ * \brief Construct a ControlInfo from a list of valid boolean values
+ * \param[in] values The control valid boolean vaalues
+ * \param[in] def The control default boolean value
+ *
+ * Construct a ControlInfo from a list of valid boolean values. The ControlInfo
+ * minimum and maximum values are set to the first and last members of the
+ * values list respectively. The default value is set to \a def.
+ */
+ControlInfo::ControlInfo(std::set<bool> values, bool def)
+ : def_(def)
+{
+ if (values.size() == 1) {
+ min_ = max_ = *values.begin();
+ } else {
+ min_ = false;
+ max_ = true;
+ }
+
+ def_ = def;
+
+ assert(values.count(def));
+
+ values_.reserve(2);
+ for (const bool &value : values)
+ values_.push_back(value);
+}
+
/**
* \fn ControlInfo::min()
* \brief Retrieve the minimum value of the control
diff --git a/test/controls/control_info.cpp b/test/controls/control_info.cpp
index 1e05e131..b3d2bd54 100644
--- a/test/controls/control_info.cpp
+++ b/test/controls/control_info.cpp
@@ -44,6 +44,39 @@ protected:
return TestFail;
}
+ /*
+ * Test information retrieval from a control with boolean
+ * values.
+ */
+ ControlInfo aeEnable({ false, true }, false);
+
+ if (aeEnable.min().get<bool>() != false ||
+ aeEnable.def().get<bool>() != false ||
+ aeEnable.max().get<bool>() != true) {
+ cout << "Invalid control range for AeEnable" << endl;
+ return TestFail;
+ }
+
+ if (aeEnable.values()[0].get<bool>() != false ||
+ aeEnable.values()[1].get<bool>() != true) {
+ cout << "Invalid control values for AeEnable" << endl;
+ return TestFail;
+ }
+
+ ControlInfo awbEnable({ true }, true);
+
+ if (awbEnable.min().get<bool>() != true ||
+ awbEnable.def().get<bool>() != true ||
+ awbEnable.max().get<bool>() != true) {
+ cout << "Invalid control range for AwbEnable" << endl;
+ return TestFail;
+ }
+
+ if (awbEnable.values()[0].get<bool>() != true) {
+ cout << "Invalid control values for AwbEnable" << endl;
+ return TestFail;
+ }
+
return TestPass;
}
};
--
2.27.0
More information about the libcamera-devel
mailing list