/
usb_serial.py
76 lines (59 loc) · 2.06 KB
/
usb_serial.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
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
import serial
import time
ser = serial.Serial('/dev/tty.usbserial-DPE0BNVH')
i = 0
print 'Sending image...'
if False:
t = 0
switchColor = True
for y2 in range(0, 12):
switchColor = not switchColor
for y in range(0, 64):
for checker in range(0, 16):
for x in range(0, 64):
if switchColor:
if ((checker%2) == 0):
bitstring = '11111100'
else:
bitstring = '00111100'
else:
if ((checker%2) == 0):
bitstring = '00111100'
else:
bitstring = '11111100'
#print 'Writing %s, %s' % (bitstring, x)
byteval = chr(int(bitstring, 2))
ser.write(byteval)
time.sleep(0.00001)
t += 1
print "Sent",t,"pixels"
ser.close()
if True:
f = open('matlab/bono_pixels.coe')
f.readline()
f.readline()
while True:
## example of sending binary data (i.e. as read from a coe file)
if i == 784643:
i = 0
else:
i += 1
img_byte = f.readline()[:-3]
#print 'Sending line #%s %s' % (i, img_byte)
bitstring = img_byte
#bitstring = '00000000'
#bitstring = '11110000'
byteval = chr(int(bitstring,2))
#raw_input('Press enter to send "%s" which has byteval %s' % (bitstring, repr(byteval))) ## need to use repr for printing bytes
ser.write(byteval)
#time.sleep(0.5)
time.sleep(0.00001)
ser.close()
if False:
for i in range(0, 512):
bitstring = bin(i%256)
byteval = chr(int(bitstring,2))
ser.write(byteval)
time.sleep(0.00001)
ser.close()