-
Notifications
You must be signed in to change notification settings - Fork 0
/
SD.ino
154 lines (135 loc) · 3.28 KB
/
SD.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
void nextAvailableFileName(){
fileNameFromNumber(ui.shutter_count);
while( fileExists( SD, next_filename )){ //check for next available filename
ui.shutter_count ++;
fileNameFromNumber(ui.shutter_count);
}
}
bool fileExists( fs::FS &fs, const char * path ){
File file = fs.open(path);
Serial.print( path );
if(!file){
Serial.println(" does not exist");
return false;
}
Serial.println(" exists");
return true;
}
void fileNameFromNumber(int number){
//next_filename = "0000000.jpg";
int place = 1;
int position = 6;
while(number > 0){
int value = ( number % (place * 10) );
int digit = value / place;
next_filename[position] = digit + 48;
number = number - value;
place = place * 10;
position = position - 1;
}
}
void readFile(fs::FS &fs, const char * path){
File file = fs.open(path);
if(!file){
tPrintln("Failed to open file for reading");
return;
}
while(file.available()){
tWrite(file.read());
}
file.close();
}
void writeFile(fs::FS &fs, const char * path, const char * message){
File file = fs.open(path, FILE_WRITE);
if(!file){
tPrintln("Failed to open file for writing");
return;
}
if(! file.print(message)){
tPrintln("Write failed");
}
file.close();
}
void appendFile(fs::FS &fs, const char * path, const char * message){
File file = fs.open(path, FILE_APPEND);
if(!file){
tPrintln("Failed to open file for appending");
return;
}
if(!file.print(message)){
tPrintln("Append failed");
}
file.close();
}
void listDir(fs::FS &fs, const char * path){
File dir = fs.open(path);
while (true){
File next = dir.openNextFile();
if (! next){
break; //no more files
}
tPrintln(next.name());
}
}
uint16_t countImages(fs::FS &fs, const char * path){
File dir = fs.open(path);
uint16_t count = 0;
while (true){
File next = dir.openNextFile();
if (! next){
return count;
}
if( next.name()[1] != 46 && next.name()[10] == 103){ // only files that do not start with "." and which DO end with "g" as in jpg
count ++;
}
}
}
const char* getFileNameByCount(fs::FS &fs, const char * path, uint16_t count_no){
File dir = fs.open(path);
uint16_t count = 0;
while (true){
File next = dir.openNextFile();
if (! next){
return ("No files found");
}
if( next.name()[1] != 46 && next.name()[10] == 103){ // only files that do not start with "." and which DO end with "g" as in jpg
if(count == count_no){
return next.name();
}
count ++;
}
}
}
void deleteFile(fs::FS &fs, const char * path){
if(!fs.remove(path)){
tPrintln("Delete failed");
}
}
void miniSDTest(){
tPrint("SD Card: ");
if(!SD.begin(5)){
tPrintln("Mount Failed.");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
tPrintln("No SD card.");
return;
}
writeFile(SD, "/card_test.txt", "Ok");
readFile(SD, "/card_test.txt");
tPrintln("");
//deleteFile(SD, "/card_test.txt");
}
void oneKTest(fs::FS &fs){
File file = fs.open("/oneKTest.txt", FILE_WRITE);
if(!file){
tPrintln("Failed to open One K file for writing");
return;
}
for (int i =0; i< 24000; i++);
if(! file.print("?")){
tPrintln("Write failed");
}
file.close();
}