-
Notifications
You must be signed in to change notification settings - Fork 13.3k
EEPROM Library: #2487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EEPROM Library: #2487
Conversation
Improved put function, compare data and only in case are different set _dirty flag, copy the data
Current coverage is 27.80% (diff: 100%)@@ master #2487 diff @@
==========================================
Files 20 20
Lines 3625 3625
Methods 335 335
Messages 0 0
Branches 656 656
==========================================
Hits 1008 1008
Misses 2441 2441
Partials 176 176
|
@@ -53,8 +53,11 @@ class EEPROMClass { | |||
if (address < 0 || address + sizeof(T) > _size) | |||
return t; | |||
|
|||
memcpy(_data + address, (const uint8_t*) &t, sizeof(T)); | |||
_dirty = true; | |||
_dirty = (memcmp(_data + address, (const uint8_t*) &t, sizeof(T)) !=0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it really OK to overwrite _dirty? Consider this scenario:
- EEPROM.write is called. _dirty is set to true
- EEPROM.put is called with a struct that is identical to what is already on the flash. _dirty is set to false
- EEPROM.commit is called. Since _dirty is false, commit returns true without actually writing anything.
I'm not an expert c/c++ programmer, so I might have missed something. Sorry for bothering you if that is the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is actually happening is that he checks the RAM copy of eeprom against what he is about to write and if different, then dirty is set to true and data is written to the RAM copy, ready for writing to flash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. So that means that the eeprom.write in step 1 won't be lost in step 3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct. If in step 2 there is no change, first change will be gone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could modify the code in this way:
if (memcmp(data + address, (const uint8_t) &t, sizeof(T)) !=0) {
_dirty = true;
memcpy(data + address, (const uint8_t) &t, sizeof(T));
}
It will grant that the _dirty flag is reset only at EEPROM.commit() and no changes are lost
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shorter and better. I like it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! Works well. I was confused for a bit when I didn't see get/put in .cpp and thought maybe the only change was the one noted above. Any reason they're in .h?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just changed the lines above.
put and get are function template and are part of class definition.
…nd no changes are lost
* EEPROM Library: Improved put function, compare data and only in case are different set _dirty flag, copy the data * It will grant that the _dirty flag is reset only at EEPROM.commit() and no changes are lost
Improved put function, compare data and only in case are different set _dirty flag, copy the data