Pages: [1]   Go Down
Author Topic: Need to call an uninstantiated method from object method  (Read 813 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 93
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
void encoderInterrupt() {}

The error I get is this:
Code:
/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:
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.
« Last Edit: August 09, 2011, 12:07:36 am by GreyGnome » Logged

Portland, OR
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 93
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
typedef void (*PCIntvoidFuncPtr)(void);
So I have tried to do this:
Code:
typedef void (*PCIntvoidFuncPtr)(void *);
So that I can do this in my Class:
Code:
PCintPort::attachInterrupt(pinA, interruptWrapper((void *) this), CHANGE);
but I get this error:
Code:
/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:
PCintPort::attachInterrupt(pinA, &interruptWrapper((void *) this), CHANGE);
I get this:
Code:
/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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: