[libcamera-devel] [PATCH v5 3/4] libcamera: controls: Generate fixed- and variable-sized Span Controls

Jacopo Mondi jacopo at jmondi.org
Fri Jun 3 08:49:01 CEST 2022


Hi Christian

On Thu, Jun 02, 2022 at 12:18:01AM +0100, Christian Rauch via libcamera-devel wrote:
> This defines Controls with a 'size' as either variable-sized Span<T> or as
> fixed-sized Span<T,N>.
>
> Signed-off-by: Christian Rauch <Rauch.Christian at gmx.de>
> ---
>  utils/gen-controls.py | 32 ++++++++++++++++++++++----------
>  1 file changed, 22 insertions(+), 10 deletions(-)
>
> diff --git a/utils/gen-controls.py b/utils/gen-controls.py
> index 3f99b5e2..c9e79a22 100755
> --- a/utils/gen-controls.py
> +++ b/utils/gen-controls.py
> @@ -7,6 +7,7 @@
>  # gen-controls.py - Generate control definitions from YAML
>
>  import argparse
> +import math
>  import string
>  import sys
>  import yaml
> @@ -22,6 +23,25 @@ def format_description(description):
>      return '\n'.join([(line and ' * ' or ' *') + line for line in description])
>
>
> +def get_ctrl_type(ctrl):
> +    ctrl_type = ctrl['type']
> +    ctrl_size_arr = ctrl.get('size')
> +    ctrl_is_span = ctrl_size_arr is not None
> +
> +    if ctrl_type == 'string':
> +        return 'std::string'
> +    elif ctrl_is_span:
> +        ctrl_span_size = math.prod(ctrl_size_arr) if len(ctrl_size_arr) > 0 else None
> +        if ctrl_span_size:
> +            # fixed-sized Span
> +            return f"Span<const {ctrl_type}, {ctrl_span_size}>"
> +        else:
> +            # variable-sized Span
> +            return f"Span<const {ctrl_type}>"
> +    else:
> +        return ctrl_type

You can save a few indentation levels with

def get_ctrl_type(ctrl):
    ctrl_type = ctrl['type']
    ctrl_size_arr = ctrl.get('size')

    if ctrl_type == 'string':
        return 'std::string'

    if ctrl_size_arr is not None:
        if len(ctrl_size_arr) > 0:
            # fixed-sized Span
            ctrl_span_size = math.prod(ctrl_size_arr)
            return f"Span<const {ctrl_type}, {ctrl_span_size}>"
        else:
            # variable-sized Span
            return f"Span<const {ctrl_type}>"

    return ctrl_type

As you prefer.

Reviewed-by: Jacopo Mondi <jacopo at jmondi.org>

Thanks
   j

> +
> +
>  def generate_cpp(controls):
>      enum_doc_start_template = string.Template('''/**
>   * \\enum ${name}Enum
> @@ -50,11 +70,7 @@ ${description}
>          name, ctrl = ctrl.popitem()
>          id_name = snake_case(name).upper()
>
> -        ctrl_type = ctrl['type']
> -        if ctrl_type == 'string':
> -            ctrl_type = 'std::string'
> -        elif ctrl.get('size'):
> -            ctrl_type = 'Span<const %s>' % ctrl_type
> +        ctrl_type = get_ctrl_type(ctrl)
>
>          info = {
>              'name': name,
> @@ -135,11 +151,7 @@ def generate_h(controls):
>
>          ids.append('\t' + id_name + ' = ' + str(id_value) + ',')
>
> -        ctrl_type = ctrl['type']
> -        if ctrl_type == 'string':
> -            ctrl_type = 'std::string'
> -        elif ctrl.get('size'):
> -            ctrl_type = 'Span<const %s>' % ctrl_type
> +        ctrl_type = get_ctrl_type(ctrl)
>
>          info = {
>              'name': name,
> --
> 2.34.1
>


More information about the libcamera-devel mailing list