I tried to set DBabstraction as static, therefore added a DBabstraction *getInstance() function and set constructor to private scope. Then in the Runner implementation, I modified the way I assign *globalDB to use globalDB->getInstance(_HardSerial) rather than new().
It doesn't compile, with (almost the same) compilation result:
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_376243/sketch/DBabstraction.cpp.o: In function `DBabstraction::DBabstraction(HardwareSerial*)':
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_376243/sketch/DBabstraction.cpp:26: multiple definition of `rec_struct'
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_376243/sketch/CPPTEST.ino.cpp.o:/Users/Thibault/Desktop/CPPtest_v2/CPPTEST.ino:27: first defined here
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_376243/sketch/Runner.cpp.o: In function `Runner::run()':
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_376243/sketch/Runner.cpp:9: multiple definition of `rec_struct'
/var/folders/y8/2cf22w216j7cxt1k7g54v1y80000gp/T/arduino_build_376243/sketch/CPPTEST.ino.cpp.o:/Users/Thibault/Desktop/CPPtest_v2/CPPTEST.ino:27: first defined here
collect2: error: ld returned 1 exit status
Using library EDB at version 1.0.4 in folder: /Users/Thibault/Documents/Arduino/libraries/EDB
Using library LStorage in folder: /Users/Thibault/Library/Arduino15/packages/LinkIt/hardware/arm/1.1.23/libraries/LStorage (legacy)
exit status 1
Error compiling for board LinkIt ONE.
CPPTEST.ino:
#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);
// Add setup code
void setup()
{
UsbSerial.begin(115200);
UsbSerial.printf("starting");
myRunner.begin();
}
// 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"
class Runner {
public:
Runner(HardwareSerial *serial);
void run();
void begin();
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() {
if(_HardSerial) _HardSerial->printf("[Runner] begin()\n");
if (globalDB) globalDB->begin();
}
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();
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() {
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, set flag 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
Thanks for your help.