-
Notifications
You must be signed in to change notification settings - Fork 1
/
EEPROMString.cpp
30 lines (24 loc) · 986 Bytes
/
EEPROMString.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
#include "EEPROMString.h"
boolean writeStringToEEPROM(int addressOffset, const String &str, int maxLength) {
int len = (maxLength>=0 && maxLength<str.length()) ? maxLength : str.length();
//Serial.println("Writing String '" + strToWrite.substring(0,len) + "' to EEPROM. (Length: " + len + ")");
EEPROM.write(addressOffset, len);
for (int i=0; i<len; i++) {
EEPROM.write(addressOffset+1+i, str.charAt(i));
}
boolean committed = EEPROM.commit();
//Serial.println(committed ? "EEPROM successfully committed\n" : "ERROR! EEPROM commit failed\n");
return committed;
}
void readStringFromEEPROM(int addressOffset, String *strRead, int maxLength) {
int len = EEPROM.read(addressOffset);
if (maxLength > -1 && maxLength < len) {
len = maxLength; // limit to max length if shorter than the actual length
}
char data[len + 1];
for (int i=0; i<len; i++) {
data[i] = EEPROM.read(addressOffset+1+i);
}
data[len] = '\0';
*strRead = String(data);
}