Ruff 1.1.0 Documentation
Table of Contents
Ruff Driver#
The ruff-driver
module provides you a facility for Ruff driver developers to build their device driver. You may include this module with require('ruff-driver-runner')
;
Declare driver I/O#
Each Ruff driver has a description file: driver.json
, which defines inputs and outputs of the driver.
The inputs here are the interfaces required by the driver, for instance, a button will need a GPIO interface.
{
"inputs": {
"btnGpio": {
"type": "gpio",
"args": {
"direction": "in"
}
}
}
}
btnGpio
is named here for later use in driver code. The type
attribute is defined as: gpio
.
Write driver code#
Here is as example of how to create your driver with this module:
var driver = require('ruff-driver');
var Gpio = require('gpio');
module.exports = driver({
attach: function(inputs) {
var _this = this;
this.gpio = inputs.getRequired('btnGpio');
this.gpio.setDirection(Gpio.IN);
this.gpio.setEdge(Gpio.EDGE_BOTH);
this.gpio.on('interrupt', function() {
var data = _this.gpio.read();
if (data === 0) {
_this.emit('pressed');
} else {
_this.emit('released');
}
});
},
events: {
pressed : 'message when button is pressed',
released : 'message when button is released'
},
});
attach#
attach
will be called upon initialization of application. It takes a parameter defined in driver.json
for configuration. Following is a prototype of attach
:
attach: function(options) {
}
A required parameter can be retrieved by calling getRequired
.
inputs.getRequired('btnGpio');
getOptional
can be used to get optional parameters.
inputs.getOptional('optionalArg', defaultValue);
If optionalArg
is missing, defaultValue
will be returned.
Both getRequired
and getOptional
will take the names defined in driver.json
as their parameters.
detach#
detach
will be called upon appilcation stop. Any resource the driver applied could be released here. Simply ignore detach
if you need it. A prototype of detach
is as follow:
detach: function() {
}
events#
The driver can also declare its supported events. Events can be emitted from interruption, e.g. press a button.
events
will have two parts: name and description.
events: {
pressed : 'message when button is pressed',
released : 'message when button is released'
}
states#
The application developer sometimes needs to query device's state, e.g. you can query a temperature sensor for its temperature. The state in Ruff is supposed to be a read-only function.
state
can be declared as follows:
states: {
temperature : function() {
return your_temperature_implementation
}
}
The application developer can use state as a property.
$('#temp').temperature;
exports#
Customized functions can be used as controllers for the application developers to control the hardware behavior.
All the customized functions should be declared in exports
.
var driver = require('ruff-driver');
module.exports = driver({
attach: function(inputs) {
...
},
events: {
...
},
exports: {
customizedFunction: function() {
...
}
}
});
The application developers may use those functions directly in application.
$('#myDevice').customizedFunction();
getDevice#
getDevice
is supposed to be defined when the device is an output device.
var driver = require('ruff-driver');
module.exports = driver({
attach: function(inputs) {
...
},
getDevice: function(name, options) {
}
});
getDevice
has two arguments:
name
, output interface nameoptions
, the configuration parameter
extend#
extend
is a framework-provide function which is used to extend driver instance behaviour.
Your device object can extend a trait.
$('#led').extend(ToggleTrait);
or define a new function
$('#led').extend({
toggle: function() {
}
});
Now, your device object can use the new function:
$('#led').toggle();