Skip to content

Commit 4636370

Browse files
committed
Improve example with better messages & use of onboard buttons
1 parent 1ad529d commit 4636370

File tree

1 file changed

+71
-15
lines changed

1 file changed

+71
-15
lines changed

examples/dos_archiver.py

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5-
import os
6-
import sdcardio
7-
import board
8-
import storage
9-
import adafruit_floppy
10-
115
"""DOS floppy archiver for Adafruit Floppsy
126
137
Insert an SD card & hook up your floppy drive.
148
Open the REPL / serial connection
159
Insert a floppy and press Enter to archive it
1610
Do this for as many floppies as you like."""
1711

12+
import os
13+
import sdcardio
14+
import board
15+
import storage
16+
import usb_cdc
17+
import adafruit_aw9523
18+
import adafruit_floppy
19+
20+
i2c = board.I2C() # uses board.SCL and board.SDA
21+
aw = adafruit_aw9523.AW9523(i2c)
22+
aw.directions = 0
23+
KEY_BITS = 0xF
24+
1825
floppy = adafruit_floppy.Floppy(
1926
densitypin=board.DENSITY,
2027
indexpin=board.INDEX,
@@ -34,11 +41,12 @@
3441
)
3542

3643
_image_counter = 0
44+
last_filename = None
3745

3846

3947
def open_next_image(extension="img"):
4048
"""Return an opened numbered file on the sdcard, such as "img01234.jpg"."""
41-
global _image_counter # pylint: disable=global-statement
49+
global _image_counter, last_filename # pylint: disable=global-statement
4250
try:
4351
os.stat("/sd")
4452
except OSError as exc: # no SD card!
@@ -51,21 +59,63 @@ def open_next_image(extension="img"):
5159
except OSError:
5260
break
5361
print("Writing to", filename)
62+
last_filename = filename
5463
return open(filename, "wb")
5564

5665

57-
sdcard = sdcardio.SDCard(board.SPI(), board.SD_CS)
58-
vfs = storage.VfsFat(sdcard)
59-
storage.mount(vfs, "/sd")
66+
def smart_input(prompt):
67+
print(end=prompt)
68+
69+
console = usb_cdc.console
70+
serial_connected = console.connected
71+
console.flush()
72+
keys = aw.inputs & KEY_BITS
73+
74+
while True:
75+
new_connected = console.connected
76+
if new_connected and not serial_connected:
77+
print(end="\r")
78+
print(end=prompt)
79+
serial_connected = new_connected
80+
81+
if n := console.in_waiting:
82+
console.read(n)
83+
break
84+
85+
new_keys = aw.inputs & KEY_BITS
86+
if ~new_keys & keys: # A bit went to 0 -> a key was pressed
87+
break
88+
keys = new_keys
89+
90+
print()
91+
92+
93+
print("\033[H\033[2JFloppy Archiver")
94+
print("Archive standard DOS floppies to SD card in IMG format")
95+
print()
96+
97+
try:
98+
sdcard = sdcardio.SDCard(board.SPI(), board.SD_CS)
99+
vfs = storage.VfsFat(sdcard)
100+
storage.mount(vfs, "/sd")
101+
print("Mounted SD card.")
102+
except Exception as e:
103+
print("Failed to mount SD card:")
104+
print(e)
105+
raise SystemExit # pylint: disable=raise-missing-from
60106

61107
dev = None
62108
blockdata = bytearray(512)
63109
baddata = b"BADDATA0" * 64
110+
assert len(baddata) == len(blockdata)
64111

65112
while True:
66113
if dev is not None:
67114
dev.floppy.keep_selected = False
68-
input("Insert disk and press ENTER")
115+
vfsstat = vfs.statvfs(vfs)
116+
avail = vfsstat[0] * vfsstat[4] / 1024 / 1024
117+
print(f"/sd: {avail:.1f}MiB available")
118+
smart_input("Insert disk and press any key")
69119

70120
try:
71121
if dev is None:
@@ -77,6 +127,11 @@ def open_next_image(extension="img"):
77127
print(e)
78128
continue
79129

130+
dev.readblocks(0, blockdata)
131+
label = blockdata[43:54].decode("ascii", "replace").strip()
132+
fstype = blockdata[54:61].decode("ascii", "replace").strip()
133+
print(f"\033[H\033[2JArchiving {label!r} ({fstype!r})")
134+
80135
bad_blocks = good_blocks = 0
81136
total_blocks = dev.count()
82137
pertrack = dev.sectors * dev.heads
@@ -98,7 +153,8 @@ def open_next_image(extension="img"):
98153
if i % pertrack == (pertrack - 1):
99154
print()
100155

101-
print(
102-
f"{good_blocks} good + {bad_blocks} bad blocks",
103-
f"out of {total_blocks} ({total_blocks//2}KiB)",
104-
)
156+
print()
157+
print(f"Archived {label!r} to {last_filename.split('/')[-1]}")
158+
print(f"{good_blocks} good + {bad_blocks} bad blocks")
159+
print(f"out of {total_blocks} ({total_blocks//2}KiB)")
160+
print()

0 commit comments

Comments
 (0)