-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCache.cpp
More file actions
49 lines (39 loc) · 808 Bytes
/
FileCache.cpp
File metadata and controls
49 lines (39 loc) · 808 Bytes
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
#include "Arduino.h"
#include "FS.h"
#include "FileCache.h"
String _fileName;
FileCache::FileCache(String fileName)
{
SPIFFS.begin();
_fileName = fileName;
if (!SPIFFS.exists(_fileName)) {
Serial.print("Create file ");
Serial.println(_fileName);
File file = SPIFFS.open(_fileName, "w");
file.println(0);
}
}
bool FileCache::put(int value)
{
File file = SPIFFS.open(_fileName, "w");
if (!file) {
Serial.println("Unable to put into file");
file.close();
return false;
}
file.println(value);
file.close();
return true;
}
int FileCache::get()
{
File file = SPIFFS.open(_fileName, "r");
if (!file) {
Serial.println("Unable to get from file");
file.close();
return 0;
}
String myVal = file.readStringUntil('\n');
file.close();
return myVal.toInt();
}