From 93dcec63ec538726e8c3bc5fd6c7cabc4d10a3ed Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Wed, 17 Jan 2024 17:02:08 +1100 Subject: [PATCH] mavgen_python: make enumerations MavEnum instances ... instead of just bare dictionaries ... and add an is_bitmask method to them --- generator/mavgen_python.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/generator/mavgen_python.py b/generator/mavgen_python.py index 750061f28..b75eeda5a 100644 --- a/generator/mavgen_python.py +++ b/generator/mavgen_python.py @@ -44,6 +44,8 @@ def extend_with_type_info(extended, enable_type_annotations): "mavlink_message_signed_callback": ('Callable[["MAVLink", int], bool]', None), "dict_str_to_str_float_int": ("Dict[str, Union[str, float, int]]", None), "dict_str_to_dict_int_to_enumentry": ("Dict[str, Dict[int, EnumEntry]]", None), + "dict_int_to_enumentry": ("Dict[int, EnumEntry]", None), + "dict_str_to_MavEnum": ("Dict[str, MavEnum]", None), "dict_int_to_str": ("Dict[int, str]", None), "dict_str_to_str": ("Dict[str, str]", None), "dict_int_int_int_to_int": ("Dict[Tuple[int, int, int], int]", None), @@ -449,6 +451,14 @@ def generate_enums(outf, enums, enable_type_annotations): # enums +class MavEnum(${type_dict_int_to_enumentry_cast}): + def __init__(self, bitmask${type_bool_default})${type_none_ret}: + super(MavEnum, self).__init__() + self.bitmask = bitmask + + def is_bitmask(self)${type_bool_ret}: + return self.bitmask + class EnumEntry(object): def __init__(self, name${type_str}, description${type_str})${type_none_ret}: self.name = name @@ -457,14 +467,17 @@ def __init__(self, name${type_str}, description${type_str})${type_none_ret}: self.has_location = False -enums${type_dict_str_to_dict_int_to_enumentry} = {} +enums${type_dict_str_to_MavEnum} = {} """, type_info, ) for e in enums: outf.write("\n# %s\n" % e.name) - outf.write('enums["%s"] = {}\n' % e.name) + mavenum_kwargs = "" + if bool(e.bitmask): + mavenum_kwargs = "bitmask=True" + outf.write('enums["%s"] = MavEnum(%s)\n' % (e.name, mavenum_kwargs)) for entry in e.entry: outf.write("%s = %u\n" % (entry.name, entry.value)) description = entry.description.replace("\t", " ")