Skip to content

Commit 4518e07

Browse files
committed
More improvements
1 parent c66123f commit 4518e07

File tree

1 file changed

+49
-27
lines changed

1 file changed

+49
-27
lines changed

adafruit_matrixportal/matrixportal.py

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ def __init__(
7979
):
8080

8181
self._debug = debug
82-
self._graphics = Graphics(
82+
self.graphics = Graphics(
8383
default_bg=default_bg, bit_depth=bit_depth, width=64, height=32, debug=debug
8484
)
85-
self.display = self._graphics.display
85+
self.display = self.graphics.display
8686

87-
self._network = Network(
87+
self.network = Network(
8888
status_neopixel=status_neopixel,
8989
esp=esp,
9090
external_spi=external_spi,
@@ -100,11 +100,11 @@ def __init__(
100100

101101
self._regexp_path = regexp_path
102102

103-
self.splash = self._graphics.splash
103+
self.splash = self.graphics.splash
104104

105105
# Add any JSON translators
106106
if json_transform:
107-
self._network.ad.json_transform(json_transform)
107+
self.network.add_json_transform(json_transform)
108108

109109
self._text = []
110110
self._text_color = []
@@ -166,15 +166,31 @@ def add_text(
166166
if self._debug:
167167
print("Init text area")
168168
self._text.append(None)
169-
self._text_color.append(text_color)
169+
self._text_color.append(self.html_color_convert(text_color))
170170
self._text_position.append(text_position)
171171
self._text_wrap.append(text_wrap)
172172
self._text_maxlen.append(text_maxlen)
173173
self._text_transform.append(text_transform)
174174
self._text_scrolling.append(scrolling)
175175

176+
if scrolling and self._scrolling_index is None: # Not initialized yet
177+
self._scrolling_index = self._get_next_scrollable_text_index()
178+
176179
# pylint: enable=too-many-arguments
177180

181+
@staticmethod
182+
def html_color_convert(color):
183+
"""Convert an HTML color code to an integer
184+
185+
:param color: The color value to be converted
186+
187+
"""
188+
if isinstance(color, str):
189+
if color[0] == "#":
190+
color = color.lstrip("#")
191+
return int(color, 16)
192+
return color # Return unconverted
193+
178194
def set_headers(self, headers):
179195
"""Set the headers used by fetch().
180196
@@ -189,7 +205,7 @@ def set_background(self, file_or_color, position=None):
189205
:param file_or_color: The filename of the chosen background image, or a hex color.
190206
191207
"""
192-
self._graphics.set_background(file_or_color, position)
208+
self.graphics.set_background(file_or_color, position)
193209

194210
def preload_font(self, glyphs=None):
195211
# pylint: disable=line-too-long
@@ -205,6 +221,18 @@ def preload_font(self, glyphs=None):
205221
if self._text_font and self._text_font is not terminalio.FONT:
206222
self._text_font.load_glyphs(glyphs)
207223

224+
def set_text_color(self, color, index=0):
225+
"""Update the text color, with indexing into our list of text boxes.
226+
227+
:param int color: The color value to be used
228+
:param index: Defaults to 0.
229+
230+
"""
231+
if self._text[index]:
232+
color = self.html_color_convert(color)
233+
self._text_color[index] = color
234+
self._text[index].color = color
235+
208236
def set_text(self, val, index=0):
209237
"""Display text, with indexing into our list of text boxes.
210238
@@ -248,7 +276,7 @@ def set_text(self, val, index=0):
248276

249277
def get_local_time(self, location=None):
250278
"""Accessor function for get_local_time()"""
251-
return self._network.get_local_time(location=location)
279+
return self.network.get_local_time(location=location)
252280

253281
def _get_next_scrollable_text_index(self):
254282
index = self._scrolling_index
@@ -272,7 +300,7 @@ def push_to_io(self, feed_key, data):
272300
273301
"""
274302

275-
self._network.push_to_io(feed_key, data)
303+
self.network.push_to_io(feed_key, data)
276304

277305
def get_io_data(self, feed_key):
278306
"""Return all values from the Adafruit IO Feed Data that matches the feed key
@@ -281,7 +309,7 @@ def get_io_data(self, feed_key):
281309
282310
"""
283311

284-
return self._network.get_io_data(feed_key)
312+
return self.network.get_io_data(feed_key)
285313

286314
def get_io_feed(self, feed_key, detailed=False):
287315
"""Return the Adafruit IO Feed that matches the feed key
@@ -290,15 +318,15 @@ def get_io_feed(self, feed_key, detailed=False):
290318
:param bool detailed: Whether to return additional detailed information
291319
292320
"""
293-
return self._network.get_io_feed(feed_key, detailed)
321+
return self.network.get_io_feed(feed_key, detailed)
294322

295323
def get_io_group(self, group_key):
296324
"""Return the Adafruit IO Group that matches the group key
297325
298326
:param str group_key: Name of group key to match.
299327
300328
"""
301-
return self._network.get_io_group(group_key)
329+
return self.network.get_io_group(group_key)
302330

303331
def scroll(self):
304332
"""Scroll any text that needs scrolling by a single frame. We also
@@ -307,32 +335,26 @@ def scroll(self):
307335
"""
308336

309337
if self._scrolling_index is None: # Not initialized yet
310-
next_index = self._get_next_scrollable_text_index()
311-
if next_index is None:
312-
return
313-
self._scrolling_index = next_index
338+
return
314339

315340
self._text[self._scrolling_index].x = self._text[self._scrolling_index].x - 1
316341
line_width = self._text[self._scrolling_index].bounding_box[2]
317342
if self._text[self._scrolling_index].x < -line_width:
318343
# Find the next line
319344
self._scrolling_index = self._get_next_scrollable_text_index()
320-
self._text[self._scrolling_index].x = self._graphics.display.width
345+
self._text[self._scrolling_index].x = self.graphics.display.width
321346

322347
def scroll_text(self, frame_delay=0.02):
323348
"""Scroll the entire text all the way across. We also
324349
we want to queue up multiple lines one after another. To get
325350
simultaneous lines, we can simply use a line break.
326351
"""
327352
if self._scrolling_index is None: # Not initialized yet
328-
next_index = self._get_next_scrollable_text_index()
329-
if next_index is None:
330-
return
331-
self._scrolling_index = next_index
353+
return
332354

333-
self._text[self._scrolling_index].x = self._graphics.display.width
355+
self._text[self._scrolling_index].x = self.graphics.display.width
334356
line_width = self._text[self._scrolling_index].bounding_box[2]
335-
for _ in range(self._graphics.display.width + line_width + 1):
357+
for _ in range(self.graphics.display.width + line_width + 1):
336358
self.scroll()
337359
sleep(frame_delay)
338360

@@ -345,7 +367,7 @@ def fetch(self, refresh_url=None, timeout=10):
345367
self._url = refresh_url
346368
values = []
347369

348-
values = self._network.fetch_data(
370+
values = self.network.fetch_data(
349371
self._url,
350372
headers=self._headers,
351373
json_path=self._json_path,
@@ -412,10 +434,10 @@ def url(self):
412434
@url.setter
413435
def url(self, value):
414436
self._url = value
415-
if value and not self._network.uselocal:
416-
self._network.connect()
437+
if value and not self.network.uselocal:
438+
self.network.connect()
417439
if self._debug:
418-
print("My IP address is", self._network.ip_address)
440+
print("My IP address is", self.network.ip_address)
419441

420442
@property
421443
def json_path(self):

0 commit comments

Comments
 (0)