Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-libgpiod

sombriks1.7kMIT0.5.0TypeScript support: included

Native nodejs bindings for [libgpiod][libgpiod]

libgpiod, node, linux, raspberry-pi, raxda-rock

readme

node-libgpiod

Native nodejs bindings for libgpiod

npm Build status MIT

Requirements / Dependencies

  • libgpiod 1.5 minimum required (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 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 @development-tools g++ \
 libgpiod libgpiod-devel libgpiod-utils \
 nodejs nodejs-devel
# openSUSE
sudo zypper in -t pattern devel_basis
sudo zypper in libgpiod libgpiod-devel libgpiod-utils \ 
 nodejs nodejs-devel

DEB based

# debian and its variants
sudo apt install build-essential 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.

  • lines should not be defined twice. if, for example, line 19 was defined in the program already, then it shall not be redefined unless it gets released first:

    // ...
    let l17 = new Line(chip0, 17)
    l17.requestOutputMode()
    l17.setValue(1)
    
    l17 = new Line(chip0, 17) // ERROR: we didn't release the previous one
    l17.requestOutputMode()
    l17.setValue(1)
  • 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

  • <input checked="" disabled="" type="checkbox"> Basic read/write
  • <input checked="" disabled="" type="checkbox"> Basic instant read/write
  • <input checked="" disabled="" type="checkbox"> Chip/Line abstractions
  • <input checked="" disabled="" type="checkbox"> Simulator configuration
  • <input disabled="" type="checkbox"> GPIO monitoring callbacks
  • <input disabled="" type="checkbox"> Bulk read/write
  • <input disabled="" type="checkbox"> Test cases covering entire gpiod API

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 Node
get line's instant value Misc gpiod_ctxless_get_value getInstantLineValue
set line's instant value Misc gpiod_ctxless_set_value setInstantLineValue
get chip name Chip gpiod_chip_name getChipName
get chip label Chip gpiod_chip_label getChipLabel
get number of lines in a chip Chip gpiod_chip_num_lines getNumberOfLines
get all line names from chip Chip - getLineNames
get line by name or number Chip - getLine
get line/pin offset number Line gpiod_line_offset getLineOffset
get line/pin name Line gpiod_line_name getLineName
get line consumer Line gpiod_line_consumer getLineConsumer
get line direction Line gpiod_line_direction getLineDirection
get line active state Line gpiod_line_active_state getLineActiveState
get line bias Line gpiod_line_bias getLineBias
check if line is used Line gpiod_line_is_used isLineUsed
check if line is open drain Line gpiod_line_is_open_drain isLineOpenDrain
check if line is open source Line gpiod_line_is_open_source isLineOpenSource
update line info Line gpiod_line_update update
check if line needs update Line gpiod_line_needs_update isLineOpenSource
get line/pin value Line gpiod_line_get_value getValue
set line/pin value Line gpiod_line_set_value setValue
request line (pass config options) Line gpiod_line_request lineRequest
request line for input Line gpiod_line_request_input requestInputMode
request line for output Line gpiod_line_request_output requestOutputMode
request rising edge events Line gpiod_line_request_rising_edge_events requestRisingEdgeEvents
request falling edge events Line gpiod_line_request_falling_edge_events requestFallingEdgeEvents
request both edges events Line gpiod_line_request_both_edges_events requestBothEdgesEvents
request input with flags Line gpiod_line_request_input_flags requestInputModeFlags
request output with flags Line gpiod_line_request_output_flags requestOutputModeFlags
request rising edge events with flags Line gpiod_line_request_rising_edge_events_flags requestRisingEdgeEventFlags
request falling edge events with flags Line gpiod_line_request_falling_edge_events_flags requestFallingEdgeEventFlags
request both edges events with flags Line gpiod_line_request_both_edges_events_flags requestBothEdgesEventFlags
release the line Line gpiod_line_release release

Other implementations

Those are other notable libgpiod wrapper implementations:

Official C++ binding

Official Python binding

Golang binding

Rust binding

Contributing

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

changelog

Changelog

Noteworthy changes

2024-06-30

  • closed the oldest issue, issue #4, covered it with a specific test case
  • recomissioned chai. it must be v4 since it's a commonsjs project. more info here.
  • CI/CD still broken, but if i self-host maybe i can get it working! time to homelab again!

2024-06-29

  • acquired an [raxda rock 3c][https://radxa.com/products/rock3/3c/] so i can test on real hardware again. my rasp collection is aging and isn't reliable anymore.
  • did a fedora38 virtual machine and to my happy surprise this version has the gpio-sim kernel module built and offers libgpiod 1.6.4! best of two worlds.
  • github CI remains unable to run testsuite sadly, tweaked the yaml script to use a fedora 38 container but it's just not the same thing.
  • on the plus side, thanks to the working gpio-sim on my dev environment i could finally cover everything already implemented quite fast. time to look at the issues and missing functions, notably bulk and watch operations.

2024-04-20

0.4.3

  • added a more expressive message in exceptions, bubbling the errno value in message of thrown exception, many thanks to @splitice

2024-04-06

0.4.2

  • adding compile flag so the wrapper work again with older libgpiod versions. thanks @alejandroclaro and @splitice for the report, guidance and time.
  • working on docs to make people aware of api breaking even inside 1.x series of libgpiod.
  • working on script to simulate mockup somehow

2024-03-29

0.4.1

2024-02-24

0.4.0

  • Finally branched main into two distinct heads
  • Prepared a new virtual machine to be able to work with 1.x series
  • Still looking for a way to test the native part properly. There is [gpiomock][gpiomock] but i can't find it built on any distro i tried (opensuse/fedora). for 2.x there is gpio-sim, IF i figure out how to use it correctly.
  • This same log entry on main-2x will be different.

2023-11-15

0.3.2

  • added new Pin(n) as a syntax sugar to get a new Line(n) from Chip(0).
  • put the workflow to run tests but mostly are ignored for the moment. i am looking for a proper way to simulate a chip.

2023-11-13

0.3.1

  • Merged PR #12 from @khill-fbmc fixing errors in index.d.ts
  • Some dependency version bumps, it compiles again under node 18.
  • Added GitHub Action to ease release process

I'll try to streamline more the changes we receive, let me know if anything needs immediate attention.

2022-04-17

0.3.0

  • Added Chip::getNumberOfLines which returns the number of available GPIO lines on the chip
  • Added Chip::getChipName which returns chip's name as represented by the kernel
  • Added Chip::getChipLabel which returns chip's label as represented by the kernel
  • Added Line::getLineOffset which returns line's offset number
  • Added Line::getLineName which returns line's name, if set, otherwise undefined
  • Added Line::getLineConsumer which returns line's consumer, if set, otherwise undefined
  • Extended the Typescript .d.ts file with the new methods and added jsdoc comments
  • Added a condition in the binding.gyp to prevent builds on non-Linux environments (aka macs or Windows machines)

2022-04-15

0.2.2

Fix for double line instantiation on same process contributed by dmitrydvorkin

2022-03-11

created the changelog file so we can properly track changes and contributions

0.2.1

Fixed argument index of consumer name on requestInputMode by noctarius

2022-03-09

0.2.0

code fix and type definitions contributed by noctarius

2020-12-30

0.1.4

first really usable version. present on node-libgpiod-examples

2020-12-17

0.0.1

initial release on npm