-
Notifications
You must be signed in to change notification settings - Fork 16
/
display.py
274 lines (249 loc) · 11.7 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
from machine import Pin, ADC
from util import singleton
from utime import sleep_us
@singleton
class Display:
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.count = 0
self.leds = [[0] * 32 for i in range(0, 8)]
self.leds_changed = False
self.disp_offset = 2
self.initialise_fonts()
self.initialise_icons()
self.scheduler = scheduler
# CPU freq needs to be increase to 250 for better results
self.backlight_sleep = [10, 100, 300, 1500] # From 10 (low) to 1500(High)
self.current_backlight = 3
self.auto_backlight = True
self.show_icon("AutoLight")
self.update_auto_backlight_value(None)
self.scheduler.schedule("enable-leds", 1, self.enable_leds)
self.scheduler.schedule("update_auto_backlight_value", 1000, self.update_auto_backlight_value)
def enable_leds(self, t):
self.count += 1
self.row = (self.row + 1) % 8
led_row = self.leds[self.row]
if True:
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.leds_changed = False
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)
def clear(self, x=0, y=0, w=24, h=7):
for yy in range(y, y + h + 1):
for xx in range(x, x + w + 1):
self.leds[yy][xx] = 0
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
self.leds_changed = True
def show_text(self, text, pos=0):
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
char = self.ziku[c]
self.show_char(c, pos)
width = self.ziku[c].width
pos += width + 1
def show_icon(self, name):
icon = self.Icons[name]
for w in range(icon.width):
self.leds[icon.y][icon.x + w] = 1
self.leds_changed = True
def hide_icon(self, name):
icon = self.Icons[name]
for w in range(icon.width):
self.leds[icon.y][icon.x + w] = 0
self.leds_changed = True
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
def switch_backlight(self):
if self.auto_backlight:
self.auto_backlight = False
self.hide_icon("AutoLight")
self.current_backlight = 0
self.scheduler.remove("update_auto_backlight_value")
elif self.current_backlight == 3:
self.show_icon("AutoLight")
self.auto_backlight = True
self.update_auto_backlight_value(None)
self.scheduler.schedule("update_auto_backlight_value", 1000, self.update_auto_backlight_value)
else:
self.current_backlight += 1
def update_auto_backlight_value(self, t):
aim = self.ain.read_u16()
if aim > 60000: # Low light
self.current_backlight = 0
elif aim > 58000:
self.current_backlight = 1
elif aim > 40000:
self.current_backlight = 2
else:
self.current_backlight = 3
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),
}
day_of_week = {
0: "Mon",
1: "Tue",
2: "Wed",
3: "Thur",
4: "Fri",
5: "Sat",
6: "Sun"
}
def show_day(self, int):
self.clear()
self.show_icon(self.day_of_week[int])
# 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]),
"H": self.Character(width=4, rows=[0x09,0x09,0x09,0x0F,0x09,0x09,0x09]),
"L": self.Character(width=4, rows=[0x01,0x01,0x01,0x01,0x01,0x01,0x0F]),
"N": self.Character(width=4, rows=[0x09,0x09,0x0B,0x0D,0x09,0x09,0x09]),
"O": self.Character(width=4, rows=[0x0F,0x09,0x09,0x09,0x09,0x09,0x0F]),
"P": self.Character(width=4, rows=[0x07,0x09,0x09,0x07,0x01,0x01,0x01]),
"U": self.Character(width=4, rows=[0x09,0x09,0x09,0x09,0x09,0x09,0x06]),
":": self.Character(width=2, rows=[0x00,0x03,0x03,0x00,0x03,0x03,0x00]), #2×7
" :": self.Character(width=2, rows=[0x00,0x00,0x00,0x00,0x00,0x00,0x00]), # colon width space
"°C": self.Character(width=4, rows=[0x01,0x0C,0x12,0x02,0x02,0x12,0x0C]), # celcuis 5×7
"°F": self.Character(width=4, rows=[0x01,0x1E,0x02,0x1E,0x02,0x02,0x02]), # farenheit
" ": self.Character(width=4, rows=[0x00,0x00,0x00,0x00,0x00,0x00,0x00]), # space
"Y": self.Character(width=4, rows=[0x1F,0x04,0x04,0x04,0x04,0x04,0x04]), # 5*7
".": self.Character(width=1, rows=[0x00,0x00,0x00,0x00,0x00,0x00,0x01]), # 1×7
"-": self.Character(width=2, rows=[0x00,0x00,0x00,0x03,0x00,0x00,0x00]), # 2×7
"M": self.Character(width=4, rows=[0x00,0x11,0x1B,0x15,0x11,0x11,0x11,0x11]), # 5×7
"/": self.Character(width=2, rows=[0x02,0x02,0x02,0x01,0x01,0x01,0x01,0x01]), # 3×7
"°C2": self.Character(width=4, rows=[0x00,0x01,0x0C,0x12,0x02,0x02,0x12,0x0C]), # 5×7
"°F2": self.Character(width=4, rows=[0x00,0x01,0x1E,0x02,0x1E,0x02,0x02,0x02]),
"V": self.Character(width=5, rows=[0x11,0x11,0x11,0x11,0x11,0x0A,0x04]), # 5×7
"W": self.Character(width=5, rows=[0x11,0x11,0x11,0x15,0x15,0x1B,0x11]), # 5×7
}
self.digital_tube = {
"0": [0x0F, 0x09, 0x09, 0x09, 0x09, 0x09, 0x0F],
"1": [0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08],
"2": [0x0F, 0x08, 0x08, 0x0F, 0x01, 0x01, 0x0F],
"3": [0x0F, 0x08, 0x08, 0x0F, 0x08, 0x08, 0x0F],
"4": [0x09, 0x09, 0x09, 0x0F, 0x08, 0x08, 0x08],
"5": [0x0F, 0x01, 0x01, 0x0F, 0x08, 0x08, 0x0F],
"6": [0x0F, 0x01, 0x01, 0x0F, 0x09, 0x09, 0x0F],
"7": [0x0F, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08],
"8": [0x0F, 0x09, 0x09, 0x0F, 0x09, 0x09, 0x0F],
"9": [0x0F, 0x09, 0x09, 0x0F, 0x08, 0x08, 0x0F],
"A": [0x0F, 0x09, 0x09, 0x0F, 0x09, 0x09, 0x09],
"B": [0x01, 0x01, 0x01, 0x0F, 0x09, 0x09, 0x0F],
"C": [0x0F, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0F],
"D": [0x08, 0x08, 0x08, 0x0F, 0x09, 0x09, 0x0F],
"E": [0x0F, 0x01, 0x01, 0x0F, 0x01, 0x01, 0x0F],
"F": [0x0F, 0x01, 0x01, 0x0F, 0x01, 0x01, 0x01],
"H": [0x09, 0x09, 0x09, 0x0F, 0x09, 0x09, 0x09],
"L": [0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0F],
"N": [0x0F, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09],
"P": [0x0F, 0x09, 0x09, 0x0F, 0x01, 0x01, 0x01],
"U": [0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x0F],
":": [0x00, 0x03, 0x03, 0x00, 0x03, 0x03, 0x00], # 2×7
"°C": [0x01, 0x1E, 0x02, 0x02, 0x02, 0x02, 0x1E], # celcius 5×7
"°F": [0x01, 0x1E, 0x02, 0x1E, 0x02, 0x02, 0x02], # farenheit
" ": [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
"T": [0x1F, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04], # 5*7
".": [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01], # 2×7
"-": [0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00], # 2×7
"M": [0x00, 0x11, 0x1B, 0x15, 0x11, 0x11, 0x11, 0x11], # 5×7
"/": [0x00, 0x04, 0x04, 0x02, 0x02, 0x02, 0x01, 0x01], # 3×7
"°C2": [0x00, 0x01, 0x0C, 0x12, 0x02, 0x02, 0x12, 0x0C], # celcuis 5x7
"°F2": [0x00, 0x01, 0x1E, 0x02, 0x1E, 0x02, 0x02, 0x02], # farenheit
"V": [0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F], # 5×7
"W": [0x11, 0x11, 0x11, 0x15, 0x15, 0x1B, 0x11], # 5×7
}