-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_display.py
More file actions
executable file
·300 lines (266 loc) · 11.3 KB
/
Copy pathcustom_display.py
File metadata and controls
executable file
·300 lines (266 loc) · 11.3 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
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
import gc
import displayio
from adafruit_display_shapes.line import Line
from adafruit_display_shapes.filled_polygon import FilledPolygon
# Big digit patterns: 5 cols wide x 10 rows tall.
# Each int is one row; bit 4 (MSB of 5) = leftmost col.
# 2-pixel-wide strokes to match original display style.
_BIG = {
'0': [0b11111, 0b11111, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11111, 0b11111],
'1': [0b00011, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011],
'2': [0b11111, 0b11111, 0b00011, 0b00011, 0b11111, 0b11111, 0b11000, 0b11000, 0b11111, 0b11111],
'3': [0b11111, 0b11111, 0b00011, 0b00011, 0b11111, 0b11111, 0b00011, 0b00011, 0b11111, 0b11111],
'4': [0b11011, 0b11011, 0b11011, 0b11011, 0b11111, 0b11111, 0b00011, 0b00011, 0b00011, 0b00011],
'5': [0b11111, 0b11111, 0b11000, 0b11000, 0b11111, 0b11111, 0b00011, 0b00011, 0b11111, 0b11111],
'6': [0b11111, 0b11111, 0b11000, 0b11000, 0b11111, 0b11111, 0b11011, 0b11011, 0b11111, 0b11111],
'7': [0b11111, 0b11111, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011],
'8': [0b11111, 0b11111, 0b11011, 0b11011, 0b11111, 0b11111, 0b11011, 0b11011, 0b11111, 0b11111],
'9': [0b11111, 0b11111, 0b11011, 0b11011, 0b11111, 0b11111, 0b00011, 0b00011, 0b11111, 0b11111],
'.': [0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11000, 0b11000],
'-': [0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b00000, 0b00000, 0b00000, 0b00000],
}
_BIG_W = 5
_BIG_H = 10
_BIG_X = [1, 8, 15, 22, 25] # absolute x start per position
# Small digit patterns: 4 cols wide x 7 rows tall.
# Each int is one row; bit 3 (MSB of 4) = leftmost col.
_SMALL = {
'0': [0b1111, 0b1001, 0b1001, 0b1001, 0b1001, 0b1001, 0b1111],
'1': [0b0001, 0b0001, 0b0001, 0b0001, 0b0001, 0b0001, 0b0001],
'2': [0b1111, 0b0001, 0b0001, 0b1111, 0b1000, 0b1000, 0b1111],
'3': [0b1111, 0b0001, 0b0001, 0b1111, 0b0001, 0b0001, 0b1111],
'4': [0b1001, 0b1001, 0b1001, 0b1111, 0b0001, 0b0001, 0b0001],
'5': [0b1111, 0b1000, 0b1000, 0b1111, 0b0001, 0b0001, 0b1111],
'6': [0b1111, 0b1000, 0b1000, 0b1111, 0b1001, 0b1001, 0b1111],
'7': [0b1111, 0b0001, 0b0001, 0b0001, 0b0001, 0b0001, 0b0001],
'8': [0b1111, 0b1001, 0b1001, 0b1111, 0b1001, 0b1001, 0b1111],
'9': [0b1111, 0b1001, 0b1001, 0b1111, 0b0001, 0b0001, 0b1111],
}
_SMALL_W = 4
_SMALL_H = 7
_SMALL_X = [2, 7, 12] # absolute x start per position
_SMALL_Y = [14, 24] # absolute y start per row (row 1 = index 0, row 2 = index 1)
class CustomDisplay(displayio.Group):
def __init__(self, display, *):
super().__init__()
self.display = display
self.bitmap = displayio.Bitmap(32, 32, 16)
self.color = displayio.Palette(13)
self.color[0] = 0x000000 # black background
self.color[1] = 0xFF0000 # red
self.color[2] = 0xCC4000 # amber
self.color[3] = 0x00FF00 # green
self.color[4] = 0x00FFFF # cyan
self.color[5] = 0x666666 # lighter white
self.color[6] = 0x006AFF # cold
self.color[7] = 0x00FF0F # good
self.color[8] = 0xFFAA00 # warm
self.color[9] = 0xFF1000 # hot
self.color[10] = 0x9900FF # purple
self.color[11] = 0xFF00FF # magenta
self.color[12] = 0xFFFFFF # white
self.tile_grid = displayio.TileGrid(self.bitmap, pixel_shader=self.color)
self.root_group = displayio.Group()
self.root_group.append(self)
self.append(self.tile_grid) # bitmap layer first (behind shape objects)
self.aqi_text = displayio.Group()
self.append(self.aqi_text)
self.lines = displayio.Group()
self.append(self.lines)
self.icons = displayio.Group()
self.append(self.icons)
self.hidden = True
self.temperature = 888.8
self.humidity = 888
self.aqi = 888
self.night_time = True
self.is_home = True
self.has_error = None
self.is_stale = True
self._night_mode_received = False
self._is_home_received = False
self.init_aqi()
self.make_lines()
self.make_icons()
self.update_display()
def _draw_digit(self, patterns, x, y, width, color_idx):
for row_idx, row_bits in enumerate(patterns):
for col in range(width):
if row_bits & (1 << (width - 1 - col)):
self.bitmap[x + col, y + row_idx] = color_idx
else:
self.bitmap[x + col, y + row_idx] = 0
def make_big_digit(self, digit, position, color_idx):
self._draw_digit(_BIG.get(digit, [0] * _BIG_H), _BIG_X[position], 1, _BIG_W, color_idx)
def make_small_digit(self, digit, position, row, color_idx):
self._draw_digit(_SMALL.get(digit, [0] * _SMALL_H), _SMALL_X[position], _SMALL_Y[row - 1], _SMALL_W, color_idx)
def make_icons(self):
self.icons.append(
FilledPolygon(
[(28, 15), (30, 17), (30, 19), (29, 20), (27, 20), (26, 19), (26, 17), (28, 15)],
stroke=1, outline=self.color[4], fill=self.color[4],
)
)
self.icons.append(
FilledPolygon(
[(18, 14), (19, 14), (19, 15), (18, 15), (18, 14)],
stroke=1, outline=self.color[4], fill=self.color[4],
)
)
self.icons.append(
FilledPolygon(
[(21, 19), (22, 19), (22, 20), (21, 20), (21, 19)],
stroke=1, outline=self.color[4], fill=self.color[4],
)
)
self.icons.append(Line(22, 14, 18, 20, self.color[4]))
def init_aqi(self):
self.aqi_text.append(self.make_aqi_text_layer(self.color[5]))
def make_aqi(self, aqi, aqi_str):
if aqi <= 50:
aqi_color_idx = 3
elif aqi <= 100:
aqi_color_idx = 2
elif aqi <= 150:
aqi_color_idx = 8
elif aqi <= 200:
aqi_color_idx = 1
elif aqi <= 300:
aqi_color_idx = 10
else:
aqi_color_idx = 11
for i, ch in enumerate(aqi_str):
self.make_small_digit(ch, i, 2, aqi_color_idx)
def make_aqi_text_layer(self, color):
aqi_text_layer = displayio.Group()
aqi_text_layer.append(Line(18, 25, 18, 30, color))
aqi_text_layer.append(Line(19, 24, 20, 24, color))
aqi_text_layer.append(Line(19, 27, 20, 27, color))
aqi_text_layer.append(Line(21, 25, 21, 30, color))
aqi_text_layer.append(Line(23, 25, 23, 29, color))
aqi_text_layer.append(Line(24, 30, 26, 28, color))
aqi_text_layer.append(Line(25, 29, 26, 30, color))
aqi_text_layer.append(Line(26, 25, 26, 28, color))
aqi_text_layer.append(Line(24, 24, 25, 24, color))
aqi_text_layer.append(Line(28, 24, 30, 24, color))
aqi_text_layer.append(Line(29, 24, 29, 30, color))
aqi_text_layer.append(Line(28, 30, 30, 30, color))
return aqi_text_layer
def make_lines(self, color=None):
if color is None:
color = self.color[5]
self.display.auto_refresh = False
while len(self.lines) > 0:
self.lines.pop()
self.lines.append(Line(1, 12, 30, 12, color))
self.lines.append(Line(1, 22, 30, 22, color))
self.display.refresh()
self.display.auto_refresh = True
def make_droplet(self, humidity):
if humidity < 20:
self.icons[0] = FilledPolygon(
[(28, 19), (29, 20), (27, 20), (28, 19)],
stroke=1, outline=self.color[4], fill=self.color[4],
)
elif humidity < 40:
self.icons[0] = FilledPolygon(
[(27, 18), (29, 18), (30, 19), (29, 20), (27, 20), (26, 19), (27, 18)],
stroke=1, outline=self.color[4], fill=self.color[4],
)
elif humidity < 60:
self.icons[0] = FilledPolygon(
[(28, 16), (30, 18), (30, 19), (29, 20), (27, 20), (26, 19), (26, 18), (28, 16)],
stroke=1, outline=self.color[4], fill=self.color[4],
)
else:
self.icons[0] = FilledPolygon(
[(28, 15), (30, 17), (30, 19), (29, 20), (27, 20), (26, 19), (26, 17), (28, 15)],
stroke=1, outline=self.color[4], fill=self.color[4],
)
def update_display(self):
self.display.auto_refresh = False
try:
temp_str = "{0:.1f}".format(self.temperature)
except ValueError:
temp_str = "{0:.1f}".format(888.8)
if self.temperature < 0.0 and self.temperature > -10.0:
temp_str = "+" + temp_str
if self.temperature < 100.0 and self.temperature >= 0.0:
temp_str = "+" + temp_str
if self.temperature < 10.0 and self.temperature >= 0.0:
temp_str = "+" + temp_str
if self.temperature < 65.0:
temp_color_idx = 6
elif self.temperature < 75.0:
temp_color_idx = 7
elif self.temperature < 85.0:
temp_color_idx = 8
else:
temp_color_idx = 9
for i, ch in enumerate(temp_str):
self.make_big_digit(ch, i, temp_color_idx)
try:
hum_str = str(round(self.humidity))
except ValueError:
hum_str = str(888)
if int(hum_str) < 100:
hum_str = "+" + hum_str
if int(hum_str) < 10:
hum_str = "+" + hum_str
for i, ch in enumerate(hum_str):
self.make_small_digit(ch, i, 1, 4)
self.make_droplet(self.humidity)
try:
aqi_str = str(round(self.aqi))
except ValueError:
aqi_str = str(888)
if int(aqi_str) < 100:
aqi_str = "+" + aqi_str
if int(aqi_str) < 10:
aqi_str = "+" + aqi_str
self.make_aqi(self.aqi, aqi_str)
gc.collect()
self.display.refresh()
self.display.auto_refresh = True
def set_environmentals(self, temperature, humidity, aqi):
self.temperature = temperature
self.humidity = humidity
self.aqi = aqi
def set_temperature(self, temperature):
self.temperature = temperature
def set_humidity(self, humidity):
self.humidity = int(humidity)
def set_aqi(self, aqi):
self.aqi = int(aqi)
def _refresh_lines(self):
if self.has_error:
self.make_lines(self.color[1])
elif self.is_stale:
self.make_lines(self.color[2])
else:
self.make_lines(self.color[5])
def set_error(self, has_error):
if self.has_error == has_error:
return
self.has_error = has_error
self._refresh_lines()
def set_stale(self, is_stale):
if self.is_stale == is_stale:
return
self.is_stale = is_stale
self._refresh_lines()
def _update_hidden(self):
self.hidden = self.night_time or (not self.is_home)
print("visibility: night=%s home=%s hidden=%s" % (self.night_time, self.is_home, self.hidden))
def set_night_time(self, night_time):
self._night_mode_received = True
if self.night_time == night_time:
return
self.night_time = night_time
self._update_hidden()
def set_is_home(self, is_home):
self._is_home_received = True
if self.is_home == is_home:
return
self.is_home = is_home
self._update_hidden()