I'm trying to write a library for Arduino using the Wire library. Here's my code:
PiCom.h:
#ifndef PiCom_h
#define PiCom_h
#include "Arduino.h"
#include <Wire.h>
class PiCom
{
public:
PiCom(int address, int* integers[], double* doubles[]);
static void onReceiveCallback(int numbytes);
static void onRequestCallback();
private:
//Actually arrays of pointers to the data to be sent, has to be declared that way cause the size of the array isn't known at compile time
int** _integers;
double** _doubles;
void onReceive(int numbytes);
void onRequest();
static PiCom instance;
};
#endif
PiCom.cpp:
#include "Arduino.h"
#include "PiCom.h"
static void onReceiveCallback(int numbytes)
{
}
PiCom::PiCom(int address, int* integers[], double* doubles[])
{
_integers = integers;
_doubles = doubles;
Wire.begin(address);
void (*receiveFunction) (int);
receiveFunction = &PiCom::onReceiveCallback;
Wire.onReceive(receiveFunction);//Here i get an error
}
I want the constructor to be created when a new instance is created. A mehtod in this object should be called when data is requested or received. When trying to pass onRequest(int) directly to Wire.onRequest(void (*) (int)) an error is thrown. Same happend when saving it to receiveFunction.
This problem was solved by using a static function (onReceiveCallback(int). I wanted this static function to call onRequest(int) in the object saved into PiCom::instance.
But now i'm getting another error:
PiCom\PiCom.cpp.o: In function `PiCom':
C:\Program Files\Arduino\libraries\PiCom/PiCom.cpp:26: undefined reference to `PiCom::onReceiveCallback(int)'
C:\Program Files\Arduino\libraries\PiCom/PiCom.cpp:26: undefined reference to `PiCom::onReceiveCallback(int)'