Use Wire.onReceive within a custom class

Hello everybody,

I am creating a custom protocol to share data over I2C bus. It is still a work in progress, but I am hoping to be able to complete it soon. I need that protocol, for a project of mine, which will allow me to better communicate between several Arduinos (uno, mega… etc.). I have made some tests, have written some code and voala - it is far from perfect, and surely needs a lot of improvement, but it works.

Recently, I decided to export that protocol and create it in a separate library called DataFrame, in order to be able to use this piece of code in other projects, and this is where my problems began…

The problem, that I am currently facing, is in the class constructor. Here is the relevant code:

/*
  Library for using data protocol over I2C bus
  Created by Atanas Hristov
*/

#include "DataFrame.h"
#include "Arduino.h"
#include "Wire.h"

DataFrame::DataFrame(uint8_t devAddress){
	Wire.begin(devAddress);
	Wire.onReceive(receiveDataPacket);   //Attach interrupt for receive
	Wire.onRequest(respondToMaster);  //Attach interrupt for request	
	
	setDefaultCommand();		//Used to set default data to private variables
	
}	//End of default class constructor DataFrame();

The problem is located in those two lines:

	Wire.onReceive(receiveDataPacket);   //Attach interrupt for receive
	Wire.onRequest(respondToMaster);  //Attach interrupt for request

Both functions receiveDataPacket and respondToMaster are member functions of the class DataFrame. I have tried some variations, but for some reason this is not working and it is still beyond my knowledge.

I am also attaching the whole library, if you think that would help as well.

If you need me to clarify something, or to provide further information, don’t hesitate to ask :slight_smile:

I would appreciate any help making this work.

Thank you in advance!

DataFrame.zip (2.25 KB)

When the interrupt happens, which instance of the class should be notified? How do you expect that to happen?

The methods must be static methods (belonging to the class) not member functions (belonging to an instance).

Hi,

After some more in-dept research and reading I have found the answer, but it was too late to post it on the forum.

Here is a bit more details explanation, of what PaulS ment.

The type of pointer-to-member-function is different from pointer-to-function.

The type of a function is different depending on whether it is an ordinary function or a non-static member function of some class:

int f(int x);

the type is "int (*)(int)" // since it is an ordinary function




And



int Mat::f2(int x);
the type is "int (Mat::*)(int)" // since it is a non-static member function of class Mat




Note: if it's a static member function of class Fred, its type is the same as if it were an ordinary function: `int (*)(char,float)`

Please excuse me, for the late answer, but I was unable to post here untill today.

I am already working on redisining the library, so when I am finished, I will post it here, if, in case, it posts any interest to anyone.