Page 1 of 1

WiringPi and interrupts - multiple pins

Posted: Sat Jul 07, 2018 3:06 pm
by jmaurin

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?


Re: WiringPi and interrupts - multiple pins

Posted: Sun Jul 08, 2018 11:33 am
by RogerW
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.

Re: WiringPi and interrupts - multiple pins

Posted: Mon Jul 09, 2018 5:07 pm
by RogerW
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;

int main(int argc, char **argv)
    std::vector<int> list =  {2,3};
    //static int pin = 2;
    for(const int& pin : list)
        std::cout << digitalRead(pin) << std::endl;

    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