Go Down

Topic: Need to call an uninstantiated method from object method (Read 920 times) previous topic - next topic

GreyGnome

Aug 09, 2011, 07:04 am Last Edit: Aug 09, 2011, 07:07 am by GreyGnome Reason: 1
Hi,
I am using the PinChangeInt library to set up a number of interrupts for a (relatively) arbitrary number of rotary encoders on my Arduino Uno.

I am creating my own Encoder library, which relies on the PinChangeInt library.

I have a call in my Encoder library that looks like this:
Code: [Select]
void AdaEncoder::attachInterrupt(int pinA, int pinB) {
   PCintPort::attachInterrupt(pinA, encoderInterrupt, CHANGE);
}


Note the AdaEncoder::attachInterrupt() method, above, is not static.  But the PCintPort::attachInterrupt() method is.  I have a stub encoderInterrupt() method:

Code: [Select]
void encoderInterrupt() {}


The error I get is this:
Code: [Select]
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp:65: error: no matching function for call to 'PCintPort::attachInterrupt(int&, <unresolved overloaded function type>, int)'
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/../PinChangeInt/PinChangeInt.h:87: note: candidates are: static void PCintPort::attachInterrupt(uint8_t, void (*)(), int)


I think what I have to do is make the
Code: [Select]
encoderInterrupt() method static, but I'd like to avoid that.  Is there any way I can do so?  Or do I need to make encoderInterrupt() static, then solve my problem by utilizing static pointers or arrays?

Thanks.

kerthyn

Quote
I think what I have to do is make the encoderInterrupt() method static

Yes. C++ nonstatic member functions have a different function signature - they have an 'invisible' extra parameter, the 'this' pointer.

crimony

Page 5 of this has an example of the sort of thing you need to do (make a wrapper). You will need a static entry point, but that function can bounce to the handler specified for the individual object.

GreyGnome

Thanks, that helps.  But now I need to pass "this" as an argument to my function.

The function is specified in the PinChangeInt.h like this:
Code: [Select]
typedef void (*PCIntvoidFuncPtr)(void);
So I have tried to do this:
Code: [Select]
typedef void (*PCIntvoidFuncPtr)(void *);
So that I can do this in my Class:
Code: [Select]
PCintPort::attachInterrupt(pinA, interruptWrapper((void *) this), CHANGE);
but I get this error:
Code: [Select]
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp: In member function 'void AdaEncoder::attachInterrupt(int, int)':
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp:68: error: invalid use of void expression


if I do:
Code: [Select]
PCintPort::attachInterrupt(pinA, &interruptWrapper((void *) this), CHANGE);
I get this:
Code: [Select]
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp: In member function 'void AdaEncoder::attachInterrupt(int, int)':
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp:68: error: lvalue required as unary '&' operand


I can't figure out how to pass this through my static wrapper so as to open up the world of possibilities to me!  I need it so that I know which object the interrupt applies to.

Thanks again.

PaulS

Quote
I can't figure out how to pass this through my static wrapper so as to open up the world of possibilities to me!  I need it so that I know which object the interrupt applies to.

You can't. Something external to the Arduino generated an interrupt. How is that external device supposed to know which object should react?

What you are trying to do makes little sense, as I understand what you are trying to do.

Perhaps if you explained just what you want to do, without code. Just the basic idea.

Go Up