@@ -9,29 +9,27 @@ EEPROMMate::EEPROMMate() {}
9
9
10
10
void EEPROMMate::begin (size_t eepromSize) {
11
11
EEPROM.begin (eepromSize);
12
+
12
13
if (readTotalUsedBytes () > eepromSize) {
13
- factoryReset (); // first run
14
+ factoryReset (); // reset eeprom on the first run
14
15
}
15
16
}
16
17
17
- void EEPROMMate::commit () {
18
- // writes the updated data to flash, so next reboot it will be readable
19
- EEPROM.commit ();
20
- }
18
+ // /////////
19
+ // Write //
20
+ // /////////
21
21
22
22
void EEPROMMate::factoryReset () {
23
23
// write 0 in all bytes of the EEPROM
24
24
for (uint16_t i = 0 ; i < EEPROM.length (); i++) {
25
25
EEPROM.write (i, 0 );
26
26
}
27
+ EEPROM.commit ();
27
28
}
28
29
29
- // /////////
30
- // Write //
31
- // /////////
32
-
33
30
void EEPROMMate::writeTotalUsedBytes (uint16_t value) {
34
31
writeIntToEEPROM (0 , value);
32
+ EEPROM.commit ();
35
33
}
36
34
37
35
bool EEPROMMate::writeData (uint8_t * key, uint8_t keyLength, uint8_t * value, uint16_t valueLength) {
@@ -79,6 +77,31 @@ bool EEPROMMate::writeKeyValue(uint32_t key, uint8_t* value, uint16_t valueLengt
79
77
return true ;
80
78
}
81
79
80
+ void EEPROMMate::writeIntToEEPROM (uint16_t address, uint16_t value) {
81
+ byte _1 = value >> 8 ;
82
+ byte _2 = value & 0xFF ;
83
+ EEPROM.write (address, _1);
84
+ EEPROM.write (address + 1 , _2);
85
+ }
86
+
87
+ void EEPROMMate::writeLongToEEPROM (uint16_t address, uint32_t value) {
88
+ if (address + 3 >= EEPROM.length ()) {
89
+ Serial.println (" Error: Address out of range" );
90
+ return ;
91
+ }
92
+ // decomposition from a long to 4 bytes by using bitshift
93
+ // 1 = most significant -> 4 = least significant byte
94
+ byte byte1 = value & 0xFF ;
95
+ byte byte2 = (value >> 8 ) & 0xFF ;
96
+ byte byte3 = (value >> 16 ) & 0xFF ;
97
+ byte byte4 = (value >> 24 ) & 0xFF ;
98
+ // write the 4 bytes into the simulated EEPROM memory
99
+ EEPROM.write (address, byte1);
100
+ EEPROM.write (address + 1 , byte2);
101
+ EEPROM.write (address + 2 , byte3);
102
+ EEPROM.write (address + 3 , byte4);
103
+ }
104
+
82
105
// ////////
83
106
// Read //
84
107
// ////////
@@ -190,10 +213,6 @@ bool EEPROMMate::readAllSavedData() {
190
213
return true ;
191
214
}
192
215
193
- // ///////////
194
- // PRIVATE //
195
- // ///////////
196
-
197
216
uint16_t EEPROMMate::readIntFromEEPROM (uint16_t address) {
198
217
byte _1 = EEPROM.read (address);
199
218
byte _2 = EEPROM.read (address + 1 );
@@ -214,30 +233,9 @@ uint32_t EEPROMMate::readLongFromEEPROM(uint16_t address) {
214
233
return (byte1) | (byte2 << 8 ) | (byte3 << 16 ) | (byte4 << 24 );
215
234
}
216
235
217
- void EEPROMMate::writeIntToEEPROM (uint16_t address, uint16_t value) {
218
- byte _1 = value >> 8 ;
219
- byte _2 = value & 0xFF ;
220
- EEPROM.write (address, _1);
221
- EEPROM.write (address + 1 , _2);
222
- }
223
-
224
- void EEPROMMate::writeLongToEEPROM (uint16_t address, uint32_t value) {
225
- if (address + 3 >= EEPROM.length ()) {
226
- Serial.println (" Error: Address out of range" );
227
- return ;
228
- }
229
- // decomposition from a long to 4 bytes by using bitshift
230
- // 1 = most significant -> 4 = least significant byte
231
- byte byte1 = value & 0xFF ;
232
- byte byte2 = (value >> 8 ) & 0xFF ;
233
- byte byte3 = (value >> 16 ) & 0xFF ;
234
- byte byte4 = (value >> 24 ) & 0xFF ;
235
- // write the 4 bytes into the simulated EEPROM memory
236
- EEPROM.write (address, byte1);
237
- EEPROM.write (address + 1 , byte2);
238
- EEPROM.write (address + 2 , byte3);
239
- EEPROM.write (address + 3 , byte4);
240
- }
236
+ // /////////
237
+ // PRINT //
238
+ // /////////
241
239
242
240
void EEPROMMate::printArrayHex (uint8_t arr[], uint16_t arrayLength) {
243
241
for (int i = 0 ; i < arrayLength; i++) {
0 commit comments