-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisk.ino
112 lines (89 loc) · 2.41 KB
/
disk.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
void diskSetup() {
char key[10];
for (int d=0; d<MAX_DRIVE; d++) {
drive[d].filename[0] = 0;
drive[d].mounted = false;
sprintf(key, "Drive%d", d);
if (fdcPrefs.isKey(key)) {
fdcPrefs.getString(key, drive[d].filename, sizeof(drive[d].filename));
if (drive[d].filename[0]) {
mountDrive(d, drive[d].filename);
}
}
}
}
bool mountDrive(int driveno, const char *filename) {
if (driveno < 0 || driveno >= MAX_DRIVE) {
cliConsole->printf("Drive %d: invalid drive number\r\n");
return false;
}
// Try to initialize SD card if not ready
if (!sdReady) {
if (!sdSetup()) {
return false;
}
}
if (drive[driveno].mounted) {
unmountDrive(driveno);
}
if (SD.exists(filename)) {
drive[driveno].diskImg=SD.open(filename, "r+");
if (!drive[driveno].diskImg) {
cliConsole->printf("Drive %d: could not open '%s'\r\n", driveno, filename);
return false;
}
drive[driveno].mounted = true;
drive[driveno].size = drive[driveno].diskImg.size();
strncpy(drive[driveno].filename, filename, sizeof(drive[driveno].filename));
cliConsole->printf("Drive %d: mounted as '%s'.\r\n", driveno, filename+1);
lastDrive = -1;
lastTrack = -1;
confChanged = true;
}
else {
cliConsole->printf("File '%s' does not exist\r\n", filename);
}
return true;
}
void unmountDrive(int driveno) {
if (driveno >= 0 && driveno < MAX_DRIVE) {
if (drive[driveno].mounted || drive[driveno].diskImg) {
drive[driveno].diskImg.close();
drive[driveno].mounted = 0;
drive[driveno].filename[0]= 0;
cliConsole->printf("Drive %d: unmounted\r\n", driveno);
lastDrive = -1;
lastTrack = -1;
confChanged = true;
}
}
}
void listDir(fs::SDFS &fs, const char * dirname, uint8_t levels){
const char *f;
// Try to initialize SD card if not ready
if (!sdReady) {
if (!sdSetup()) {
return;
}
}
cliConsole->printf("Listing directory: %s\r\n", dirname);
File root = fs.open(dirname);
if(!root){
cliConsole->printf("Failed to open directory\r\n");
return;
}
if(!root.isDirectory()){
cliConsole->printf("Not a directory\r\n");
return;
}
File file = root.openNextFile();
while(file) {
f = file.name();
if (*f != '.') {
cliConsole->printf("%-32.32s %8d\r\n", f, file.size());
}
file.close();
file = root.openNextFile();
}
root.close();
}