-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathadafruit_dash_display.py
407 lines (335 loc) · 12.9 KB
/
adafruit_dash_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
# SPDX-FileCopyrightText: Copyright (c) 2021 Eva Herrada for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_dash_display`
================================================================================
CircuitPython library for creating Adafruit IO dashboards.
* Author(s): Eva Herrada
Implementation Notes
--------------------
**Hardware:**
* This library currently only officially supports the
`Adafruit Funhouse <https://www.adafruit.com/product/4985>`_ but other boards are coming soon.
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
"""
try:
from typing import Tuple, Callable, Optional, Any
from adafruit_io.adafruit_io import IO_MQTT
from digitalio import DigitalInOut
except ImportError:
pass
import time
from collections import OrderedDict
import displayio
import terminalio
from adafruit_display_shapes.rect import Rect
from adafruit_display_text.label import Label
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Dash_Display.git"
class Feed:
"""Feed object to make getting and setting different feed properties easier
:param str key: The Adafruit IO key.
:param str default_text: Text to display before data has been pulled from Adafruit IO.
:param str formatted_text: String with formatting placeholders within it ready to have
data formatted into it.
:param Callable callback: A function to call when the feed is fetched.
:param int color: Hex color code for the feed text.
:param Callable pub: a function to call when data is published to the feed.
"""
def __init__(
self,
key: str,
default_text: str,
formatted_text: str,
callback: Optional[Callable],
color: Optional[int],
pub: Optional[Callable],
index: int,
): # pylint: disable=too-many-arguments
self._key = key
self.default_text = default_text
self._text = formatted_text
self._callback = callback
self._color = color
self._pub = pub
self.index = index
self._last_val = None
@property
def key(self) -> str:
"""Getter for feed key. Will give the value of the feed key"""
return self._key
@key.setter
def key(self, value: str) -> None:
"""Setter for feed key. Sets a new value for the feed key property _key
:param str value: The new value of the feed key.
"""
self._key = value
@property
def text(self) -> str:
"""Getter for text ready to be formatted. Will give the feed text"""
return self._text
@text.setter
def text(self, value: str) -> None:
"""Setter for text ready to be formatted. Allows to change the feed text
:param str value: The new value of the feed text.
"""
self._text = value
@property
def callback(self) -> Optional[Callable]:
"""Getter for callback function. Returns the feed callback"""
return self._callback
@callback.setter
def callback(self, value: Callable) -> None:
"""Setter for callback function. Changes the feed callback
:param Callable value: A function to call when the feed is fetched.
"""
self._callback = value
@property
def color(self) -> int:
"""Getter for text color callback function. Will return the color for the feed"""
return self._color
@color.setter
def color(self, value: int) -> None:
"""Setter for text color callback function
:param int value: The new value of the feed color.
"""
self._color = value
@property
def pub(self) -> Optional[Callable]:
"""Getter for publish function, called when a new value is published by this library."""
return self._pub
@pub.setter
def pub(self, value: Callable) -> None:
"""Setter for publish function"""
self._pub = value
@property
def last_val(self) -> Optional[str]:
"""Getter for the last value received"""
return self._last_val
@last_val.setter
def last_val(self, value: str) -> None:
"""Setter for last received value
:param str value: The newly received value.
"""
self._last_val = value
class Hub: # pylint: disable=too-many-instance-attributes
"""
Object that lets you make an IOT dashboard
:param displayio.Display display: The display for the dashboard.
:param IO_MQTT io_mqtt: MQTT communications object.
:param Tuple[DigitalInOut, ...] nav: The navigation pushbuttons.
"""
def __init__(
self,
display: displayio.Display,
io_mqtt: IO_MQTT,
nav: Tuple[DigitalInOut, ...],
):
self.display = display
self.io_mqtt = io_mqtt
self.up_btn, self.select, self.down, self.back, self.submit = nav
self.length = 0
self.selected = 1
self.feeds = OrderedDict()
self.io_mqtt.on_mqtt_connect = self.connected
self.io_mqtt.on_mqtt_disconnect = self.disconnected
self.io_mqtt.on_mqtt_subscribe = self.subscribe
self.io_mqtt.on_message = self.message
print("Connecting to Adafruit IO...")
io_mqtt.connect()
self.display.root_group = None
self.splash = displayio.Group()
self.rect = Rect(0, 0, 240, 30, fill=0xFFFFFF)
self.splash.append(self.rect)
self.display.root_group = self.splash
def simple_text_callback(
# pylint: disable=unused-argument
self,
client: IO_MQTT,
feed_id: str,
message: str,
) -> str:
"""Default callback function that uses the text in the Feed object and the color callback
to set the text
:param IO_MQTT client: The MQTT client to use.
:param str feed_id: The Adafruit IO feed ID.
:param str message: The text to display.
:return: A string with data formatted into it.
"""
feed_id = feed_id.split("/")[-1]
feed = self.feeds[feed_id]
try:
text = feed.text.format(message)
except ValueError:
text = feed.text.format(float(message))
return text
def update_text(self, client: IO_MQTT, feed_id: str, message: str) -> None:
"""Updates the text on the display
:param IO_MQTT client: The MQTT client to use.
:param str feed_id: The Adafruit IO feed ID.
:param str message: The text to display.
"""
feed = self.feeds[feed_id]
feed.callback(client, feed_id, message)
self.splash[feed.index + 1].text = feed.callback(client, feed_id, str(message))
if feed.color:
self.splash[feed.index + 1].color = feed.color(message)
def base_pub(self, var: Any) -> None:
"""Default function called when a feed is published to"""
def add_device(
self,
feed_key: str,
default_text: Optional[str] = None,
formatted_text: Optional[str] = None,
color_callback: Optional[int] = None,
callback: Optional[Callable] = None,
pub_method: Optional[Callable] = None,
): # pylint: disable=too-many-arguments
"""Adds a feed/device to the UI
:param feed_key: The Adafruit IO feed key.
:param str default_text: The default text for the device.
:param str formatted_text: The formatted text for the device.
:param int color_callback: The color to use for the device
:param Callable callback: The callback function to be called
when data is fetched.
:param Callable pub_method: The pub_method to be called
when data is published.
"""
if not callback:
callback = self.simple_text_callback
if not pub_method:
pub_method = self.base_pub
if not formatted_text:
formatted_text = f"{feed_key} : "
formatted_text = formatted_text + "{}"
if not default_text:
default_text = feed_key
self.io_mqtt.subscribe(feed_key)
if len(self.splash) == 1:
self.splash.append(
Label(
font=terminalio.FONT,
text=default_text,
x=3,
y=15,
anchored_position=(3, 15),
scale=2,
color=0x000000,
)
)
else:
self.splash.append(
Label(
font=terminalio.FONT,
x=3,
y=((len(self.splash) - 1) * 30) + 15,
text=default_text,
color=0xFFFFFF,
anchored_position=(3, ((len(self.splash) - 2) * 30) + 15),
scale=2,
)
)
self.length = len(self.splash) - 2
self.feeds[feed_key] = Feed(
key=feed_key,
default_text=default_text,
formatted_text=formatted_text,
callback=callback,
color=color_callback,
pub=pub_method,
index=len(self.feeds),
)
def get(self) -> None:
"""Gets all the subscribed feeds"""
for feed in self.feeds.keys():
print(f"getting {feed}")
self.io_mqtt.get(feed)
time.sleep(0.1)
self.io_mqtt.loop()
# pylint: disable=unused-argument
@staticmethod
def connected(client: IO_MQTT) -> None:
"""Callback for when the device is connected to Adafruit IO
:param IO_MQTT client: The MQTT client to use.
"""
print("Connected to Adafruit IO!")
@staticmethod
def subscribe(client: IO_MQTT, userdata: Any, topic: str, granted_qos: str) -> None:
"""Callback for when a new feed is subscribed to
:param IO_MQTT client: The MQTT client to use.
:param str userdata: The userdata to subscribe to.
:param str topic: The topic to subscribe to.
:param str granted_qos: The QoS level.
"""
print(f"Subscribed to {topic} with QOS level {granted_qos}")
@staticmethod
def disconnected(client: IO_MQTT) -> None:
"""Callback for when the device disconnects from Adafruit IO
:param IO_MQTT client: The MQTT client to use.
"""
print("Disconnected from Adafruit IO!")
def message(self, client: IO_MQTT, feed_id: str, message: str) -> None:
"""Callback for whenever a new message is received
:param IO_MQTT client: The MQTT client to use.
:param str feed_id: The Adafruit IO feed ID.
:param str message: The message received.
"""
print(f"Feed {feed_id} received new value: {message}")
feed_id = feed_id.split("/")[-1]
feed = self.feeds[feed_id]
feed.last_val = message
self.update_text(client, feed_id, str(message))
def publish(self, feed: Feed, message: str) -> None:
"""Callback for publishing a message
:param Feed feed: The feed to publish to.
:param str message: The message to publish.
"""
print(f"Publishing {message} to {feed}")
self.io_mqtt.publish(feed, message)
def loop(self) -> None:
"""Loops Adafruit IO and also checks to see if any buttons have been pressed"""
self.io_mqtt.loop()
if self.select.value:
feed = self.feeds[list(self.feeds.keys())[self.selected - 1]]
if feed.pub:
feed.pub(feed.last_val)
self.display.root_group = self.splash
while self.select.value:
pass
if self.down.value and self.selected < self.length + 1:
rgb = self.splash[self.selected].color
color = (
((255 - ((rgb >> 16) & 0xFF)) << 16)
+ ((255 - ((rgb >> 8) & 0xFF)) << 8)
+ (255 - (rgb & 0xFF))
)
self.splash[self.selected].color = color
self.rect.y += 30
self.selected += 1
rgb = self.splash[self.selected].color
color = (
((255 - ((rgb >> 16) & 0xFF)) << 16)
+ ((255 - ((rgb >> 8) & 0xFF)) << 8)
+ (255 - (rgb & 0xFF))
)
self.splash[self.selected].color = color
if self.up_btn.value and self.selected > 1:
rgb = self.splash[self.selected].color
color = (
((255 - ((rgb >> 16) & 0xFF)) << 16)
+ ((255 - ((rgb >> 8) & 0xFF)) << 8)
+ (255 - (rgb & 0xFF))
)
self.splash[self.selected].color = color
self.rect.y -= 30
self.selected -= 1
rgb = self.splash[self.selected].color
color = (
((255 - ((rgb >> 16) & 0xFF)) << 16)
+ ((255 - ((rgb >> 8) & 0xFF)) << 8)
+ (255 - (rgb & 0xFF))
)
self.splash[self.selected].color = color