GPIO control and interrupt detection for Raspberry Pi
@bratbit/onoff is a GPIO control and interrupt detection package for nodejs intended for use with Raspberry Pi.
The package strives to offer an API compatible to that of fivdi/onoff, while using native bindings.
apt install build-essential libgpiod2 libgpiod-dev
pacman -Syq base-build libgpiod
apk add build-base python3 libgpiod libgpiod-dev
dnf install gcc make pkg-config python3 libgpiod libgpiod-devel
Add the package to your project.
npm add @bratbit/onoff
export type High = 1;
type Low = 0;
type Direction = "in" | "out" | "high" | "low";
type Edge = "none" | "rising" | "falling" | "both";
type Options = {
debounceTimeout?: number;
activeLow?: boolean;
reconfigureDirection?: boolean;
};
type ValueCallback = (err: Error | null | undefined, value: BinaryValue) => void;
type BinaryValue = High | Low;
- constructor(gpio: Number, direction: Direction)
- constructor(gpio: Number, direction: Direction, edge: Edge)
- constructor(gpio: Number, direction: Direction, options: Options)
- constructor(gpio: Number, direction: Direction, edge: Edge, options: Options)
- readSync(): BinaryValue
- read(): Promise
- read(callback: ValueCallback): void
- writeSync(value: BinaryValue): number
- write(value: BinaryValue): Promise
- write(value: BinaryValue, callback: (err?: Error | null | undefined) => void): void
- direction(): Direction
- setDirection(direction: Direction): void
- edge(): Edge
- setEdge(edge: Edge): void
- activeLow(): boolean
- setActiveLow(invert: boolean): void
- watch(callback: ValueCallback): void
- unwatch(callback: ValueCallback): void
- unwatchAll(): void
Gpio(gpio: Number, direction: Direction)
Gpio(gpio: Number, direction: Direction, edge: Edge)
Gpio(gpio: Number, direction: Direction, options: Options)
Gpio(gpio: Number, direction: Direction, edge: Edge, options: Options)
Gpio class constructor
@param: gpio
Number of the Gpio pin to use.@param: direction
Direction of the line. This can be a string set to:
inConfigure the line as input.outConfigure the line as output.highConfigure the line as output with the initial value set to logical high.lowConfigure the line as output with the initial value set to logical low.@param: edge (optional)
Configure the line for edge detection. This can be a string set to:
noneDo not listen for edge events. This is the default if not configured.risingListen for rising edge events.fallingListen for falling edge events.bothListen for both rising and falling edge events.@param: options (optional)
Object containing the following keys:activeLowIf set totrue, configure the line to have its logical state inversed from the physical state. Default isfalse.debounceTimeoutWhen listening to events, they will be debounced for the ammount of milliseconds specified here. Default is0.reconfigureDirectionIf set totruethe line will be opened as-is without reconfiguring it. Default isfalse.
Read the line synchronously
@return: BinaryValue indicating the logical state of the line.
Read the line and return a Promise resolving to the logical state
@return: A promise, that will resolve to a BinaryValue indicating the logical state of the line.
Read the line and call the
callbackfunction passing an error, and value parameters. Value represents the logical state of the line.@param: callback
callback is a function of type ValueCallback, that will be called when the value of the line is read.
Set the logical line state to
valuesynchronously.@parmam: value
Set the logical line state tovalue.
Set the logical line state to
valueand resolve the Promise when done.@param: value
Set the logical line state tovalue.
Set the logical line state to
valueand trigger acallbackwhen done.@param: value
Set the logical line state tovalue.
Get the direction of the line.
@return: Direction
Return the direction setting of the line.
Set the direciton of the line.
@param: direction
Set the direction of the line.
Get the edge setting of the Gpio object.
@return: edge
Set the edge detection of the line. The line must be set to
infor this to take any effect.@param: edge
Get value of activeLow option of the Gpio object. This will tell you if the line is set to invert logical state.
@return: boolean
trueif the logical state is inverted.
Set the activeLow option of the Gpio object. This will make the line values invert the physical value.
@param: invert
Set this totrueif you want the line state to be inverted.
Add a
callbackfunction to the set of callbacks that will be triggered when an edge event is detected. Theedgeoption must not benoneand thedirectionneeds to bein, in order to listen to edge events.@param: callback
Function to trigger when an edge event is detected.
Remove a function from the list of those that will be triggered when an edge event is detected.
@param: callback
Function to remove from the list of listeners.
Remove all listeners and stop listening to edge events.
Read the value of GPIO pin.
import { Gpio } from '@bratbit/onoff';
let gpio12: Gpio = new Gpio(12, 'in');
console.log(`Pin value is ${gpio12.readSync()}`);import { Gpio } from '@bratbit/onoff';
let gpio12 = new Gpio(12, 'in');
console.log(`Pin value is ${gpio12.readSync()}`);const { Gpio } = require('@bratbit/onoff');
let gpio12 = new Gpio(12, 'in');
console.log(`Pin value is ${gpio12.readSync()}`);Write a one second pulse to GPIO pin.
import { Gpio } from '@bratbit/onoff';
import { timer } from 'rxjs';
let gpio13: Gpio = new Gpio(13, 'out');
gpio13.writeSync(Gpio.HIGH);
timer(1000).subscribe(() => {
gpio13.writeSync(Gpio.LOW)
});import { Gpio } from '@bratbit/onoff';
import { timer } from 'rxjs';
let gpio13 = new Gpio(13, 'out');
gpio13.writeSync(Gpio.HIGH);
timer(1000).subscribe(() => {
gpio13.writeSync(Gpio.LOW)
});const { Gpio } = require('@bratbit/onoff');
const { timer } = require('rxjs');
let gpio13 = new Gpio(13, 'out');
gpio13.writeSync(Gpio.HIGH);
timer(1000).subscribe(() => {
gpio13.writeSync(Gpio.LOW)
});Listen for interrupts for 30 seconds and print the line value, when an interrupt occurs.
import { Gpio, BinaryValue } from '@bratbit/onoff';
import { timer } from 'rxjs';
let gpio12: Gpio = new Gpio(12, 'in', 'both', {debounceTimeout: 300});
function handler (err: Error | null | undefined, value: BinaryValue): void {
console.log(`Interrupt! Line value is ${value}`);
}
gpio12.watch(handler);
timer(30000).subscribe(() => {
gpio12.unwatch(handler);
});import { Gpio } from '@bratbit/onoff';
import { timer } from 'rxjs';
let gpio12 = new Gpio(12, 'in', 'both', {debounceTimeout: 300});
function handler (err, value) {
console.log(`Interrupt! Line value is ${value}`);
}
gpio12.watch(handler);
timer(30000).subscribe(() => {
gpio12.unwatch(handler);
});const { Gpio } = require('@bratbit/onoff');
const { timer } = require('rxjs');
let gpio12 = new Gpio(12, 'in', 'both', {debounceTimeout: 300});
function handler (err, value) {
console.log(`Interrupt! Line value is ${value}`);
}
gpio12.watch(handler);
timer(30000).subscribe(() => {
gpio12.unwatch(handler);
});