gpio-promise

1.1.2 • Public • Published

gpio-promise

A fork of https://github.com/EnotionZ/GpiO to use es6 promises instead of callbacks. I have also changed the API slightly.

The code is written in es6 but compiled with 6to5 to es5 to be used with node as `require('gpio-promise').

Installation

Get node.js on your Raspberry Pi

On Raspbian, you can simply run apt-get install nodejs, otherwise, compile it

Usage

This library is an npm package, just define gpio-promise in your package.json dependencies or

npm install gpio-promise

Note: you must be running as root or have the proper priviledges to access the gpio headers

Standard setup

var GpioPin = require("gpio-promise");
 
var gpio4 = new GpioPin(4);
 
gpio4
   .open('out')
   .then(function() {
      // All methods returns Promise
      return gpio4.high();
   });

The constructor also takes an optional object containing options

var gpio4 = new GpioPin(4, {interval: 200});

Header direction "in"

If you plan to set the header voltage externally, use direction in and read value from your program.

var GpioPin = require("gpio-promise");
var gpio4 = new GpioPin(4);
 
gpio4
   .open('in')
   .then(function() {
      // Its ready now
   });

API Methods

All API Methods below return Promise.

var GpioPin = require("gpio-promise");
var gpio4 = new GpioPin(4);
 
/** Open pin */
gpio4.open('in');
gpio4.open('out');
 
// Or use the convenient methods
gpio4.in();
gpio4.out();
 
/** Set value */
gpio4.set(0);
gpio4.set(1);
gpio4.set('low');
gpio4.set('high');
 
/** Toggle value between 1 and 0 */
gpio4.toggle();
 
/*
 * Unexport program when done
 */
process.on('SIGINT', function() {
   GpioPin.unexport(4);
});

EventEmitter

This library uses node's EventEmitter which allows you to watch for value changes and fire a callback.

var GpioPin = require("gpio-promise");
var gpio4 = new GpioPin(4);
 
/** On every change */
gpio4.on('change', function(value) {
   
});
 
/** Changing from 0 to 1 */
gpio4.on('rising-edge', function(value) {
   
});
 
/** Changing from 1 to 0 */
gpio4.on('falling-edge', function(value) {
      
});
            
// unbind a particular callback from the "change" event
gpio4.removeListener("change", processPin4);
      
// unbind all callbacks from the "change" event
gpio4.removeAllListeners("change");

Tests

Run the tests with npm test, these use a mock gpio and are only testing this librarys interface.

Package Sidebar

Install

npm i gpio-promise

Weekly Downloads

4

Version

1.1.2

License

none

Last publish

Collaborators

  • halmhatt