Hi,
Here is how I try to implement your suggestion:
"What if you, temporarily, create the EDB instance in the sketch, and pass it to the DBAbstraction instance? Does that get rid of the multiple instances issue?"
But no, unfortunately it still doesn't compile, with the very same error below:
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_449996/sketch/DBabstraction.cpp.o: In function `DBabstraction::DBabstraction(HardwareSerial*)':
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_449996/sketch/DBabstraction.cpp:27: multiple definition of `rec_struct'
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_449996/sketch/CPPTEST_v3.ino.cpp.o:/Users/Thibault/Desktop/CPPtest_v2/CPPTEST_v3/CPPTEST_v3.ino:28: first defined here
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_449996/sketch/Runner.cpp.o: In function `Runner::run()':
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_449996/sketch/Runner.cpp:9: multiple definition of `rec_struct'
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_449996/sketch/CPPTEST_v3.ino.cpp.o:/Users/Thibault/Desktop/CPPtest_v2/CPPTEST_v3/CPPTEST_v3.ino:28: first defined here
CPPTEST_v3:
#include "Arduino.h"
#include "Runner.h"
//#include <EDB.h>
//#include "LSD.h"
//#include "LStorage.h"
//#include "LFlash.h"
//#include "DBrecord.h"
//#include "DBabstraction.h"
//#include "DBrecord.h"
// Define variables and constants
#define UsbSerial Serial // rename Serial ports
// Variables
Runner myRunner(&UsbSerial);
EDB *myDB;
// Add setup code
void setup()
{
UsbSerial.begin(115200);
UsbSerial.printf("starting");
myRunner.begin(myDB);
}
// Add loop code
void loop()
{
delay(500);
myRunner.run();
}
Runner.h:
#ifndef Runner_h
#define Runner_h
#include "Arduino.h"
#include "DBabstraction.h"
#include "HardwareSerial.h"
#include <EDB.h>
class Runner {
public:
Runner(HardwareSerial *serial);
void run();
void begin(EDB *currDB);
static DBabstraction *globalDB;
HardwareSerial * _HardSerial;
void globalConfigWriter( unsigned long address, byte data ) {
globalDB->dbWriter(address, data);
}
byte globalConfigReader( unsigned long address ) {
return globalDB->dbReader(address);
}
};
#endif // Runner_h
Runner.cpp:
#include "Runner.h"
// Code
/**************************************************/
DBabstraction *Runner::globalDB = NULL;
// public functions
Runner::Runner(HardwareSerial *serial) {
_HardSerial = serial;
if(_HardSerial) _HardSerial->printf("[Runner] constructor\n");
if (globalDB == NULL) globalDB = globalDB->getInstance(_HardSerial);
}
void Runner::run() {
if(_HardSerial) _HardSerial->printf("[Runner] run()\n");
}
void Runner::begin(EDB *currDB) {
if(_HardSerial) _HardSerial->printf("[Runner] begin()\n");
if (globalDB) globalDB->begin(currDB);
}
DBrecord.h:
#ifndef DBrecord_h
#define DBrecord_h
// Arbitrary record definition for this table.
struct Record_Struct {
int id;
int temperature;
} rec_struct;
#endif // DBrecord_h
DBabstraction.h:
#ifndef DBabstraction_h
#define DBabstraction_h
#include "Arduino.h"
#include <EDB.h>
#include "LSD.h"
#include "LStorage.h"
#include "LFlash.h"
#include "DBrecord.h"
#include "HardwareSerial.h"
#define Drv LFlash // use Internal 10M Flash
#define TABLE_SIZE 8192 // arbitrary for now
#define FILE_PATH "db.txt"
#define RECORDS_TO_CREATE 10 // This should be less than (TABLE_SIZE - sizeof(EDB_Header)) / sizeof(LogEvent).
class DBabstraction {
public:
static DBabstraction * getInstance(HardwareSerial *serial);
EDB *myDB;
void begin(EDB *currDB);
static LFile * myFile;
static void dbWriter( unsigned long address, byte data );
static byte dbReader( unsigned long address );
static HardwareSerial * _HardSerial;
private:
static DBabstraction * pInstance; // pointer to the DBabstraction instance NEW:
DBabstraction(HardwareSerial *serial);
};
#endif // DBabstraction_h
DBabstraction.cpp:
#include "DBabstraction.h"
DBabstraction *DBabstraction::pInstance = NULL;
LFile *DBabstraction::myFile = NULL;
HardwareSerial *DBabstraction::_HardSerial = NULL;
/* returns an instance of the object
* If no instance exists, it will create one
*/
DBabstraction * DBabstraction::getInstance(HardwareSerial *serial) {
if (pInstance == NULL) {
pInstance = new DBabstraction(serial);
}
return pInstance;
}
/* private constructor with HardwareSerial object pointer arg */
DBabstraction::DBabstraction(HardwareSerial *serial) {
_HardSerial = serial;
pInstance = this;
}
/* The raw writer function exposed to the EDB library */
void DBabstraction::dbWriter(unsigned long address, byte data ) {
/* nothing */
if(_HardSerial) _HardSerial->printf("[DBabstraction] dbWriter\n");
if (myFile) {
myFile->seek(address);
myFile->write(data);
myFile->flush();
}
}
/* The raw writer function exposed to the EDB library */
byte DBabstraction::dbReader( unsigned long address) {
/* nothing */
if(_HardSerial) _HardSerial->printf("[DBabstraction] dbReader\n");
if (myFile) {
myFile->seek(address);
return myFile->read();
}
}
/* The Arduino Begin() function of the class */
void DBabstraction::begin(EDB *currDB) {
myDB = currDB;
if(_HardSerial) _HardSerial->printf("[DBabstraction] Begin\n");
// start the flash drive
Drv.begin();
// try to open the DB file
*myFile = Drv.open(FILE_PATH, FILE_WRITE);
// if the file opened okay,
if (myFile) {
myFile->close();
} else {
// if the file didn't open, tell it
if(_HardSerial) _HardSerial->printf("[DBabstraction] error opening the db file\n");
}
// retry to open the file for reading:
*myFile = Drv.open(FILE_PATH, FILE_WRITE);
if (myFile) {
if(_HardSerial) _HardSerial->printf("Opening current table\n");
EDB_Status result = myDB->open(0);
if (result == EDB_OK) //
myFile->seek(0);
} else {
// if the file didn't open, print an error:
if(_HardSerial) _HardSerial->printf("[DBabstraction] error opening the db file, aborted\n");
}
// create the EDB instance with the DBabstraction reader/writer
myDB = new EDB(DBabstraction::dbWriter, DBabstraction::dbReader);
}
// Code DBabstraction.cpp
Thank you PaulS