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

node-libgpiod

v0.4.6

Published

Native nodejs bindings for [libgpiod][libgpiod]

Downloads

1,575

Readme

node-libgpiod

Native nodejs bindings for libgpiod

npm Build Status MIT

Requirements / Dependencies

  • libgpiod 1.x (and devel headers)
  • nodejs (and devel headers)
  • linux (tested on fedora 33 running on raspberry pi model 3 B+ and rasp pi os on zero w)
  • c/c++ development tools

How to use into your project

First install libgpiod and node development packages, if not installed yet:

RPM based

# fedora
sudo dnf install libgpiod libgpiod-devel libgpiod-utils nodejs-devel
# openSUSE
sudo zypper in libgpiod libgpiod-devel libgpiod-utils nodejs-devel

DEB based

# debian and its variants
sudo apt install gpiod libgpiod2 libgpiod-dev libnode-dev

Then just add it as a regular nodejs dependency:

npm i node-libgpiod

node-gyp will do the rest for you.

Tested platforms

Technically speaking it should work with any modern vanilla kernel and libgpiod 1.x.

What about libgpiod 2.x?

We're still working on libgpiod 2.x

Status

We already are able to read and write pins!

Here goes the sample blink led hello-world.js:

const { version, Chip, Line } = require("node-libgpiod");

global.chip = new Chip(0);
global.line = new Line(chip, 17); // led on GPIO17
let count = 10;

console.log(version());
line.requestOutputMode();

const blink = () => {
  if(count){
    line.setValue(count-- % 2);
    setTimeout(blink,1000);
  } // else line.release(); 
  // not needed, libgpiod releases resources on process exit  
};

setTimeout(blink,1000);

Another example:

const { version, Chip, Line } = require("node-libgpiod");
const express = require("express");

const app = express();
// avoid chip and line being gc-collected
app.chip = new Chip(0);
app.line = new Line(app.chip, 17); // led on GPIO17

console.log(version());
app.line.requestOutputMode();

app.get("/on", (req,res) => {
  app.line.setValue(1);
  res.send("it's on");
});

app.get("/off", (req,res) => {
  app.line.setValue(0);
  res.send("it's off");
});

app.listen(3000);
console.log("running");

See our testcases for more information

See node-libgpiod-examples for more sample code

known issues

  • libgpiod 2.x series is around the corner, and its API is incompatible with 1.x the 2.x branch (under development) will handle 2.x while 0.x and 1.x will support libgpiod 1.x series.

  • gpio character device needs special udev rules in order to belong to a special group so non-root users could access it freely

    # /etc/udev/rules.d/85-gpiochip.rules 
    KERNEL=="gpiochip*", SUBSYSTEM=="gpio", MODE="0660", GROUP="wheel"
  • libgpiod must be installed in the system correctly with development headers otherwise npm install will fail.

  • inside libgpiod 1.x series there is a set of new flags created on 1.5.x version around 2019 and they where no back ported to previous ones libgpiod releases. Your build might break because of this, we're working on solve this.

  • node will garbage collect Chip and Line too early on certain cases. When writing the samples, the following error kept being thrown:

    /home/sombriks/git/sample-node-libgpiod/index2.js:12
        line.setValue(count-- % 2);
            ^
    
    Error: Unable to set value for this line
        at Timeout.blink [as _onTimeout] (/home/sombriks/git/sample-node-libgpiod/index2.js:12:10)
        at listOnTimeout (internal/timers.js:554:17)
        at processTimers (internal/timers.js:497:7)

    It occurs because main module body was already evaluated and finished while interval/timeout function still active, but has no local reference for Chip or Line instances. Therefore, v8 thinks that those objects can be garbage-collected releasing the underlying resources, giving us the error. To avoid this, make sure your objects will be present on function scope:

    const { version, Chip, Line } = require("node-libgpiod");
    
    const chip = new Chip(0);
    const line = new Line(chip, 17); // led on GPIO17
    let count = 20;
    
    console.log(version());
    line.requestOutputMode();
    
    const blink = function () {
      // avoid early gc
      this.chip = chip
      this.line = line
      if(count){
        line.setValue(count-- % 2);
        setTimeout(blink,500);
      }
    };
    
    setTimeout(blink,500);

    Or, probably it is even better to create you chip and line instances globally:

    global.mychip = new Chip(0);
    global.line1 = new Line(chip, 17);

Roadmap

  • [X] basic read/write
  • [X] basic instant read/write
  • [X] Chip/Line abstractions
  • [ ] GPIO monitoring callbacks
  • [ ] Bulk read/write
  • [ ] Complete API parity with underlying C/C++ library

All features present on libgpiod eventually will be added to node bindings, then the node package will finally enter in 1.x series.

Also see our changelog and project updates for details.

Functionality parity

This is the api parity table:

| Description | Scope | C/C++ | Node | |----------------------------------------------|---------------|--------------------------------|-----------------------| | get line's instant value | Miscellaneous | gpiod_ctxless_get_value | getInstantLineValue | | set line's instant value | Miscellaneous | gpiod_ctxless_set_value | setInstantLineValue | | get number of lines in a chip | Chip | gpiod_chip_num_lines | getNumberOfLines | | get chip name | Chip | gpiod_chip_name | getChipName | | get chip label | Chip | gpiod_chip_label | getChipLabel | | get line/pin offset number | Line | gpiod_line_offset | getLineOffset | | get line/pin name | Line | gpiod_line_name | getLineName | | get line/pin value | Line | gpiod_line_get_value | getValue | | set line/pin value | Line | gpiod_line_set_value | setValue | | get line consumer | Line | gpiod_line_consumer | getLineConsumer | | set line for input (read) | Line | gpiod_line_request_input | requestInputMode | | set line for input with flags | Line | gpiod_line_request_input_flags | requestInputModeFlags | | set line for output (write) | Line | gpiod_line_request_output | requestOutputMode | | release the line | Line | gpiod_line_release | release |

Other implementations

Those are other notable libgpiod wrapper implementations:

Official C++ binding

  • https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/cxx?h=v1.6.x

Official Python binding

  • https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/python?h=v1.6.x

Golang binding

Rust binding

Contributing

This is open source, i am willing to evaluate PR's :sunglasses: