B4R Tutorial EEPROM Utility

Excuse my English, it's not my native language
I am not an arduino expert, I want to contribute with this code.

one of the main problems is to save information, one of this is in the EEPROM memory,
I made this little code that simplifies the way to save or read the information in the native data of B4R
Since the EEPROM memory has a lifetime of writings (100,000 times, I'm not sure), so this code validates if the stored data is the same, and avoids writing, otherwise write the new data.

this works like arrays accesed by index, This needs rEEPROM and rRandomAccess libraries.

This simple code shows the main advantages
B4X:
Private Sub AppStart

    Serial1.Initialize(115200)
    Delay(3000)
    Log("EEPROM Test")
    Log("EEPROM Memory Size:", uEEPROM.Size)  ' This shows the amount of EEPROM memory
    Log("Is Configured:", uEEPROM.IsConfigured)  ' This is to test if something was stored in the EEPROM
    uEEPROM.ShowOptimalSettings  ' This shows the optimal size of MaxElements supported by the memory
End Sub

we will explain by parts
B4X:
    uEEPROM.ShowOptimalSettings

what does this mean?
you can make changes to the uEEPROM.bas file to use the memory according to your device, for example runnign in Arduino MEGA 2560:

--- Optimal Settings ---
EEPROM.Size =4096 bytes, StringSize=35 (34+1 byte of length) at Line #4
MaxElements must be 113 at Line #3
Using this configuration would occupy 4068 of 4096 bytes
------------------------

This mean, you can store 113 elements of Boolean, Int, UInt, Long, uLong, Double and Strings (with 35 bytes of Lengtht)

#3 Public const MaxElements As Int = 113
#4 Public const StringSize As Int = 35

To write you need use something like
uEEPROM.Write{TYPE}( Index, Value)

Write Example:
B4X:
for i = 0 to 4
 uEEPROM.WriteBool(i, False)           ' Boolean
 uEEPROM.WriteInt(i, -32768 + i)       ' Integer
 uEEPROM.WriteUInt(i, i)               ' Uint
 uEEPROM.WriteLong(i, -2147483648 + i) ' Long
 uEEPROM.WriteULong(i, i)              ' ULong
 uEEPROM.WriteDouble(i, -i-0.5)        ' Double
 uEEPROM.WriteString(i, JoinStrings(Array As String("Some String Here", i)))
Next

Read Example:
B4X:
 Log("Boolean at Index 2: ",uEEPROM.ReadBool(2))
 Log("String at Index 10:", uEEPROM.ReadString(10))

I hope you find it useful

This is the version 1.2 with a fix for String Offset in uEEPROM.bas
 

Attachments

  • uEEPROM_v1.2.zip
    3.5 KB · Views: 321
Last edited:
Top