forked from bertrandom/snowball-thrower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin2c.py
51 lines (38 loc) · 1.14 KB
/
bin2c.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
#!/bin/python
import sys, getopt
def main(argv):
opts, args = getopt.getopt(argv, "hi")
invertColormap = False
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt == '-i':
invertColormap = True
data = open(args[0], 'rb').read()
str_out = "#include <stdint.h>\n#include <avr/pgmspace.h>\n\nconst uint8_t image_data[0x12c1] PROGMEM = {"
for i in range(0, (320*120) / 8):
val = 0;
for j in range(0, 8):
val |= ord(data[(i * 8) + j]) << j
if (invertColormap):
val = ~val & 0xFF;
else:
val = val & 0xFF;
str_out += hex(val) + ", "
str_out += "0x0};\n"
with open('image.c', 'w') as f:
f.write(str_out)
if (invertColormap):
print("{} converted with inverted colormap and saved to image.c".format(args[0]))
else:
print("{} converted with original colormap and saved to image.c".format(args[0]))
def usage():
print("To convert to image.c: bin2c.py yourImage.data")
print("To convert to an inverted image.c: bin2c.py -i yourImage.data")
if __name__ == "__main__":
if len(sys.argv[1:]) == 0:
usage()
sys.exit
else:
main(sys.argv[1:])