Files
chest_strap/code/l452_code/descriptor_generator.py
2026-05-05 14:09:15 -05:00

58 lines
2.0 KiB
Python

import cxxheaderparser.simple
header_text = open("Core/Inc/packet.hpp","r").read().replace("__inline__", "")
data = cxxheaderparser.simple.parse_string(header_text)
work_string = ""
for class_type in data.namespace.classes:
class_name = class_type.class_decl.typename.segments[0].name
to_format_string = "%s %d %d"
format_args = f'"{class_name}", typecode<{class_name}>(), sizeof({class_name})'
for field in class_type.fields:
if isinstance(field.type, cxxheaderparser.types.Array):
element_type = field.type.array_of.typename.segments[0].name
element_num = field.type.size.tokens[0].value
element_name = field.name
to_format_string += " %s %d %d"
format_args += f', "{element_type} {element_name}[{element_num}]", offsetof({class_name}, {element_name}), sizeof({element_type})'
elif isinstance(field.type, cxxheaderparser.types.Type):
element_type = field.type.typename.segments[0].name
element_name = field.name
to_format_string += " %s %d %d"
format_args += f', "{element_type} {element_name}", offsetof({class_name}, {element_name}), sizeof({element_type})'
to_format_string += "\\n\\r"
work_string += f' cx = snprintf(buff, sizeof(buff), "{to_format_string}", {format_args});\n'
work_string += ' if (to_file) {unsigned int bw; f_write(&file, buff, cx, &bw);}\n'
work_string += ' if (to_usb) {CDC_Transmit_FS((uint8_t*)buff, cx);}\n'
work_string += f' HAL_Delay(50);\n\n'
h_file_text = """
void data_description(bool to_usb, bool to_file);
"""
c_file_text = ("""#include "datadescriptor.hpp"
#include <cstddef>
#include <cstdio>
#include "fatfs.h"
#include "usbd_cdc_if.h"
#include "packet.hpp"
extern FIL file;
void data_description(bool to_usb, bool to_file) {
char buff[200];
int cx;
""" + work_string +
"}\n")
with open("Core/Src/datadescriptor.cpp", "w") as f:
f.write(c_file_text)
with open("Core/Inc/datadescriptor.hpp", "w") as f:
f.write(h_file_text)