forked from filmote/WiFiManagerGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheeprom.ino
More file actions
301 lines (199 loc) · 8.71 KB
/
eeprom.ino
File metadata and controls
301 lines (199 loc) · 8.71 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// -------------------------------------------------------------------------------------------------------
// Test to see if valid SSID and password settings have been saved previously (test to see if the byte
// at position EEPROM_TEST_CHAR_POS equals the EEPROM_TEST_CHAR_POS_VALUE)..
bool validEEPROMSettings() {
char testChar = EEPROM.read(EEPROM_TEST_CHAR_POS);
#if defined(DEBUG) && defined(DEBUG_EEPROM)
Serial.println(" ");
Serial.println("validEEPROMSettings()");
Serial.print(" testChar = ");
Serial.println(testChar, DEC);
Serial.print(" (testChar == EEPROM_TEST_CHAR_POS_VALUE) = ");
Serial.println(testChar == EEPROM_TEST_CHAR_POS_VALUE);
#endif
return (testChar == EEPROM_TEST_CHAR_POS_VALUE);
}
// -------------------------------------------------------------------------------------------------------
// Update the EEPROM_TEST_CHAR_POS byte with the value of EEPROM_TEST_CHAR_POS_VALUE to indicate that
// the SSID and password have been saveed previously.
void writeTestChar() {
EEPROM.write(EEPROM_TEST_CHAR_POS, EEPROM_TEST_CHAR_POS_VALUE);
#if defined(DEBUG) && defined(DEBUG_EEPROM)
char testChar = EEPROM.read(EEPROM_TEST_CHAR_POS);
Serial.println(" ");
Serial.println("writeTestChar()");
Serial.print(" testChar = ");
Serial.println(EEPROM.read(EEPROM_TEST_CHAR_POS), DEC);
Serial.print(" (testChar == EEPROM_TEST_CHAR_POS_VALUE) = ");
Serial.println(testChar == EEPROM_TEST_CHAR_POS_VALUE ? "true" : "false");
#endif
}
// -------------------------------------------------------------------------------------------------------
// Clear the EEPROM_TEST_CHAR_POS byte that is used to indicate that a valid SSID and password have been
// saveed previously.
void clearTestChar() {
EEPROM.write(EEPROM_TEST_CHAR_POS, 0);
#if defined(DEBUG) && defined(DEBUG_EEPROM)
Serial.println(" ");
Serial.println("clearTestChar()");
Serial.print(" testChar = ");
Serial.println(EEPROM.read(EEPROM_TEST_CHAR_POS), DEC);
#endif
}
// -------------------------------------------------------------------------------------------------------
// Read the length of a previously saved SSID value. The length is stored BEFORE the actual value in
// byte EEPROM_SSID_LENGTH_POS.
int readSSIDLength() {
#if defined(DEBUG) && defined(DEBUG_EEPROM)
Serial.println(" ");
Serial.println("readSSIDLength()");
Serial.print(" length = ");
Serial.println(EEPROM.read(EEPROM_SSID_LENGTH_POS));
#endif
return (EEPROM.read(EEPROM_SSID_LENGTH_POS) > EEPROM_SSID_MAX_LENGTH ? EEPROM_SSID_MAX_LENGTH : EEPROM.read(EEPROM_SSID_LENGTH_POS));
}
// -------------------------------------------------------------------------------------------------------
// Read a previously saved SSID value from position EEPROM_SSID_START_POS. The length of the value is
// stored immediately before the value in position EEPROM_SSID_LENGTH_POS.
String readSSIDValue() {
String value = "";
int ssidLength = readSSIDLength();
for (int i = EEPROM_SSID_START_POS; i < EEPROM_SSID_START_POS + ssidLength; i++) {
value += char(EEPROM.read(i));
}
#if defined(DEBUG) && defined(DEBUG_EEPROM)
Serial.println(" ");
Serial.println("readSSIDValue()");
Serial.print(" value = ");
Serial.println(value);
#endif
return value;
}
// -------------------------------------------------------------------------------------------------------
// Write the SSID value starting at position EEPROM_SSID_START_POS.
//
// Note: Function truncates SSID values in excess of EEPROM_SSID_MAX_LENGTH.
void writeSSIDValue(String value) {
EEPROM.write(EEPROM_SSID_LENGTH_POS, (value.length() > EEPROM_SSID_MAX_LENGTH ? EEPROM_SSID_MAX_LENGTH : value.length()));
for (int i = EEPROM_SSID_START_POS; i < EEPROM_SSID_START_POS + (value.length() > EEPROM_SSID_MAX_LENGTH ? EEPROM_SSID_MAX_LENGTH : value.length()); i++) {
EEPROM.write(i, value[i - EEPROM_SSID_START_POS]);
}
#if defined(DEBUG) && defined(DEBUG_EEPROM)
String testValue = "";
int ssidLength = value.length();
for (int i = EEPROM_SSID_START_POS; i < EEPROM_SSID_START_POS + ssidLength; i++) {
testValue += char(EEPROM.read(i));
}
Serial.println(" ");
Serial.println("writeSSIDValue()");
Serial.print(" length = ");
Serial.println(EEPROM.read(EEPROM_SSID_LENGTH_POS));
Serial.print(" value = ");
Serial.println(testValue);
#endif
}
// -------------------------------------------------------------------------------------------------------
// Read the length of a previously saved password value. The length is stored BEFORE the actual value in
// byte EEPROM_PASSWORD_LENGTH_POS.
int readPasswordLength() {
#if defined(DEBUG) && defined(DEBUG_EEPROM)
Serial.println(" ");
Serial.println("readPasswordLength()");
Serial.print(" length = ");
Serial.println(EEPROM.read(EEPROM_PASSWORD_LENGTH_POS));
#endif
return (EEPROM.read(EEPROM_PASSWORD_LENGTH_POS) > EEPROM_PASSWORD_MAX_LENGTH ? EEPROM_PASSWORD_MAX_LENGTH : EEPROM.read(EEPROM_PASSWORD_LENGTH_POS));
}
// -------------------------------------------------------------------------------------------------------
// Read a previously saved SSID value from position EEPROM_PASSWORD_START_POS. The length of the value is
// stored immediately before the value in position EEPROM_PASSWORD_LENGTH_POS.
String readPasswordValue() {
String value = "";
int passwordLength = readPasswordLength();
for (int i = EEPROM_PASSWORD_START_POS; i < EEPROM_PASSWORD_START_POS + passwordLength; i++) {
value += char(EEPROM.read(i));
}
#if defined(DEBUG) && defined(DEBUG_EEPROM)
Serial.println(" ");
Serial.println("readPasswordValue()");
Serial.print(" value = ");
Serial.println(value);
#endif
return value;
}
// -------------------------------------------------------------------------------------------------------
// Write the password value starting at position EEPROM_PASSWORD_START_POS.
//
// Note: Function truncates passwords in excess of EEPROM_PASSWORD_MAX_LENGTH.
void writePasswordValue(String value) {
EEPROM.write(EEPROM_PASSWORD_LENGTH_POS, (value.length() > EEPROM_PASSWORD_MAX_LENGTH ? EEPROM_PASSWORD_MAX_LENGTH : value.length()));
for (int i = EEPROM_PASSWORD_START_POS; i < EEPROM_PASSWORD_START_POS + (value.length() > EEPROM_PASSWORD_MAX_LENGTH ? EEPROM_PASSWORD_MAX_LENGTH : value.length()); i++) {
EEPROM.write(i, value[i - EEPROM_PASSWORD_START_POS]);
}
#if defined(DEBUG) && defined(DEBUG_EEPROM)
String testValue = "";
int passwordLength = (value.length() > EEPROM_PASSWORD_MAX_LENGTH ? EEPROM_PASSWORD_MAX_LENGTH : value.length());
for (int i = EEPROM_PASSWORD_START_POS; i < EEPROM_PASSWORD_START_POS + passwordLength; i++) {
testValue += char(EEPROM.read(i));
}
Serial.println(" ");
Serial.println("writePasswordValue()");
Serial.print(" length = ");
Serial.println(EEPROM.read(EEPROM_PASSWORD_LENGTH_POS));
Serial.print(" value = ");
Serial.println(testValue);
#endif
}
// -------------------------------------------------------------------------------------------------------
// Write test character, SSID and password to EEPROM ..
void writeSSIDandPassword(String ssidVal, String passwordVal) {
writeTestChar();
writeSSIDValue(ssidVal);
writePasswordValue(passwordVal);
EEPROM.commit();
}
// -------------------------------------------------------------------------------------------------------
// Dump the first 512 bytes of the EEPROM in a table .. code copied from EEPROMUtils library.
#if defined(DEBUG) && defined(DEBUG_EEPROM)
void dumpEEPROMValues() {
Serial.println(" ");
Serial.println("-------------------------------------------------------------");
int bytesPerRow = 16;
// address counter
int i;
// row bytes counter
int j;
// byte read from eeprom
byte b;
// temporary buffer for sprintf
char buf[10];
// initialize row counter
j = 0;
// go from first to last eeprom address
for (i = 0; i <= 511; i++) {
// if this is the first byte of the row,
// start row by printing the byte address
if (j == 0) {
sprintf(buf, "%03X: ", i);
Serial.print(buf);
}
// read current byte from eeprom
b = EEPROM.read(i);
// write byte in hex form
sprintf(buf, "%02X ", b);
// increment row counter
j++;
// if this is the last byte of the row,
// reset row counter and use println()
// to start a new line
if (j == bytesPerRow) {
j = 0;
Serial.println(buf);
}
// else just print the hex value with print()
else {
Serial.print(buf);
}
}
}
#endif