Ruff 0.9.1 Documentation
Table of Contents
GPIO#
The gpio
module allows you to create your own device driver with GPIO output.Following line of code includes the I2C module: require('gpio')
;
GPIO driver developers may extend this module by adding more functions.
Create GPIO driver#
Using Ruff Framework, you just need to write some easy implementation.
Below is an example of a GPIO driver:
var SysGpio = Gpio.driver({
attach: function(options, gpio) {
...
},
detach: function() {
...
},
getDevice: function(key, inputs) {
...
},
exports: {
setDirection: function(direction) {
...
},
read: function() {
...
},
write: function(value) {
...
},
setEdge: function(edge) {
...
},
setActiveLow: function(isActiveLow) {
...
}
},
events: {
'interrupt' : 'notify interrupt coming'
}
});
export GPIO resource in your driver#
- You may declare driver
output
resources indriver.json
Example:
{
"outputs" : {
"gpio0": {"type": "gpio"},
"gpio1": {"type": "gpio"},
"gpio2": {"type": "gpio"},
"gpio4": {"type": "gpio"},
"gpio5": {"type": "gpio"},
"gpio6": {"type": "gpio"},
"gpio7": {"type": "gpio"}
}
}
gpio0
is a key index to get gpio resource in Ruff Framework, gpio
is a type keyword for outputs resource.
- Use
getDevice(key, inputs)
in your driver and register viaGpio.driver()
key
: the key index we mentioned above to get the resource declared in outputs
inputs
: other information specified by user
Example:
var outputsKey = ['gpio0', 'gpio1', 'gpio2', 'gpio3',
'gpio4', 'gpio5', 'gpio6', 'gpio7'];
module.exports = $.driver({
attach: function(inputs) {
this._i2c = inputs.getRequire('i2c');
this._data = 0;
this._offset = 0;
},
getDevice: function(key, inputs) {
getDevice: function(key, inputs) {
var index = outputsKey.indexOf(key);
inputs.setValue('pin', index);
var GpioClass = Gpio.driver({
attach: function(inputs, adapter, index) {
McpGpio.call(this, inputs, adapter, index);
},
exports: McpGpio.prototype
});
return new GpioClass(inputs, this, index);
},
...
});
Use GPIO interface#
Driver developers may declare different types of GPIO in driver.json
in oder to customize the default GPIO attribute provided by ruff driver framework, ruff will allocate GPIO resources and set them to correct attributes accodingly to your driver.
{
"inputs": {
"gpio": {
"type" : "gpio",
"args" : {
"direction": "in"
}
}
}
}
direction
: valid directions
in
: set direction to input
out
: set direction to output
out_low
: set direction to output and set output value to low before changing direction
out_high
: set direction to output and set output value to high before changing direction
activeLow
: true
or false
edge
: valid edge
none
: disable interrupt
falling
: trigger interrupt on falling
rising
: trigger interrupt on rising
both
: trigger interrupt on both rising
and falling
API#
.read()#
Return the current value of the pin.
Example:
try {
var value = gpio.read();
console.log(value);
} catch(error) {
console.log(error);
}
.write(value)#
Write value
for gpio. NOTE: Will fail if the direction of pin is not out
.
value
: Should be either a numeric0
or1
. Any value that isn't0
or1
will be coerced to boolean, and then converted to 0 (false) or 1 (true).
.close()#
Close a GPIO.
.setDirection(direction)#
Changes the direction from input
to output
or vice-versa.
direction
: valid directionsIN
: set direction to inputOUT
: set direction to outputOUT_LOW
: set direction to output and set output value to low before changing directionOUT_HIGH
: set direction to output and set output value to high before changing direction
Example:
var Gpio = require('gpio');
gpio.setDirection(Gpio.IN);
.setActiveLow(isActiveLow)#
invert the value attribute while reading or writing if isActiveLow
is true
isActiveLow
: settrue
to indicate value attribute inverts upon read or write
.getDirection()#
Return the direction of a GPIO.
.setEdge(edge)#
Set GPIO interrupt trigger level when a GPIO is configured as input
edge
:EDGE_NONE
: disable interruptEDGE_RISING
: trigger interrupt when risingEDGE_FALLING
: trigger interrupt when fallingEGGE_BOTH
: trigger interrupt bothrising
andfalling
.getEdge()#
Return the edge of a GPIO.
.getPin()#
Return the pin number of a GPIO.
.on('interrupt', callback)#
Register interrupt handler for GPIO.
callback
: Will be called when the interrupt is triggered
.removeListener('interrupt', callback)#
Unregister specific callback
for a GPIO interrupt
callback
: specify the callback that will be unregistered. if thecallback
is null, disable all the callbacks for a GPIOterrupt.