-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledmatrix.py
270 lines (203 loc) · 7.45 KB
/
ledmatrix.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!./venv/bin/python
import asyncio
import gzip
from logging import critical, debug, error, info, warning
from pathlib import Path
from tempfile import NamedTemporaryFile
import PIL.BdfFontFile
import PIL.Image
import PIL.ImageDraw
import PIL.ImageFont
import PIL.PcfFontFile
import datetime
# this maxes arithmetic around the 4 tuples used for regions (boxes in PIL parlance)
# a little easier
class Box:
def __init__(self, left, top, right=None, bottom=None, size=None, width=None, height=None):
self.left = left
self.top = top
if size is not None:
width, height = size
if right is None:
self.right = width + left
else:
self.right = right
if bottom is None:
self.bottom = height + top
else:
self.bottom = bottom
def __repr__(self):
return f'(#box {self.left},{self.top},{self.right},{self.bottom})'
@property
def width(self):
return self.right - self.left
@property
def height(self):
return self.bottom - self.top
@property
def box(self):
return self.left, self.top, self.right, self.bottom
@property
def size(self):
return self.right - self.left, self.bottom - self.top
@property
def topleft(self):
return self.left, self.top
# given an iterable object, return
# 0, 1, .., N-2, N-1, N-2, .. 1; repeat if endless=True
def ping_pong_iter(src_iter, endless=False):
arr = list()
# first round, keep a copy
for k in src_iter:
arr.append(k)
yield k
while True:
# reverse back
for k in arr[-2:0:-1]:
yield k
if not endless:
break
# forward again
for k in arr:
yield k
class SquareAnimation:
def __init__(self, filename):
img = PIL.Image.open(filename) # assume N squares sz * sz
self.imgsz = img.size[1] # width and height of a square
n_phases = img.size[0] // img.size[1] # number of squares
self.img_arr = [] # store individual tiles, croped out
for j in range(n_phases):
# left, top, right, bottom
rect = [self.imgsz * j, 0, self.imgsz*(j+1), self.imgsz]
self.img_arr.append(img.crop(rect))
@property
def size(self):
return self.img_arr[0].size
@property
def width(self):
return self.img_arr[0].size[0]
@property
def height(self):
return self.img_arr[0].size[1]
def __iter__(self):
return ping_pong_iter(self.img_arr, True)
class Canvas:
def __init__(self):
pass
def stamp_into(self, dst):
pass
def tick(self):
pass
class TextScrollCanvas(Canvas):
def __init__(self, box: Box, text: str, font: PIL.ImageFont.ImageFont, dx=1.0, transparent=False):
self.box = box
self.update_txt(text, font, dx)
self.x_offs = 0.0
self.dx = dx
self.transparent = transparent
self.anim_box = None
self.anim_iter = None
info(f'New TextScrollCanvas at {self.box}: {text}')
def place_animation(self, x, y, anim):
self.anim_box = Box(x, y, size=anim.size)
self.anim_iter = iter(anim)
def remove_animation(self):
self.anim_box = None
self.anim_iter = None
def update_txt(self, s, fnt:PIL.ImageFont.ImageFont, dx):
bbox = fnt.getbbox(s) # left, top, right, bottom
txt_width = bbox[2]- bbox[0]
# option 1, <space> <text> <space>, makes sure that there
# is a period where the whole matrix is empty before/after text is shown
img_width = txt_width + 2 * self.box.width - 1
self.img = PIL.Image.new(mode='L', size=(img_width, self.box.height))
drw = PIL.ImageDraw.Draw(self.img)
drw.text((self.box.width, 0), s, font=fnt, fill=(0xff, ))
self.x_offs = 1.0
self.dx = dx
def stamp_into(self, dst: PIL.Image):
x_offs_int = int(self.x_offs)
crop_img = self.img.crop(Box(
x_offs_int,
0,
width=self.box.width,
height=self.box.height
).box)
if self.transparent:
mask = crop_img
else:
mask = None
dst.paste(crop_img, self.box.topleft, mask)
def tick(self):
if self.dx == 0.0:
return
if self.anim_iter:
self.img.paste(next(self.anim_iter), self.anim_box.box)
self.x_offs += self.dx
if self.dx > 0.0:
while self.x_offs >= self.img.size[0] - self.box.width:
self.x_offs -= self.img.size[0] - self.box.width
if self.dx < 0.0:
while self.x_offs < 0:
self.x_offs += self.img.size[0] - self.box.width
class LedMatrix:
def __init__(self, width, height, matrix_hw=None):
self.width = width
self.height = height
self.matrix_hw = matrix_hw
self.img = PIL.Image.new('L', size=(self.width, self.height))
self.canvases = list()
self.animations = list()
self.fonts = list()
def add_animation(self, filename):
self.animations.append(SquareAnimation(filename))
def add_font(self, filename):
if not isinstance(filename, Path):
filename = Path(filename)
font = None
if '.bdf' in filename.suffixes:
constr = PIL.BdfFontFile.BdfFontFile
elif '.pcf' in filename.suffixes:
constr = PIL.PcfFontFile.PcfFontFile
elif '.pil' in filename.suffixes:
font = PIL.ImageFont.load(filename)
else:
raise RuntimeError(f'Unknown font format for {filename}.')
if font is None:
if '.gz' in filename.suffixes:
reader = gzip.open(filename)
else:
reader = filename.open('rb')
with NamedTemporaryFile('wb', suffix='.pil') as pil_font_file:
raw_font_file = constr(reader)
raw_font_file.save(pil_font_file.name)
font = PIL.ImageFont.load(pil_font_file.name)
info(f'Adding font {filename}.')
self.fonts.append(font)
async def main_loop(self):
td_1min = datetime.timedelta(minutes=1)
ts_now = datetime.datetime.now().astimezone()
ts_next = None
self.canvases = [
TextScrollCanvas(Box(0, 0, self.width, self.height),
'SOCO Südost Chaos Organization on CCCamp2023!', self.fonts[0], +0.5),
None # will be replaced by a clock
]
anim = SquareAnimation(Path('pacman_20x20_right_to_left.png'))
self.canvases[0].place_animation(100, 0, anim)
while self.matrix_hw.running:
ts_now = datetime.datetime.now().astimezone()
if ts_next is None or ts_now > ts_next:
ts_next = ts_now.replace(microsecond=0, second=0) + td_1min
time_str = ts_now.strftime('%B, %d %Y, %H:%M')
self.canvases[1] = TextScrollCanvas(Box(80, 12, width=40, height=9),
time_str, self.fonts[1], 0.2, True)
self.img.paste((0x00, ), [0, 0, self.width, self.height])
for canvas in self.canvases:
canvas.stamp_into(self.img)
canvas.tick()
self.matrix_hw.update(self.img)
if self.canvases:
await asyncio.sleep(1.0/60) # 60 Hz
else:
await asyncio.sleep(1.0)