+/*
+ * interruptHandler:
+ * This is a thread and gets started to wait for the interrupt we're
+ * hoping to catch. It will call the user-function when the interrupt
+ * fires.
+ *********************************************************************************
+ */
+
+static void *interruptHandler (void *arg)
+{
+ int myPin = *(int *)arg ;
+
+ (void)piHiPri (55) ; // Only effective if we run as root
+
+ for (;;)
+ if (waitForInterruptSys (myPin, -1) > 0)
+ isrFunctions [myPin] () ;
+
+ return NULL ;
+}
+
+
+/*
+ * wiringPiISR:
+ * Take the details and create an interrupt handler that will do a call-
+ * back to the user supplied function.
+ *********************************************************************************
+ */
+
+int wiringPiISR (int pin, int mode, void (*function)(void))
+{
+ pthread_t threadId ;
+ char fName [64] ;
+ char *modeS ;
+ char pinS [8] ;
+ pid_t pid ;
+
+ pin &= 63 ;
+
+ if (wiringPiMode == WPI_MODE_UNINITIALISED)
+ {
+ fprintf (stderr, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
+ exit (EXIT_FAILURE) ;
+ }
+ else if (wiringPiMode == WPI_MODE_PINS)
+ pin = pinToGpio [pin] ;
+
+// Now export the pin and set the right edge
+// We're going to use the gpio program to do this, so it assumes
+// a full installation of wiringPi. It's a bit 'clunky', but it
+// is a way that will work when we're running in "Sys" mode, as
+// a non-root user. (without sudo)
+
+ if (mode != INT_EDGE_SETUP)
+ {
+ /**/ if (mode == INT_EDGE_FALLING)
+ modeS = "falling" ;
+ else if (mode == INT_EDGE_RISING)
+ modeS = "rising" ;
+ else
+ modeS = "both" ;
+
+ sprintf (pinS, "%d", pin) ;
+
+ if ((pid = fork ()) < 0) // Fail
+ return pid ;
+
+ if (pid == 0) // Child, exec
+ {
+ execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
+ return -1 ; // Failure ...
+ }
+ else // Parent, wait
+ wait (NULL) ;
+ }
+
+// Now pre-open the /sys/class node - it may already be open if
+// we had set it up earlier, but this will do no harm.
+
+ sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
+ if ((sysFds [pin] = open (fName, O_RDWR)) < 0)
+ return -1 ;
+
+ isrFunctions [pin] = function ;
+
+ pthread_create (&threadId, NULL, interruptHandler, &pin) ;
+
+ delay (1) ;
+
+ return 0 ;
+}
+
+