-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom.h
81 lines (64 loc) · 1.61 KB
/
eeprom.h
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
boolean f(int addr) {
return ((addr >= 0) && (addr <= 4096));
}
boolean eeprom_is_addr_ok(int addr) {
return ((addr >= 0) && (addr <= 4096));
}
boolean eeprom_write_bytes(int startAddr, const byte* array, int numBytes) {
int i;
if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {
return false;
}
for (i = 0; i < numBytes; i++) {
EEPROM.write(startAddr + i, array[i]);
}
return true;
}
boolean eeprom_write_string(int addr, const char* string) {
int numBytes;
numBytes = strlen(string) + 1;
return eeprom_write_bytes(addr, (const byte*)string, numBytes);
}
boolean eeprom_read_bytes(int startAddr, byte array[], int numBytes) {
int i;
if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {
return false;
}
for (i = 0; i < numBytes; i++) {
array[i] = EEPROM.read(startAddr + i);
}
return true;
}
boolean eeprom_read_string(int addr, char* buffer, int bufSize) {
byte ch;
int bytesRead;
if (!eeprom_is_addr_ok(addr)) {
return false;
}
if (bufSize == 0) {
return false;
}
if (bufSize == 1) {
buffer[0] = 0;
return true;
}
bytesRead = 0;
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch;
bytesRead++;
while ( (ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= 4096) ) {
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch;
bytesRead++;
}
if ((ch != 0x00) && (bytesRead >= 1)) {
buffer[bytesRead - 1] = 0;
}
return true;
}
void eeprom_erase_all(byte b = 0xFF) {
int i;
for (i = 0; i <= 4096; i++) {
EEPROM.write(i, b);
}
}