px4-firmware/Tools/px_generate_params.py

101 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
import xml.etree.ElementTree as ET
import os
if len(os.sys.argv) != 2:
2015-10-06 16:48:13 -03:00
print("Error in %s" % os.sys.argv[0])
print("Usage: %s <parameters.xml>" % os.sys.argv[0])
raise SystemExit
fp_header = open("px4_parameters.h", "w")
fp_src = open("px4_parameters.c", "w")
tree = ET.parse(os.sys.argv[1])
root = tree.getroot()
# Generate the header file content
header = """
#include <stdint.h>
#include <systemlib/param/param.h>
// DO NOT EDIT
// This file is autogenerated from parameters.xml
__BEGIN_DECLS
struct px4_parameters_t {
"""
start_name = ""
end_name = ""
for group in root:
if group.tag == "group" and "no_code_generation" not in group.attrib:
header += """
/*****************************************************************
* %s
****************************************************************/""" % group.attrib["name"]
for param in group:
if not start_name:
start_name = param.attrib["name"]
end_name = param.attrib["name"]
header += """
const struct param_info_s __param__%s;""" % param.attrib["name"]
header += """
const unsigned int param_count;
};
extern const struct px4_parameters_t px4_parameters;
"""
# Generate the C file content
src = """
#include <px4_parameters.h>
// DO NOT EDIT
// This file is autogenerated from paramaters.xml
const
#ifndef __PX4_DARWIN
__attribute__((used, section("__param")))
#endif
struct px4_parameters_t px4_parameters = {
"""
i=0
for group in root:
if group.tag == "group" and "no_code_generation" not in group.attrib:
src += """
/*****************************************************************
* %s
****************************************************************/""" % group.attrib["name"]
for param in group:
if not start_name:
start_name = param.attrib["name"]
end_name = param.attrib["name"]
val_str = "#error UNKNOWN PARAM TYPE, FIX px_generate_params.py"
if (param.attrib["type"] == "FLOAT"):
val_str = ".val.f = "
elif (param.attrib["type"] == "INT32"):
val_str = ".val.i = "
i+=1
src += """
{
"%s",
PARAM_TYPE_%s,
%s%s
},
""" % (param.attrib["name"], param.attrib["type"], val_str, param.attrib["default"])
src += """
%d
};
//extern const struct px4_parameters_t px4_parameters;
__END_DECLS
""" % i
fp_header.write(header)
fp_src.write(src)