forked from n0xa/m5stick-nemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd.h
49 lines (44 loc) · 1.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
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);
}
}
#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
}