[libcamera-devel] [PATCH 02/23] libcamera: controls: Parse 'enum' in gen-controls.py

Jacopo Mondi jacopo at jmondi.org
Mon Jan 13 17:42:24 CET 2020


In preparation to add libcamera Camera properties definitions by re-using
the control generation framework, augment the gen_controls.py script to
support parsing the 'enum' yaml tag and generate documentation and
definition of possible values associated with a Control or a Property
and defined through an enumeration of supported values.

Reviewed-by: Niklas Söderlund <niklas.soderlund at ragnatech.se>
Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
Signed-off-by: Jacopo Mondi <jacopo at jmondi.org>
---
 src/libcamera/gen-controls.py | 48 +++++++++++++++++++++++++++++++----
 1 file changed, 43 insertions(+), 5 deletions(-)

diff --git a/src/libcamera/gen-controls.py b/src/libcamera/gen-controls.py
index 940386cc68c8..7369ab4b0fdb 100755
--- a/src/libcamera/gen-controls.py
+++ b/src/libcamera/gen-controls.py
@@ -16,7 +16,18 @@ def snake_case(s):
     return ''.join([c.isupper() and ('_' + c) or c for c in s]).strip('_')
 
 
+def format_description(description):
+    description = description.strip('\n').split('\n')
+    description[0] = '\\brief ' + description[0]
+    return '\n'.join([(line and ' * ' or ' *') + line for line in description])
+
+
 def generate_cpp(controls):
+    enum_doc_start_template = string.Template('''/**
+ * \\enum ${name}Values
+ * \\brief Supported ${name} values''')
+    enum_doc_value_template = string.Template(''' * \\var ${name}Values::${value}
+${description}''')
     doc_template = string.Template('''/**
  * \\var extern const Control<${type}> ${name}
 ${description}
@@ -31,17 +42,30 @@ ${description}
         name, ctrl = ctrl.popitem()
         id_name = snake_case(name).upper()
 
-        description = ctrl['description'].strip('\n').split('\n')
-        description[0] = '\\brief ' + description[0]
-        description = '\n'.join([(line and ' * ' or ' *') + line for line in description])
-
         info = {
             'name': name,
             'type': ctrl['type'],
-            'description': description,
+            'description': format_description(ctrl['description']),
             'id_name': id_name,
         }
 
+        enum = ctrl.get('enum')
+        if enum:
+            enum_doc = []
+            enum_doc.append(enum_doc_start_template.substitute(info))
+
+            for value in enum:
+                value_info = {
+                    'name' : name,
+                    'value': list(value.keys())[0],
+                    'description': format_description(value['description']),
+                }
+                enum_doc.append(enum_doc_value_template.substitute(value_info))
+
+            enum_doc = '\n *\n'.join(enum_doc)
+            enum_doc += '\n */'
+            ctrls_doc.append(enum_doc)
+
         ctrls_doc.append(doc_template.substitute(info))
         ctrls_def.append(def_template.substitute(info))
         ctrls_map.append('\t{ ' + id_name + ', &' + name + ' },')
@@ -54,6 +78,8 @@ ${description}
 
 
 def generate_h(controls):
+    enum_template_start = string.Template('''enum ${name}Values {''')
+    enum_value_template = string.Template('''\t${name} = ${value},''')
     template = string.Template('''extern const Control<${type}> ${name};''')
 
     ctrls = []
@@ -71,6 +97,18 @@ def generate_h(controls):
             'type': ctrl['type'],
         }
 
+        enum = ctrl.get('enum')
+        if enum:
+            ctrls.append(enum_template_start.substitute(info))
+
+            for value in enum:
+                value_info = {
+                    'name': list(value.keys())[0],
+                    'value': value['value'],
+                }
+                ctrls.append(enum_value_template.substitute(value_info))
+            ctrls.append("};")
+
         ctrls.append(template.substitute(info))
         id_value += 1
 
-- 
2.24.0



More information about the libcamera-devel mailing list