-
Notifications
You must be signed in to change notification settings - Fork 8
/
sdfunc.h
238 lines (221 loc) · 6.2 KB
/
sdfunc.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#pragma once
bool sdcardMounted = false;
#if defined(SDCARD)
#include <FS.h>
#include <SD.h>
#include <SPI.h>
#if defined(CARDPUTER)
SPIClass* sdcardSPI = NULL;
#else
SPIClass sdcardSPI;
#endif
SemaphoreHandle_t sdcardSemaphore;
void appendToFile(fs::FS& fs, const char* path, const char* text) {
if (xSemaphoreTake(sdcardSemaphore, portMAX_DELAY) == pdTRUE) {
File file = fs.open(path, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for appending");
xSemaphoreGive(sdcardSemaphore);
return;
}
Serial.printf("Appending text '%s' to file: %s\n", text, path);
if (file.println(text)) {
Serial.println("Text appended");
} else {
Serial.println("Append failed");
}
file.close();
xSemaphoreGive(sdcardSemaphore);
}
}
String choosefile(fs::FS &fs, const char *dirname, uint8_t number) {
uint8_t numberindex=0;
// scrolling menu
File root = fs.open(dirname);
if (!root) {
Serial.println("Failed to open directory");
return "error";
}
if (!root.isDirectory()) {
Serial.println("Not a directory");
return "error";
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
} else {
if (numberindex == number)
{
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
return file.name();
}
numberindex++;
}
file = root.openNextFile();
}
return "false";
}
String readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return "false";
}
String rturnstr;
Serial.print("Read from file: ");
while(file.available()){
//Serial.write(file.read());
//rturnstr = rturnstr + String(file.read());
rturnstr += (char)file.read();
}
file.close();
return rturnstr;
}
#endif
bool setupSdCard() {
#if defined(SDCARD)
sdcardSemaphore = xSemaphoreCreateMutex();
#if defined (CARDPUTER)
sdcardSPI = new SPIClass(FSPI);
sdcardSPI->begin(SD_CLK_PIN, SD_MISO_PIN, SD_MOSI_PIN, SD_CS_PIN);
#else
sdcardSPI.begin(SD_CLK_PIN, SD_MISO_PIN, SD_MOSI_PIN, SD_CS_PIN); // start SPI communications
#endif
delay(10);
#if defined (CARDPUTER)
if (!SD.begin(SD_CS_PIN, *sdcardSPI)) {
sdcardSPI->end();
#else
if (!SD.begin(SD_CS_PIN, sdcardSPI)) { //excluded * -> poiter indicator -
sdcardSPI.end(); // Closes SPI connections and release pin header.
#endif
Serial.println("Failed to mount SDCARD");
return false;
} else {
Serial.println("SDCARD mounted successfully");
sdcardMounted = true;
return true;
}
#else
return false;
#endif
}
#if defined(SDCARD)
#ifndef CARDPUTER
void ToggleSDCard()
{
rstOverride = true;
isSwitching = true;
uint8_t cardType = NULL;
uint64_t cardSize = NULL;
//current_proc=1;
DISP.fillScreen(BGCOLOR);
DISP.setCursor(5, 1);
if (sdcardMounted == true) {
sdcardMounted = false;
uint8_t cardType = 0;
uint64_t cardSize = 0;
SD.end();
sdcardSPI.end(); // Closes SPI connections and release pins.
DISP.println("SD Card Unmouted..");
DISP.println("pin header released.");
Serial.println("SD Card Unmounted...");
} else {
sdcardSPI.begin(SD_CLK_PIN, SD_MISO_PIN, SD_MOSI_PIN, SD_CS_PIN);
delay(10);
if (!SD.begin(SD_CS_PIN, sdcardSPI)) {
Serial.println("Card Mount Failed");
DISP.println("Card Mount Failed");
sdcardSPI.end();
} else {
//
sdcardMounted = true;
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
DISP.println("None SD Card");
Serial.println("None SD Card");
}
DISP.print("SD Card Type: ");
if (cardType == CARD_MMC) {
DISP.println("MMC");
Serial.println("MMC");
} else if (cardType == CARD_SD) {
DISP.println("SDSC");
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
DISP.println("SDHC");
Serial.println("SDHC");
} else {
DISP.println("UNKNOWN");
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
DISP.printf("SD Card Size: %lluMB\n", cardSize);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
}
}
delay(1500);
}
#endif
#endif
#ifdef SDCARD
#ifdef CARDPUTER
String Inputfilename(String defaultname){
DISP.fillScreen(BGCOLOR);
DISP.setSwapBytes(true);
DISP.setTextSize(MEDIUM_TEXT);
DISP.setTextColor(BGCOLOR, FGCOLOR);
DISP.setCursor(0, 0);
DISP.println(" File name ");
DISP.setTextSize(TINY_TEXT);
DISP.setTextColor(FGCOLOR, BGCOLOR);
DISP.setTextSize(SMALL_TEXT);
uint8_t ssidTextCursorY = DISP.getCursorY();
String currentname = String(defaultname.c_str());
DISP.printf("%s", currentname.c_str());
bool ssid_ok = false;
while(!ssid_ok){
M5Cardputer.update();
if(M5Cardputer.Keyboard.isChange() && M5Cardputer.Keyboard.isPressed()) {
Keyboard_Class::KeysState status = M5Cardputer.Keyboard.keysState();
if(status.del) {
currentname.remove(currentname.length() - 1);
}
if(status.enter) {
ssid_ok = true;
}
if(currentname.length() >= 32) {
continue;
}
for(auto i : status.word) {
if(i != '?' && i != '$' && i != '\"' && i != '[' && i != '\\' && i != ']' && i != '+'){
currentname += i;
}
}
DISP.fillRect(0, ssidTextCursorY, DISP.width(), DISP.width()- ssidTextCursorY, BLACK);
DISP.setCursor(0, ssidTextCursorY);
DISP.printf("%s", currentname.c_str());
}
}
return currentname;
}
#else
String Inputfilename(String defaultname){
uint8_t i=0;
String name;
while (1)
{
i++;
name = defaultname + String(i) + ".txt";
if(!(readFile(SD, name.c_str()) == "false"))
{
return String(i);
}
}
}
#endif
#endif