npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gpio2

v0.1.0

Published

Control Raspberry Pi GPIO pins with node.js. Provide synchronous and asynchronous Promise API.

Downloads

1

Readme

gpio2

Export elegant API to control Raspberry Pi GPIO pins with Node.js. Provide both synchronous and asynchronous Promise API. Use interrupts to detect the state change of the GPIO pins and fire events what is much faster and more sensitive than file monitoring such as fs.watch or chokidar.

Installation

npm install gpio2

Usage

with ES5

require('gpio2').Gpio;

const pinIn = 'gpio21', //or 40
      pinOut = 32;

let gpio = new Gpio(portIn);

gpio.export(Gpio.DIR_IN, {
  debounceTimeout: 500
});

gpio.on('rising', function(e){
  console.log('rising', e);
});

with ES6

'use strict'

const Gpio = require("gpio2.js").Gpio;

const sleep = (millsec) => {
  console.log("sleep..." + millsec);
  return new Promise((resolve) => setTimeout(() => resolve(), millsec));
};

let gpio0 = new Gpio(40),
    gpio1 = new Gpio(32);

let main = async () => {
  await gpio1.export('in','falling');

  gpio1.watch(function(err, value){
    console.log('watched', gpio1.edge, gpio1.value, value);
  });

  await gpio0.export();

  while(1){
    await gpio0.toggleValue(); //make a signal changed per second.
    await sleep(1000);
  }
}

process.on("SIGINT", function(){
  gpio0.unexport();
  gpio1.unexport();

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

main();

API

Class Gpio extends events.EventEmitter

Methods

Properties

Events

Constants

  • Gpio.HIGH = 1;
  • Gpio.LOW = 0;
  • Gpio.DIR_IN = 'in';
  • Gpio.DIR_OUT = 'out';
  • Gpio.EDGE_NONE = 'none';
  • Gpio.EDGE_RISING = 'rising';
  • Gpio.EDGE_FALLING = 'falling';
  • Gpio.EDGE_BOTH = 'both';

constructor

new Gpio(pin) creates a GPIO pin instance. The arguments pin can be a number (pin number) or a string starts with 'gpio' (gpio number):

let pin1 = new Gpio('gpio21'); //create gpio21

//equal to: let pin12 = new Gpio('gpio12');
let pin2 = new Gpio(32); //create #32 pin of Pi, which is gpio12

The pin number mapping to GPIO number as below:

export([options])

Exports a GPIO to userspace.

  • [options: object] Additional options.

    The options argument supports the following:

    • direction: A string specifying whether the GPIO should be configured as an input or output. The valid values are: 'in', 'out'..

    • activeLow: 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.

    • debounceTimeout: Can be used to software debounce a button or switch using a timeout. Specified in milliseconds. The default value is 0.

unexport()

Unexports a GPIO from userspace and release all resources.

setValue(value)

Set a velue to a GPIO asynchronously. Returns a promise.

getValue(value)

Set a velue to a GPIO asynchronously. Returns a promise.

toggleValue()

Change GPIO value asynchronously. Returns a promise.

async function run(gpio){
    while(1){
        gpio.toggleValue(); //blink gpio value every 0.5 second to send a signal.
        sleep(500);
    }
}
value:0|1

Get or set a velue to a GPIO synchronously.

direction:string

The pin direction, pass either Gpio.DIR_IN for read mode or Gpio.DIR_OUT for write mode. Defaults to DIR_OUT.

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.


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 should be configured implictly and the GPIO will trigger the rising event.

gpio.on('rising', function(){
	console.log('A rising signal detected!');
});
event:falling

When register listener to falling event the falling interrupt edges 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 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:

let gpio = new Gpio(40);
gpio.export(Gpio.DIR_IN);
assertEqual(gpio.edge, Gpio.EDGE_NONE); 
gpio.on('rising', function(){...});
assertEqual(gpio.edge, Gpio.EDGE_RISING); 
gpio.on('falling', function(){...});
assertEqual(gpio.edge, Gpio.EDGE_BOTH);
gpio.removeListener('rising');
assertEqual(gpio.edge, Gpio.EDGE_FALLING);
gpio.removeListener('falling'); 
assertEqual(gpio.edge, Gpio.EDGE_NONE);

Thanks

onoff - awesome API for GPIO access. A few codes are borrow from there.

rpi-gpio - first library I used to control my Raspberry Pi. Inspire me to create this project.

LICENSE

MIT