code.py
  1  # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
  2  # SPDX-FileCopyrightText: 2020 Erin St Blaine for Adafruit Industries
  3  #
  4  # SPDX-License-Identifier: MIT
  5  
  6  """
  7  Slideshow Example using the Matrix Portal and 64 x 32 LED matrix display
  8  Written by Melissa LeBlanc-Williams and Erin St Blaine for Adafruit Industries
  9  Images smaller than 64 pixel width will be aligned alternating left or right
 10  Press Up button to pause/resume Slideshow
 11  Press Down button to cycle between slideshow folders
 12  """
 13  import time
 14  import board
 15  import adafruit_lis3dh
 16  import displayio
 17  import busio
 18  from digitalio import DigitalInOut, Pull
 19  from adafruit_matrixportal.matrix import Matrix
 20  from adafruit_slideshow import SlideShow, HorizontalAlignment
 21  from adafruit_debouncer import Debouncer
 22  
 23  '''
 24  Display will go to sleep after SLEEP_DURATION seconds have elapsed with no accelerometer movement
 25  Each slide will play for IMAGE_DURATION - customizable for each folder
 26  Add folders to the list to add more slideshows.
 27  '''
 28  
 29  SLEEP_DURATION = 60
 30  IMAGE_DURATION = (1, 0.5, 10)
 31  IMAGE_FOLDER = (
 32      "/bmps",
 33      "/bmps2",
 34      "/bmps3",
 35  )
 36  # pylint: disable=invalid-name
 37  # --- Display setup ---
 38  matrix = Matrix(bit_depth=6)
 39  display = matrix.display
 40  ACCEL = adafruit_lis3dh.LIS3DH_I2C(busio.I2C(board.SCL, board.SDA),
 41                                     address=0x19)
 42  _ = ACCEL.acceleration # Dummy reading to blow out any startup residue
 43  
 44  pin_down = DigitalInOut(board.BUTTON_DOWN)
 45  pin_down.switch_to_input(pull=Pull.UP)
 46  button_down = Debouncer(pin_down)
 47  pin_up = DigitalInOut(board.BUTTON_UP)
 48  pin_up.switch_to_input(pull=Pull.UP)
 49  button_up = Debouncer(pin_up)
 50  
 51  ALIGN_RIGHT = True
 52  auto_advance = True
 53  
 54  i = 0
 55  NUM_FOLDERS = 2 #first folder is numbered 0
 56  
 57  slideshow = SlideShow(
 58      display,
 59      None,
 60      folder=IMAGE_FOLDER[i],
 61      order=0,
 62      auto_advance=False,
 63      fade_effect=False,
 64      dwell=IMAGE_DURATION[i],
 65      h_align=HorizontalAlignment.RIGHT,
 66  )
 67  
 68  LAST_ADVANCE = time.monotonic()
 69  last_movement = time.monotonic()
 70  MODE = 1
 71  
 72  def advance():
 73      ''' go to the next slide '''
 74      # pylint: disable=global-statement
 75      global ALIGN_RIGHT, LAST_ADVANCE
 76      ALIGN_RIGHT = not ALIGN_RIGHT
 77      if ALIGN_RIGHT:
 78          slideshow.h_align = HorizontalAlignment.RIGHT
 79      else:
 80          slideshow.h_align = HorizontalAlignment.LEFT
 81      LAST_ADVANCE = time.monotonic()
 82      slideshow.advance()
 83  
 84  empty_group = displayio.Group()
 85  
 86  while True:
 87      if ACCEL.shake(shake_threshold=10):
 88          print("Moving!")
 89          if MODE == 0:
 90              slideshow = SlideShow(
 91                  display,
 92                  None,
 93                  folder=IMAGE_FOLDER[i],
 94                  order=0,
 95                  auto_advance=False,
 96                  fade_effect=False,
 97                  dwell=IMAGE_DURATION[i],
 98                  h_align=HorizontalAlignment.RIGHT,
 99              )
100          last_movement = time.monotonic()
101          MODE = 1
102      elif time.monotonic() > last_movement + SLEEP_DURATION:
103          MODE = 0
104          display.show(empty_group)
105      if MODE == 1:
106          if auto_advance and time.monotonic() > LAST_ADVANCE + IMAGE_DURATION[i]:
107              advance()
108          button_down.update()
109          button_up.update()
110          if button_up.fell:
111              auto_advance = not auto_advance
112          if button_down.fell:
113              i = i + 1
114              if i > NUM_FOLDERS:
115                  i = 0
116              slideshow = SlideShow(
117                  display,
118                  None,
119                  folder=IMAGE_FOLDER[i],
120                  order=0,
121                  auto_advance=False,
122                  fade_effect=False,
123                  dwell=IMAGE_DURATION[i],
124                  h_align=HorizontalAlignment.RIGHT,
125              )