-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage.ino
62 lines (50 loc) · 1.55 KB
/
Storage.ino
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
#include <EEPROM.h>
void formatStorage() {
for (int i = 0; i < 512; i++) EEPROM.write(i, 0);
saveDefaultStorageValues();
}
void saveDefaultStorageValues() {
saveBoolean(EEPROM_LOCKED, false);
saveBoolean(EEPROM_DRIVING_LIGHTS, true);
saveBoolean(EEPROM_STANDBY_LIGHTS, true);
saveByte(EEPROM_FRONT_SENSOR, 20);
saveByte(EEPROM_BACK_SENSOR, 20);
}
void saveInt(int address, int value) {
EEPROM.write(address, highByte(value));
EEPROM.write(address + 1, lowByte(value));
}
int loadInt(int address) {
byte high = EEPROM.read(address);
byte low = EEPROM.read(address + 1);
return int(word(high, low));
}
void saveLong(int address, unsigned long value) {
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
long loadLong(int address) {
unsigned long four = EEPROM.read(address);
unsigned long three = EEPROM.read(address + 1);
unsigned long two = EEPROM.read(address + 2);
unsigned long one = EEPROM.read(address + 3);
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
void saveByte(int address, byte value) {
EEPROM.write(address, value);
}
byte loadByte(int address) {
EEPROM.read(address);
}
void saveBoolean(int address, boolean value) {
EEPROM.write(address, value);
}
boolean loadBoolean(int address) {
return EEPROM.read(address);
}