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

perf-gpio

v0.6.1

Published

High performance GPIO tool kit for raspberry pi, including quadrature_decoder, DC motor control, soft PWM, buttons, etc. Based on c wiringpi.

Downloads

44

Readme

perf-gpio

High performance GPIO tool kit for raspberry pi, including quadrature_decoder, DC motor control, soft PWM, buttons, etc. Based on c wiringpi.

install

npm install perf-gpio --save

Note: perf-gpio depend on dynamic-linked wiringPi at run-time, make sure you have wiringPi installed (/usr/local/lib/libwiringPi.so file).

API Example

https://pinout.xyz/pinout/wiringpi# is the best doc I can find for wiringpi Pin number mappings.

onoff -- simple

var onoff = require('perf-gpio').onoff;

// wiring-pi 2 = GPIO27 = PIN13
var led = onoff(2);
led.set(1);

onoff -- flash

var onoff = require('perf-gpio').onoff;

// wiring-pi 2 = GPIO27 = PIN13
var led = onoff(2);

var current = false;

setTimeout(timeout, 1000);
function timeout() {
    setTimeout(timeout, 1000);
    current = !current;
    led.set(current);
    console.log(current);
}

button -- simple read

var button = require('perf-gpio').button;

// wiring-pi 3 = GPIO22 = PIN15
var b = button(3, button.PUD_UP); // use raspberry pi build-in 50k pull-up resistor

setTimeout(timeout, 1000);
function timeout() {
    setTimeout(timeout, 1000);
    var current = b.get();
    console.log(current);
}

button -- callback

var button = require('perf-gpio').button;

// wiring-pi 3 = GPIO22 = PIN15
var b = button(3, button.PUD_UP);

b.watch(function(err, status) {
    console.log(status? 'UP' : 'DOWN');
});

inputBank (setup multiple pins, read all at once, as bit mask)

var inputBank = require('perf-gpio').inputBank();
// wiring-pi 25/24/23 = GPIO26/19/13 = PIN37/35/33
var bank = inputBank([25, 24, 23], "PUD_DOWN");

setTimeout(timeout, 1000);
function timeout() {
    setTimeout(timeout, 1000);
    var val = bank();
    // bit mask: bit 0 <= wp25, bit 1 <= wp24, bit 2 <= wp23
    console.log(val);
}
  • Note: inputBank support batch read, but don't support edge-triggered callback. If you need callback, consider using button.
  • Note: inputBank and button can share the same pin. Under the hood they are both "input" mode, thus no conflict.

quadrature_decoder

var qd = require('perf-gpio').quadrature_decoder;

// quadrature_decoder you need 2 pins, phase A and B
// wiringpi PIN 0 and 1, maps to BCM GPIO 17 and 18
var counter = qd(0,1);

timeout();

function timeout() {
    setTimeout(timeout, 1000);
    console.log(counter.getCounter());
}

led (DMA-PWM basic)

var led = require('perf-gpio').led();
var pin = led(25); // wiringPi 25, GPIO 26
pin(0.5); // 50% on

led (DMA-PWM cnt.)

var led = require('perf-gpio').led();
var pin = led(25); // wiringPi 25, GPIO 26
pin(1);

setTimeout(function() {
    pin(0.5);
    setTimeout(function() {
        pin.close(); // turn off led before quit
        led.shutdown(); // shutdown DMA device before quit
        process.exit(0);
    }, 2000);
}, 2000);

DC motor control

var motor = require('perf-gpio').motor();
// Need 2 pin to control a DC motor (forward/backword)
// you need a H-bridge module to drive a DC motor
var pin = motor(23, 25);

pin(-0.5); // anywhere range from [-1.0, +1.0]

function timeout() {
    pin.close(); // turn off motor before quit
    motor.shutdown(); // shutdown DMA device before quit
    process.exit(0);
}

setTimeout(timeout, 2000);

Note: DC motor and LED shares the same DMA channel (14 for pi2/3/zero and 7 for pi4), 5ms cycle with 2us step size.

Servo control

var servo = require('perf-gpio').servo();
var pin = servo(29); // wiringpi_25=gpio_21=Phys_40

pin(1500); // 1.5ms (mid)

setTimeout(function () {
  pin(500); // 0.5ms (min)
  setTimeout(function () {
    pin(2500); // 2.5ms (max)
    setTimeout(function () {
      pin(1500); // 1.5ms (mid)
      setTimeout(function () {
        servo.shutdown();
        process.exit(0);
      }, 500);
    }, 1000);
  }, 1000);
}, 1000);

Note: Servo runs on it's own DMA channel (13 for pi2/3/zero and 6 for pi4), 20ms cycle with 10us step size.

Clock Output

var clockOut = require('./index').clockOut();

// wiring-pi 22 = GPIO6 = PIN31
var pinNum = 22;
var pin = clockOut(pinNum);
pin.setFeq(10000); // 10kHz
.....
pin.shutdown(); 
  • Note: This hardware clock, only available from a few pin.
  • Note: If don't shutdown, pi continue output clock even after your programe exits!
  • Note: valid range from [4.7k, 19.2M] (Hz)

Why perf-gpio?

  • Performance is the main reason I write this library.
  • perf-gpio is based on c wiringPi, most of the interrupt handling is done in c code. This is especially important for quadrature_decoder, so far perf-gpio is probably the best quadrature decoder available on raspberry-pi.
  • The DMA-PWM is based on rpio-pwm, which is the best (if not the only) DMA-PWM solution on node.js (that's the reason I spend my time created it).
  • perf-gpio also supports pull-up/down resistors (which is a must-have for my project).

license

MIT