The following shows GPIO module APIs available for each platform.
| Linux (Ubuntu) |
Tizen (Raspberry Pi) |
Raspbian (Raspberry Pi) |
NuttX (STM32F4-Discovery) |
TizenRT (Artik053) |
|
|---|---|---|---|---|---|
| gpio.open | X | O | O | O | O |
| gpio.openSync | X | O | O | O | O |
| gpiopin.setDirectionSync | X | O | O | O | O |
| gpiopin.write | X | O | O | O | O |
| gpiopin.writeSync | X | O | O | O | O |
| gpiopin.read | X | O | O | O | O |
| gpiopin.readSync | X | O | O | O | O |
| gpiopin.close | X | O | O | O | O |
| gpiopin.closeSync | X | O | O | O | O |
The GPIO module provides access the General Purpose Input Output pins of the hardware.
On Linux each pin has a logical number starting from 1.
The logical number might be different from the physical
pin number of the board. The mapping is available
in the documentation of a given board.
- On Tizen, the pin number is defined in this documentation.
- On NuttX, the pin number is defined in the documentation of the target board. For more information, please check the following list: STM32F4-discovery
INInput pin.OUTOutput pin.
An enumeration which can be used to specify the direction of the pin.
NONENone.PULLUPPull-up (pin direction must beIN).PULLDOWNPull-down (pin direction must beIN).FLOATFloat (pin direction must beOUT).PUSHPULLPush-pull (pin direction must beOUT).OPENDRAINOpen drain (pin direction must beOUT).
An enumeration which can be used to specify the mode of the pin. These options are only supported on NuttX.
NONENone.RISINGRising.FALLINGFalling.BOTHBoth.
An enumeration which can be used to specify the edge of the pin.
configuration{Object} Configuration for open GPIOPin.pin{number} Pin number. Mandatory field.direction{gpio.DIRECTION} Pin direction. Default:gpio.DIRECTION.OUTmode{gpio.MODE} Pin mode. Default:gpio.MODE.NONEedge{gpio.EDGE} Pin edge. Default:gpio.EDGE.NONE
callback{Function}error{Error|null}gpioPin{Object} An instance of GPIOPin.
- Returns: {Object} An instance of GPIOPin.
Get GPIOPin object with configuration asynchronously.
The callback function will be called after
opening is completed. The error argument is an
Error object on failure or null otherwise.
Example
var gpio = require('gpio');
var gpio10 = gpio.open({
pin: 10,
direction: gpio.DIRECTION.OUT,
mode: gpio.MODE.PUSHPULL,
edge: gpio.EDGE.RISING
}, function(err, pin) {
if (err) {
throw err;
}
});configuration{Object} Configuration for open GPIOPin.pin{number} Pin number. Mandatory field.direction{gpio.DIRECTION} Pin direction. Default:gpio.DIRECTION.OUTmode{gpio.MODE} Pin mode. Default:gpio.MODE.NONEedge{gpio.EDGE} Pin edge. Default:gpio.EDGE.NONE
- Returns: {Object} An instance of GPIOPin.
Get GPIOPin object with configuration synchronously.
Example
var gpio = require('gpio');
var gpio10 = gpio.openSync({
pin: 10,
direction: gpio.DIRECTION.IN,
mode: gpio.MODE.PULLUP
});This class represents an opened and configured GPIO pin. It allows getting and setting the status of the pin.
direction{gpio.DIRECTION} Pin direction.
Set the direction of a GPIO pin.
Example
gpio10.setDirectionSync(gpio.DIRECTION.OUT);
gpio10.writeSync(1);
gpio10.setDirectionSync(gpio.DIRECTION.IN);
var value = gpio10.readSync();value{number|boolean}callback{Function}error{Error|null}
Asynchronously writes out a boolean value to a GPIO pin
(a number value is converted to boolean first).
The optional callback function will be called
after the write is completed. The error argument
is an Error object on failure or null otherwise.
Example
gpio10.write(1, function(err) {
if (err) {
throw err;
}
});value{number|boolean}
Writes out a boolean value to a GPIO pin synchronously
(a number value is converted to boolean first).
Example
gpio10.writeSync(1);callback{Function}error{Error|null}value{boolean}
Asynchronously reads a boolean value from a GPIO pin.
The optional callback function will be called
after the read is completed. The error argument
is an Error object on failure or null otherwise.
The value argument contains the boolean value
of the pin.
Example
gpio10.read(function(err, value) {
if (err) {
throw err;
}
console.log('value:', value);
});- Returns: {boolean}
Returns the boolean value of a GPIO pin.
Example
console.log('value:', gpio10.readSync());callback{Function}error{Error|null}
Asynchronously closes a GPIO pin.
The optional callback function will be called
after the close is completed. The error argument
is an Error object on failure or null otherwise.
Example
gpio10.close(function(err) {
if (err) {
throw err;
}
// prints: gpio pin is closed
console.log('gpio pin is closed');
});Synchronously closes a GPIO pin.
Example
gpio10.closeSync();
// prints: gpio pin is closed
console.log('gpio pin is closed');