Registering I2C event in class file

Hello,

Using two Uno's and I2C to communicate with each other, I have this working fine and doing as I want. But now I want to move the worker (slave) code from the sketch into my own class library.

What I want to do is remove all reference of Wire from the sketch and put it intomy class file source code. So in my class INIT() I have the Wire.begin(), which works, however I cannot get right how to register my own class method "receiveEvent()" in the Wire.onReceive(receiveEvent). I keep getting compiler errors.

In my class header I declare ...
public:
void receiveEvent (int howMany)

Then in the INIT() method of class source (ie. class called "MyClass").

Wire.onReceive(receiveEvent); //this won't compile
Wire.onReceive(MyClass::receiveEvent); //this won't compile

Sorry I don't have exact compiler error message with my, but its along the lines of "can't find method 'receiveEvent'", and/or no Wire.onReceive method found to match signature.

Anyone know how to declare your ISR events in your own class source file?

Thanks for any tips/comments.

Cheers,
Eric

Please post your code and your error messages.

Read this before posting a programming question

How to use this forum

however I cannot get right how to register my own class method "receiveEvent()" in the Wire.onReceive(receiveEvent).

For which instance of your class do you expect the Wire class to call it's method? "There will only ever be one" doesn't answer the question. The compiler has no way of knowing that.

You need to supply either a function or a static method. Since the compiler KNOWS that there is only one instance of the static method, shared by all instances of the class, it's happy with the method.

Of course, in the static method, you need to figure out how you are going to communicate with the correct instance of the class. That part is left as an exercise for the reader. It's not hard, if the class is a singleton. If it isn't, it is hard(er).