sombriks / node-libgpiod

libgpiod node bindings
MIT License
28 stars 8 forks source link
libgpiod linux nodejs raspberrypi raxdarock

node-libgpiod

Native nodejs bindings for libgpiod

npm Build Status MIT

Requirements / Dependencies

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

Roadmap

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

Official Python binding

Golang binding

Rust binding

Contributing

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