Skip to content

Commit

Permalink
mavgen_python: make enumerations MavEnum instances
Browse files Browse the repository at this point in the history
... instead of just bare dictionaries

... and add an is_bitmask method to them
  • Loading branch information
peterbarker committed Jan 18, 2024
1 parent f2bfe88 commit 93dcec6
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions generator/mavgen_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand All @@ -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", " ")
Expand Down

0 comments on commit 93dcec6

Please sign in to comment.