jmaurin
Posts: 3
Joined: Wed Nov 11, 2015 4:14 pm

WiringPi and interrupts - multiple pins

Sat Jul 07, 2018 3:06 pm

Hi.

Is possible to 'know' which pin caused the interrupt inside interrupt function?
My idea is to create 'configurable' pins inside my program. User can select N pins to act as IR sensor. Each of these sensors is connected to an interrupt routine which receive data and work as needed. But now, I have an static function declaration for each pin and I would like to make only one function and 'detect' the exactly pin inside function.

This is something that I have now:

Code: Select all

// Sensor 1
    pinMode(sensors[0].pin, INPUT);
    pullUpDnControl(sensors[0].pin, PUD_UP);
    wiringPiISR(sensors[0].pin, INT_EDGE_BOTH, s0_handler);
's0_handler' is a static function....and I have to write one for each pin right now. I would liek to make only one function for all pins and detect which pin inside. Is it possible?

Tks!

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: WiringPi and interrupts - multiple pins

Sun Jul 08, 2018 11:33 am

I suspect it is possible to do this using lambdas but my C++ is too rusty and out of date to be sure.
Maybe someone else can comment.

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: WiringPi and interrupts - multiple pins

Mon Jul 09, 2018 5:07 pm

OK I have had a go but failed.

Code: Select all

#include <iostream>
#include <vector>
#include <wiringPi.h>

void respond(const int i)
{
    static int count = 0;
    std::cout << "pin " << i << "count " << count <<std::endl;
    count++;
}

int main(int argc, char **argv)
{
    wiringPiSetupGpio();
    
    std::vector<int> list =  {2,3};
    
    //static int pin = 2;
    for(const int& pin : list)
    {
        pinMode(pin,INPUT);
        pullUpDnControl(pin,PUD_UP);
 
        std::cout << digitalRead(pin) << std::endl;
    
        wiringPiISR(pin,INT_EDGE_FALLING,[](){respond(pin);});
    }

    while(true);
     
    return 0;
}
 
It fails to compile the call to wiringPiISR because pin is not captured.
If I comment out the for statement and reinstate the previous declaration for pin it works - but only for the single value of pin.

I have used lambdas in python in similar circumstances but can they be used like this for C++?

Are there any C++ gurus out there?

Roger Woollett

Return to “C/C++”