-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtextview.py
executable file
·85 lines (73 loc) · 2.47 KB
/
textview.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
import threading
import time
from PIL import Image
from pixmodule import PixModule
class TextView(PixModule):
text = "Hello YouTube! "
font = "/home/pi/pix-table/pixmodules/font.gif"
# image file for font
fontData = None
stringMap = None
textPosition = 0
textPositionCounter = 0
fontColor = [200,200,200]
backgroundColor = [27,68,29]
def __init__(self, spiDevice, base=None):
super(TextView,self).__init__(spiDevice)
fontMap = Image.open(self.font)
fontMap = fontMap.convert("RGBA")
self.fontData = fontMap.getdata()
self.stringMap = self.getStringMap(self.text)
def render(self):
self.textPositionCounter += 1
if self.textPositionCounter > 15:
self.textPositionCounter = 0
self.textPosition += 1
self.rollOutPixMap()
def rollOutPixMap(self):
#self.displayLetterByLetter()
self.displayRunningText()
pass
# merges two lettern to a single to get running effect (OK)
def displayRunningText(self):
letterPosition = self.textPosition % (len(self.stringMap)*4)
absoluteLetterPosition = letterPosition % 4
firstLetter = self.stringMap[int(letterPosition/4)]
secondLetter = self.stringMap[((int(letterPosition / 4)) +1) % len(self.stringMap)]
mergeLetter = []
for i in range(20):
mergeLetter.insert(i+(int(i/4)*4),firstLetter[i])
mergeLetter.insert(i+4+(int(i/4)*4),secondLetter[i])
self.pixels = [[self.backgroundColor for x in range(5)] for x in range(5)]
for i in range(25):
if mergeLetter[(i%5)+(i/5)*8+absoluteLetterPosition] == True:
self.pixels[4-int(i/5)][4-i%5] = self.fontColor
self.correctPixView()
# display text in seperated letters (OK)
def displayLetterByLetter(self):
letterMap = self.stringMap[(self.textPosition % len(self.stringMap))]
self.pixels = [[self.backgroundColor for x in range(5)] for x in range(5)]
index = 0
for pixel in letterMap:
if pixel == True:
self.pixels[4-int(index/4)][index%4] = self.fontColor
index+=1
self.correctPixView()
# array of 5x4 letter arrays (OK)
def getStringMap(self, string):
stringMap = []
for char in string:
letterMap = self.getLetterMap(char)
stringMap.append(letterMap)
return stringMap
# 5x4 array (20 elements) for one character (OK)
def getLetterMap(self,c):
number = ord(c)
data = []
for letter in range(20):
x = (number * 4) % (16 * 4) + (letter % 4)
y = (int(number / 16) * 16 * 4 * 6) + int(letter / 4) * 64
data.append((True if self.fontData[x+y][0] < 100 else False))
return data
def getColor(self):
return [27,68,29]