radiodan/wiring-pi — pull request #1 by andrewn (2014-04-23 10:28:58+00:00)
Title: Add interrupt handling

The approach is as follows:

#### A. `lib/exports.js`: The javascript part of the add-on

1. Exporting a `wiringPiISR()` method that is the public API. Users of the library call to add an interrupt to a specific GPIO pin and the callback supplied is added to an EventEmitter instance for that pin.
2. Adding a new `addInterruptListener` binding to the native code that calls the wiring-pi native library's interrupt function.
3. When the hardware interrupt fires in the native code, a single callback `oninterrupt` will get called by the v8 execution environment, triggering the event listeners added in (1).

#### B. `src/bindings.cc`: The native binding

1. Implement `addInterruptListener` - checking supplied arguments are valid and then calling the wiring-pi native library's `wiringPiISR` function which sets-up up the hardware interrupt with a static callback function for the specific pin number e.g. `interruptCallback6`
2. `interruptCallbackForPin` - a generic function that is called when the interrupt fires. This happens on a different thread than v8 so libuv's uv_queue_work function is used to schedule the `interruptEmit` callback to run on the v8 thread.
3. `interruptEmit` runs on the v8 thread a fires the `oninterrupt` callback in javascript land in A3 above

This approach is based on the following blog post:
http://blog.trevnorris.com/2013/07/node-with-threads.html

The wiring-pi native library:
http://wiringpi.com/reference/priority-interrupts-and-threads/