-
Notifications
You must be signed in to change notification settings - Fork 0
/
piDisplayLine.py
130 lines (110 loc) · 4.38 KB
/
piDisplayLine.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import threading
class piDisplayLine:
scrollspeed = 8
lineno = 0
line = ""
lineb = ""
linescrolled = ""
scrollcount = -1
linerest = ""
lineOriginal = ""
lineTempPeriod = 0
updatedLine = ""
def __init__(self, number):
print "Create displayline:", number
self.lineno = number
self.lineQ = []
self.special = ['', '', '', '', '', '']
self.lock = threading.Lock()
def setSpecial(self, number, chari):
self.special[number] = chari
def rmSpecial(self, number):
self.special[number] = ''
def specialSize(self):
size = 0
for chari in self.special:
if(chari):
size += 1
return size
def getLock(self):
self.lock.acquire()
def unLock(self):
self.lock.release()
def process(self):
try:
self.getLock()
# If tempperiod then the line we are showing is temporary
if(self.lineTempPeriod > 0):
self.lineTempPeriod -= 1
if(self.lineTempPeriod == 0):
# temp period expired
if(self.lineQ):
nextline = self.lineQ.pop(0)
orig = self.lineOriginal
self.setlinePeriodDo(nextline[0],
nextline[1])
self.lineOriginal = orig
else:
self.setlinePeriodDo(self.lineOriginal,
0)
if(self.scrollcount >= 0):
self.scrollcount += 1
if(self.scrollcount > self.scrollspeed):
# Time to scroll
if len(self.linerest) > 0:
self.scrollcount = 0
self.line = self.line[1:] + self.linerest[:1]
self.linerest = self.linerest[1:]
else:
self.line = self.linescrolled
self.scrollcount = -1
sSize = self.specialSize()
if len(self.line) > 16 - sSize:
# needs to scroll
self.linescrolled = self.line[:16 - sSize]
self.linerest = self.line[16 - sSize:]
self.line = self.line[:16 - sSize]
self.scrollcount = 0
myline = self.getline()
updated = self.lineb != myline
self.lineb = myline
finally:
self.unLock()
return updated
def getline(self):
myline = ""
for chari in self.special:
if(chari):
myline += chari
myline += self.line
retv = myline.ljust(16)
return myline.ljust(16)
def clearq(self):
self.lineQ = []
self.lineTempPeriod = 0
self.setline(self.lineOriginal)
def setline(self, line):
self.setlinePeriod(line, 0)
def setlinePeriod(self, line, tempPeriod):
try:
self.getLock()
self.setlinePeriodDo(line, tempPeriod)
finally:
self.unLock()
def setlinePeriodDo(self, line, tempPeriod):
print("%d:setlineperiod:%s,%d:%s current:%d" % (self.lineno, line,
tempPeriod, self.lineQ, self.lineTempPeriod))
if(tempPeriod == 0):
# Do now, clear queue
self.lineTempPeriod = 0
if(self.lineTempPeriod == 0):
print "Set line:", line
self.lineOriginal = self.line
self.lineTempPeriod = tempPeriod
self.line = line
self.linescrolled = ""
self.linerest = ""
self.scrollcount = -1
else:
print "Q line:" + line + ",", tempPeriod
self.lineQ.append((line, tempPeriod))