Native nodejs bindings for [libgpiod](https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/about/)
npm install @clevabit/node-libgpiodNative nodejs bindings for libgpiod

- libgpiod (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
Just add it as a regular nodejs dependency:
``bash`
npm i node-libgpiod
node-gyp will do the rest for you.
- raspberry pi model 3 B+ (64 bits, 1GB ram) running fedora
- raspberry pi zero w (32 bits, 512MB ram) running rasp pi os
technically speaking it should work with any modern vanilla kernel and libgpio.
We already are able to read and write pins!
Here goes the sample blink led hello-world.js:
`javascript
const { version, Chip, Line } = require("node-libgpiod");
const chip = new Chip(0);
const 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:
`javascript
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
- gpio character device needs
special udev rules in order
to belong to a special group so non-root users could access it freely
`bash`
# /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.
- node will garbage collect Chip and line too early on certain cases. When
writing the samples, sometimes the following error kept being thrown:
`bash
/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:
`javascript
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);
``
- [X] basic read/write
- [X] basic instant read/write
- [X] Chip/Line abstractions
- [ ] GPIO monitoring callbacks
- [ ] Bulk read/write
All features present on libgpiod eventually will be added to node bindings.
This is open source, i am willing to evaluate PR's :sunglasses: