Skip to content

Commit a094e80

Browse files
authored
Merge pull request #2420 from adafruit/TheKitty-patch-3
PR for the Floppy with Display Learn Project
2 parents 0804d36 + c7056f0 commit a094e80

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SPDX-FileCopyrightText: 2023 Anne Barela for Adafruit Industries
2+
3+
SPDX-License-Identifier: MIT
4+
5+
This directory contains the files for the Adafruit Learning System project
6+
7+
A Floppy Thumb Drive with a Color File Icon Display
8+
9+
https://learn.adafruit.com/a-floppy-thumb-drive-with-a-color-file-icon-display/overview

PyPortal_Floppy_with_Display/code.py

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

0 commit comments

Comments
 (0)