-
Notifications
You must be signed in to change notification settings - Fork 10
/
display.py
430 lines (374 loc) · 16.2 KB
/
display.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from machine import Pin, ADC
from constants import SCHEDULER_ANIMATION, SCHEDULER_UPDATE_BACKLIGHT_VALUE
import uasyncio
import time
from util import partial, singleton
from utime import sleep_us
from configuration import Configuration
import helpers
@singleton
class Display:
class WaitForAnimation:
def __init__(self, callback, *args, **kwargs) -> None:
self.callback = partial(callback, *args, **kwargs)
def __init__(self, scheduler):
self.a0 = Pin(16, Pin.OUT)
self.a1 = Pin(18, Pin.OUT)
self.a2 = Pin(22, Pin.OUT)
self.oe = Pin(13, Pin.OUT)
self.sdi = Pin(11, Pin.OUT)
self.clk = Pin(10, Pin.OUT)
self.le = Pin(12, Pin.OUT)
self.ain = ADC(26)
self.row = 0
self.leds = [[0] * 32 for i in range(0, 8)]
self.animating = False
self.showing_time = False
self.display_text_width = 32
self.disp_offset = 2
self.display_queue = []
self.initialise_fonts()
self.initialise_icons()
self.initialise_days()
self.scheduler = scheduler
self.config = Configuration()
self.initialise_backlight()
self.show_temperature_icon()
def enable_leds(self):
while True:
self.row = (self.row + 1) % 8
led_row = self.leds[self.row]
for col in range(32):
self.clk.value(0)
self.sdi.value(led_row[col])
self.clk.value(1)
self.le.value(1)
self.le.value(0)
self.a0.value(1 if self.row & 0x01 else 0)
self.a1.value(1 if self.row & 0x02 else 0)
self.a2.value(1 if self.row & 0x04 else 0)
self.oe.value(0)
sleep_us(self.backlight_sleep[self.current_backlight])
self.oe.value(1)
async def animate_text(self, text: str, delay=1000, clear=True, force=False):
if self.animating and not force:
self.display_queue.append(
self.WaitForAnimation(self.animate_text, text, delay, clear=clear))
return
# add blank whitespace for text to show correctly
text = text + " "
if clear:
self.clear_text()
await self.show_text(text)
self.animate(delay)
def animate(self, delay=1000):
self.runs = 0
self.animating = True
self.scheduler.schedule(
SCHEDULER_ANIMATION, 200, self.scroll_text_left, delay)
async def scroll_text_left(self):
for row in range(8):
led_row = self.leds[row]
for col in range(self.display_text_width):
if row > 0 and col > 2:
self.leds[row][col-1] = led_row[col]
self.runs += 1
if self.runs == self.display_text_width - 3: # account for whitespace
self.animating = False
self.scheduler.remove(SCHEDULER_ANIMATION)
await self.process_callback_queue()
async def process_callback_queue(self, *args):
if len(self.display_queue) == 0:
if not self.showing_time:
await self.show_time()
else:
await self.display_queue[0].callback()
self.display_queue.pop(0)
def clear(self, x=0, y=0, w=24, h=7):
self.display_text_width = 0
for yy in range(y, y + h + 1):
for xx in range(x, x + w + 1):
self.leds[yy][xx] = 0
def clear_text(self):
self.animating = False
self.scheduler.remove(SCHEDULER_ANIMATION)
self.clear(x=2, y=1, w=24, h=6)
def reset(self):
self.clear_text()
self.display_queue = []
def show_char(self, character, pos):
pos += self.disp_offset # Plus the offset of the status indicator
char = self.ziku[character]
for row in range(1, 8):
byte = char.rows[row - 1]
for col in range(0, char.width):
self.leds[row][pos + col] = (byte >> col) % 2
async def show_text_for_period(self, text, pos=0, clear=True, display_period=5000):
if self.animating:
self.display_queue.append(
self.WaitForAnimation(self.show_text_for_period, text, pos, display_period))
return
await self.show_text(text, pos, clear)
await uasyncio.sleep_ms(display_period)
await self.process_callback_queue()
async def show_text(self, text, pos=0, clear=True):
if self.animating:
self.display_queue.append(
self.WaitForAnimation(self.show_text, text, pos, clear))
return
if clear:
self.clear_text()
i = 0
while i < len(text):
if text[i:i + 2] in self.ziku:
c = text[i:i + 2]
i += 2
else:
c = text[i]
i += 1
width = self.ziku[c].width
self.display_text_width += width
# account for whitespace between text words
self.display_text_width += len(text) + 1
# handle small text by resetting back to 32
if self.display_text_width < 32:
self.display_text_width = 32
self.set_new_led_rows()
i = 0
while i < len(text):
if text[i:i + 2] in self.ziku:
c = text[i:i + 2]
i += 2
else:
c = text[i]
i += 1
self.show_char(c, pos)
width = self.ziku[c].width
pos += width + 1
async def show_time(self, time=None):
self.showing_time = True
if time != None:
self.time = time
await self.show_text(self.time)
async def show_temperature(self, temp):
self.showing_time = False
symbol = ""
if self.config.temp == "c":
symbol = "°C"
else:
temp = helpers.convert_celsius_to_temperature(temp)
symbol = "°F"
temp = str(temp)
await self.animate_text(self.time + " " + temp +
symbol, delay=0, clear=False)
async def show_message(self, text: str):
self.showing_time = False
if len(text) > 3:
await self.animate_text(text)
else:
await self.show_text_for_period(text, display_period=8000)
def show_icon(self, name):
icon = self.Icons[name]
for w in range(icon.width):
self.leds[icon.y][icon.x + w] = 1
def hide_icon(self, name):
icon = self.Icons[name]
for w in range(icon.width):
self.leds[icon.y][icon.x + w] = 0
def hide_temperature_icons(self):
self.hide_icon("°F")
self.hide_icon("°C")
def set_new_led_rows(self):
# copy days of week led row
day_of_week_row = self.leds[0]
icon_column_one = []
icon_column_two = []
# copy column 1 and two (icons)
for row in range(1, 9):
icon_column_one.append(self.leds[row - 1][0])
icon_column_two.append(self.leds[row - 1][1])
# reset led array to use new display width (allows for text bigger than screen)
new_leds = [[0] * self.display_text_width for i in range(0, 8)]
# put back previously captured values as they were reset in led reset
new_leds[0] = day_of_week_row
for row in range(1, 9):
new_leds[row - 1][0] = icon_column_one[row - 1]
new_leds[row - 1][1] = icon_column_two[row - 1]
self.leds = new_leds
def sidelight_on(self):
self.leds[0][2] = 1
self.leds[0][5] = 1
def sidelight_off(self):
self.leds[0][2] = 0
self.leds[0][5] = 0
# this is called whenever the user presses the "light" button to switch to a different setting
def switch_backlight(self):
if self.auto_backlight:
self.auto_backlight = False
self.hide_icon("AutoLight")
self.current_backlight = 0
self.scheduler.remove(
SCHEDULER_UPDATE_BACKLIGHT_VALUE)
self.config.update_autolight_value(False)
elif self.current_backlight == len(self.backlight_sleep)-1:
self.show_icon("AutoLight")
self.auto_backlight = True
self.update_auto_backlight_value()
self.scheduler.schedule(
SCHEDULER_UPDATE_BACKLIGHT_VALUE, 1000, self.update_backlight_callback)
self.config.update_autolight_value(True)
else:
self.current_backlight += 1
def initialise_backlight(self):
# CPU freq needs to be increase to 250 for better results
# From 10 (low) to 1250(High)
self.backlight_sleep = [10, 100, 300, 400, 700, 1250, 2000]
self.current_backlight = 6
self.auto_backlight = self.config.autolight
self.update_auto_backlight_value()
self.last_backlight_update = time.ticks_ms()
if self.auto_backlight:
self.show_icon("AutoLight")
self.scheduler.schedule(
SCHEDULER_UPDATE_BACKLIGHT_VALUE, 1000, self.update_backlight_callback)
def update_auto_backlight_value(self):
backlight = 0
aim = self.ain.read_u16()
if aim > 65000: # Low light
backlight = 0
elif aim > 60000:
backlight = 1
elif aim > 40000:
backlight = 2
else:
backlight = 3
if backlight != self.current_backlight:
self.current_backlight = backlight
self.last_backlight_update = time.ticks_ms()
async def update_backlight_callback(self):
tm = time.ticks_ms()
difference = time.ticks_diff(tm, self.last_backlight_update)
if difference > 3000:
self.update_auto_backlight_value()
def show_temperature_icon(self):
if self.config.temp == "c":
self.show_icon("°C")
self.hide_icon("°F")
else:
self.show_icon("°F")
self.hide_icon("°C")
def print(self):
for row in range(0, 8):
for pos in range(0, 24):
print("X" if self.leds[row][pos] == 1 else " ", end="")
print("")
def square(self):
'''
Prints a crossed square. For debugging purposes.
'''
for row in range(1, 8):
self.leds[row][2] = 1
self.leds[row][23] = 1
for col in range(2, 23):
self.leds[1][col] = 1
self.leds[7][col] = 1
self.leds[int(col / 24 * 7) + 1][col] = 1
self.leds[7 - int(col / 24 * 7)][col] = 1
class Character:
def __init__(self, width, rows, offset=2):
self.width = width
self.rows = rows
self.offset = offset
class Icon:
def __init__(self, x, y, width=1):
self.x = x
self.y = y
self.width = width
def initialise_icons(self):
self.Icons = {
"MoveOn": self.Icon(0, 0, width=2),
"AlarmOn": self.Icon(0, 1, width=2),
"CountDown": self.Icon(0, 2, width=2),
"°F": self.Icon(0, 3),
"°C": self.Icon(1, 3),
"AM": self.Icon(0, 4),
"PM": self.Icon(1, 4),
"CountUp": self.Icon(0, 5, width=2),
"Hourly": self.Icon(0, 6, width=2),
"AutoLight": self.Icon(0, 7, width=2),
"Mon": self.Icon(3, 0, width=2),
"Tue": self.Icon(6, 0, width=2),
"Wed": self.Icon(9, 0, width=2),
"Thur": self.Icon(12, 0, width=2),
"Fri": self.Icon(15, 0, width=2),
"Sat": self.Icon(18, 0, width=2),
"Sun": self.Icon(21, 0, width=2),
}
def initialise_days(self):
self.days_of_week = {
0: "Mon",
1: "Tue",
2: "Wed",
3: "Thur",
4: "Fri",
5: "Sat",
6: "Sun"
}
def show_day(self, int):
for key in self.days_of_week:
if key == int:
self.show_icon(self.days_of_week[key])
else:
self.hide_icon(self.days_of_week[key])
# Derived from c code created by yufu on 2021/1/23.
# Modulus method: negative code, reverse, line by line, 4X7 font
def initialise_fonts(self):
self.ziku = {
"all": self.Character(width=3, rows=[0x05, 0x05, 0x03, 0x03, 0x03, 0x03, 0x03]),
"0": self.Character(width=4, rows=[0x06, 0x09, 0x09, 0x09, 0x09, 0x09, 0x06]),
"1": self.Character(width=4, rows=[0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x0E]),
"2": self.Character(width=4, rows=[0x06, 0x09, 0x08, 0x04, 0x02, 0x01, 0x0F]),
"3": self.Character(width=4, rows=[0x06, 0x09, 0x08, 0x06, 0x08, 0x09, 0x06]),
"4": self.Character(width=4, rows=[0x08, 0x0C, 0x0A, 0x09, 0x0F, 0x08, 0x08]),
"5": self.Character(width=4, rows=[0x0F, 0x01, 0x07, 0x08, 0x08, 0x09, 0x06]),
"6": self.Character(width=4, rows=[0x04, 0x02, 0x01, 0x07, 0x09, 0x09, 0x06]),
"7": self.Character(width=4, rows=[0x0F, 0x09, 0x04, 0x04, 0x04, 0x04, 0x04]),
"8": self.Character(width=4, rows=[0x06, 0x09, 0x09, 0x06, 0x09, 0x09, 0x06]),
"9": self.Character(width=4, rows=[0x06, 0x09, 0x09, 0x0E, 0x08, 0x04, 0x02]),
"A": self.Character(width=4, rows=[0x06, 0x09, 0x09, 0x0F, 0x09, 0x09, 0x09]),
"B": self.Character(width=4, rows=[0x07, 0x09, 0x09, 0x07, 0x09, 0x09, 0x07]),
"C": self.Character(width=4, rows=[0x06, 0x09, 0x01, 0x01, 0x01, 0x09, 0x06]),
"D": self.Character(width=4, rows=[0x07, 0x09, 0x09, 0x09, 0x09, 0x09, 0x07]),
"E": self.Character(width=4, rows=[0x0F, 0x01, 0x01, 0x0F, 0x01, 0x01, 0x0F]),
"F": self.Character(width=4, rows=[0x0F, 0x01, 0x01, 0x0F, 0x01, 0x01, 0x01]),
"G": self.Character(width=4, rows=[0x06, 0x09, 0x01, 0x0D, 0x09, 0x09, 0x06]),
"H": self.Character(width=4, rows=[0x09, 0x09, 0x09, 0x0F, 0x09, 0x09, 0x09]),
"I": self.Character(width=3, rows=[0x07, 0x02, 0x02, 0x02, 0x02, 0x02, 0x07]),
"J": self.Character(width=4, rows=[0x0F, 0x08, 0x08, 0x08, 0x09, 0x09, 0x06]),
"K": self.Character(width=4, rows=[0x09, 0x05, 0x03, 0x01, 0x03, 0x05, 0x09]),
"L": self.Character(width=4, rows=[0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0F]),
"M": self.Character(width=5, rows=[0x11, 0x1B, 0x15, 0x11, 0x11, 0x11, 0x11]),
"N": self.Character(width=4, rows=[0x09, 0x09, 0x0B, 0x0D, 0x09, 0x09, 0x09]),
"O": self.Character(width=4, rows=[0x06, 0x09, 0x09, 0x09, 0x09, 0x09, 0x06]),
"P": self.Character(width=4, rows=[0x07, 0x09, 0x09, 0x07, 0x01, 0x01, 0x01]),
"Q": self.Character(width=5, rows=[0x0E, 0x11, 0x11, 0x11, 0x15, 0x19, 0x0E]),
"R": self.Character(width=4, rows=[0x07, 0x09, 0x09, 0x07, 0x03, 0x05, 0x09]),
"S": self.Character(width=4, rows=[0x06, 0x09, 0x02, 0x04, 0x08, 0x09, 0x06]),
"T": self.Character(width=5, rows=[0x1F, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04]),
"U": self.Character(width=4, rows=[0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x06]),
"V": self.Character(width=5, rows=[0x11, 0x11, 0x11, 0x11, 0x11, 0x0A, 0x04]),
"W": self.Character(width=5, rows=[0x11, 0x11, 0x11, 0x15, 0x15, 0x1B, 0x11]),
"X": self.Character(width=5, rows=[0x11, 0x0A, 0x04, 0x04, 0x04, 0x0A, 0x11]),
"Y": self.Character(width=4, rows=[0x1F, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04]),
"Z": self.Character(width=4, rows=[0x0F, 0x08, 0x04, 0x02, 0x01, 0x0F, 0x00]),
":": self.Character(width=2, rows=[0x00, 0x03, 0x03, 0x00, 0x03, 0x03, 0x00]),
# colon width space
" :": self.Character(width=2, rows=[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
# temp symbol
"°": self.Character(width=2, rows=[0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00]),
# space
" ": self.Character(width=2, rows=[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
".": self.Character(width=1, rows=[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]),
"-": self.Character(width=2, rows=[0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00]),
"/": self.Character(width=2, rows=[0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01]),
}