diff --git a/stm-firmware/updater/ram-code/bin2carray.py b/stm-firmware/updater/ram-code/bin2carray.py index c0f0ca3..124b118 100755 --- a/stm-firmware/updater/ram-code/bin2carray.py +++ b/stm-firmware/updater/ram-code/bin2carray.py @@ -1,35 +1,45 @@ #!env python -# Convert a file to a c array -# bin2carray +""" +Convert a file to a c array +bin2carray +""" import os import os.path import sys -if len(sys.argv) < 3: - sys.exit(-1) +def main(): + """ + Main script function + """ + if len(sys.argv) < 3: + return -1 -source_file = sys.argv[2] -dest_file = sys.argv[1] + source_file = sys.argv[2] + dest_file = sys.argv[1] -print("%s --> %s" % (source_file, dest_file)) + print(f'{source_file} --> {dest_file}') -with open(source_file, "rb") as src: - data = src.read() + with open(source_file, 'rb') as src: + data = src.read() -with open(dest_file, "w") as dest: - header_guard = "__" + os.path.basename(dest_file).replace('.', '_').replace('-', '_') + "_H__" - dest.write("#ifndef %s\n" % (header_guard)) - dest.write("#define %s\n" % (header_guard)) - dest.write("static const char binary_blob[%d] = {\n" % (len(data))) - for current,idx in zip(data, range(len(data))): - if ((idx+1) % 4 == 0): - dest.write(hex(current)+",\n") - else: - dest.write(hex(current)+",") + with open(dest_file, 'w', encoding='utf-8') as dest: + header_guard = '_' + os.path.basename(dest_file).replace('.', '_').replace('-', '_') + '_H_' + header_guard = header_guard.upper() + dest.write(f'#ifndef {header_guard}\n') + dest.write(f'#define {header_guard}\n') + dest.write(f'static const char binary_blob[{len(data)}] = {{\n') + for idx, current in enumerate(data, start=1): + if idx % 4 == 0: + dest.write(hex(current)+',\n') + else: + dest.write(hex(current)+',') - dest.write("};\n") - dest.write("#endif /* %s */\n" % (header_guard)) + dest.write('};\n') + dest.write(f'#endif /* {header_guard} */\n') -sys.exit(0) + return 0 + +if __name__ == '__main__': + sys.exit(main())