-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrollingtext.py
More file actions
70 lines (60 loc) · 2.27 KB
/
scrollingtext.py
File metadata and controls
70 lines (60 loc) · 2.27 KB
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
from lbdict import LetterBitmapDictionary
import os
def makeLibrary():
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "Font/kongtext.ttf"
rel_path_save = "Font/"
abs_font_path = os.path.join(script_dir, rel_path)
abs_save_dir = os.path.join(script_dir, rel_path_save)
return LetterBitmapDictionary(abs_font_path, 8, abs_save_dir, transposed=True, sety=8)
LIBRARY = makeLibrary()
class LEDText:
def __init__(self, text, width, height):
self.height = height
self.width = width
self.text = text
self.textgen = TextGenerator(self.text)
self.scrollindex = 0
self.display = [False] * (width * height)
#self._last_pos = None
self._init_led_states()
def _init_led_states(self):
for x in xrange(self.width):
col = next(self.textgen)
for y in xrange(len(col)):
self.display[x*self.height + y] = col[y]
def __iter__(self): #reversing
for x in xrange(self.width):
for y in xrange(self.height):
if x & 1:
yield self.display[(self.scrollindex + x+1) % self.width*self.height + y]
else:
yield self.display[(self.scrollindex + x+1) % self.width*self.height + (self.height - 1 - y)] #invert column
def nextview(self):
start = self.scrollindex * self.height
l2 = next(self.textgen)
for l_i in xrange(len(l2)):
self.display[start + l_i] = l2[l_i]
self.scrollindex = (self.scrollindex - 1) % self.width
class TextGenerator(object):
def __init__(self, text):
self.text = text
self.char_lst = None
self.text_ind = 0
self.col_ind = 0
def __iter__(self):
return self
def __next__(self):
self.next()
def next(self):
if self.char_lst is None:
self.char_lst = LIBRARY[self.text[self.text_ind]]
col = self.char_lst[self.col_ind]
self.col_ind += 1
if self.col_ind == len(self.char_lst):
self.col_ind = 0
self.text_ind += 1
if self.text_ind == len(self.text):
self.text_ind = 0
self.char_lst = LIBRARY[self.text[self.text_ind]]
return col