-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitmap2screen.py
executable file
·57 lines (47 loc) · 1.68 KB
/
bitmap2screen.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
#!/usr/bin/env python
#this code is WTFPL, please reuse it, improve it and share it as much as you like and pay me a beer !
#(c)2011 guyzmo at leloop dot org
#
# /!\ Still not tested on the wild /!\
import sys, serial, fcntl, os
def send_to_serial(serial, baudrate, infile):
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
with serial.Serial(serial, baudrate) as ser:
print ser.readline()
# set binary mode
ser.write(bytearray([27,'0']))
print ser.readline()
ser.flush()
for pixel in matrix_adapt(infile):
ser.write(pixel)
print ser.read()
ser.flush()
def clumper(s, count=4):
"""http://stackoverflow.com/questions/5676302/taking-four-items-instead-of-on-in-a-for-loop"""
for x in range(0, len(s), count):
yield s[x:x+count]
def matrix_adapt(file):
with open(file,'r') as infile:
lines = infile.readlines()
dotmat = []
for disp in clumper(lines,8*96):
disp.reverse()
for cols in clumper(disp,8):
cols.reverse()
for col in zip(*cols):
for pixel in col:
if pixel == '\n':
continue
if pixel == ' ' or pixel == '0':
dotmat += [0]
else:
dotmat += [1]
dotmat.reverse()
return dotmat
if __name__ == '__main__':
if len(sys.argv) != 4:
print "Usage: bitmap2screen /dev/ttyUSB0 9600 file.bitmap"
exit(1)
send_to_serial(sys.argv[1],sys.argv[2],sys.argv[3])