ShadowNode

Use Node.js in your end devices(QQ: 796448809)

View the Project on GitHub yodaos-project/ShadowNode

Platform Support

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

GPIO

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

Class: GPIO

new GPIO()

Returns a new GPIO object which can access any GPIO pins.

DIRECTION

An enumeration which can be used to specify the direction of the pin.

MODE

An enumeration which can be used to specify the configuration of the pin.

gpio.open(configuration[, callback])

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;
  }
});

Class: GPIOPin

This class represents an opened and configured GPIO pin. It allows getting and setting the status of the pin.

gpiopin.write(value[, callback])

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;
  }
});

gpiopin.writeSync(value)

Writes out a boolean value to a GPIO pin synchronously (a number value is converted to boolean first).

Example

gpio10.writeSync(1);

gpiopin.read([callback])

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);
});

gpiopin.readSync()

Returns the boolean value of a GPIO pin.

Example

console.log('value:', gpio10.readSync());

gpiopin.close([callback])

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');
});

gpiopin.closeSync()

Closes a GPIO pin.

Example

gpio10.closeSync();

// prints: gpio pin is closed
console.log('gpio pin is closed');