Skip to content

Commit dd7617b

Browse files
committed
\o/ basics of the text slide working
1 parent 45d5329 commit dd7617b

File tree

1 file changed

+84
-34
lines changed

1 file changed

+84
-34
lines changed

adafruit_slideshow.py

Lines changed: 84 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
import random
4646
import displayio
4747
import json
48+
try:
49+
from adafruit_display_text import bitmap_label
50+
import terminalio
51+
TEXT_SLIDES_ENABLED = True
52+
except ImportError:
53+
TEXT_SLIDES_ENABLED = False
4854

4955
__version__ = "0.0.0-auto.0"
5056
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Slideshow.git"
@@ -192,13 +198,14 @@ def __init__(
192198
v_align=VerticalAlignment.TOP,
193199
):
194200
def _check_json_file(file):
195-
with open(file) as f:
196-
try:
197-
json_data = json.loads(f.read())
198-
if "text" in json_data:
199-
return True
200-
except ValueError:
201-
return False
201+
if TEXT_SLIDES_ENABLED:
202+
with open(file) as f:
203+
try:
204+
json_data = json.loads(f.read())
205+
if "text" in json_data:
206+
return True
207+
except ValueError:
208+
return False
202209
return False
203210
self.loop = loop
204211
"""Specifies whether to loop through the slides continuously or play through the list once.
@@ -323,6 +330,42 @@ def _fade_down(self):
323330
self._set_backlight(self.brightness * i / steps)
324331
time.sleep(0.01)
325332

333+
def _create_label(self, file):
334+
json_data = json.loads(file.read())
335+
label = bitmap_label.Label(terminalio.FONT, text=json_data['text'])
336+
if "h_align" not in json_data or json_data["h_align"] == "LEFT":
337+
x_anchor_point = 0.0
338+
x_anchored_position = 0
339+
elif json_data["h_align"] == "CENTER":
340+
x_anchor_point = 0.5
341+
x_anchored_position = self._display.width // 2
342+
elif json_data["h_align"] == "RIGHT":
343+
x_anchor_point = 1.0
344+
x_anchored_position = self._display.width - 1
345+
else:
346+
# wrong value for align
347+
x_anchor_point = 0.0
348+
x_anchored_position = 0
349+
350+
if "v_align" not in json_data or json_data["v_align"] == "TOP":
351+
y_anchor_point = 0.0
352+
y_anchored_position = 0
353+
elif json_data["v_align"] == "CENTER":
354+
y_anchor_point = 0.5
355+
y_anchored_position = self._display.height // 2
356+
elif json_data["v_align"] == "BOTTOM":
357+
y_anchor_point = 1.0
358+
y_anchored_position = self._display.height - 1
359+
else:
360+
# wrong value for align
361+
y_anchor_point = 0.0
362+
y_anchored_position = 0
363+
364+
label.anchor_point = (x_anchor_point, y_anchor_point)
365+
label.anchored_position = (x_anchored_position, y_anchored_position)
366+
return label
367+
368+
326369
def update(self):
327370
"""Updates the slideshow to the next image."""
328371
now = time.monotonic()
@@ -344,7 +387,8 @@ def advance(self):
344387
# Try to load slides until a valid file is found or we run out of options. This
345388
# loop stops because we either set odb or reduce the length of _file_list.
346389
odb = None
347-
while not odb and self._file_list:
390+
lbl = None
391+
while not odb and not lbl and self._file_list:
348392
if 0 <= self._current_slide_index < len(self._file_list):
349393
pass
350394
elif not self.loop:
@@ -357,35 +401,41 @@ def advance(self):
357401
self._current_slide_index -= slide_count
358402
self._reorder_slides()
359403

360-
image_name = self._file_list[self._current_slide_index]
361-
self._slide_file = open(image_name, "rb")
362-
try:
363-
odb = displayio.OnDiskBitmap(self._slide_file)
364-
except ValueError:
365-
self._slide_file.close()
366-
self._slide_file = None
367-
del self._file_list[self._current_slide_index]
368-
369-
if not odb:
370-
raise RuntimeError("No valid images")
371-
372-
if self._h_align == HorizontalAlignment.RIGHT:
373-
self._group.x = self._display.width - odb.width
374-
elif self._h_align == HorizontalAlignment.CENTER:
375-
self._group.x = round(self._display.width / 2 - odb.width / 2)
376-
else:
377-
self._group.x = 0
404+
file_name = self._file_list[self._current_slide_index]
405+
self._slide_file = open(file_name, "rb")
406+
if file_name.endswith(".bmp"):
407+
try:
408+
odb = displayio.OnDiskBitmap(self._slide_file)
409+
except ValueError:
410+
self._slide_file.close()
411+
self._slide_file = None
412+
del self._file_list[self._current_slide_index]
413+
elif file_name.endswith(".json"):
414+
lbl = self._create_label(self._slide_file)
415+
416+
if not odb and not lbl:
417+
raise RuntimeError("No valid images or text json files")
418+
419+
if odb:
420+
if self._h_align == HorizontalAlignment.RIGHT:
421+
self._group.x = self._display.width - odb.width
422+
elif self._h_align == HorizontalAlignment.CENTER:
423+
self._group.x = round(self._display.width / 2 - odb.width / 2)
424+
else:
425+
self._group.x = 0
378426

379-
if self._v_align == VerticalAlignment.BOTTOM:
380-
self._group.y = self._display.height - odb.height
381-
elif self._v_align == VerticalAlignment.CENTER:
382-
self._group.y = round(self._display.height / 2 - odb.height / 2)
383-
else:
384-
self._group.y = 0
427+
if self._v_align == VerticalAlignment.BOTTOM:
428+
self._group.y = self._display.height - odb.height
429+
elif self._v_align == VerticalAlignment.CENTER:
430+
self._group.y = round(self._display.height / 2 - odb.height / 2)
431+
else:
432+
self._group.y = 0
385433

386-
image_tilegrid = displayio.TileGrid(odb, pixel_shader=displayio.ColorConverter())
434+
image_tilegrid = displayio.TileGrid(odb, pixel_shader=displayio.ColorConverter())
387435

388-
self._group.append(image_tilegrid)
436+
self._group.append(image_tilegrid)
437+
if lbl:
438+
self._group.append(lbl)
389439

390440
if hasattr(self._display, "refresh"):
391441
self._display.refresh()

0 commit comments

Comments
 (0)