-
Notifications
You must be signed in to change notification settings - Fork 0
/
flipdotfont.py
126 lines (107 loc) · 3.73 KB
/
flipdotfont.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
class Font:
"""
read a .bdf font file
letter() gets a character and returns a list of 8-bit-integers.
example 3x4 "T"
[0b11110000,
0b01100000,
0b01100000]
"""
def __init__(self, filename, width, height):
f = open(filename, 'r')
self.fontlist = f.readlines()
f.close()
self.width = width
self.height = height
def letter(self, l):
try:
index = self.fontlist.index('ENCODING '+str(ord(l))+'\n') + 5
except:
index = self.fontlist.index('ENCODING '+str(32)+'\n') + 5
letter = []
for i in range(self.height):
letter.append(int(self.fontlist[index+i], 16))
#print(bin(int(self.fontlist[index+i], 16)))
return letter
def small_font():
return Font("ressources/4x6.bdf", 4, 6)
def big_font():
return Font("ressources/clR6x12.bdf", 6, 12)
class TextScroller:
"""Write Text on Flipdotdisplays. A simple usage with a FlipDot-Simulator
is shown in the following.
>>> import flipdotfont
>>> import flipdotsim
>>> import time
>>> fds = flipdotsim.FlipDotSim(28, 13)
>>> t = flipdotfont.TextScroller(fds, "Hello world.",
... flipdotfont.big_font())
>>> for _ in range(20):
... t.scrolltext()
... fds.show()
... time.sleep(0.1)
"""
def __init__(self, flipdotdisplay, text, font):
self.fdd = flipdotdisplay
self.font = font
self.text = text
self.changetext(text)
self.x = 0
self.y = 0
self.statictext(self.text, (self.x, self.y))
def statictext(self, text, start=(0, 0)):
"""Show the given given text with the given font on the display."""
for l_index in range(len(text)):
letter = self.font.letter(text[l_index])
y1 = start[1]
y2 = y1 + self.font.height
x1 = start[0] + l_index * self.font.width
x2 = x1 + self.font.width
for x in range(max(x1, 0), min(x2, self.fdd.width)):
for y in range(max(y1, 0), min(y2, self.fdd.height)):
""" make the bit pattern of the letter row right-aligned """
letter_row = letter[y-y1] >> (7-self.font.width)
""" mask one bit of the pattern """
draw_dot = letter_row & (1<<(x2-x+1)) == (1<<(x2-x+1))
self.fdd.px(x, y, draw_dot)
def scrolltext(self, step=1):
"""Scroll the text (one step)."""
if abs(self.x) + step >= (len(self.text)//2) * self.font.width:
self.x = 0
else:
self.x = self.x-step
self.statictext(self.text, (self.x, self.y))
def changetext(self, text):
"""Change the text and add some spaces."""
self.text = text
spaces = max((self.fdd.width // self.font.width) - len(self.text), 0) + 1
self.text = self.text + ' '*spaces
self.text = self.text*2
def _clear(self):
for y in range(self.fdd.height):
for x in range(self.fdd.width):
self.fdd.px(x, y, False)
self.fdd.show()
def test_text_scroller():
import flipdotsim
import time
fdd = flipdotsim.FlipDotSim()
font = small_font()
tsc = TextScroller(fdd, "Hallo", font)
fdd.show()
time.sleep(0.5)
fdd.close()
def demo_text_lower():
print("starting text scoller")
import displayprovider
import time
fdd = displayprovider.get_display()
print("using display", fdd)
txt = TextScroller(fdd, "", small_font())
#time.sleep(1)
txt.statictext("labor", (0,8))
fdd.show()
#time.sleep(5)
if __name__ == "__main__":
#test_text_scroller()
demo_text_lower()