Skip to content

Commit 0a88397

Browse files
authored
Add files via upload
1 parent 31d3b15 commit 0a88397

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

PyPortal_Floppy_with_Display/code.py

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
# SPDX-FileCopyrightText: 2023 Anne Barela for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Faux Floppy Disk with LCD Screen
6+
# Display file icons on screen
7+
8+
import os
9+
import board
10+
import time
11+
import displayio
12+
import adafruit_imageload
13+
import terminalio
14+
import adafruit_touchscreen
15+
from adafruit_display_text import label
16+
from adafruit_display_shapes.rect import Rect
17+
18+
# Get a dictionary of filenames at the passed base directory
19+
# each entry is a tuple (filename, bool) where bool = true
20+
# means the filename is a directory, else false.
21+
def get_files(base):
22+
files = os.listdir(base)
23+
filenames = []
24+
for i, file in enumerate(files):
25+
if not file.startswith("."):
26+
if (file != "boot_out.txt") and (file != "System Volume Information"):
27+
stats = os.stat(base + file)
28+
isdir = stats[0] & 0x4000
29+
if isdir:
30+
filenames.append((file, True))
31+
else:
32+
filenames.append((file, False))
33+
print("Files found = ", len(filenames))
34+
return filenames
35+
36+
def get_touch(ts):
37+
p = None
38+
while p is None:
39+
time.sleep(0.05)
40+
p = ts.touch_point
41+
return p[0]
42+
43+
# Icon Positions
44+
ICONSIZE = 48
45+
SPACING = 18
46+
LEFTSPACE = 38
47+
TOPSPACE = 10
48+
TEXTSPACE = 10
49+
ICONSACROSS = 4
50+
ICONSDOWN = 3
51+
PAGEMAXFILES = ICONSACROSS * ICONSDOWN # For the chosen display, this is the
52+
# maximum number of file icons that will fit
53+
# on the display at once (display dependent)
54+
# File Types
55+
BLANK = 0
56+
FILE = 1
57+
DIR = 2
58+
BMP = 3
59+
WAV = 4
60+
PY = 5
61+
RIGHT = 6
62+
LEFT = 7
63+
64+
# Use the builtin display
65+
display = board.DISPLAY
66+
WIDTH = board.DISPLAY.width
67+
HEIGHT = board.DISPLAY.height
68+
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
69+
board.TOUCH_YD, board.TOUCH_YU,
70+
calibration=((5200, 59000), (5800, 57000)),
71+
size=(WIDTH, HEIGHT))
72+
73+
# Create base display group
74+
displaygroup = displayio.Group()
75+
76+
# Load the bitmap (this is the "spritesheet")
77+
sprite_sheet, palette = adafruit_imageload.load("/icons.bmp")
78+
79+
background = Rect(0, 0, WIDTH - 1, HEIGHT - 1, fill=0x000000)
80+
displaygroup.append(background)
81+
82+
# Create enough sprites & labels for the icons that will fit on screen
83+
sprites = []
84+
labels = []
85+
for _ in range(PAGEMAXFILES):
86+
sprite = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
87+
width=1, height=1, tile_height=48,
88+
tile_width=48,)
89+
sprites.append(sprite) # Append the sprite to the sprite array
90+
displaygroup.append(sprite)
91+
filelabel = label.Label(terminalio.FONT, color=0xFFFFFF)
92+
labels.append(filelabel)
93+
displaygroup.append(filelabel)
94+
95+
# Make the more files and less files icons (> <)
96+
moresprite = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
97+
width=1, height=1, tile_height=48,
98+
tile_width=48,)
99+
displaygroup.append(moresprite)
100+
moresprite.x = WIDTH - ICONSIZE + TEXTSPACE
101+
moresprite.y = int((HEIGHT - ICONSIZE) / 2)
102+
lesssprite = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
103+
width=1, height=1, tile_height=48,
104+
tile_width=48,)
105+
displaygroup.append(lesssprite)
106+
lesssprite.x = -10
107+
lesssprite.y = int((HEIGHT - ICONSIZE) / 2)
108+
109+
display.show(displaygroup)
110+
111+
filecount = 0
112+
xpos = LEFTSPACE
113+
ypos = TOPSPACE
114+
115+
base = "/" # Get file names in base directory
116+
filenames = get_files(base)
117+
118+
currentfile = 0 # Which file is being processed in all files
119+
spot = 0 # Which spot on the screen is getting a file icon
120+
PAGE = 1 # Which page of icons is displayed on screen, 1 is first
121+
122+
while True:
123+
if currentfile < len(filenames) and spot < PAGEMAXFILES:
124+
filename, dir = filenames[currentfile]
125+
if dir:
126+
type = DIR
127+
elif filename.endswith(".bmp"):
128+
type = BMP
129+
elif filename.endswith(".wav"):
130+
type = WAV
131+
elif filename.endswith(".py"):
132+
type = PY
133+
else:
134+
type = FILE
135+
# Set icon location information and icon type
136+
sprites[spot].x = xpos
137+
sprites[spot].y = ypos
138+
sprites[spot][0] = type
139+
#
140+
# Set filename
141+
labels[spot].x = xpos
142+
labels[spot].y = ypos + ICONSIZE + TEXTSPACE
143+
# The next line gets the filename without the extension, first 11 chars
144+
labels[spot].text = filename.rsplit('.', 1)[0][0:10]
145+
# labels[spot].anchor_point = (0.5, 1.0)
146+
147+
currentpage = PAGE
148+
149+
# Pagination Handling
150+
if spot >= PAGEMAXFILES - 1:
151+
if currentfile < (len(filenames) + 1):
152+
# Need to display the greater than touch sprite
153+
moresprite[0] = RIGHT
154+
else:
155+
# Blank out more and extra icon spaces
156+
moresprite[0] = BLANK
157+
if PAGE > 1: # Need to display the less than touch sprite
158+
lesssprite[0] = LEFT
159+
else:
160+
lesssprite[0] = BLANK
161+
162+
# Time to check for user touch of screen (BLOCKING)
163+
touch_x = get_touch(ts)
164+
print("Touch Registered ")
165+
# Check if touch_x is around the LEFT or RIGHT arrow
166+
currentpage = PAGE
167+
if touch_x >= int(WIDTH - ICONSIZE): # > Touched
168+
if moresprite[0] != BLANK: # Ensure there are more
169+
if spot == (PAGEMAXFILES - 1): # Page full
170+
if currentfile < (len(filenames)): # and more files
171+
PAGE = PAGE + 1 # Increment page
172+
print("> Touched! ")
173+
if touch_x <= ICONSIZE: # < Touched
174+
if PAGE > 1:
175+
PAGE = PAGE - 1 # Decrement page
176+
print("< Touched! ")
177+
else:
178+
lesssprite[0] = BLANK # Not show < for first page
179+
print("Page ", PAGE)
180+
# print("currentfile after page =", currentfile)
181+
# Icon Positioning
182+
# print("currentpage = ", currentpage)
183+
# print("PAGE = ", PAGE)
184+
if PAGE != currentpage: # We have a page change
185+
# Reset icon locations to upper left
186+
xpos = LEFTSPACE
187+
ypos = TOPSPACE
188+
spot = 0
189+
if currentpage > PAGE:
190+
# Decrement files by a page (current page & previous page)
191+
currentfile = currentfile - (PAGEMAXFILES * 2) + 1
192+
else:
193+
# Forward go to the next file
194+
currentfile = currentfile + 1
195+
else:
196+
currentfile += 1 # Increment file counter
197+
spot += 1 # Increment icon space counter
198+
if spot == PAGEMAXFILES: # Last page ended with
199+
print("hit")
200+
# calculate next icon location
201+
if spot % ICONSACROSS: # not at end of icon row
202+
xpos += SPACING + ICONSIZE
203+
else: # start new icon row
204+
ypos += ICONSIZE + SPACING + TEXTSPACE
205+
xpos = LEFTSPACE
206+
# End If Changed Page
207+
# Blank out rest if needed
208+
if currentfile == len(filenames):
209+
print("blanking")
210+
for i in range(spot, PAGEMAXFILES):
211+
print("blanking spot ", i)
212+
sprites[i][0] = BLANK
213+
labels[i].text = " "
214+
# End while
215+
216+
print("At end of program")
217+
while True:
218+
pass
19.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)