EXROM - an extension to the EEPROM library.

So, while trying to do some work with the EEPROM, i ended up writing several useful functions for passing various arrays and other things to and from EEPROM. hence EXROM - EXtended eepROM library! so... here is the code!

to write to the EEPROM, you use the function

EXROM.write(pointer, data, size).

Pointer is an integer between 0 and 511 (pointers to EEPROM memory bytes).
Data is something in byte, char, boolean, or int format, and all of these variable types can be used in arrays.
Size is needed to write the correct number of bytes to the EEPROM. best way to do this is to use the sizeof() command. If only passing a single byte, char, or boolean value, this can be ommited.

EXROM.read(pointer, data, size).

This function has the same inputs, however there are a few differences.
Pointer is the same as the write function - the memory location you want to read.
Data is the variable you want to save the data read from EEPROM to. If only passing a single byte, char or boolean, this must be a pointer, and so must be prefixed with an & symbol to work properly.
Size works the same as in the write function, and is also only needed if reading an array of data.

Any comments, criticisms, improvements, etc are welcome. Also, if anyone knows how to convert a float to a 4 byte array, and then back again, it would be much appreciated - I lost about 4 hours trying to figure that out before giving up :S.

EXROM.h

/*
  EXROM.h - Extended EEPROM library
  Written/extended by Tom Bloor aka. TBSliver.
  
  Based on:
  EEPROM library
  Copyright (c) 2006 David A. Mellis.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef EXROM_h
#define EXROM_h

#include "WProgram.h"

class EXROMClass
{
public:
//          write functions
void write(int, byte);
void write(int, byte*, int);
void write(int, char);
void write(int, char*, int);
void write(int, int);
void write(int, int*, int);
//          read functions
void read(int, byte*);
void read(int, byte*, int);
void read(int, char*);
void read(int, char*, int);
void read(int, int*);
void read(int, int*, int);
private:
byte read(int);
};

extern EXROMClass EXROM;

#endif

EXROM.cpp

/*
  EXROM.cpp - Extended EEPROM library
  Written/extended by Tom Bloor aka. TBSliver.
  
  Based on:
  
  EEPROM library
  Copyright (c) 2006 David A. Mellis.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <avr/eeprom.h>
#include "WProgram.h"
#include "EXROM.h"

//          Write Functions

void EXROMClass::write(int writePointer, byte writeDataStore)                   //write a byte to EEPROM
{
    eeprom_write_byte((unsigned char *) writePointer, writeDataStore);
}

void EXROMClass::write(int writePointer, byte writeDataStore[], int writeSize)  //write a byte array to EEPROM
{
  for (int i = 0; i < writeSize; i++)
  {
    int j = writePointer + i;
    write(j, writeDataStore[i]);
  }
}

void EXROMClass::write(int writePointer, char writeDataStore)                   //write a char to EEPROM
{
  write(writePointer, byte(writeDataStore));
}

void EXROMClass::write(int writePointer, char writeDataStore[], int writeSize)  //write a string to EEPROM
{
  for (int i = 0; i < writeSize; i++)
  {
    int j = writePointer + i;
    write(j, byte(writeDataStore[i]));
  }
}

void EXROMClass::write(int writePointer, int writeDataStore)                    //write an int to EEPROM
{
  write(writePointer, byte(writeDataStore));
  write(writePointer+1, byte(writeDataStore>>8));
}

void EXROMClass::write(int writePointer, int writeDataStore[], int writeSize)   //write an int array to EEPROM
{
  for(int i=0; i < writeSize/2; i++)
  {
    int j = writePointer + i*2;
    write(j, writeDataStore[i]);
  }
}

//          Read Functions

void EXROMClass::read(int readPointer, byte* readDataStore)                     //read a byte from EEPROM
{
  *readDataStore = read(readPointer);
}

void EXROMClass::read(int readPointer, byte readDataStore[], int readSize)      //read a byte array from EEPROM
{
  for (int i = 0; i < readSize; i++)
  {
    int j = readPointer + i;
    readDataStore[i]=read(j);
  }
}

void EXROMClass::read(int readPointer, char* readDataStore)                     //read a char from EEPROM
{
  *readDataStore = char(read(readPointer));
}

void EXROMClass::read(int readPointer, char readDataStore[], int readSize)      //read a string from EEPROM
{
  for (int i = 0; i < readSize; i++)
  {
    int j = readPointer + i;
    readDataStore[i] = char(read(j));
  }
}

void EXROMClass::read(int readPointer, int* readDataStore)                      //read an int from EEPROM
{
  *readDataStore = int(read(readPointer)) | int(read(readPointer+1)<<8);
}

void EXROMClass::read(int readPointer, int readDataStore[], int readSize)       //read an int array from EEPROM
{
  for(int i=0; i < readSize/2; i++)
  {
    int j = readPointer + i*2;
    read(j, &readDataStore[i]);
  }
}

//private functions

byte EXROMClass::read(int readPointer)
{
  return eeprom_read_byte((unsigned char *) readPointer);
}

EXROMClass EXROM;

Example code (uses every type)

#include <EXROM.h>


void setup()
{
  Serial.begin(9600);
  
  byte array[] = {43,23,67,176,23,79,81};
  byte array2[sizeof(array)];
  EXROM.write(0, array, sizeof(array));
  EXROM.read(0, array2, sizeof(array2));
  Serial.println("Byte Array Test");
  Serial.println("original array");
  
  for(int i=0;i<sizeof(array);i++)
  {
    Serial.println(array[i], DEC);
  }
  
  Serial.println("new array");
  
  for(int i=0;i<sizeof(array);i++)
  {
    Serial.println(array2[i], DEC);
  }
  
  
  char array3[] = "Arduino";
  char array4[sizeof(array3)];
  EXROM.write(7, array3, sizeof(array3));
  EXROM.read(7, array4, sizeof(array4));
  Serial.println("Char Array Test");
  Serial.println("original array");
  
  for(int i=0;i<sizeof(array3);i++)
  {
    Serial.println(array3[i]);
  }
  
  Serial.println("new array");
  
  for(int i=0;i<sizeof(array4);i++)
  {
    Serial.println(array4[i]);
  }
  
  byte test = 55;
  byte test2;
  EXROM.write(99, test);
  EXROM.read(99, &test2);
  Serial.println("Byte test");
  Serial.println(test, DEC);
  Serial.println(test2, DEC);
  
  char test3 = 'a';
  char test4;
  EXROM.write(100, test3);
  EXROM.read(100, &test4);
  Serial.println("Char test");
  Serial.println(test3);
  Serial.println(test4);
  
  boolean test5 = true;
  boolean test6;
  EXROM.write(101, test5);
  EXROM.read(101, &test6);
  Serial.println("Boolean test");
  Serial.println(test5, DEC);
  Serial.println(test6, DEC);
  
  boolean array5[] = {true, false, true, true, false};
  char array6[sizeof(array5)];
  EXROM.write(7, array5, sizeof(array5));
  EXROM.read(7, array6, sizeof(array6));
  Serial.println("Boolean Array Test");
  Serial.println("original array");
  
  for(int i=0;i<sizeof(array5);i++)
  {
    Serial.println(array5[i], DEC);
  }
  
  Serial.println("new array");
  
  for(int i=0;i<sizeof(array6);i++)
  {
    Serial.println(array6[i], DEC);
  }
  
  int int1 = 3459;
  int int2;
  EXROM.write(101, int1);
  EXROM.read(101, &int2);
  Serial.println("Int test");
  Serial.println(int1);
  Serial.println(int2);
  
  int inta[] = {43,23,67,176,23,79,81};
  int inta2[sizeof(inta)/2];
  EXROM.write(76, inta, sizeof(inta));
  EXROM.read(76, inta2, sizeof(inta2));
  Serial.println("Int Array Test");
  Serial.println("original array");
  
  for(int i=0;i<sizeof(inta)/2;i++)
  {
    Serial.println(inta[i]);
  }
  
  Serial.println("new array");
  
  for(int i=0;i<sizeof(inta2)/2;i++)
  {
    Serial.println(inta2[i]);
  }
}

void loop()
{
  
}

This example should give an output of

Byte Array Test
original array
43
23
67
176
23
79
81
new array
43
23
67
176
23
79
81
Char Array Test
original array
A
r
d
u
i
n
o

new array
A
r
d
u
i
n
o

Byte test
55
55
Char test
a
a
Boolean test
1
1
Boolean Array Test
original array
1
0
1
1
0
new array
1
0
1
1
0
Int test
3459
3459
Int Array Test
original array
43
23
67
176
23
79
81
new array
43
23
67
176
23
79
81

[UNTESTED CODE, BUT SHOULD WORK]

void setup() {
      union FloatToArray {
            float f;
            byte a[4];
      };

      FloatToArray ftaSend;
      ftaSend.f = 3.14;

      byte store[4];
      store[0] = ftaSend.a[0];
      store[1] = ftaSend.a[1];
      store[2] = ftaSend.a[2];
      store[3] = ftaSend.a[3];

      EXROM.write(0, store, 4);
      byte recieve[4];
      EXROM.read(0, recieve, 4);

      FloatToArray ftaRecieve;
      ftaRecieve.a[0] = recieve[0];
      ftaRecieve.a[1] = recieve[1];
      ftaRecieve.a[2] = recieve[2];
      ftaRecieve.a[3] = recieve[3];

      Serial.begin(9600);
      Serial.println(ftaRecieve.f);
}

:slight_smile:

Good job on the library

You might want to look at this if you have not already seen it:
http://www.arduino.cc/playground/Code/EEPROMWriteAnything

Mowcius

Thanks for the code AlphaBeta, it does work aswell :stuck_out_tongue: will look at incorporating it into the library.

mowcius: hadnt actually seen that until now, will have to test it to see how well it handles everything. The main aim of this library is for it to be able to write ANY data to the EEPROM, although I'm a few variable types off :stuck_out_tongue:

mowcius: hadnt actually seen that until now, will have to test it to see how well it handles everything. The main aim of this library is for it to be able to write ANY data to the EEPROM, although I'm a few variable types off

Ok, sounds good. I currently use the EEPROMWriteAnything to write floating point variables to EEPROM...

Mowcius

well, ive managed to get every signed data type working for writing on the Arduino, however i've made an interesting discovery. The avr/eeprom.h library actually includes several functions for reading and writing 16bit, 32bit, and blocks of data that arent used by the Arduino EEPROM library. So now im going to go see about incorporating these functions into the library, and possibly make this library work faster and smaller. other thing is, i think these functions support all the data types natively, so this might cut down the number of functions aswell. Wish me luck!!!

Well, didnt manage to get the new found bits working for float variables, so that will be for another day. HOWEVER I did manage to get everything working under the old format of the library, which can be found here:
http://code.google.com/p/arduino-exrom/

enjoy! usual installation method: unzip to hardware/libraries folder.