-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdcard.cpp
138 lines (128 loc) · 3.39 KB
/
sdcard.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2018 Andreas Signer <[email protected]>
*
* This file is part of kosmos_tape_emulator.
*
* kosmos_tape_emulator is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* kosmos_tape_emulator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with kosmos_tape_tool. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sdcard.h"
#include "config.h"
#include "lcd.h"
#include <SD.h>
// SD-Card (other pins are default ICSP pins)
#define SD_CS 8
int nofFiles = 0;
void sdcard_init() {
if (!SD.begin(SD_CS)) {
lcd_showStatus(CENTER, "SDC not ready!");
while (1);
}
File root = SD.open("/");
for (;;) {
File entry = root.openNextFile();
if (! entry) {
// no more files
break;
}
if (!entry.isDirectory()) {
nofFiles++;
}
entry.close();
}
}
int sdcard_filesAvailable() {
return nofFiles;
}
bool sdcard_getFileName(int i, char* dest) {
if (i < 0 || i >= nofFiles) {
return false;
}
File root = SD.open("/");
for (;;) {
File entry = root.openNextFile();
if (! entry) {
// no more files
break;
}
if (!entry.isDirectory()) {
if (i-- == 0) {
strncpy(dest, entry.name(), NAME_LEN);
entry.close();
return true;
}
}
entry.close();
}
return false;
}
void sdcard_pickFileName(char* dest) {
// Find max number
int max = -1;
File root = SD.open("/");
for (;;) {
File entry = root.openNextFile();
if (! entry) {
// no more files
break;
}
if (entry.isDirectory()) {
continue;
}
strncpy(dest, entry.name(), NAME_LEN);
#ifdef DEBUG
Serial.print("sdcard_pickFileName: name = "); Serial.println(dest);
#endif
if (strncmp(dest, "IMG", 3) == 0 && strncmp(&dest[6], ".BIN", 4) == 0 && isdigit(dest[3]) && isdigit(dest[4]) && isdigit(dest[5])) {
int nr = (dest[3]-'0')*100 + (dest[4]-'0')*10 + (dest[5]-'0');
#ifdef DEBUG
Serial.print("sdcard_pickFileName: nr = "); Serial.println(nr, DEC);
#endif
if (nr > max) {
max = nr;
#ifdef DEBUG
Serial.print("sdcard_pickFileName: new max = "); Serial.println(max, DEC);
#endif
}
}
entry.close();
}
sprintf(dest, "IMG%03d.BIN", max+1);
#ifdef DEBUG
Serial.print("sdcard_pickFileName: result = "); Serial.println(dest);
#endif
}
int sdcard_load(const char *filename, uint8_t *buf) {
File file = SD.open(filename, FILE_READ);
int len = file.size();
if (len != 256 && len != 512) {
lcd_showTitle(CENTER, "Error!");
lcd_showStatus(CENTER, "Invalid length.");
delay(2000);
return 0;
}
file.read(buf, len);
file.close();
return len;
}
void sdcard_save(const char *filename, uint8_t *buf, int len) {
if (len != 256 && len != 512) {
lcd_showTitle(CENTER, "Error!");
lcd_showStatus(CENTER, "Invalid length.");
delay(2000);
return;
}
File file = SD.open(filename, FILE_WRITE);
file.write(buf, len);
file.close();
}