reflow-oven-control-sw/stm-firmware/updater/ram-code/bin2carray.py

35 lines
803 B
Python
Raw Permalink Normal View History

2020-12-07 21:39:07 +01:00
#!env python
# Convert a file to a c array
# bin2carray <output file> <input file>
2020-12-07 21:39:07 +01:00
import os
import sys
if len(sys.argv) < 3:
sys.exit(-1)
source_file = sys.argv[2]
dest_file = sys.argv[1]
print("%s --> %s" % (source_file, dest_file))
with open(source_file, "rb") as src:
data = src.read()
with open(dest_file, "w") as dest:
header_guard = "__" + 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)+",")
dest.write("};\n")
dest.write("#endif /* %s */\n" % (header_guard))
2020-12-07 21:39:07 +01:00
sys.exit(0)