Code Monkey home page Code Monkey logo

pi-gpioutil's Introduction

gpioUtil: A Node wrapper for Wiring Pi's gpio

gpioUtil is a Node wrapper for the Wiring Pi gpio utility, which manages the Raspberry Pi’s GPIO pins. It currently supports the core functions:

  • export, unexport, unexportall,
  • read, write, pwm,
  • readall, exports,
  • mode, edge,
  • version.

(Need anything else? File an issue.)

If you’re not sure what all this is about, please read the gpio utility’s page. gpioUtil is really just a very thin wrapper that allows comfortable invocation from a Node program. Here’s the most important bit:

gpio can control, read and write the GPIO pins, and export/unexport pins via the /sys/class/gpio interface, where they will then be available to user programs (without requiring root privileges).

gpio and gpioUtil are by nature not suited for very frequent read/writes (say, more than one call per minute).
(If that’s what you need, try [wiring-pi][wpi-node-addon], which has Node bindings to the native Wiring Pi library. [In my experience][piswitch], it’s fast enough for sub-millisecond precision.)

[wpi-node-addon]: https://github.com/eugeneware/wiring-pi) [piswitch]: https://github.com/tjanson/PiSwitch

Wiring Pi Installation

If you haven’t already, please install Wiring Pi, which includes the gpio utility, [as described on their website][wpi-install]. [wpi-install]: http://wiringpi.com/download-and-install/

Usage

Here’s a simple example of how gpioUtil might be used:

var gpioUtil = require('pi-gpioutil');
var pinBcm = 17; // BCM numbering
var pinWiring = 0; // Wiring Pi numbering (yes, this is confusing)
gpioUtil.export(pinBcm, 'in', function(err) {
  if (!err) {
    gpioUtil.read(pinWiring, function(err, stdout, stderr, value) {
      if (value) console.log('Input is HIGH!');
    });
  }
});

Reference

General information

All functions have the general form fct([[pin, [params]], [callback]), and do the following:

  1. the input parameters are checked for (syntactic) validity
  2. the gpio utility is called via child_process.exec()
  3. the exec()s err, stdout, stderr are passed to the callback (i.e., you may inspect the raw output)
  4. if the call is meant to return data, the utility’s output is converted for easier processing and passed as a fourth parameter (e.g., readall provides a JSON representation of all pins)

Pin numbers are passed untouched, which means that the Broadcom numbers are used for [un]export, while the Wiring Pi numbering scheme is used for everything else. Neither of course matches the physical pin layout.
Confused? So in everyone else, but there’s this really pretty pinout reference by Philip Howard to make up for it.

When reading pins, high and low voltages are converted to the boolean true/false, respectively.

Important functions

Once again, please refer to the gpio utility’s page for details of what a function does; the following will tell you how to call it.
Callback will always be passed err, stdout, stderr as the first three arguments.
Pin numbers are in Wiring Pi format if not noted otherwise.

pi-gpioutil's People

Contributors

tjanson avatar

Watchers

Ryan Schneider avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.