3 Replies Latest reply on Jun 13, 2015 1:37 PM by Koen.Bruyninckx

    Are the mraa Gpio interrupt methods safe to use in Node.js?


      The mraa Gpio class allows you to install a handler that's invoked when an interrupt is triggered by a pin.


      Normally you have to be very careful in what you do in an interrupt handler in order to ensure you don't disturb the interrupted environment - accidentally doing so causes very difficult to diagnose behavior when the handler returns and normal operation resumes.


      This is true of the handlers you pass to the mraa C++ isr method and the C mraa_gpio_isr function.


      But is it also true of the Gpio isr method of the Node.js mraa wrappers?


      The high level Javascript world of Node.js isn't designed for the low level interrupt world.


      So actually the mraa wrappers ensure that in Node.js you don't have to worry about your callback running in an interrupt environment, it is actually queued to be run from the normal event loop.


      But as there's no documentation for the Node.js wrappers I had to go through the underlying logic to reassure myself of this.


      I've written up what I found here - node-js-mraa-isr.md


      Hope it's of some interest to people working with Node.js and mraa.

        • 1. Re: Are the mraa Gpio interrupt methods safe to use in Node.js?

          The interrupt handler should only do ONE very fast 'thing' and exit as quickly as possible (this is true regardless of interface C, C++, Python, Node.js, etc).

          For instance, when the interrupt fires (rising edge on pin 14 say) the handler might take an ADC reading from the ADC block (not translate it, nor do anything with it, nor even convert it,  just read it into a struct or buffer somewhere) and then set a flag that says the interrupt fired.

          The interrupt handler should NOT touch anything related to the main-line code running...  fast rule.  Somewhere in the main-line code there must be a routine that checks for interrupt flags... and then handles the value read in by the handler (translation event handling whatever) and reset the interrupt flag.




          • 2. Re: Are the mraa Gpio interrupt methods safe to use in Node.js?

            What you say is true of C and C++. It is not true for Node.js.


            In Node.js you register an interrupt callback like this:


                var buttonPin = new mraa.Gpio(45);




                buttonPin.isr(mraa.EDGE_BOTH, isrCallback);


                function isrCallback() {

                    // Do something when pin state changes.



            This looks very like what you would do in C++. However quite different things happen when the interrupt occurs.


            Like you say a real interrupt callback should do as little as possible - under the covers the Node.js wrapper logic actually registers its own callback.


            And the only thing this callback does is schedule your callback to be invoked from the standard Node.js event loop.


            So your callback can do whatever it wants - it does not have to worry about running in an interrupt environment.


            It may seem odd that the Node.js wrappers provide a different behavior to those of the C++ ones.

            However Javascript doesn't provide the clear machine behavior of C or C++ that allows one to anticipate what actions would and would not trample on machine state.


            So this behavior is reasonable - in fact I assumed the Node.js wrappers would behave like this.


            But as there's no documentation on this I looked through the wrapper code to confirm this to myself.


            That was the point of my post - you can see the results of my investigation confirming things here - node-js-mraa-isr.md

            • 3. Re: Are the mraa Gpio interrupt methods safe to use in Node.js?



              I always find the example code for the interrupt to be something of the form:

              buttonPin.isr(mraa.EDGE_BOTH, isrCallback);

              But if I run that, I get an error on the fact that gpio.isr requires 3 parameters, the third being the arguments.  Unfortunately, how do I have to pass them in, nothing seems to be accepted?


              sensorPin.isr(mraa.EDGE_FALLING, function () {


              Error: Illegal number of arguments for _wrap_Gpio_isr.

              at Object. (/home/root/start.js:19:11)

              at Module._compile (module.js:456:26)

              at Object.Module._extensions..js (module.js:474:10)

              at Module.load (module.js:356:32)

              at Function.Module._load (module.js:312:12)

              at Function.Module.runMain (module.js:497:10)

              at startup (node.js:119:16)

              at node.js:906:3