Hi,
I’m writing because I am having a hard time to create and use a class library that should abstract an EDB database, and I am seeking your guidance and help .
The general project architecture is based on an INO sketch that instantiates a Runner class object which uses many other custom classes and libraries.
Among the classes used by the Runner is a class that is an abstraction (and make use) of Arduino Extended Database (EDB.h) library, to perform raw db operations while implementing high level business rules.
And it is at the very heart of my (C++) problem. >:(
The wanted workflow is: At every (sketch) loop, the runner object instance performs various tasks, for example it checks for serial port incoming bytes, and stores (write in flash file) some things with the EDB abstraction object.
I searched and found one example of similar request:Q&A_1 and Q&A_2
But after reading the given answers and trying different codes from Q&A1 and Q&A2 I still can’t compile and have questions such as:
-Where in my case should I put the read and write functions ? in the Runner.h ?
-Since there is only one class and instance using the EDB.h should I write the code to manage the instance ?
Despite all my efforts and searches and try, I’m now stuck: it doesn’t compile and my C++ knowledge is not enough.
I appreciate your help. Thank you.
Here is what I did (and doesn’t work):
The Sketch:
#include "Arduino.h"
#include <EDB.h>
#include "Runner.h"
#include "DBabstraction.h"
#define UsbSerial Serial
Runner myRunner;
void setup(){
myRunner.run();
UsbSerial.begin(115200);
UsbSerial.printf("[SETUP] start\n");
}
void loop() {delay(1000);UsbSerial.printf("[LOOP] run\n");}
The Runner.h:
#include "Arduino.h"
#include "DBabstraction.h"
class Runner {
public:
Runner();
void run();
static DBabstraction *globalDB;
void globalConfigWriter( unsigned long address, byte data ) {
globalDB->tdbWriter(address, data);
}
byte globalConfigReader( unsigned long address ) {
return globalDB->tdbReader(address);
}
private:
};
Runner.cpp
Runner::Runner(){
globalDB = new DBabstraction();
globalDB->begin();
}
void Runner::run(){
Serial.printf("ok\n");
}
DBabstraction.h
#include "Arduino.h"
#include <EDB.h>
//Use SPIFFS FS as data storage
#include "LStorage.h"
#include "LFlash.h"
#define Drv LFlash // use Internal 10M Flash
#define TABLE_SIZE 8192
// EDB records to create at begin()
#define RECORDS_TO_CREATE 10
// Arbitrary record definition for this table.
// This should be modified to reflect your record needs.
struct DataStruct {
int id;
int temperature;
}
dataStruct;
//
class DBabstraction {
public:
DBabstraction();
EDB myDB = new EDB(DBabstraction::dbWriter, DBabstraction::dbReader);
void begin();
static LFile myFile;
static void dbWriter( unsigned long address, byte data );
static byte dbReader( unsigned long address );
private:
static DBabstraction * pInstance;
};
DBabstraction.cpp
#include "DBabstraction.h"
//constructor
DBabstraction::DBabstraction(){
pInstance = this;
}
//public functions
static void DBabstraction::dbWriter(unsigned long address, byte data ){
/* nothing for now but planned SD implementation */
}
static byte DBabstraction::dbReader( unsigned long address){
/* nothing for now but planned SD implementation */
}
Again thx.