This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
forked from n0xa/m5stick-nemo
-
Notifications
You must be signed in to change notification settings - Fork 18
/
sd.h
102 lines (90 loc) · 2.21 KB
/
sd.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
bool sdcardMounted = false;
#if defined(SDCARD)
#include <FS.h>
#include <SD.h>
#include <SPI.h>
SPIClass* sdcardSPI = NULL;
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);
}
}
/*
void listHtmlFiles() {
File root = SD.open("/");
if (!root) {
Serial.println("Failed to open SD card");
return;
}
while (true) {
File file = root.openNextFile();
if (!file) {
break;
}
if (file.isDirectory()) {
// Skip directories
} else {
String filename = file.name();
if (filename.endsWith(".html")) {
Serial.print("HTML file: ");
Serial.println(filename);
}
}
file.close();
}
root.close();
}
*/
#endif
bool setupSdCard() {
#if defined(SDCARD)
sdcardSemaphore = xSemaphoreCreateMutex();
sdcardSPI = new SPIClass(FSPI);
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("Failed to mount SDCARD");
return false;
} else {
Serial.println("SDCARD mounted successfully");
sdcardMounted = true;
return true;
}
#else
return false;
#endif
}
void ToggleSDCard()
{
if (sdcardMounted == true) {
sdcardMounted = false;
SD.end();
sdcardSPI->end(); // Closes SPI connections and release pins.
Serial.println("SDCARD unmounted");
} else {
sdcardSPI = new SPIClass(FSPI);
sdcardSPI->begin(SD_CLK_PIN, SD_MISO_PIN, SD_MOSI_PIN, SD_CS_PIN);
delay(10);
if (!SD.begin(SD_CS_PIN, *sdcardSPI)) {
sdcardSPI->end();
} else {
Serial.println("SDCARD mounted successfully");
sdcardMounted = true;
}
}
delay(50);
}