Driver - Getting Started

Creating a driver

rap init driver

Enter driver information

  • Name
  • Version
  • Description

This will automatically generate package.json and driver.json.

Declaring device information

  • Declare device information in driver.json.

For example, suppose we need to create a driver with GPIO:

{
"inputs": {
"gpio": {
"type": "gpio",
"args": {
"direction": "in",
"edge": "both"
}
}
}
}

As you can see, we declared a gpio type driver. We also set up additional arguments relating to direction and edge. We defined edge as both, which means it will be triggered on both positive and negative edges.

Coding

We need to code a simple driver which reads GPIO values.

var driver = require('ruff-driver');

module.exports = driver({
attach: function(inputs) {
this._gpio = inputs.getRequired('gpio');
},

detach: function() {
},

exports: {
readValue: function() {
return this._gpio.read();
}
}
});

Notice the Ruff built-in methods used in the above example:

  • attach executes a function when other Ruff resources are ready.
  • detach executes a function before the application ends and usually does some clean-up work.
  • exports contains customized functions defined by developers. Here we defined a readValue function.

Once we finish coding, we should write test scripts to make sure the driver works. For more information, refer to Driver Programming Model

Testing driver

Ruff provides a test framework that allows you to test your drivers on your PC:

var runner = require('ruff-driver-runner').runner;
var when = require('ruff-mock').when;
var assert = require('assert');
var driverPath = ..your_driver_path_with_driver_json..

export['test should work well'] = function() {
runner.run(driverPath, function(device, context) {
var gpio = context.arg('gpio');
when(gpio).read().thenReturn(1);
assert.equal(1, device.readValue());
}
}

require('test').run(exports);

(test/driver-test.js)

Notice the test modules that are included:

  • test: a test framework that complies with CommonJS and Unit Testing standards.
  • ruff-mock: a mock framework by Ruff
  • ruff-driver-runner: a test automation runner powered by Ruff, which simulates all physical devices on your development machine.

Run the following command to test your driver:

ruff test/driver-test.js

For more information, please refer to Driver Testing

Writing a test application

The above example reflects a driver test on a development machine. You will still need to test your driver in a production environment.

Please refer to Getting Started and Application Developer’s Guide to build an application using your driver.

Instead of downloading a driver, you will need to add a device by including a local driver.

rap device add -i <device-id> -l /path/to/driver

Now you can deploy and test the application with our customized driver.

Publishing a driver

After development and testing, the driver is ready for use. Please publish the driver to our repository to benefit other application developers.

If you haven’t signed up for an account at our site, please sign up here.

Run the following command to add your user info of repository:

rap add-user

Run the following command to publish your driver:

rap publish

That’s it! Thank you for contributing your first driver to our repository!