Browse Source

HAL_ChibiOS: make embed.py work with python3

master
Andrew Tridgell 7 years ago
parent
commit
77c922e6b8
  1. 27
      libraries/AP_HAL_ChibiOS/hwdef/scripts/embed.py

27
libraries/AP_HAL_ChibiOS/hwdef/scripts/embed.py

@ -9,16 +9,17 @@ May 2017 @@ -9,16 +9,17 @@ May 2017
import os
def write_encode(out, s):
out.write(s.encode())
def embed_file(out, f, idx):
'''embed one file'''
contents = open(f).read()
out.write('''
// %s
static const uint8_t ap_romfs_%u[] = {''' % (f, idx))
contents = open(f,'rb').read()
write_encode(out, 'static const uint8_t ap_romfs_%u[] = {' % idx)
for c in contents:
out.write('%u,' % ord(c))
out.write('};\n\n');
for c in bytearray(contents):
write_encode(out, '%u,' % c)
write_encode(out, '};\n\n');
def create_embedded_h(filename, files):
'''create a ap_romfs_embedded.h file'''
@ -26,21 +27,21 @@ def create_embedded_h(filename, files): @@ -26,21 +27,21 @@ def create_embedded_h(filename, files):
this_dir = os.path.realpath(__file__)
rootdir = os.path.relpath(os.path.join(this_dir, "../../../../.."))
out = open(filename, "w")
out.write('''// generated embedded files for AP_ROMFS\n\n''')
out = open(filename, "wb")
write_encode(out, '''// generated embedded files for AP_ROMFS\n\n''')
for i in range(len(files)):
(name, filename) = files[i]
filename = os.path.join(rootdir, filename)
embed_file(out, filename, i)
out.write('''const AP_ROMFS::embedded_file AP_ROMFS::files[] = {\n''')
write_encode(out, '''const AP_ROMFS::embedded_file AP_ROMFS::files[] = {\n''')
for i in range(len(files)):
(name, filename) = files[i]
print("Embedding file %s:%s" % (name, filename))
out.write('{ "%s", sizeof(ap_romfs_%u), ap_romfs_%u },\n' % (name, i, i))
out.write('};\n')
print(("Embedding file %s:%s" % (name, filename)).encode())
write_encode(out, '{ "%s", sizeof(ap_romfs_%u), ap_romfs_%u },\n' % (name, i, i))
write_encode(out, '};\n')
out.close()
if __name__ == '__main__':

Loading…
Cancel
Save