/**************************************************************************************************
*
*  Eeprom Config
*
*   Version:      2.0.0 - Mai 2009
*   Author:       Etienne Ribeiro    / tutorial assistant caad      /  eribeiro[at]ethz.ch
*   Supervisor:   Christoph Wartmann / chair for caad - ETH Zürich  /  wartmann[at].arch.ethz.ch
*
*
*                 Desc:         Basic library to write Byte, Char, Int, Long and String to EEPROM.
*
*   Methodes:     boolean EEPROM_write1 (int address, char data)
*                   Writes 1 byte to eeprom. (writes only if same data isn't yet written)
*                 boolean EEPROM_write2 (int address, int data)
*                   Writes 2 bytes (int-value) to eeprom. (writes only if same data isn't yet written)
*                 boolean EEPROM_write4 (int address, long data)
*                   Writes 4 bytes (long-value) to eeprom. (writes only if same data isn't yet written)
*                 boolean EEPROM_writeX (int address, char* data, int length)
*                   Writes X bytes (string-value) to eeprom. (writes only if same data isn't yet written)
*                 boolean EEPROM_write1If (boolean IfCase, int address, byte data)
*                   same as write1 except: will only execute if IfCase == true
*                 boolean EEPROM_write2If (boolean IfCase, int address, int data)
*                   same as write2 except: will only execute if IfCase == true
*                 boolean EEPROM_write4If (boolean IfCase, int address, long data)
*                   same as write4 except: will only execute if IfCase == true
*                 boolean EEPROM_writeXIf (boolean IfCase, int address, char* data, int length)
*                   same as writeX except: will only execute if IfCase == true
*                 byte EEPROM_read1 (int address)
*                   reads 1 byte from eeprom.
*                 int EEPROM_read2 (int address)
*                   reads 2 bytes (int-value) from eeprom.
*                 long EEPROM_read4 (int address)
*                   reads 4 bytes (long-value) from eeprom.
*                 void EEPROM_readX (int address, char* ret, int length)
*                   reads X bytes (String-value) from eeprom.
*                 int EEPROM_LengthOfString (char* data)
*                   returns the length of a string (char array).
*                   note: a string must be terminated by 0
*                   ex: char myString[] = {"test"}; // adds a 0 at the end
*
***************************************************************************************************/

#include <EEPROM.h> // uncheck for Arduino
//#include <WEEPROM.h> // uncheck for Wiring






// Write to EEPROM

boolean EEPROM_write1 (int address, char data) {

        if (EEPROM.read (address) != data) { // only write if not same data already written
                EEPROM.write(address, data);
                return true;
        }

        return false;

}
boolean EEPROM_write2 (int address, int data) {

        boolean ret = false;
        if (EEPROM_write1 (address + 0, (char) (data >> 8)))
        ret = true;
        if (EEPROM_write1 (address + 1, (char) data))
        ret = true;
        return ret;

}
boolean EEPROM_write4 (int address, long data) {

        boolean ret = false;
        if (EEPROM_write1 (address + 0, (char) (data >> 24)))
        ret = true;
        if (EEPROM_write1 (address + 1, (char) (data >> 16)))
        ret = true;
        if (EEPROM_write1 (address + 2, (char) (data >> 8)))
        ret = true;
        if (EEPROM_write1 (address + 3, (char) data))
        ret = true;
        return ret;

}
boolean EEPROM_writeX (int address, char* data, int length) {

        boolean ret = false;
        for (int i=0;i<length;i++) {
                if (EEPROM_write1 (address + i, (char) data[i]))
                ret = true;
        }
        return ret;

}

// Write if

boolean EEPROM_write1If (boolean IfCase, int address, byte data) {

        // Case (return if false)
        if (!IfCase)
        return false;

        return EEPROM_write1 (address, data);

}
boolean EEPROM_write2If (boolean IfCase, int address, int data) {

        // Case (return if false)
        if (!IfCase)
        return false;

        return EEPROM_write2 (address, data);

}
boolean EEPROM_write4If (boolean IfCase, int address, long data) {

        // Case (return if false)
        if (!IfCase)
        return false;

        return EEPROM_write4 (address, data);

}
boolean EEPROM_writeXIf (boolean IfCase, int address, char* data, int length) {

        // Case (return if false)
        if (!IfCase)
        return false;

        return EEPROM_writeX (address, data, length);

}




// Read from EEPROM

byte EEPROM_read1 (int address) {

        return EEPROM.read (address);

}
int EEPROM_read2 (int address) {

        // Get value
        int val = 0;
        for (int i=0;i<2;i++) {

                val += EEPROM.read (address + i);
                if (i != 1) val <<= 8;

        }
        return val;

}
long EEPROM_read4 (int address) {

        // Get value
        long val = 0;
        for (int i=0;i<4;i++) {

                val += EEPROM.read (address + i);
                if (i != 3) val <<= 8;

        }
        return val;

}
void EEPROM_readX (int address, char* ret, int length) {

        // Get value
        for (int i=0;i<length;i++) {

                *ret++ = EEPROM.read (address + i);

        }

}




// LengthOfString

int EEPROM_LengthOfString (char* data) {

        int count = 0;
        char *p = data;
        while (*p != '\0') {
                p++;
                count++;
        }
        return count;

}