Here's a little database library I created that has been very useful for me, so I thought I would share. It makes use of the Arduino's EEPROM memory to store records in a table. I kept it simple to keep it small. You will have to spin your own locate and other functions like validation.
How to use in a nutshell:
- include Db.h and EEPROM.h
- declare an instance of DB, db
- define a structure for your records
- pick an address in EEPROM for the table to start
- make a little sketch that creates the table by calling db.create(address,sizeof(myRecStruct))
- in your application's sketch open the table with db.open(address)
- use the define DB_REC to cast your structure as a byte pointer when passing it as a parameter for write and read operations, this helps make the code more readable.
example snippet:
#include <EEPROM.h>
#include <DB.h>
DB db;
#define MY_TABLE 128
struct MyRec {
byte id;
char name[9];
int val;
} myrec;
db.create(MY_TBL,sizeof(myrec));
myrec.id = 1;
myrec.name[0]='O';
myrec.name[1]='N';
myrec.name[2]='E';
myrec.name[30=0;
db.append(DB_REC myrec);
.....
db.open(MY_TBL);
for (int i=1;i<db.nRecs();i++)
{
db.read(i, DB_REC myrec);
Serial.println(myrec.name);
}
\
DB.h
/*
DB.h
Database library for Arduino
Written by Madhusudana das
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 DB_PROM
#define DB_PROM
#include "EEPROM.h"
struct DB_Header
{
byte n_recs;
byte rec_size;
};
// slightly padded for the time being
#define DB_HEAD_SIZE 4
// DB_error values
#define DB_OK 0
#define DB_RECNO_OUT_OF_RANGE 1
#define DB_REC (byte*)(void*)&
typedef byte* DB_Rec;
class DB {
public:
void create(int head_ptr, byte recsize);
void open(int head_ptr);
boolean write(byte recno, const DB_Rec rec);
boolean read(byte recno, DB_Rec rec);
boolean deleteRec(byte recno); // delete is a reserved word
boolean insert(byte recno, const DB_Rec rec);
void append(DB_Rec rec);
byte nRecs();
DB_Header DB_head;
byte DB_error;
private:
int writeHead();
int readHead();
int EEPROM_dbWrite(int ee, const byte* p);
int EEPROM_dbRead(int ee, byte* p);
int DB_head_ptr;
int DB_tbl_ptr;
};
extern DB db;
#endif
DB.cpp
// DB.cpp
/*
Database library for Arduino
Written by Madhusudana das
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 "WProgram.h"
#include "DB.h"
/**************************************************/
// private functions
int DB::writeHead()
{
byte * p = (byte*)(void*)&DB_head;
int ee = DB_head_ptr;
int i;
for (i = 0; i < (int)sizeof(DB_head); i++)
EEPROM.write(ee++, *p++);
return i;
}
int DB::readHead()
{
byte* p = (byte*)(void*)&DB_head;
int ee = DB_head_ptr;
int i;
for (i = 0; i < (int)sizeof(DB_head); i++)
*p++ = EEPROM.read(ee++);
return i;
}
int DB::EEPROM_dbWrite(int ee, const byte* p)
{
int i;
for (i = 0; i < DB_head.rec_size; i++)
EEPROM.write(ee++, *p++);
return i;
}
int DB::EEPROM_dbRead(int ee, byte* p)
{
int i;
for (i = 0; i < DB_head.rec_size; i++)
*p++ = EEPROM.read(ee++);
return i;
}
/**************************************************/
// public functions
void DB::create(int head_ptr, byte recsize)
{
DB_head_ptr = head_ptr;
DB_head.n_recs = 0;
DB_head.rec_size = recsize;
writeHead();
}
void DB::open(int head_ptr)
{
DB_head_ptr = head_ptr;
DB_tbl_ptr = head_ptr + DB_HEAD_SIZE;
readHead();
}
//other operations commit DB_head edits to EEPROM so no need for a DB_close
boolean DB::write(byte recno, const DB_Rec rec)
{
DB_error = DB_OK;
if (recno>0 && recno<=DB_head.n_recs+1)
EEPROM_dbWrite(DB_tbl_ptr+((recno-1)*DB_head.rec_size), rec);
else
DB_error = DB_RECNO_OUT_OF_RANGE;
return DB_error==DB_OK;
}
boolean DB::read(byte recno, DB_Rec rec)
{
DB_error = DB_OK;
if (recno>0 && recno<=DB_head.n_recs)
EEPROM_dbRead(DB_tbl_ptr+((recno-1)*DB_head.rec_size), rec);
else
DB_error = DB_RECNO_OUT_OF_RANGE;
return DB_error==DB_OK;
}
boolean DB::deleteRec(byte recno)
{
DB_error = DB_OK;
if (recno<0 || recno>DB_head.n_recs)
{ Serial.println("recno out of range");
DB_error = DB_RECNO_OUT_OF_RANGE;
return false;
}
DB_Rec rec = (byte*)malloc(DB_head.rec_size);
for (int i=recno+1; i<=DB_head.n_recs; i++)
{
read(i,rec);
write(i-1,rec);
}
free(rec);
DB_head.n_recs--;
EEPROM.write(DB_head_ptr,DB_head.n_recs);
return true;
}
boolean DB::insert(byte recno, DB_Rec rec)
{
DB_error = DB_OK;
if (recno<0 || recno>DB_head.n_recs)
{ Serial.println("recno out of range");
DB_error = DB_RECNO_OUT_OF_RANGE;
return false;
}
DB_Rec buf = (byte*)malloc(DB_head.rec_size);
for (int i=DB_head.n_recs; i>=recno; i--)
{
read(i,buf);
write(i+1,buf);
}
free(buf);
write(recno,rec);
DB_head.n_recs++;
EEPROM.write(DB_head_ptr,DB_head.n_recs);
return true;
}
void DB::append(DB_Rec rec)
{
DB_error = DB_OK;
DB_head.n_recs++;
write(DB_head.n_recs,rec);
EEPROM.write(DB_head_ptr,DB_head.n_recs);
}
byte DB::nRecs()
{
return DB_head.n_recs;
}