using interrupts from a Library (class)?

Hi~ So I am still tinkering with my XY plotter type project. Since I control 2 DC motors and 2 encoders, I thought I would try making a library (class) instead of having lots of duplicate code in my sketch. I got pretty far with it but it is choking on the 'attachInterrupt' call:

RP_Motor.cpp: In member function 'void RP_Motor::initMotor(int, int, int, int, int, int, int, int)':
RP_Motor.cpp:61: error: argument of type 'void (RP_Motor::)()' does not match 'void (*)()'

This is line 61: attachInterrupt(interruptPin, doEncoder, CHANGE);  

There is a function named 'doEncoder' in the class. If I comment out that line, the library compiles and I can control the motors but I am not getting any encoder data (since there are no interrupts).

I tried copying parts of the Wiring Encoder lib: the extern references mainly, but the compiler complainer about duplicate declarations. I do #include <avr/interrupt.h> but that doesn't work.

I suppose I can do something stupid like have the ISRs in my sketch and have them call the class instances but...

tx for any tips! Library/classes are pretty easy and fun (until I trip over something like this :wink:

--Roy

The function that you pass to attachInterrupt() can't be a class method; it has to be a stand-alone function. You could make a stand-alone function that calls the appropriate method on an instance of the class (stored in a global variable). Or someone else may have an idea for a better approach.

Thanks; I was looking at the Encoder lib for the Wiring board and saw that it uses 'attachInterrupt' within the lib so I figured it would work. (here's a link to the Encoder lib source: http://tinyurl.com/6belea).

I did end up using a function in the main sketch for the interrupts and call a function on the class instance from there. Wondering if there is a speed hit in the extra hop: interrupt > doEncoder > obj.serviceInterrupt()

--Roy

You can attachInterrupt() with a static method. You declare the method static in the header file.