Skip to content

๐Ÿš€ GPIO Pin control in JS using the gpiochip character device

License

Notifications You must be signed in to change notification settings

shmishtopher/GPIO

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

85 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

AR_GPIO

๐Ÿš€ GPIO Pin control in JS using the gpiochip character device

AR_GPIO aims to provide the portablity of the sysfs GPIO interface while preserving the speed of direct regiter access (achieved by using the gpiochip character device in /dev/gpiochipN). The libray is very mininal, providing only the core GPIO functionality and is fast enough to work with soft-PWMs.

  1. Install
  2. Examples
  3. Building
  4. API

Platform Support

Confirmed Working Should Work* Confirmed not Working
Raspberry Pi Zero W Raspberry Pi 2
Raspberry Pi Zero Raspberry Pi 3
C.H.I.P Beagle Bone

*Untested

Install

AR_GPIO is build on fastcall, which uses cMake instead of node-gyp. Install cMake before installing AR_GPIO

# Install C++ Toolchain
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install cmake  # AR_GPIO depends on cmake
npm i --save https://github.com/shmishtopher/GPIO  # Node dependency

Example

cylon eyes

// Cylon Eyes

const { GPIOChip, OUTPUT, HIGH, LOW } = require('ar_gpio')

const chip = new GPIOChip(0)
const lines = [
  chip.line(14, OUTPUT),
  chip.line(15, OUTPUT),
  chip.line(18, OUTPUT),
  chip.line(23, OUTPUT),
  chip.line(24, OUTPUT)
]

function sleep (ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

async function main () {
  while (true) {
    for (let i = 0; i < 4; i++) {
      await sleep(50)
      lines[i + 0].set(LOW) 
      lines[i + 1].set(HIGH)
    }

    for (let i = 4; i > 0; i--) {
      await sleep(50)
      lines[i + 0].set(LOW)
      lines[i - 1].set(HIGH)
    }
  }
}

main()

Building

Building requires the Rust compiler. The easist way to get RustC and Cargo is with Rustup

curl https://sh.rustup.rs -sSf | sh -s -- --help

With the Rust compiler installed you can build with the NPM script.

npm run build

API

Initilize a new GPIOChip:

/**
 * @param {Number} chip;  The gpio chip to use.  
 * If your gpio chip is at /dev/gpiochip0 then 
 * the `chip` param is `0`
 */

const myChip = new GPIOChip(0)

Export a GPIO line:

/**
 * @param {Number} line;  The BCM pin to export
 * @param {Number} flags;  The flags you need, ORd together
 */

const inputLine = myChip.line(17, INPUT | ACTIVE_LOW)
const outputLine = myChip.line(21, OUTPUT)

Read a GPIO line:

/**
 * The line must be exported as an INPUT before using `.get()`
 * @returns {HIGH | LOW} state;
 */

if (inputLine.get() === HIGH) {
  console.log('Line 17 is HIGH')
}

Write to a GPIO line:

/**
 * The line must be exported as an OUTPUT before using `.set()`
 * @param {HIGH | LOW} state;  The state to set.
 */

outputLine.set(HIGH)
// Or...
outputLine.set(1)


outputLine.set(LOW)
// Or...
outputLine.set(0)

About

๐Ÿš€ GPIO Pin control in JS using the gpiochip character device

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published