Use Node.js in your end devices(QQ: 796448809)
The following shows GPIO module APIs available for each platform.
Linux (Ubuntu) |
Raspbian (Raspberry Pi) |
NuttX (STM32F4-Discovery) |
TizenRT (Artik053) |
|
---|---|---|---|---|
gpio.open | O | O | O | O |
gpiopin.write | O | O | O | O |
gpiopin.writeSync | O | O | O | O |
gpiopin.read | △ | △ | O | O |
gpiopin.readSync | O | O | O | O |
gpiopin.close | O | O | O | O |
gpiopin.closeSync | 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 NuttX, the pin number is defined in target board module. For more information, please check the following list: STM32F4-discovery
Returns a new GPIO object which can access any GPIO pins.
IN
Input pin.OUT
Output pin.An enumeration which can be used to specify the direction of the pin.
NONE
None.PULLUP
Pull-up (pin direction must be IN
).PULLDOWN
Pull-down (pin direction must be IN
).FLOAT
Float (pin direction must be OUT
).PUSHPULL
Push-pull (pin direction must be OUT
).OPENDRAIN
Open drain (pin direction must be OUT
).An enumeration which can be used to specify the configuration of the pin.
configuration
{Object}
pin
{number} Pin number. Mandatory field.direction
{GPIO.DIRECTION} Pin direction. Default: GPIO.DIRECTION.OUT
mode
{GPIO.MODE} Pin mode. Default: GPIO.MODE.NONE
callback
{Function}
error {Error |
null} |
Opens the specified GPIO pin and sets the pin configuration.
The mode argument is ignored on Linux.
The optional 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 gpio = new GPIO();
var gpio10 = gpio.open({
pin: 10,
direction: gpio.DIRECTION.OUT,
mode: gpio.MODE.NONE
}, function(err) {
if (err) {
throw err;
}
});
This class represents an opened and configured GPIO pin. It allows getting and setting the status of the pin.
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 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');
});
Closes a GPIO pin.
Example
gpio10.closeSync();
// prints: gpio pin is closed
console.log('gpio pin is closed');