Anyone have a simple method to write a string to EEPROM location using I2C object?
Don M
Posts: 1,652
in Propeller 1
Need a way to store 16 characters.
Comments
Andy
To read it back, you can do:
This code is untested, and might have some off-by-one errors.
Note all of the extra logic in rdstr to not read more bytes than available. If the extra checking wasn't there, rdstr could overwrite all of your RAM if the string was bad and was too long or was missing its null terminator. The random resets that this would likely cause aren't fun. If the string is too long, it will read as much as fits but the last byte will still be a null, and it will return -1 to indicate that it didn't fit instead of returning the length of the string it read.
You might also try storing the length of the string as a byte in front of the string, instead of implicitly as a 0 byte at the end. The maximum length checking for that wouldn't be nearly as complicated.
EDIT:
Wait, you only need 16 characters? Then just do this to write: and this to read:
And make sure you make your array 17 bytes so you can store the null terminator at the end if the string takes up all 16 bytes, and make sure you actually put a null terminator there. Also, you can put the null terminator there once at compile time in a DAT block and never overwrite it, and it will always be there.
I am using the code in my GPS Scavenger Hunt Box, but I have not completed the project yet. However, if you need to see another example of the Propeller EEPROM object, I will post the code that does have a working EEPROM stored variable section. Made it very easy for me to have my project remember where it last left off each time it boots.
One way the Morris Internet Worm of 1988 spread was by abusing the Unix finger daemon, which used gets() to read data coming in over a network socket.
Fortunately, gets has been marked as a security problem in the manual page ever since the Worm, and it was removed from the C standard in C11.