WiringPiISR Interrupts

Hi everyone!
I’m trying to setup WiringPi’s interrupts within my openFrameworks app using the wiringPiISR function, documented here:
https://projects.drogon.net/raspberry-pi/wiringpi/functions/

When I compile, I get the following error:

“error: cannot convert ‘void (testApp::)()’ to ‘void ()()’ for argument ‘3’ to ‘int wiringPiISR(int, int, void (*)())’”

Any ideas how I resolve this?

Here’s my code:

void testApp::setup(){

wiringPiSetup();

pinMode(0, INPUT);
pinMode(1, INPUT);

pullUpDnControl(0, PUD_UP);
pullUpDnControl(1, PUD_UP);
	
wiringPiISR(0,INT_EDGE_BOTH, &testApp::updateEncoders);
wiringPiISR(1,INT_EDGE_BOTH, &testApp::updateEncoders);
}

void testApp::updateEncoders(){
pinAValue = digitalRead(0);
pinBValue = digitalRead(1);
}

Any help most appreciated!

My end goal is to get a rotary encoder working with my Raspberry Pi. In my previous tests where everything was in the update loop, several changes in the knobs values were being dropped. I thought this may be due to the rate the pins were being queried, so I figured and interrupt based approach may be better. (I thought wiringPi’s baked in functions might be faster than openFrameworks events system.)

Thanks a bunch!

I resolved this issue by making updateEncoders a global function. I thought this was considered bad form, so if anyone has another suggestion, I’m all ears!

This is a tricky situation. C-style callbacks (function pointers) are easy with global functions or static member functions.

So, to quickly summarize, if you want to avoid a global, you can probably add a static method that looks like:

// testApp.h
static void updateEncoders();
// testApp.cpp
void testApp::updateEncoders()
{
  // do your work here
}

This should work fine for you, but if you need access to member variables of your testApp instance, you won’t have access to those in a static function. In that case you’ll have to do something more fancy. If you search google or stackoverflow for something like c++ callback member function … there are lots of ways, some easier than others to implement in oF.

2 Likes

Brilliant! Thanks for pointing me in the right direction!

i am right at this point now.
my interrupt routine needs to have access to other member variables. but i can’t work out how to use the static function with variables.
i always get raspberry PI2 compiler errors like this:

   error: ‘this’ is unavailable for static member functions
 this->counterValue += (this->A_set != this->B_set) ? +1 : -1;

i thought using this->counterValue would help but it did not.
?
thanks.

i managed to make this to work like so. But i am not sure if this is the most elegant method.