Skip to content

Commit 0f66d97

Browse files
committed
EEPROM.commit()
1 parent adba65b commit 0f66d97

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

Diff for: turtlpass-firmware/EEPROMMate.cpp

+35-37
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,27 @@ EEPROMMate::EEPROMMate() {}
99

1010
void EEPROMMate::begin(size_t eepromSize) {
1111
EEPROM.begin(eepromSize);
12+
1213
if (readTotalUsedBytes() > eepromSize) {
13-
factoryReset(); // first run
14+
factoryReset(); // reset eeprom on the first run
1415
}
1516
}
1617

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+
///////////
2121

2222
void EEPROMMate::factoryReset() {
2323
// write 0 in all bytes of the EEPROM
2424
for (uint16_t i = 0; i < EEPROM.length(); i++) {
2525
EEPROM.write(i, 0);
2626
}
27+
EEPROM.commit();
2728
}
2829

29-
///////////
30-
// Write //
31-
///////////
32-
3330
void EEPROMMate::writeTotalUsedBytes(uint16_t value) {
3431
writeIntToEEPROM(0, value);
32+
EEPROM.commit();
3533
}
3634

3735
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
7977
return true;
8078
}
8179

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+
82105
//////////
83106
// Read //
84107
//////////
@@ -190,10 +213,6 @@ bool EEPROMMate::readAllSavedData() {
190213
return true;
191214
}
192215

193-
/////////////
194-
// PRIVATE //
195-
/////////////
196-
197216
uint16_t EEPROMMate::readIntFromEEPROM(uint16_t address) {
198217
byte _1 = EEPROM.read(address);
199218
byte _2 = EEPROM.read(address + 1);
@@ -214,30 +233,9 @@ uint32_t EEPROMMate::readLongFromEEPROM(uint16_t address) {
214233
return (byte1) | (byte2 << 8) | (byte3 << 16) | (byte4 << 24);
215234
}
216235

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+
///////////
241239

242240
void EEPROMMate::printArrayHex(uint8_t arr[], uint16_t arrayLength) {
243241
for (int i = 0; i < arrayLength; i++) {

Diff for: turtlpass-firmware/EEPROMMate.h

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class EEPROMMate {
3333
public:
3434
EEPROMMate();
3535
void begin(size_t eepromSize);
36-
void commit();
3736
void factoryReset();
3837
bool writeData(uint8_t* key, uint8_t keyLength, uint8_t* value, uint16_t valueLength);
3938
bool writeKeyValue(uint32_t key, uint8_t* value, uint16_t valueLength);

0 commit comments

Comments
 (0)