code.py
 1  # SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import os
 6  import board
 7  import busio
 8  import digitalio
 9  import storage
10  import sdcardio
11  from adafruit_slideshow import PlayBackOrder, SlideShow, PlayBackDirection
12  
13  # Default location to look is in internal memory
14  IMAGE_DIRECTORY = "/images"
15  
16  switch = digitalio.DigitalInOut(board.D3)
17  switch.direction = digitalio.Direction.INPUT
18  switch.pull = digitalio.Pull.UP
19  
20  spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
21  try:
22      sdcard = sdcardio.SDCard(spi, board.SD_CS)
23      vfs = storage.VfsFat(sdcard)
24      storage.mount(vfs, "/sd")
25      IMAGE_DIRECTORY = "/sd/images"
26  except OSError as error:
27      print("No SD card, will only look on internal memory")
28  
29  def print_directory(path, tabs=0):
30      for file in os.listdir(path):
31          stats = os.stat(path + "/" + file)
32          filesize = stats[6]
33          isdir = stats[0] & 0x4000
34  
35          if filesize < 1000:
36              sizestr = str(filesize) + " by"
37          elif filesize < 1000000:
38              sizestr = "%0.1f KB" % (filesize / 1000)
39          else:
40              sizestr = "%0.1f MB" % (filesize / 1000000)
41  
42          prettyprintname = ""
43          for _ in range(tabs):
44              prettyprintname += "   "
45          prettyprintname += file
46          if isdir:
47              prettyprintname += "/"
48          print('{0:<20} Size: {1:>6}'.format(prettyprintname, sizestr))
49  
50          # recursively print directory contents
51          if isdir:
52              print_directory(path + "/" + file, tabs + 1)
53  
54  try:
55      print_directory(IMAGE_DIRECTORY)
56  except OSError as error:
57      raise Exception("No images found on flash or SD Card")
58  
59  # Create the slideshow object that plays through once alphabetically.
60  slideshow = SlideShow(board.DISPLAY, None, folder=IMAGE_DIRECTORY, loop=True,
61                        order=PlayBackOrder.ALPHABETICAL, dwell=0)
62  while True:
63      if not switch.value:
64          print("Click!")
65          slideshow.direction = PlayBackDirection.FORWARD
66          slideshow.advance()
67          while not switch.value:
68              pass