[libcamera-devel] [PATCH 6/7] cam: Print user-friendly camera names

Niklas Söderlund niklas.soderlund at ragnatech.se
Thu Aug 6 15:09:36 CEST 2020


Instead of only printing the camera ID which is not intended for humans
to read and parse create a more user friendly string when prating camera
names. The ID is still printed as it is one option used to select camera
using the --camera option.

Signed-off-by: Niklas Söderlund <niklas.soderlund at ragnatech.se>
---
 src/cam/main.cpp | 41 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/src/cam/main.cpp b/src/cam/main.cpp
index cc3facd5a5b22092..5af76f6965ef2387 100644
--- a/src/cam/main.cpp
+++ b/src/cam/main.cpp
@@ -21,6 +21,39 @@
 
 using namespace libcamera;
 
+std::string cameraName(const Camera *camera)
+{
+	const ControlList &props = camera->properties();
+	std::string name;
+
+	if (props.contains(properties::Model))
+		name += props.get(properties::Model) + " ";
+
+	if (props.contains(properties::Location)) {
+		switch (props.get(properties::Location)) {
+		case properties::CameraLocationFront:
+			name += "facing front ";
+			break;
+		case properties::CameraLocationBack:
+			name += "facing back ";
+			break;
+		case properties::CameraLocationExternal:
+			name += "external ";
+			break;
+		}
+	}
+
+	if (props.contains(properties::Rotation))
+		name += "rotated " + std::to_string(props.get(properties::Rotation)) + " degrees ";
+
+	if (!name.empty())
+		name += "with id ";
+
+	name += camera->id();
+
+	return name;
+}
+
 class CamApp
 {
 public:
@@ -117,7 +150,7 @@ int CamApp::init(int argc, char **argv)
 			return -EINVAL;
 		}
 
-		std::cout << "Using camera " << camera_->id() << std::endl;
+		std::cout << "Using camera " << cameraName(camera_.get()) << std::endl;
 
 		ret = prepareConfig();
 		if (ret) {
@@ -323,12 +356,12 @@ int CamApp::infoConfiguration()
 
 void CamApp::cameraAdded(std::shared_ptr<Camera> cam)
 {
-	std::cout << "Camera Added: " << cam->id() << std::endl;
+	std::cout << "Camera Added: " << cameraName(cam.get()) << std::endl;
 }
 
 void CamApp::cameraRemoved(std::shared_ptr<Camera> cam)
 {
-	std::cout << "Camera Removed: " << cam->id() << std::endl;
+	std::cout << "Camera Removed: " << cameraName(cam.get()) << std::endl;
 }
 
 int CamApp::run()
@@ -340,7 +373,7 @@ int CamApp::run()
 
 		unsigned int index = 1;
 		for (const std::shared_ptr<Camera> &cam : cm_->cameras()) {
-			std::cout << index << ": " << cam->id() << std::endl;
+			std::cout << index << ": " << cameraName(cam.get()) << std::endl;
 			index++;
 		}
 	}
-- 
2.28.0



More information about the libcamera-devel mailing list