Skip to content

Commit

Permalink
Merge pull request #21 from makermelissa/master
Browse files Browse the repository at this point in the history
Added alignment for images smaller/larger than display
  • Loading branch information
tannewt committed Sep 23, 2020
2 parents dbea5a0 + 0e55f95 commit a92f6ab
Showing 1 changed file with 80 additions and 4 deletions.
84 changes: 80 additions & 4 deletions adafruit_slideshow.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
====================================================
CircuitPython helper library for displaying a slideshow of images on a display.
* Author(s): Kattni Rembor, Carter Nelson, Roy Hooper
* Author(s): Kattni Rembor, Carter Nelson, Roy Hooper, Melissa LeBlanc-Williams
Implementation Notes
--------------------
Expand All @@ -49,6 +49,26 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Slideshow.git"


class HorizontalAlignment:
"""Defines possible horizontal alignment orders."""

# pylint: disable=too-few-public-methods
LEFT = 1
CENTER = 2
RIGHT = 3
# pylint: enable=too-few-public-methods


class VerticalAlignment:
"""Defines possible vertical alignment orders."""

# pylint: disable=too-few-public-methods
TOP = 1
CENTER = 2
BOTTOM = 3
# pylint: enable=too-few-public-methods


class PlayBackOrder:
"""Defines possible slideshow playback orders."""

Expand Down Expand Up @@ -102,6 +122,10 @@ class SlideShow:
:param PlayBackDirection direction: The playback direction.
:param HorizonalAlignment h_align: The Horizontal alignment of smaller/larger images
:param VerticalAlignment v_align: The Vertical alignment of smaller/larger images
Example code for Hallowing Express. With this example, the slideshow will play through once
in alphabetical order:
Expand Down Expand Up @@ -162,7 +186,9 @@ def __init__(
dwell=3,
fade_effect=True,
auto_advance=True,
direction=PlayBackDirection.FORWARD
direction=PlayBackDirection.FORWARD,
h_align=HorizontalAlignment.LEFT,
v_align=VerticalAlignment.TOP,
):
self.loop = loop
"""Specifies whether to loop through the images continuously or play through the list once.
Expand Down Expand Up @@ -196,6 +222,10 @@ def __init__(
"""The order in which the images display. You can choose random (``RANDOM``) or
alphabetical (``ALPHA``)."""

# Default positioning
self._h_align = h_align
self._v_align = v_align

self._current_image = -1
self._image_file = None
self._brightness = 0.5
Expand Down Expand Up @@ -289,9 +319,9 @@ def update(self):

return self.advance()

# pylint: disable=too-many-branches
def advance(self):
"""Displays the next image. Returns True when a new image was displayed, False otherwise.
"""
"""Displays the next image. Returns True when a new image was displayed, False otherwise."""
if self._image_file:
self._fade_down()
self._group.pop()
Expand Down Expand Up @@ -328,6 +358,20 @@ def advance(self):
if not odb:
raise RuntimeError("No valid images")

if self._h_align == HorizontalAlignment.RIGHT:
self._group.x = self._display.width - odb.width
elif self._h_align == HorizontalAlignment.CENTER:
self._group.x = round(self._display.width / 2 - odb.width / 2)
else:
self._group.x = 0

if self._v_align == VerticalAlignment.BOTTOM:
self._group.y = self._display.height - odb.height
elif self._v_align == VerticalAlignment.CENTER:
self._group.y = round(self._display.height / 2 - odb.height / 2)
else:
self._group.y = 0

try:
sprite = self._sprite_class(odb, pixel_shader=displayio.ColorConverter())
except TypeError:
Expand All @@ -340,3 +384,35 @@ def advance(self):
self._img_start = time.monotonic()

return True

# pylint: enable=too-many-branches

@property
def h_align(self):
"""Get or Set the Horizontal Alignment"""
return self._h_align

@h_align.setter
def h_align(self, val):
if val not in (
HorizontalAlignment.LEFT,
HorizontalAlignment.CENTER,
HorizontalAlignment.RIGHT,
):
raise ValueError("Alignment must be LEFT, RIGHT, or CENTER")
self._h_align = val

@property
def v_align(self):
"""Get or Set the Vertical Alignment"""
return self._v_align

@v_align.setter
def v_align(self, val):
if val not in (
VerticalAlignment.TOP,
VerticalAlignment.CENTER,
VerticalAlignment.BOTTOM,
):
raise ValueError("Alignment must be TOP, BOTTOM, or CENTER")
self._v_align = val

0 comments on commit a92f6ab

Please sign in to comment.