Skip to content

Commit 81ac74c

Browse files
authored
Merge pull request #560 from adafruit/TheKitty-patch-60
Add Python file
2 parents 950f4f6 + 02c6e57 commit 81ac74c

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

PyPortal_ViewMaster/code.py

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

0 commit comments

Comments
 (0)