-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathespEasyMemory.cpp
169 lines (124 loc) · 3.64 KB
/
espEasyMemory.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "Arduino.h"
#include "espEasyMemory.h"
#include <FS.h>
/* Return values: Success -> 1 if filename already existed, else 2.
Fail -> -1 */
int espEasyMemory::storeIntVariable(int variable, String filename){
int returnValue;
File file;
if (SPIFFS.exists(filename)){ //file already existed
returnValue = 1;
}
else{
returnValue = 2;
}
file = SPIFFS.open(filename, "w");
file.println(String(variable)); // writes as a String for simplicity, since we can easily add a '\n' with println. We could do a traditional write here too, but then we have to use traditional read on the recover methods.
file.close();
if (!SPIFFS.exists(filename)){ //Check if the file was created
return -1;
}
else{
return (returnValue);
}
}
int espEasyMemory::storeFloatVariable(float variable, int decimalPoints, String filename){
int returnValue;
File file;
if (SPIFFS.exists(filename)){ //file already existed
returnValue = 1;
}
else{
returnValue = 2;
}
file = SPIFFS.open(filename, "w");
file.println(String(variable, decimalPoints)); //Writes as a String for simplicity, since we can easily add a '\n' with println. We could do a traditional write here too, but then we have to use traditional read on the recover methods.
file.close();
if (!SPIFFS.exists(filename)){ //Check if the file was created
return -1;
}
else{
return (returnValue);
}
}
int espEasyMemory::storeDoubleVariable(double variable, int decimalPoints, String filename){
int returnValue;
File file;
if (SPIFFS.exists(filename)){ //file already existed
returnValue = 1;
}
else{
returnValue = 2;
}
file = SPIFFS.open(filename, "w");
file.println(String(variable, decimalPoints)); //Writes as a String for simplicity, since we can easily add a '\n' with println. We could do a traditional write here too, but then we have to use traditional read on the recover methods.
file.close();
if (!SPIFFS.exists(filename)){ //Check if the file was created
return -1;
}
else{
return (returnValue);
}
}
int espEasyMemory::storeStringVariable(String variable, String filename){
int returnValue;
File file;
if (SPIFFS.exists(filename)){
returnValue = 1;
}
else{
returnValue = 2;
}
file = SPIFFS.open(filename, "w");
file.println(variable);
file.close();
if (!SPIFFS.exists(filename)){
return -1;
}
else{
return (returnValue);
}
}
int espEasyMemory::recoverIntVariable(int *variable, String filename){
if (!SPIFFS.exists(filename)){ // Trying to recover from a file that does not exist
return -1;
}
File file = SPIFFS.open(filename, "r");
int recoveredValue = file.parseInt();
file.close();
*variable = recoveredValue;
return 1;
}
int espEasyMemory::recoverStringVariable(String *variable, String filename){
if (!SPIFFS.exists(filename)){ // Trying to recover from a file that does not exist
return -1;
}
File file = SPIFFS.open(filename, "r");
String recoveredString = file.readStringUntil('\n');
file.close();
*variable = recoveredString;
return 1;
}
int espEasyMemory::recoverFloatVariable(float *variable, String filename){
if (!SPIFFS.exists(filename)){ // Trying to recover from a file that does not exist
return -1;
}
File file = SPIFFS.open(filename, "r");
int recoveredValue = file.parseFloat();
file.close();
*variable = recoveredValue;
return 1;
}
int espEasyMemory::recoverDoubleVariable(double *variable, String filename){
if (!SPIFFS.exists(filename)){ // Trying to recover from a file that does not exist
return -1;
}
File file = SPIFFS.open(filename, "r");
int recoveredValue = file.parseFloat();
file.close();
*variable = recoveredValue;
return 1;
}
void espEasyMemory::format(){
SPIFFS.format();
}