utils: codegen: gen-controls.py: Move helper classes to separate file
The ControlEnum and Control helper classes defined in gen-controls.py are useful for other generator scripts. Move them to a separate file to make it possible to share them. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
This commit is contained in:
parent
dc067c4bce
commit
6a96113107
4 changed files with 116 additions and 105 deletions
|
@ -88,6 +88,7 @@ foreach mode, entry : controls_map
|
||||||
command : [gen_controls, '-o', '@OUTPUT@',
|
command : [gen_controls, '-o', '@OUTPUT@',
|
||||||
'--mode', mode, '-t', template_file,
|
'--mode', mode, '-t', template_file,
|
||||||
'-r', ranges_file, '@INPUT@'],
|
'-r', ranges_file, '@INPUT@'],
|
||||||
|
env : py_build_env,
|
||||||
install : true,
|
install : true,
|
||||||
install_dir : libcamera_headers_install_dir)
|
install_dir : libcamera_headers_install_dir)
|
||||||
endforeach
|
endforeach
|
||||||
|
|
|
@ -151,7 +151,8 @@ foreach mode, inout_files : controls_mode_files
|
||||||
output : output_file,
|
output : output_file,
|
||||||
command : [gen_controls, '-o', '@OUTPUT@',
|
command : [gen_controls, '-o', '@OUTPUT@',
|
||||||
'--mode', mode, '-t', template_file,
|
'--mode', mode, '-t', template_file,
|
||||||
'-r', ranges_file, '@INPUT@'])
|
'-r', ranges_file, '@INPUT@'],
|
||||||
|
env : py_build_env)
|
||||||
endforeach
|
endforeach
|
||||||
|
|
||||||
libcamera_public_sources += control_sources
|
libcamera_public_sources += control_sources
|
||||||
|
|
112
utils/codegen/controls.py
Normal file
112
utils/codegen/controls.py
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
# Copyright (C) 2019, Google Inc.
|
||||||
|
#
|
||||||
|
# Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
|
||||||
|
#
|
||||||
|
# Helper classes to handle source code generation for libcamera controls
|
||||||
|
|
||||||
|
|
||||||
|
class ControlEnum(object):
|
||||||
|
def __init__(self, data):
|
||||||
|
self.__data = data
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self):
|
||||||
|
"""The enum description"""
|
||||||
|
return self.__data.get('description')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
"""The enum name"""
|
||||||
|
return self.__data.get('name')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def value(self):
|
||||||
|
"""The enum value"""
|
||||||
|
return self.__data.get('value')
|
||||||
|
|
||||||
|
|
||||||
|
class Control(object):
|
||||||
|
def __init__(self, name, data, vendor):
|
||||||
|
self.__name = name
|
||||||
|
self.__data = data
|
||||||
|
self.__enum_values = None
|
||||||
|
self.__size = None
|
||||||
|
self.__vendor = vendor
|
||||||
|
|
||||||
|
enum_values = data.get('enum')
|
||||||
|
if enum_values is not None:
|
||||||
|
self.__enum_values = [ControlEnum(enum) for enum in enum_values]
|
||||||
|
|
||||||
|
size = self.__data.get('size')
|
||||||
|
if size is not None:
|
||||||
|
if len(size) == 0:
|
||||||
|
raise RuntimeError(f'Control `{self.__name}` size must have at least one dimension')
|
||||||
|
|
||||||
|
# Compute the total number of elements in the array. If any of the
|
||||||
|
# array dimension is a string, the array is variable-sized.
|
||||||
|
num_elems = 1
|
||||||
|
for dim in size:
|
||||||
|
if type(dim) is str:
|
||||||
|
num_elems = 0
|
||||||
|
break
|
||||||
|
|
||||||
|
dim = int(dim)
|
||||||
|
if dim <= 0:
|
||||||
|
raise RuntimeError(f'Control `{self.__name}` size must have positive values only')
|
||||||
|
|
||||||
|
num_elems *= dim
|
||||||
|
|
||||||
|
self.__size = num_elems
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self):
|
||||||
|
"""The control description"""
|
||||||
|
return self.__data.get('description')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def enum_values(self):
|
||||||
|
"""The enum values, if the control is an enumeration"""
|
||||||
|
if self.__enum_values is None:
|
||||||
|
return
|
||||||
|
for enum in self.__enum_values:
|
||||||
|
yield enum
|
||||||
|
|
||||||
|
@property
|
||||||
|
def enum_values_count(self):
|
||||||
|
"""The number of enum values, if the control is an enumeration"""
|
||||||
|
if self.__enum_values is None:
|
||||||
|
return 0
|
||||||
|
return len(self.__enum_values)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_enum(self):
|
||||||
|
"""Is the control an enumeration"""
|
||||||
|
return self.__enum_values is not None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def vendor(self):
|
||||||
|
"""The vendor string, or None"""
|
||||||
|
return self.__vendor
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
"""The control name (CamelCase)"""
|
||||||
|
return self.__name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def type(self):
|
||||||
|
typ = self.__data.get('type')
|
||||||
|
size = self.__data.get('size')
|
||||||
|
|
||||||
|
if typ == 'string':
|
||||||
|
return 'std::string'
|
||||||
|
|
||||||
|
if self.__size is None:
|
||||||
|
return typ
|
||||||
|
|
||||||
|
if self.__size:
|
||||||
|
return f"Span<const {typ}, {self.__size}>"
|
||||||
|
else:
|
||||||
|
return f"Span<const {typ}>"
|
|
@ -12,110 +12,7 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
from controls import Control
|
||||||
class ControlEnum(object):
|
|
||||||
def __init__(self, data):
|
|
||||||
self.__data = data
|
|
||||||
|
|
||||||
@property
|
|
||||||
def description(self):
|
|
||||||
"""The enum description"""
|
|
||||||
return self.__data.get('description')
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""The enum name"""
|
|
||||||
return self.__data.get('name')
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
"""The enum value"""
|
|
||||||
return self.__data.get('value')
|
|
||||||
|
|
||||||
|
|
||||||
class Control(object):
|
|
||||||
def __init__(self, name, data, vendor):
|
|
||||||
self.__name = name
|
|
||||||
self.__data = data
|
|
||||||
self.__enum_values = None
|
|
||||||
self.__size = None
|
|
||||||
self.__vendor = vendor
|
|
||||||
|
|
||||||
enum_values = data.get('enum')
|
|
||||||
if enum_values is not None:
|
|
||||||
self.__enum_values = [ControlEnum(enum) for enum in enum_values]
|
|
||||||
|
|
||||||
size = self.__data.get('size')
|
|
||||||
if size is not None:
|
|
||||||
if len(size) == 0:
|
|
||||||
raise RuntimeError(f'Control `{self.__name}` size must have at least one dimension')
|
|
||||||
|
|
||||||
# Compute the total number of elements in the array. If any of the
|
|
||||||
# array dimension is a string, the array is variable-sized.
|
|
||||||
num_elems = 1
|
|
||||||
for dim in size:
|
|
||||||
if type(dim) is str:
|
|
||||||
num_elems = 0
|
|
||||||
break
|
|
||||||
|
|
||||||
dim = int(dim)
|
|
||||||
if dim <= 0:
|
|
||||||
raise RuntimeError(f'Control `{self.__name}` size must have positive values only')
|
|
||||||
|
|
||||||
num_elems *= dim
|
|
||||||
|
|
||||||
self.__size = num_elems
|
|
||||||
|
|
||||||
@property
|
|
||||||
def description(self):
|
|
||||||
"""The control description"""
|
|
||||||
return self.__data.get('description')
|
|
||||||
|
|
||||||
@property
|
|
||||||
def enum_values(self):
|
|
||||||
"""The enum values, if the control is an enumeration"""
|
|
||||||
if self.__enum_values is None:
|
|
||||||
return
|
|
||||||
for enum in self.__enum_values:
|
|
||||||
yield enum
|
|
||||||
|
|
||||||
@property
|
|
||||||
def enum_values_count(self):
|
|
||||||
"""The number of enum values, if the control is an enumeration"""
|
|
||||||
if self.__enum_values is None:
|
|
||||||
return 0
|
|
||||||
return len(self.__enum_values)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_enum(self):
|
|
||||||
"""Is the control an enumeration"""
|
|
||||||
return self.__enum_values is not None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def vendor(self):
|
|
||||||
"""The vendor string, or None"""
|
|
||||||
return self.__vendor
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""The control name (CamelCase)"""
|
|
||||||
return self.__name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def type(self):
|
|
||||||
typ = self.__data.get('type')
|
|
||||||
size = self.__data.get('size')
|
|
||||||
|
|
||||||
if typ == 'string':
|
|
||||||
return 'std::string'
|
|
||||||
|
|
||||||
if self.__size is None:
|
|
||||||
return typ
|
|
||||||
|
|
||||||
if self.__size:
|
|
||||||
return f"Span<const {typ}, {self.__size}>"
|
|
||||||
else:
|
|
||||||
return f"Span<const {typ}>"
|
|
||||||
|
|
||||||
|
|
||||||
def snake_case(s):
|
def snake_case(s):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue