2

I am attempting to make an I2C slave out of my Arduino and I can't get past the Wire.onReceive function. Here is my code. The documentation says to just add a function that has the style of void myHandler(int bytes) but I can't figure out why my readPI function does not meet these requirements.

    #include "piCom.h"
    #include "WireAddresses.h"
    #include <Wire.h>

    pi::pi(){
       Wire.begin(NB_INFO); // is 0x04
       Wire.onReceive( readPI );
       //Wire.onRequest(sendPI);
    }

    void pi::readPI(int byteCount){
       while( Wire.available() ){

             ref_IMU = Wire.read();

       }//end while

    }//end readPI

    void pi::sendPI(){

       Wire.write(1);
       //use to send pressure info to pi

    }//end sendPI


    //double pi::getref_IMU(){return ref_IMU;}

then here is the error

piCom.cpp: In constructor 'pi::pi()':
piCom.cpp:7: error: no matching function for call to'TwoWire::onReceive(<unresolved overloaded function type>)'
 Wire.onReceive( readPI );
                        ^

no matching function for call to 'TwoWire::onReceive(<unresolved overloaded function type>)'

Any help is greatly appreciated.

2
    void pi::sendPI(){

That is a class function (method) which has an implicit this pointer. You can't use it as a static ISR.

In general, classes cannot implement ISRs for this reason. There are a few workarounds, one being to make it a static class function. However then it will affect the entire class, not just one instance.

  • I ended up just putting the functions inside the .ino file. it's not as neat as I'd like but it works. Thanks for teaching me something new. – Ximidar Aug 10 '15 at 1:57
  • You can make a static class function (one that does not belong to a particular instance) and keep the function inside your class then. – Nick Gammon Aug 10 '15 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.