Hi Paul,
Thank you for your reply.
I would like to go with option 2 because we do not depend on GUI for anything. Also, How to load the input ewu file into EW before compilation?
Below is my sample script to create a ew file you suggested above. Could you tell me how to load this script via ew before start compilation.
Script:
import re
import os
def parse_enum_and_macros(c_file_path):
"""
Parses the given C file to extract enum definitions and #define macros.
:param c_file_path: The path to the C file.
:return: A dictionary with enum names and their values, and a dictionary of macros.
"""
enums = {}
macros = {}
# Define regex patterns for detecting enum declarations and #define macros
enum_pattern = r'enum\s+(\w+)\s*\{([^}]*)\}'
define_pattern = r'#define\s+(\w+)\s+([^/]+)'
try:
with open(c_file_path, 'r') as file:
content = file.read()
# Find all enum definitions using regex
enum_matches = re.findall(enum_pattern, content)
for enum_name, enum_values in enum_matches:
values = [value.strip() for value in enum_values.split(',') if value.strip()]
enum_dict = {}
for value in values:
# Handle the case where values like `DEVICE_OFF` are present
value_parts = value.split('=')
if len(value_parts) == 2:
enum_dict[value_parts[0].strip()] = int(value_parts[1].strip())
else:
enum_dict[value_parts[0].strip()] = None # Assign None if no value is specified
enums[enum_name] = enum_dict
# Find all #define macros using regex
define_matches = re.findall(define_pattern, content)
for macro_name, macro_value in define_matches:
macros[macro_name] = macro_value.strip()
except FileNotFoundError:
print(f"Error: File '{c_file_path}' not found.")
except Exception as e:
print(f"Error: {e}")
return enums, macros
def generate_embedded_file(enums, macros, output_file_path):
"""
Generates an embedded C header file from the extracted enums and macros.
:param enums: The dictionary containing enums to write to file.
:param macros: The dictionary containing macros to write to file.
:param output_file_path: Path to the output embedded file (header or source file).
"""
with open(output_file_path, 'w') as file:
file.write("#ifndef DEVICE_CONFIG_H\n")
file.write("#define DEVICE_CONFIG_H\n\n")
# Generate enum data as embedded C structures
for enum_name, values in enums.items():
file.write(f"typedef enum {{\n")
for enum_value, enum_int in values.items():
if enum_int is not None:
file.write(f" {enum_value} = {enum_int},\n")
else:
file.write(f" {enum_value},\n")
file.write(f"}} {enum_name};\n\n")
# Generate #define macros
for macro_name, macro_value in macros.items():
file.write(f"#define {macro_name} {macro_value}\n")
file.write("\n#endif // DEVICE_CONFIG_H\n")
print(f"Embedded file generated at: {output_file_path}")
def main():
c_file_path = "example.c" # Path to your C file
output_file_path = "device_config.h" # Path for the output header file
enums, macros = parse_enum_and_macros(c_file_path)
if enums or macros:
generate_embedded_file(enums, macros, output_file_path)
if __name__ == "__main__":
main()