An open API service indexing awesome lists of open source software.

https://github.com/akira-cn/rpio2

Control Raspberry Pi GPIO pins with node.js. Fast and easy to use.
https://github.com/akira-cn/rpio2

Last synced: about 1 month ago
JSON representation

Control Raspberry Pi GPIO pins with node.js. Fast and easy to use.

Awesome Lists containing this project

README

        

# rpio2

[![npm status](https://img.shields.io/npm/v/rpio2.svg)](https://www.npmjs.org/package/rpio2)
[![build status](https://api.travis-ci.org/akira-cn/rpio2.svg?branch=master)](https://travis-ci.org/akira-cn/rpio2)
[![dependency status](https://david-dm.org/akira-cn/rpio2.svg)](https://david-dm.org/akira-cn/rpio2)
[![coverage status](https://img.shields.io/coveralls/akira-cn/rpio2.svg)](https://coveralls.io/github/akira-cn/rpio2)

Export elegant OOP APIs to control Raspberry Pi GPIO pins with Node.js. Based on [node-rpio](https://github.com/jperkin/node-rpio) which is a high performace library.

## Installation

```bash
npm install rpio2 --production
```

By default the module will use `/dev/gpiomem` when using simple GPIO access.
To access this device, your user will need to be a member of the `gpio` group,
and you may need to configure udev with the following rule (as root):

```console
$ cat >/etc/udev/rules.d/20-gpiomem.rules <

+ 3.3v12+ 5v

I2C SDA / GPIO 23
4+ 5v

I2C SCL / GPIO 356Ground

Clock / GPIO 478TX / GPIO 14

--910RX / GPIO 15

GPIO 171112GPIO 18

GPIO 271314--

GPIO 221516GPIO 23

+ 3.3V1718GPIO 24

SPI MOSI / GPIO 101920--

SPI MISO / GPIO 92122GPIO 25

SPI SCLK / GPIO 112324SPI CE0 / GPIO 8

--2526SPI CE1 / GPIO 7

Model A+ and Model B+ additional pins

ID_SD2728ID_SC

GPIO 52930--

GPIO 63132GPIO 12

GPIO 133334--

GPIO 193536GPIO 16

GPIO 263738GPIO 20

--3940GPIO 21

If you want to map to gpio number directly, see [Gpio.init(options)](#gpioinitoptions)

---

### Methods

##### open(mode[, state])

Open a pin for input or output. Valid modes are:

- Gpio.INPUT: pin is input (read-only).
- Gpio.OUTPUT: pin is output (read-write).

For output pins, the second parameter defines the initial value of the pin, rather than having to issue a separate .write() call. This can be critical for devices which must have a stable value, rather than relying on the initial floating value when a pin is enabled for output but hasn't yet been configured with a value.

For input pins, the second parameter can be used to configure the internal pullup or pulldown resistors state, see [mode:0|1](#mode01) [state:0|1](#state01) for more details.

##### close()

Unexports a GPIO from userspace and release all resources.

##### read()

Set a velue to a GPIO.

##### write(value)

Set a velue to a GPIO.

##### toggle()

Change GPIO value.

##### sleep(ms[,async])

Sleep for a few milliseconds. If async is `true`, it will return a promise.

##### createReadStream(pin[, options])

Experimental. See the following example:

**log input value to stdout**

```js
const Gpio = require('../lib/index.js').Gpio;

var gs = Gpio.createReadStream(32, {throttle: 100});

gs.pipe(process.stdout);

process.on("SIGINT", function(){
gs.end();
process.exit(0);
});
```

##### createWriteStream(pin[, options])

Experimental. See the following example:

**read from stdin and set value**

```js
const Gpio = require('../lib/index.js').Gpio;

var gs = Gpio.createWriteStream(40, {
mode: Gpio.OUTPUT,
state: Gpio.HIGH
});

console.log('Please input value of P40.');

process.stdin.pipe(gs);

process.on("SIGINT", function(){
gs.end();
process.exit(0);
});
```

**trace input/output pins**

```js
const Gpio = require('../lib/index.js').Gpio;

var input = Gpio.createReadStream(32, {throttle: 100});

var output = Gpio.createWriteStream(40);

input.pipe(output);

process.on("SIGINT", function(){
input.end();
output.end();
process.exit(0);
});
```

##### group(pins[, activeLow])

Create GpioGroup instance. See [GpioGroup(pins[, activeLow]) - Constructor](#gpiogrouppins-activelow).

---

### Properties

##### value:0|1

Get or set a velue to a GPIO **synchronously**.

##### mode:0|1

The pin direction, pass either Gpio.INPUT for read mode or Gpio.OUTPUT for write mode.

##### state:0|1

The pin state. For input pins, state can be either Gpio.POLL\_HIGHT or Gpio.POLL\_LOW. For output pins, it is equivalent to `value` that state can be either Gpio.HIGH or Gpio.LOW.

##### activeLow: boolean

Specifies whether the values read from or written to the GPIO should be inverted. The interrupt generating edge for the GPIO also follow this this setting. The valid values for activeLow are true and false. Setting activeLow to true inverts. The default value is false.

---

### Statics

##### Gpio.init([options])

Initialise the bcm2835 library. This will be called automatically by `.open()`
using the default option values if not called explicitly. The default values
are:

```js
var options = {
gpiomem: true, /* Use /dev/gpiomem */
mapping: 'physical', /* Use the P1-P40 numbering scheme */
}
```

##### `gpiomem`

There are two device nodes for GPIO access. The default is `/dev/gpiomem`
which, when configured with `gpio` group access, allows users in that group to
read/write directly to that device. This removes the need to run as root, but
is limited to GPIO functions.

For non-GPIO functions (i²c, PWM, SPI) the `/dev/mem` device is required for
full access to the Broadcom peripheral address range and the program needs to
be executed as the root user (e.g. via sudo). If you do not explicitly call
`.init()` when using those functions, the library will do it for you with
`gpiomem: false`.

You may also need to use `gpiomem: false` if you are running on an older Linux
kernel which does not support the `gpiomem` module.

rpio will throw an exception if you try to use one of the non-GPIO functions
after already opening with `/dev/gpiomem`, as well as checking to see if you
have the necessary permissions.

Valid options:

* `true`: use `/dev/gpiomem` for non-root but GPIO-only access
* `false`: use `/dev/mem` for full access but requires root

##### `mapping`

There are two naming schemes when referring to GPIO pins:

* By their physical header location: Pins 1 to 26 (A/B) or Pins 1 to 40 (A+/B+)
* Using the Broadcom hardware map: GPIO 0-25 (B rev1), GPIO 2-27 (A/B rev2, A+/B+)

Confusingly however, the Broadcom GPIO map changes between revisions, so for
example P3 maps to GPIO0 on Model B Revision 1 models, but maps to GPIO2 on all
later models.

This means the only sane default mapping is the physical layout, so that the
same code will work on all models regardless of the underlying GPIO mapping.

If you prefer to use the Broadcom GPIO scheme for whatever reason (e.g. to use
the P5 header pins on the Raspberry Pi 1 revision 2.0 model which aren't
currently mapped to the physical layout), you can set `mapping` to `gpio` to
switch to the GPIOxx naming.

Valid options:

* `gpio`: use the Broadcom GPIOxx naming
* `physical`: use the physical P01-P40 header layout

##### Gpio.sleep(ms[,async])

Sleep for a few milliseconds. If async is `true`, it will return a promise.

---

### Events

If a pin is in direction of Gpio.DIR_IN. Three type of events can be fired when needed.

##### event:rising

When register listener to `rising` event [the rising interrupt edges](https://www.raspberrypi.org/documentation/hardware/raspberrypi/gpio/README.md) should be configured implictly and the GPIO will trigger the `rising` event.

```js
gpio.on('rising', function(){
console.log('A rising signal detected!');
});
```

##### event:falling

When register listener to `falling` event [the falling interrupt edges](https://www.raspberrypi.org/documentation/hardware/raspberrypi/gpio/README.md) should be configured implictly and the GPIO will trigger the `falling` event.

##### event:change

When register listener to `change` event [the both(rising and falling) interrupt edges](https://www.raspberrypi.org/documentation/hardware/raspberrypi/gpio/README.md) should be configured implictly and the GPIO will trigger the `change` event(on both rising and falling edges).

##### Note:

Registering events to rising and falling will implictly change the interrupt edges as well as unregistering events:

```js
let gpio = new Gpio(40);
gpio.open(Gpio.INPUT);
assertEqual(gpio.edge, Gpio.POLL_NONE);
gpio.on('rising', function(){...});
assertEqual(gpio.edge, Gpio.POLL_HIGH);
gpio.on('falling', function(){...});
assertEqual(gpio.edge, Gpio.POLL_BOTH);
gpio.removeListener('rising');
assertEqual(gpio.edge, Gpio.POLL_LOW);
gpio.removeListener('falling');
assertEqual(gpio.edge, Gpio.POLL_NONE);
```

---
##### GpioGroup(pins, activeLow)

Experimental. See the following example:

```js
var GpioGroup = require('./lib/index.js').GpioGroup;
var pins = [32, 40, 33]; //RGB
var group = new GpioGroup(pins, true);
group.open(GpioGroup.OUTPUT);

var color = {
yellow: 0b110,
red: 0b100,
green: 0b010
};

function turn(color){
return new Promise(function(resolve, reject) {
group.value = color;
resolve();
})
}
function wait(time){
return new Promise(function(resolve, reject) {
setTimeout(resolve,time);
})
}

void function (){
turn(color.green)
.then(wait.bind(null, 5000))
.then(turn.bind(null, color.yellow))
.then(wait.bind(null, 2000))
.then(turn.bind(null, color.red))
.then(wait.bind(null, 5000))
.then(arguments.callee)
}();

process.on("SIGINT", function(){
group.close();

console.log('shutdown!');
process.exit(0);
});
```

## LICENSE

GPL