uORB generate message print functions

This commit is contained in:
Daniel Agar
2018-03-28 09:15:41 -04:00
parent 8d6bff08bb
commit 3db17a04fc
4 changed files with 96 additions and 2 deletions
@@ -68,6 +68,20 @@ msgtype_size_map = {
'char': 1,
}
type_printf_map = {
'int8': '%d',
'int16': '%d',
'int32': '%" PRId32 "',
'int64': '%" PRId64 "',
'uint8': '%u',
'uint16': '%u',
'uint32': '%" PRIu32 "',
'uint64': '%" PRIu64 "',
'float32': '%.3f',
'float64': '%.3f',
'bool': '%u',
'char': '%c',
}
def bare_name(msg_type):
"""
@@ -165,6 +179,70 @@ def convert_type(spec_type):
return c_type
def px4_printf(field):
"""
Echo printf line
"""
# skip padding
if ("_padding" in field.name):
return
bare_type = field.type
if '/' in field.type:
# removing prefix
bare_type = (bare_type.split('/'))[1]
msg_type, is_array, array_length = genmsg.msgs.parse_type(bare_type)
field_name = ""
if is_array:
c_type = "["
if msg_type in type_map:
p_type = type_printf_map[msg_type]
else:
for i in range(array_length):
print("printf(\" " + field.type + " " + field.name + "[" + str(i) + "]\");")
print(" print_message(message." + field.name + "[" + str(i) + "]);")
return
for i in range(array_length):
if i > 0:
c_type += ", "
field_name += ", "
if "float32" in field.type:
field_name += "(double)message." + field.name + "[" + str(i) + "]"
else:
field_name += "message." + field.name + "[" + str(i) + "]"
c_type += str(p_type)
c_type += "]"
else:
c_type = msg_type
if msg_type in type_map:
c_type = type_printf_map[msg_type]
field_name = "message." + field.name
# cast double
if field.type == "float32":
field_name = "(double)" + field_name
else:
print("printf(\" " + field.name + "\");")
print("\tprint_message(message."+ field.name + ");")
return
print("printf(\"\t" + field.name + ": " + c_type + "\\n\", " + field_name + ");" )
def print_field_def(field):
"""
Print the C type from a field