Building a device driver using MRAA and JavaScript: Introduction

First post in a series of posts covering how to build a device driver using NodeJS and MRAA

If you’re using NodeJS to build a program for an IoT device like the Intel Edison, then chances are you have, or will, run into a case where there is not a driver for the peripheral device that you’re trying to use. My team and I ran into this very scenario a couple of weeks ago on a project where we were trying to use the SparkFun OLED Edison Block as the main display for a prototyped wearable technology. There is a C++ driver for it, but it looked like it would be a challenge to get it running with the JavaScript interface we were already writing, so as a learning experience, I decided to rewrite the driver in JavaScript, leveraging the MRAA library for JavaScript.

Note: Intel published a driver for this block via UPM a week after we decided to build our own.

In this post, I will go through the MRAA interface for JavaScript and share the best practices I’ve learned from my experience. Through this blog post series, I will take you through each of the device interfaces and give you samples of how to use them. At the end, I will show you how to package your driver into a NPM package for other developers to consume.

I will primarily be referencing the Intel Edison board during this post, but MRAA supports other platforms like Raspberry Pi and Beaglebone Black. A full list of boards is here.

What is MRAA?

MRAA is a low-level device interface library for Linux-based microcontroller platforms. It is based in C/C++ and also has language projections for Python and JavaScript. The library abstracts device-level interfaces like analog pins, GPIO pins and other communication bus technologies like PWM, I2C and SPI.

Before you get started, it’s a good idea to make sure you have the latest MRAA build on your device. To do that, follow these instructions.

IDE

The easiest way that I have found to work with Intel-based devices is through using their IDE – Intel XDK IoT Edition. It has interfaces for connecting to and deploying code to your boards. It also has a built-in debugger, which comes in handy and the code editor is based on Brackets, an open source code editor.

Introduction to the MRAA JavaScript interface

The JavaScript interface is architected to work with a NodeJS-based application. The full API reference can be found here – http://iotdk.intel.com/docs/master/mraa/node/modules/mraa.html. One thing that I have noticed using the JS library is that not all of the projected methods are documented under the node docs, so you may need to reference the actual library source in some cases.

Analog

The analog pins interface is pretty straightforward. The readings are returned as 10-bit ADC values, which means that it has the ability to detect 1,024(210) discrete analog levels (source). This is common to the Arduino pinout and boards that support it.

Here’s a sample of reading a value off analog pin zero taken from the Intel XDK template for reading analog.


var mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console

var analogPin0 = new mraa.Aio(0); //setup access analog input Analog pin #0 (A0)
var analogValue = analogPin0.read(); //read the value of the analog pin
console.log(analogValue); //write the value of the analog pin to the console

You can see that a new reference is created using mraa.Aio(0), specifying that it should read from analog pin zero, or A0. It’s as simple as that to read the value, but you will likely need to convert the value into something useful like a temperature reading, or motion reading. I won’t get into any of the specifics here. There are plenty of samples out on the web and each specific sensor may require its own conversion formula.

The Aio class also contains useful methods that grant you finer grain of control over how it’s read. For instance, you can change the bit resolution to 12 bits(max capable in MRAA) on the pin by calling setBit(12).

GPIO

General purpose input/output is also a very common basic interface in the world of microcontrollers. With GPIO, you can do simple things like power and LED or Piezo buzzer to reading input from a button.

Let’s take a look at how GPIO can be used to power the onboard LED of the Intel Edison. This is sourced from the Intel XDK template as well.

 
var mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console

//var myOnboardLed = new mraa.Gpio(3, false, true); //LED hooked up to digital pin (or built in pin on Galileo Gen1)
var myOnboardLed = new mraa.Gpio(13); //LED hooked up to digital pin 13 (or built in pin on Intel Galileo Gen2 as well as Intel Edison)
myOnboardLed.dir(mraa.DIR_OUT); //set the gpio direction to output
var ledState = true; //Boolean to hold the state of Led

periodicActivity(); //call the periodicActivity function

function periodicActivity()
{
 myOnboardLed.write(ledState?1:0); //if ledState is true then write a '1' (high) otherwise write a '0' (low)
 ledState = !ledState; //invert the ledState
 setTimeout(periodicActivity,1000); //call the indicated function after 1 second (1000 milliseconds)
}

In the above code sample, you can see that we created a new GPIO object that interfaces with GPIO13 on the board. In this case, that happens to be the onboard LED for the Intel Edison.

After creating the object, we need to tell what direction we expect to use the pin for. In this sample, we will set the direction to OUT, so that we can write to it. This effectively allows a HIGH or LOW voltage to be set on the pin – which can be used to turn something on/off like the LED. If you are expecting to read a value off the pin, then you would set the direction mode to IN.

Conclusion

If you’re new to the world of using JavaScript to write IoT software, then I suggest firing up some of the sample templates included in the Intel XDK IoT Edition. It does a fairly decent job of covering most of the scenarios supported by the MRAA library.

In the next post, I will cover using MRAA and JavaScript to build an I2C interface for a sensor. Stay tuned!

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s