-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex2c.py
63 lines (52 loc) · 1.99 KB
/
hex2c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
import sys
import re
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-x", "--hexfile", help="Intel hex file")
parser.add_argument("-b", "--binfile", help="Binary file")
parser.add_argument("-p", "--presets", help="Preset numbers", nargs='+', type=int, default=[0, 1, 2, 3, 4, 5, 6, 7])
args = parser.parse_args()
if not args.hexfile and not args.binfile:
parser.print_usage()
sys.exit(1)
if args.hexfile:
print("Opening Intel HEX dump file `{}`".format(args.hexfile))
with open(args.hexfile, 'r') as hexfh:
lines = hexfh.readlines()
# decode intel hex format
# 1024 * 4 bytes
# 8 presets -> 512 bytes / 128 lines by presets
# :04 0000 00 80400011 2B
# 04 bytes
# address 0000
# record type 00 (data)
# data 80 40 00 11
# checksum
pattern = re.compile(r"^:04\w{4}00(\w{2})(\w{2})(\w{2})(\w{2})\w{2}\n$")
data = [[] for i in range(8)]
for preset in range(8):
for j in range(preset*128, (preset+1)*128):
match = pattern.search(lines[j])
if match:
data[preset].append("0x" + match.group(1))
data[preset].append("0x" + match.group(2))
data[preset].append("0x" + match.group(3))
data[preset].append("0x" + match.group(4))
else:
print("Warning line not decoded:\n{}".format(lines[j]))
else:
with open(args.binfile, 'rb') as binfh:
raw = binfh.read()
data = [[] for i in range(8)]
for preset in range(8):
for i in range(512):
data[preset].append("0x{:02X}".format(raw[preset*512 + i]))
for preset in args.presets:
print("Exporting preset {}\n".format(preset))
print("const uint8_t preset{}[] PROGMEM = {{".format(preset))
a = []
for i in range(32):
a.append(", ".join(data[preset][i*16:(i+1)*16]))
print(",\n".join(a))
print("};\n")