项目作者: jeandesravines

项目描述 :
:pager: Raspberry-Pi PWM GPIO adapter with ES6 Promises.
高级语言: JavaScript
项目地址: git://github.com/jeandesravines/gpio.git
创建时间: 2016-07-15T22:21:43Z
项目社区:https://github.com/jeandesravines/gpio

开源协议:MIT License

下载


Gpio (Beta)

Build Status
codecov

Control Raspberry Pi GPIO pins with Node.js and ES6 Promises.

Table of contents

Setup

This module can then be installed with npm:

  1. # npm install @jdes/gpio # unavailable during beta
  2. npm install git://github.com/jeandesravines/gpio

Usage

!! IMPORTANT !! You must run your application as root to use the Raspberry’s GPIO.

Import module:

  1. /**
  2. * @class {Gpio}
  3. */
  4. const Gpio = require('@jdes/gpio');

Instantiate:

  1. /**
  2. * @type {Gpio}
  3. */
  4. const gpio = new Gpio();

Before all operations on channel, you have to open it with gpio.open(channel: number, direction: string): Promise

Example:

  1. // Import
  2. const Gpio = require('@jdes/gpio');
  3. // Instantiate
  4. const gpio = new Gpio();
  5. // Open the channel 7 on write mode
  6. // and write an analog value
  7. gpio.open(7, Gpio.direction.out)
  8. .then(() => gpio.setAnalogValue(7, 0.75));
  9. // Open the channel 3 on read mode
  10. // and read an analog value during 500ms
  11. gpio.open(3, Gpio.direction.in)
  12. .then(() => gpio.getAnalogValue(3, 500))
  13. .then((value) => {
  14. console.log(`Current value: ${value}`);
  15. });
  16. // Close after 5s
  17. setTimeout(() => {
  18. gpio.close(7);
  19. gpio.close(5);
  20. }, 5000);

API

Constants

Gpio.direction: Object.\

Object representing the available directions

  1. {
  2. "in": "in",
  3. "out": "in"
  4. }

Gpio.signal: Object.\

Object representing the available signals

  1. {
  2. "low": 0,
  3. "high": 1
  4. }

Methods

open(channel: number, direction: string): Promise

  • channel: Reference to the pin in the current mode’s schema.
  • direction: The pin direction, pass either Gpio.direction.in for read mode or Gpio.direction.out for write mode.
  • returns a Promise resolved if the channel was opened or rejected if an error occurs

Sets up a channel for read or write. Must be done before the channel can be used.

Example:

  1. gpio.open(7, Gpio.direction.out)
  2. .then(() => {
  3. console.log('channel 7 opened');
  4. });

close(channel: number): Promise

  • channel: Reference to the pin in the current mode’s schema.
  • returns a Promise resolved if channel was closed or rejected if an error occurs

Close a channel

Example:

  1. gpio.close(7)
  2. .then(() => {
  3. console.log('Channel 7 closed');
  4. });

getValue(channel: number): Promise.\

  • channel: Reference to the pin in the current mode’s schema.
  • returns a Promise.<number> resolvde with the current value or rejected if an error occurs

Reads a digital value of a channel.

Example:

  1. gpio.getValue(7)
  2. .then((value) => {
  3. console.log(`Channel 7's value: ${value}`);
  4. });

setValue(channel: number, value: number): Promise

  • channel: Reference to the pin in the current mode’s schema.
  • value: Boolean value to specify whether the channel will set to Gpio.signal.low or Gpio.signal.high.
  • returns a Promise resolved if the value was set or rejected if an error occurs

Writes a digital value to a channel.

Example:

  1. gpio.setValue(7, Gpio.signal.high);

getAnalogValue(channel: number, [duration: number = 500]): Promise.\

  • channel: Reference to the pin in the current mode’s schema.
  • duration: The duration (in ms) of computing
  • returns a Promise.<number> resolved with the current value or rejected if an error occurs

Reads an analog value (float) from a channel.

Example:

  1. gpio.getAnalogValue(7)
  2. .then((value) => {
  3. console.log(`Channel 7 value: ${value}`);
  4. });

setAnalogValue(channel: number, value: float, frequency: number): Promise

  • channel: Reference to the pin in the current mode’s schema.
  • value: The analog value. A float number value ∈ [0, 1]
  • returns a Promise resolved if the value was set or rejected if an error occurs

Writes an analog value (float) to a channel.

Example:

  1. gpio.setAnalogValue(7, 0.75);

getDirection(channel: number): Promise.\

  • channel: Reference to the pin in the current mode’s schema.
  • returns a Promise resolved with the direction of the channel or rejected if an error occurs

Gets the direction of a channel.
The resolved Promise.<string> will be returned with the current direction (a Gpio.direction value)

Example:

  1. gpio.getDirection(7)
  2. .then((direction) => {
  3. console.log(direction === Gpio.direction.out);
  4. });

setDirection(channel: number, direction: string): Promise

  • channel: Reference to the pin in the current mode’s schema.
  • direction: The pin direction, pass either Gpio.direction.in for read mode or Gpio.direction.out for write mode.
  • returns a Promise resolved if the direction was set or rejected if an error occurs

Changes the direction of a channel for read or write.

Example:

  1. gpio.setDirection(7, Gpio.direction.out)
  2. .then(() => {
  3. console.log('Direction set`on channel 7');
  4. });

Example

  1. const Gpio = require('@jdes/gpio');
  2. const gpio = new Gpio();
  3. // Open the channel 3 in read mode
  4. gpio.open(3, Gpio.direction.in)
  5. .then(() => {
  6. // Reads an analog value every seconds
  7. const interval = setInterval(() => {
  8. gpio.getAnalogValue(3)
  9. .then((value) => {
  10. console.log(`Current value: ${value}`);
  11. });
  12. }, 1000);
  13. // After 10 seconds, stop reading and close channel
  14. setTimeout(() => {
  15. clearInterval(interval);
  16. gpio.close(3);
  17. }, 10000);
  18. });

Environment variables

Environment variables can be passed to override the default configuration.

Frame

Time in ms for computing an analog read.
If PI_GPIO_FRAME = 1, at each ms à digital read is perform and at the end, an average value of each digital read will be produce.
If the process has to be executed during 200ms, 200 reads will be performed and the analog value will be equal to countOn / 200.

  • Options: PI_GPIO_FRAME
  • Type: Number
  • Default: 1

Revision

The default Rapsberry Pi revision if none match.
It’s useful to determine the GPIO mapping.

  • Options: PI_GPIO_REVISION
  • Type: Number
  • Default: 3

Frequency

Frequency in Hz for execute an analog write.
If PI_GPIO_FREQUENCY = 50, during a write, 20 (1000 / 50) on-off process will be performed.

  • Options: PI_GPIO_FREQUENCY
  • Type: Number
  • Default: 120

On-Off process

If PI_GPIO_FREQUENCY = 50 and the value to write is 0.75, the on-off process will be performed 20 times.
Every 20ms it writes 1 to the pin and after 15ms (20 * 0.75) it will writes 0.