-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileSystem.cpp
60 lines (54 loc) · 1.4 KB
/
fileSystem.cpp
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
#include <SPIFFS.h>
#include "fileSystem.hpp"
#include "config.hpp"
const char *ssidPath = PATH_CONFIG "/ssid";
const char *passPath = PATH_CONFIG "/pass";
const char *ipPath = PATH_CONFIG "/ip";
const char *gatewayPath = PATH_CONFIG "/gateway";
const char *serverPath = PATH_CONFIG "/server";
// Initialize SPIFFS
void initSPIFFS()
{
if (!SPIFFS.begin(true))
{
DEBUG_PRINTLN("An error has occurred while mounting SPIFFS");
}
DEBUG_PRINTLN("SPIFFS mounted successfully");
}
// Read File from SPIFFS
String readFile(fs::FS &fs, const char *path)
{
DEBUG_PRINTF("Reading file: %s\r\n", path);
File file = fs.open(path);
if (!file || file.isDirectory())
{
DEBUG_PRINTLN("- failed to open file for reading");
return String();
}
String fileContent;
while (file.available())
{
fileContent = file.readStringUntil('\n');
break;
}
return fileContent;
}
// Write file to SPIFFS
void writeFile(fs::FS &fs, const char *path, const char *message)
{
DEBUG_PRINTF("Writing file: %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file)
{
DEBUG_PRINTLN("- failed to open file for writing");
return;
}
if (file.print(message))
{
DEBUG_PRINTLN("- file written");
}
else
{
DEBUG_PRINTLN("- frite failed");
}
}