opengpio
TypeScript icon, indicating that this package has built-in type declarations

1.0.13 • Public • Published

OpenGPIO

A performant c++ based general purpose GPIO controller for linux devices. OpenGPIO is written using libgpiod, line & chip based abstractions.

While this library can be used on most devices, you'll need to know the chip and line numbers corrisponding to the GPIO pin you want to access. This information can usually be found in the datasheet for your devices SOC. If all that sounds way to complicated, you can make use of one of the official device drivers which already have the bcm and board pin mappings for GPIO pins.

Prerequisites

  • libgpiod: sudo apt install -y libgpiod-dev - This library requires libgpiod-dev to be installed before installing via npm.

Supported Features

  • GPIO - General purpose input output.
  • PWM (via GPIO) - GPIO based pulse width modulation.
  • Events - Event callbacks for rising, falling, or both edges.

Unsupported Features

  • PWM (Native PWM) - This library does not yet support native PWM, only emulated PWM via GPIO.
  • I2C - Use the openi2c library for common i2c module drivers (still highly WIP), or alternatively we recommending using the i2c-bus library directly.

Official Device Drivers

  • NanoPI NEO3
  • RaspberryPi 2B
  • RaspberryPi 3B
  • RaspberryPi 400
  • RaspberryPi 4B
  • RaspberryPi 5B
  • RaspberryPi Zero2W
  • RaspberryPi ZeroW

Using An Official Driver

Using an official device driver is simple, just import the device by its name.

import { NanoPi_NEO3, Edge } from 'opengpio';

// GPIO Output
const output = NanoPi_NEO3.output(NanoPi_NEO3.bcm.GPIO3_B0);
output.value = true; // Set the NanoPi_NEO3's GPIO3_B0 pin high
output.value = false; // Set the NanoPi_NEO3's GPIO3_B0 pin low

// GPIO Input
const input = NanoPi_NEO3.input(NanoPi_NEO3.bcm.GPIO3_B0);
console.log(input.value); // Gets the NanoPi_NEO3's GPIO3_B0 pin high/low value as true/false

// GPIO Events
// Creates a listener for events on the NanoPi_NEO3's GPIO3_B0 pin for both Rising and Falling edges.
// Available Events: "change", "rise", "fall"
const watch = NanoPi_NEO3.watch(NanoPi_NEO3.bcm.GPIO3_B0, Edge.Both);
watch.on('change', (value) => {
    console.log(value); // Contains the high/low value as true/false
});

// Get the current value of the watch
console.log(watch.value);

// GPIO PWM
// Creates a 50hz (20ms) PWM on the NanoPi NEO3's GPIO3_B0 pin, with a duty cycle of 10% (2ms)
const pwm = NanoPi_NEO3.pwm(NanoPi_NEO3.bcm.GPIO3_B0, 0.1, 50);
pwm.setDutyCycle(0.2); // Updates the duty cycle of the pwm to 20% (4ms)

Using An Unofficial Driver

If no official driver exists, you can use the Default device and provide the chip and line numbers directly. Otherwise, usage is identical.

import { Default, Edge } from 'opengpio';

// GPIO Output
const output = Default.output({ chip: 0, line: 27 });
output.value = true; // Set the pin high at chip 0 line 27

Package Sidebar

Install

npm i opengpio

Weekly Downloads

13

Version

1.0.13

License

MIT

Unpacked Size

159 kB

Total Files

69

Last publish

Collaborators

  • explorationsystems