/* Custom EEPROM Class for memory management. */
class CEEPROM
{
public:
CEEPROM()
{
this->id = ++nextID;
}
short getID()
{
return id;
}
void write(unsigned short address, unsigned char data)
{
// Wait for completion of the previous writing.
while(EECR & (1 << EEPE));
// Set up the address and data registers.
EEAR = address;
EEDR = data;
// Write a logical one to the EEMPE
EECR |= (1 << EEMPE);
// Start EEPROM writing by setting a logical one to the EEPE
EECR |= (1 << EEPE);
}
unsigned char read(unsigned short address)
{
// Wait for completion of the previous writing.
while(EECR & (1 << EEPE));
// Set up the address register.
EEAR = address;
// Start the EEPROM read by writing to the EERE.
EECR |= (1 << EERE);
// Return data from the data register.
return EEDR;
}
void clear()
{
for (int i = 0; i < uno_size; i++)
{
write(i, 0);
}
}
void clearArea(unsigned short start, unsigned short end)
{
for (int i = start; i <= end; i++)
{
write(i, 0);
}
}
protected:
private:
static inline short nextID = 0;
short id;
const short uno_size = 1024;
const short mega_size = 4096;
};
CEEPROM cp;
// Contact object using char* name and id lengths.
const int ul = 5;
const int nl = 9;
void saveContact(int pos, char* name, char* uuid)
{
// Save UUID.
for (int i = pos, index = 0; i < pos + ul; i++, index++)
{
cp.write(i, uuid[index]);
}
// Save Name.
for (int i = pos + ul, index = 0; i < pos + ul + nl; i++, index++)
{
if (name[index] != '\0')
{
cp.write(i, name[index]);
}
else
{
break;
}
}
}
void printContact(int pos)
{
// Retreive UUID (always constant).
char* uuid = new char[ul];
for (int i = pos, index = 0; i < pos + ul; i++, index++)
{
uuid[index] = cp.read(i);
}
// Retreive name using temp to shorten later.
char* temp = new char[nl];
int len = 0;
for (int i = pos + ul, index = 0; i < pos + ul + nl; i++, index++)
{
if (cp.read(i) != 0)
{
temp[index] = cp.read(i);
len++;
}
else
{
break;
}
}
char* name = new char[len];
for (int i = 0; i < len; i++)
{
name[i] = temp[i];
}
free(temp);
Serial.println("Length: " + String(len));
Serial.print("Name: ");
Serial.println(name);
Serial.print("UUID: ");
Serial.println(uuid);
}
void setup()
{
cp.clearArea(3, 17);
Serial.begin(9600);
saveContact(3, "Trent", "CF2B4");
delay(200);
printContact(3);
}
void loop()
{
}