-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.py
106 lines (83 loc) · 2.83 KB
/
scroll.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
# this module handle a scrolling on a 8x8 led screen
import font
import time
#
# construct a char representation
#
def charSequence(char,color):
e = font.printChar(char)
return toLED(e,color)
def toLED(buffer, color):
""" take a buffer, color it and return the led buffer """
s = ""
for i in range(7,-1,-1):
for j in range(0,8):
b = (buffer[j][i:i+1]) == "X"
s = (s + color) if b else (s + chr(0)+chr(0) + chr(0))
return s
def shiftBufferLeft(buffertoshift,enteringelements,outelements):
assert type(buffertoshift) == list
assert type(enteringelements) == list
assert len(buffertoshift) == 8
assert len(enteringelements) == 8
assert type(outelements) == list
for i in range(0,8):
s = buffertoshift[i]
c = s[0:1]
r = s[1:8]
buffertoshift[i] = r + enteringelements[i]
outelements[i] = c
def newBuffer():
""" allocate a new buffer for screen """
buffer = []
for i in range(0,8):
buffer.append(" ")
return buffer
def colorToPixel(r,g,b):
return chr(r) + chr(g) + chr(b)
def interpolateColor(color,factor):
rmax = ord(color[0])
gmax = ord(color[1])
bmax = ord(color[2])
return chr(int(rmax * factor * factor)) + chr(int(gmax * factor * factor)) + chr(int(bmax * factor * factor ))
def fading(client, topic, char, color , r = 1):
c = font.printChar(char)
fadingBuffer(client, topic, c, color, r)
def fadingBuffer(client, topic,buffer, color , r = 1):
start = 0 if r == 1 else 9
end = 9 if r == 1 else 0
for i in range (start,end,r):
b = toLED(buffer, interpolateColor(color, i * 1.0 / 10.0))
client.publish(topic, b)
time.sleep(0.05)
def fadinfadout(client, topic, char, color ):
fadinfadoutBuffer(client, topic, font.printChar(char), color);
def fadinfadoutBuffer(client, topic, buffer , color ):
fadingBuffer(client, topic, buffer, color, 1)
time.sleep(1)
fadingBuffer(client, topic, buffer, color, -1)
def scroll(client, message, topic):
"""
client : clientmqtt
message : message to scroll
topic: topic on which we send the elements for scroll
"""
print("message to scroll :" + str(message))
buffer = []
for i in range(0,8):
buffer.append(" ")
displayed = [buffer]
for m in message:
displayed.append(font.printChar(ord(m)))
# print(displayed)
for i in range(0,8*(len(message) + 1)):
a=[" "," "," "," "," "," "," "," "]
for j in range(len(message),-1,-1):
b=[" "," "," "," "," "," "," "," "]
shiftBufferLeft(displayed[j],a,b)
a=b
#print(" display " + str(i))
#print(displayed)
m = toLED(displayed[0], colorToPixel(10,0,0))
client.publish(topic,m)
time.sleep(0.05)