-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_display.py
99 lines (88 loc) · 2.18 KB
/
serial_display.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import serial
import sys
from Tkinter import Tk, Canvas, PhotoImage
import time
class SerialDisplay:
WIDTH = 128
HEIGHT = 128
cury = 0
state = 0
def __init__(self, tty, baudrate, timeo):
self.stop = False
self.initUI()
self.ser = serial.Serial(tty, baudrate, timeout=timeo)
self.ser.write("N")
self.last = False
def loop(self, ):
while (True):
line = self.ser.readline()
if not line:
return
self.parseLine(line)
def closeSerial(self):
self.ser.close()
def parseLine(self, line):
if ("START" in line):
self.cury = 0
print("START")
self.state = 1
elif ("END" in line):
print("END")
time.sleep(2)
self.ser.write("N")
self.state = 0
elif (line[0] == '>'):
self.last = True
self.parseBinaryLine(line[1:])
#print(pixels)
if self.cury < self.HEIGHT:
self.cury += 1
self.window.update()
#if (self.cury % 2 == 0):
# self.ser.write("N")
else:
print(line);
def parseBinaryLine(self, line):
# First xor with 0xAA to get rid of most 0's, since most of the
# picture will probably be empty
# byte ^= 0xAA;
# Escape null bytes and escape bytes
# if (byte == 0 || byte == 0x7D) {
# // Write escape byte
# *bufPtr++ = 0x7d;
# *bufPtr++ = byte ^ 0xBB;
# }
# *bufPtr++ = byte;
# byte = 0;
curx = 0
escape = False;
for char in line:
char = ord(char)
if char == 0x0D:
return
# Check if character is escape character
if char == 0x7D and not escape:
escape = True
continue
if escape:
escape = False
# XOR data with 0xBB
char ^= 0xBB
# Finally XOR data with 0xAA to get real data
char ^= 0xAA
for i in range(0, 8):
if char & (1 << i):
self.img.put("#ffffff", (curx,self.cury))
else:
self.img.put("#000000", (curx,self.cury))
curx += 1
def initUI(self):
self.window = Tk()
self.canvas = Canvas(self.window, width=self.WIDTH, height=self.HEIGHT, bg="#000000")
self.canvas.pack()
self.img = PhotoImage(width=self.WIDTH, height=self.HEIGHT)
self.canvas.create_image((self.WIDTH/2, self.HEIGHT/2), image=self.img, state="normal")
print("start")
sd = SerialDisplay('/dev/ttyACM0', 460800, 5)
sd.loop()
print('Done!')