I've modified EEPROM code and made this,
EEPROM04.cpp
#include "WProgram.h"
#include "Wire.h"
#include "EEPROM04.h"
uint8_t EEPROM04Class::read(int address)
{
//Wire.begin();
Wire.beginTransmission(0x50); // link to 24LC04
Wire.send(address); // must act as a position pointer
Wire.endTransmission();
delay(5);
Wire.requestFrom(0x50,1); // request 1 byte from slave device 24LC04
delay(5);
char c =Wire.receive();
return c;
}
void EEPROM04Class::write(int address, uint8_t value)
{
//Wire.begin();
Wire.beginTransmission(0x50); // connect to 24LC04 device address
Wire.send(address); // beginning address within EEPROM
Wire.send(value);
Wire.endTransmission();
delay(5);
}
EEPROM04Class EEPROM04;
EEPROM04.h
#ifndef EEPROM04_h
#define EEPROM04_h
//#include <inttypes.h>
class EEPROM04Class{
public:
uint8_t read(int);
void write(int, uint8_t);
};
extern EEPROM04Class EEPROM04;
#endif
DB04.cpp
#include "WProgram.h"
#include "DB04.h"
/**************************************************/
// private functions
int DB04::writeHead()
{
byte * p = (byte*)(void*)&DB04_head;
int ee = DB04_head_ptr;
int i;
for (i = 0; i < (int)sizeof(DB04_head); i++)
EEPROM04.write(ee++, *p++);
return i;
}
int DB04::readHead()
{
byte* p = (byte*)(void*)&DB04_head;
int ee = DB04_head_ptr;
int i;
for (i = 0; i < (int)sizeof(DB04_head); i++)
*p++ = EEPROM04.read(ee++);
return i;
}
int DB04::EEPROM04_dbWrite(int ee, const byte* p)
{
int i;
for (i = 0; i < DB04_head.rec_size; i++)
EEPROM04.write(ee++, *p++);
return i;
}
int DB04::EEPROM04_dbRead(int ee, byte* p)
{
int i;
for (i = 0; i < DB04_head.rec_size; i++)
*p++ = EEPROM04.read(ee++);
return i;
}
/**************************************************/
// public functions
void DB04::create(int head_ptr, byte recsize)
{
DB04_head_ptr = head_ptr;
DB04_head.n_recs = 0;
DB04_head.rec_size = recsize;
writeHead();
}
void DB04::open(int head_ptr)
{
DB04_head_ptr = head_ptr;
DB04_tbl_ptr = head_ptr + DB04_HEAD_SIZE;
readHead();
}
//other operations commit DB04_head edits to EEPROM so no need for a DB04_close
boolean DB04::write(byte recno, const DB04_Rec rec)
{
DB04_error = DB04_OK;
if (recno>0 && recno<=DB04_head.n_recs+1)
EEPROM04_dbWrite(DB04_tbl_ptr+((recno-1)*DB04_head.rec_size), rec);
else
DB04_error = DB04_RECNO_OUT_OF_RANGE;
return DB04_error==DB04_OK;
}
boolean DB04::read(byte recno, DB04_Rec rec)
{
DB04_error = DB04_OK;
if (recno>0 && recno<=DB04_head.n_recs)
EEPROM04_dbRead(DB04_tbl_ptr+((recno-1)*DB04_head.rec_size), rec);
else
DB04_error = DB04_RECNO_OUT_OF_RANGE;
return DB04_error==DB04_OK;
}
boolean DB04::deleteRec(byte recno)
{
DB04_error = DB04_OK;
if (recno<0 || recno>DB04_head.n_recs)
{ Serial.println("recno out of range");
DB04_error = DB04_RECNO_OUT_OF_RANGE;
return false;
}
DB04_Rec rec = (byte*)malloc(DB04_head.rec_size);
for (int i=recno+1; i<=DB04_head.n_recs; i++)
{
read(i,rec);
write(i-1,rec);
}
free(rec);
DB04_head.n_recs--;
EEPROM04.write(DB04_head_ptr,DB04_head.n_recs);
return true;
}
boolean DB04::insert(byte recno, DB04_Rec rec)
{
DB04_error = DB04_OK;
if (recno<0 || recno>DB04_head.n_recs)
{ Serial.println("recno out of range");
DB04_error = DB04_RECNO_OUT_OF_RANGE;
return false;
}
DB04_Rec buf = (byte*)malloc(DB04_head.rec_size);
for (int i=DB04_head.n_recs; i>=recno; i--)
{
read(i,buf);
write(i+1,buf);
}
free(buf);
write(recno,rec);
DB04_head.n_recs++;
EEPROM04.write(DB04_head_ptr,DB04_head.n_recs);
return true;
}
void DB04::append(DB04_Rec rec)
{
DB04_error = DB04_OK;
DB04_head.n_recs++;
write(DB04_head.n_recs,rec);
EEPROM04.write(DB04_head_ptr,DB04_head.n_recs);
}
byte DB04::nRecs()
{
return DB04_head.n_recs;
}
DB04.h
#ifndef DB04_PROM
#define DB04_PROM
#include "EEPROM04.h"
struct DB04_Header
{
byte n_recs;
byte rec_size;
};
// slightly padded for the time being
#define DB04_HEAD_SIZE 4
// DB04_error values
#define DB04_OK 0
#define DB04_RECNO_OUT_OF_RANGE 1
#define DB04_REC (byte*)(void*)&
typedef byte* DB04_Rec;
class DB04 {
public:
void create(int head_ptr, byte recsize);
void open(int head_ptr);
boolean write(byte recno, const DB04_Rec rec);
boolean read(byte recno, DB04_Rec rec);
boolean deleteRec(byte recno); // delete is a reserved word
boolean insert(byte recno, const DB04_Rec rec);
void append(DB04_Rec rec);
byte nRecs();
DB04_Header DB04_head;
byte DB04_error;
private:
int writeHead();
int readHead();
int EEPROM04_dbWrite(int ee, const byte* p);
int EEPROM04_dbRead(int ee, byte* p);
int DB04_head_ptr;
int DB04_tbl_ptr;
};
extern DB04 db04;
#endif
So, the EEPROM04 code works well but this DB04 is not working as expected, it gives me an error after uploading "recno out of range" and i scanned the EEPROM and it was empty.
please help me with this DB04 library.
PS: pde attached.
EEPROM04_DB04.zip (1.66 KB)