2
2
#
3
3
# SPDX-License-Identifier: Unlicense
4
4
5
- import os
6
- import sdcardio
7
- import board
8
- import storage
9
- import adafruit_floppy
10
-
11
5
"""DOS floppy archiver for Adafruit Floppsy
12
6
13
7
Insert an SD card & hook up your floppy drive.
14
8
Open the REPL / serial connection
15
9
Insert a floppy and press Enter to archive it
16
10
Do this for as many floppies as you like."""
17
11
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
+
18
25
floppy = adafruit_floppy .Floppy (
19
26
densitypin = board .DENSITY ,
20
27
indexpin = board .INDEX ,
34
41
)
35
42
36
43
_image_counter = 0
44
+ last_filename = None
37
45
38
46
39
47
def open_next_image (extension = "img" ):
40
48
"""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
42
50
try :
43
51
os .stat ("/sd" )
44
52
except OSError as exc : # no SD card!
@@ -51,21 +59,63 @@ def open_next_image(extension="img"):
51
59
except OSError :
52
60
break
53
61
print ("Writing to" , filename )
62
+ last_filename = filename
54
63
return open (filename , "wb" )
55
64
56
65
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
60
106
61
107
dev = None
62
108
blockdata = bytearray (512 )
63
109
baddata = b"BADDATA0" * 64
110
+ assert len (baddata ) == len (blockdata )
64
111
65
112
while True :
66
113
if dev is not None :
67
114
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" )
69
119
70
120
try :
71
121
if dev is None :
@@ -77,6 +127,11 @@ def open_next_image(extension="img"):
77
127
print (e )
78
128
continue
79
129
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
+
80
135
bad_blocks = good_blocks = 0
81
136
total_blocks = dev .count ()
82
137
pertrack = dev .sectors * dev .heads
@@ -98,7 +153,8 @@ def open_next_image(extension="img"):
98
153
if i % pertrack == (pertrack - 1 ):
99
154
print ()
100
155
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