B4R Tutorial Some code to write into and read from EEPROM on ESP8266

Hi,

My first tutorial.

I've been playing with code to do what the title says and I have come up with this.

It should be self explanatory.

It relies on the use of B4Rserializator.

B4X:
          '**********************************************************
    'Variable Definitions for moving objects to and from EEPROM
    '**********************************************************
    Dim be(10) As Object 'used as a storage buffer for the serializator
    Dim ListOfObjectsToStore() As Object = Array("Hello", "World",1234, False, True, False)
    Dim ByteDataToStore() As Byte
    Dim bc As ByteConverter

    Dim ByteLengthOfDataInEEPROM() As Byte
    Dim ActualLengthOfDataInEEPROM As Int
    Dim ByteDataInEEPROM() As Byte
    Dim ObjectsInEEPROM() As Object
 
    'START OF CODE
    'Convert the array of objects into a sequence of bytes.
    ByteDataToStore = serializator.ConvertArrayToBytes(ListOfObjectsToStore)
 
    Log("Writing objects into the EEPROM...")
    'Store the length of the data being written into the EEPROM as an Integer taking up two bytes at Storage Location 700 in the EEPROM
    eeprom.WriteBytes(bc.IntsToBytes(Array As Int(ByteDataToStore.Length)), 700)
    'Now write out all the bytes into the EEPROM
    eeprom.WriteBytes(ByteDataToStore, 702)
 
 
 
    Log("Reading data from the EEPROM...")
    'Get two bytes out of the EEPROM from location 700
    ByteLengthOfDataInEEPROM = eeprom.ReadBytes(700, 2)
    'Convert these two bytes into an Integer - this is the number of bytes making up the objects that are stored in the EEPROM
    ActualLengthOfDataInEEPROM =  bc.IntsFromBytes(ByteLengthOfDataInEEPROM)(0)
     
    'Get all that byte data out of the EEPROM
    ByteDataInEEPROM = eeprom.ReadBytes(702, ActualLengthOfDataInEEPROM)

    'Convert those bytes back into an array of objects
    ObjectsInEEPROM = serializator.ConvertBytesToArray(ByteDataInEEPROM, be)

    'Now list all the objects
    For Each o As Object In ObjectsInEEPROM
        Log(o)
    Next

I used location 700 and 702 as arbitrary numbers as I was using EEPROM space lower down for some other tests. You can obviously set these to whatever you want in the EEPROM space.

The code is pretty verbose, and it could be shortened, but I thought I'd make it verbose just to explain what's happening - it will help me when I completely forget how to do all this!

JMB
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…