-
Notifications
You must be signed in to change notification settings - Fork 3
/
FileManager.h
109 lines (85 loc) · 2.11 KB
/
FileManager.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
#pragma once
#include <SPI.h>
#include <SD.h>
#include <ArduinoJson.h>
#define SDSPEED 10000000
#define SCK_PIN 14
#define MISO_PIN 21
#define MOSI_PIN 13
#define CS_PIN 15
class FileManager
{
public:
static bool sdIsDetected;
static SPIClass spiSD;
FileManager()
{
}
void init()
{
pinMode(SCK_PIN,INPUT_PULLUP);
pinMode(MISO_PIN,INPUT_PULLUP);
pinMode(MOSI_PIN,INPUT_PULLUP);
pinMode(CS_PIN,INPUT_PULLUP);
#if VERSION == 2
//Need to activate mosfet
pinMode(27, OUTPUT);
digitalWrite(27, LOW);
// CS bug
pinMode(CS_PIN, OUTPUT); // wierd CS/SS bug with SPI lib
digitalWrite(CS_PIN, LOW);
#endif
spiSD.begin(SCK_PIN, MISO_PIN, MOSI_PIN, CS_PIN);//SCK,MISO,MOSI,ss
#if VERSION == 1
if(SD.begin(CS_PIN, spiSD, SDSPEED))
#elif VERSION == 2
if(SD.begin(CS_PIN, spiSD))
#endif
{
DBG("SD Card initialized.");
sdIsDetected = true;
}else{
DBG("SD Card Initialization failed.");
}
}
static File openFile(String fileName, bool forWriting = false, bool deleteIfExists = true)
{
if(forWriting && deleteIfExists) deleteFileIfExists(fileName);
if(!fileName.startsWith("/")) fileName = "/"+fileName;
File f = SD.open(fileName.c_str(), forWriting?FILE_WRITE:FILE_READ);
DBG("Open file : "+String(f.name()));
return f;
}
static void deleteFileIfExists(String path)
{
if(!sdIsDetected) return;
if (SD.exists(path.c_str()))
{
SD.remove(path.c_str());
DBG("Removed file "+path);
}
}
/*
}
File dataFile = SD.open(path.c_str());
if (dataFile.isDirectory()) {
path += "/index.htm";
dataType = "text/html";
dataFile = SD.open(path.c_str());
}
if (!dataFile) {
return false;
}
if (server.hasArg("download")) {
dataType = "application/octet-stream";
}
if (server.streamFile(dataFile, dataType) != dataFile.size()) {
DBG("Sent less data than expected!");
}
dataFile.close();
return true;
}
*/
};
SPIClass FileManager:: spiSD(HSPI);
bool FileManager::sdIsDetected = false;