This library is a work in progress. It may have bugs, and it may not follow the best practices for a library.
Please report any issues you find, and help me improve the library by submitting pull requests or at least a issue. I'm not forcing anybody to use any template for a issue, just describe the bug and the output you are getting. Pull request is always better, you know it :)
I'm not responsible for any damage caused by using this library. Use with caution, but I was not able to break anything till now.
A Node.js wrapper for the libgpiod 2 C++ API. This library provides an easy-to-use interface for GPIO manipulation on Linux systems, similar to the onoff library but using the modern libgpiod 2 API.
- Node.js >= 16.0.0
- libgpiod 2+ development headers
- A C++ compiler compatible with C++17
# Install libgpiod development headers (on Debian/Ubuntu)
sudo apt-get install libgpiod-dev
# Install the package (not wokrking currently as I did not publish anything)
npm install libgpiod2-nodeimport { Chip, Direction, Edge, Value, LineConfig, LineRequest } from 'libgpiod2-node';
// Discover available GPIO chips
const chipPaths = Chip.getChips();
console.log('Available chips:', chipPaths);
// Open GPIO chip
const chip = new Chip('gpiochip0');
console.log(`Chip label: ${chip.label}`);
console.log(`Number of lines: ${chip.numLines}`);
// Get information about a line without exporting it
const lineInfo = chip.getLineInfo(17);
console.log('Line info:', lineInfo);
// Configure GPIO line for output
const led = chip.getLine(17);
led.setDirection(Direction.OUTPUT);
led.setValue(Value.HIGH);
// Configure GPIO line for input with interrupt
const button = chip.getLine(27);
button.setDirection(Direction.INPUT);
button.setEdge(Edge.BOTH);
// Watch for button changes
button.watch((err, value) => {
if (err) {
console.error('Error:', err);
return;
}
console.log('Button value changed to:', value);
// Toggle LED when button is pressed
if (value === Value.HIGH) {
const currentValue = led.getValue();
led.setValue(currentValue === Value.HIGH ? Value.LOW : Value.HIGH);
}
});
// Example of configuring multiple lines with different settings
function configureMultipleLines() {
// Create a line configuration
const config = new LineConfig();
// Configure line 5 as output
config.setOffset(5);
config.setDirection(Direction.OUTPUT);
config.setDrive(Drive.PUSH_PULL);
config.setOutputValue(Value.HIGH);
// Configure line 6 as input with pull-up and edge detection
config.setOffset(6);
config.setDirection(Direction.INPUT);
config.setBias(Bias.PULL_UP);
config.setEdge(Edge.BOTH);
// Request both lines at once
const request = new LineRequest(chip, [5, 6], config);
// Now you can interact with both lines through the request object
console.log(`Line 6 value: ${request.getValue(6)}`);
request.setValue(5, Value.LOW);
// Remember to release when done
request.release();
}
// Remember to clean up when done
process.on('SIGINT', () => {
console.log('Cleaning up...');
button.unwatch();
button.unexport();
led.unexport();
chip.close();
process.exit(0);
});new Chip(name: string)- Open a GPIO chip by namegetLine(offset: number)- Get a GPIO line by offsetclose()- Close the chipgetChips()- Get a list of available GPIO chip pathslabel- Get the label of the chipnumLines- Get the number of lines on the chipgetLineInfo(offset: number)- Get information about a line without exporting it
setDirection(direction: Direction)- Set line direction (INPUT or OUTPUT)setValue(value: Value)- Set line value (HIGH or LOW)getValue()- Get current line valuesetEdge(edge: Edge)- Set edge detection (NONE, RISING, FALLING, or BOTH)watch(callback: (err: Error | null, value: Value) => void)- Watch for value changesunwatch()- Stop watching for changesunexport()- Release the line
setOffset(offset: number)- Set the offset for which subsequent configuration will applysetDirection(direction: Direction)- Set line directionsetEdge(edge: Edge)- Set edge detectionsetBias(bias: Bias)- Set line biassetDrive(drive: Drive)- Set line drivesetActiveLow(activeLow: boolean)- Set active lowsetOutputValue(value: Value)- Set initial output value
new LineRequest(chip: Chip, offsets: number[], config: LineConfig)- Create a new LineRequest instancegetValue(offset: number)- Get value of a requested linesetValue(offset: number, value: Value)- Set value of a requested linerelease()- Release all requested lines
Direction: INPUT, OUTPUTValue: LOW, HIGHEdge: NONE, RISING, FALLING, BOTHBias: UNKNOWN, DISABLED, PULL_UP, PULL_DOWNDrive: PUSH_PULL, OPEN_DRAIN, OPEN_SOURCE
LGPL-2.1