C guru I need you!
I'm trying to write a library for some basic motor control, and I'm using interrupts to process the pulses from the reed switches on the motors I'm using. The problem I'm having is that the attachInterrupt() function doesn't like to receive my class member function pointer. The compiler spits out the following error:
Motor.cpp:32: error: argument of type 'void (Motor::)()' does not match 'void (*)()'
this is in response to a line in my class constructor making the following call:
attachInterrupt(0, motor1Feedback,RISING);
the declaration for my interrupt handler (which I declared as a private class member function) looks like:
void Motor::motor1Feedback()
Class member functions that appear to take no arguments actually do take an invisible argument called this. The value of this is the instance of the object you're referring to. You can't attachInterrupt() to a class member function directly. You might be able to do it if the member function is static, but my approach is to have a global variable that is the instance, and the global service routine knows how to use the global variable.
MyClass theInstance = NULL;
// Somewhere, fill in an initialized copy of MyClass,
// and set theInstance to it.
ISR(TIMER1_0VF_vect)
{
if (theInstance)
theInstance->handleInterrupt();
}
If you do want to try to connect a static class member function to an interrupt vector, you should find the real definition of the ISR() macro, and try to adapt it into a CLASS_ISR() for the static class technique, like follower's page does. I think it might be a little different from the page shown, but it is on an avr development list so it's probably close.