-
Notifications
You must be signed in to change notification settings - Fork 7
/
HSDConfigFile.cpp
78 lines (62 loc) · 1.28 KB
/
HSDConfigFile.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "HSDConfigFile.hpp"
#include <FS.h>
HSDConfigFile::HSDConfigFile(String fileName)
:
m_fileName(fileName)
{
}
HSDConfigFile::~HSDConfigFile()
{
}
bool HSDConfigFile::read(char* buffer, int bufSize)
{
bool success = false;
Serial.print(F("Reading config file "));
Serial.println(m_fileName);
if(SPIFFS.exists(m_fileName))
{
File configFile = SPIFFS.open(m_fileName, "r");
if(configFile)
{
size_t size = configFile.size();
Serial.print(F("File size is ")); Serial.println(String(size) + " bytes");
if(size <= bufSize)
{
configFile.readBytes(buffer, size);
success = true;
}
else
{
Serial.println(F("File is too big"));
}
}
else
{
Serial.println(F("File open failed"));
}
configFile.close();
}
else
{
Serial.println(F("File does not exist"));
}
return success;
}
bool HSDConfigFile::write(JsonObject* data)
{
bool success = false;
Serial.print(F("Writing config file "));
Serial.println(m_fileName);
File configFile = SPIFFS.open(m_fileName, "w+");
if(configFile)
{
data->printTo(configFile);
configFile.close();
success = true;
}
else
{
Serial.println(F("File open failed"));
}
return success;
}