For the prototype I'm working on, I need to save an int16_t
several times a day (i.e. at least once, less than 20). I just need to keep track of one integer at a time: I don't care if I lose the previous ones. My integer will be about a few hundreds around zero, say between -400 and +400, including 0. That makes many possible int16_t
values out of range, so I thought I could use some of them as landmarks to find the next 2 cells in memory to be written to.
For example, to save -20, I'll fill the first 2 cells with 0xFFEC
and the following 2 with 0xDFDF
(don't fill, don't fill), which translates to an out-of-range integer. To save the next value, I scan for the last 0xDFDF
I can find and fill the next 2 cells. If I reach the end of the EEPROM, I wipe the whole memory and start over. To retrieve the last value, I scan for the last 0xDFDF
and read the previous 2 cells. This linear search is going to be slow, but I only need to do it once on each reboot, so it doesn't worry me too much.
Before I go out and start working on an implementation, however, I'd like to hear some opinions. Is my algorithm sound? Is there a simpler way that I haven't thought of?
Further info: My chip of choice is an 8-bit Nano (I think it comes with a 512-byte EEPROM). If the board were to lose power in the middle of a writing operation, thus corrupting the memory, that would be annoying, but not safety critical or financially expensive in any way. I'd like to be warned if this happens, however.