1.0.4 • Published 3 years ago

gpio-components v1.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

GPIO Components

npm Build Status npm bundle size codecov npm License tested with jest

A collection of GPIO components written for Node in TypeScript. Supports Linux boards like the Raspberry Pi or BeagleBone.

Contents

Requirements

NodeJS 12.0+

Installation

npm install gpio-components

Usage

const { LED, Switch } = require('gpio-components');

const led = new LED({
  pin: 1
});

const isOn = led.isOn(); // false

led.turnOn();

const isOn = led.isOn(); // true

// ...

const mySwitch = new Switch({
  pin: 2,
  onPress: () => console.log('Pressed!')
});

mySwitch.watch(); // Begin watching for any interrupts

Documentation

LED

Constructor

const { LED } = require('gpio-components');

const led = new LED({ pin: 1 });

LEDConfig:

PropertyTypeDescription
pinnumberThe GPIO pin for the LED

Methods

MethodDescriptionParametersReturns
isOnReturns whether or not the LED is on or offn/aboolean
toggleToggles the state of the LED, returning the new staten/aboolean
turnOnTurns the LED onn/an/a
turnOffTurns the LED offn/an/a
cleanUpCleans up the LED when finishednen/an/a

LEDSwitch

Constructor

const { LEDSwitch } = require('gpio-components');

const ledSwitchConfig = {
  ledPin: 1,
  switchPin: 2,
  onPress: () => console.log('Pressed!')
};

const ledSwitch = new LEDSwitch(ledSwitchConfig);

LEDSwitchConfig:

PropertyTypeDescription
ledPinnumberThe GPIO pin for the LED
switchPinnumberThe GPIO pin for the switch
onPressfunctionThe callback function for when the switch is pressed
switchOptionsobject(Optional) options for the switch. (See below for details)

Methods

MethodDescriptionParametersReturns
watchBegin watching the LED Switch for any interruptionsn/an/a
isOnReturns whether or not the LED is onn/aboolean
cleanUpCleans up the LEDSwitch when finishedn/an/a

RotaryEncoder

Note:

This implementation of a Rotary Encoder is for a three pin Rotary Encoder.

Constructor

const { RotaryEncoder } = require('gpio-components');

const rotaryEncoderConfig = {
  pinA: 1,
  pinB: 2,
  onIncrement: () => console.log('Incremented!'),
  onDecrement: () => console.log('Decremented!'),
  onAlways: () => console.log('Turned!')
};

const encoder = new RotaryEncoder(rotaryEncoderConfig);

RotaryEncoderConfig:

PropertyTypeDescription
pinAnumberThe GPIO pin for the first pin for the rotary encoder
pinBnumberThe GPIO pin for the second pin for the rotary encoder
onIncrementfunctionThe callback function for when the rotary encoder is turned clockwise
onDecrementfunctionThe callback function for when the rotary encoder is turned counter clockwise
onAlwaysfunctionThe callback function for when the rotary encoder is turned, regardless of direction.

Methods

MethodDescriptionParametersReturns
watchBegins watching the rotary encoder for any interruptionsn/an/a
cleanUpCleans up the Rotary Encoder when finishedn/an/a

SevenSegmentDisplay

Note:

This implementation of a Seven Segment Display assumes usage along side of single-digit BCD-to-7-segment decoder circuit (e.g CD4055).

Constructor

const { SevenSegmentDisplay } = require('gpio-components');

const sevenSegmentDisplayConfig = {
  pinZero: 1,
  pinOne: 2,
  pinTwo: 3,
  pinThree: 4
};

const display = new SevenSegmentDisplay(sevenSegmentDisplayConfig);

SevenSegmentDisplayConfig:

PropertyTypeDescription
pinZeronumberThe pin for the display which represents 2^0 digit of the display.
pinOnenumberThe pin for the display which represents 2^1 digit of the display.
pinTwonumberThe pin for the display which represents 2^2 digit of the display.
pinThreenumberThe pin for the display which represents 2^3 digit of the display.

Methods

MethodDescriptionParametersReturns
setDisplaySets the display to number, returning a promise which resolves when the display is set.number<Promise>
getValueReturn the number currently displayedn/anumber
getBinaryValueReturns a binary representation of number currently displayed.n/astring
cleanUpCleans up the Seven Segment Display when finishedn/an/a

Switch

Constructor

const { Switch } = require('gpio-components');

const switchConfig = {
  pin: 1,
  onPress: () => console.log('Pressed!')
};

const mySwitch = new Switch(switchConfig);

SwitchConfig:

PropertyTypeDescription
pinnumberThe GPIO pin for the switch
onPressfunctionThe callback function for when the switch is pressed
switchOptionsobject(Optional) options for the switch. (See below for details)

Methods

MethodDescriptionParametersReturns
watchBegins watching the switch for any interruptionsn/an/a
cleanUpCleans up the switch when finishedn/an/a

switchOptions

OptionTypeDescription
activeLowboolean(Optional) Specify whether or not the values read from or written to the GPIO should be inverted
edge"none" | "rising" | "falling" | "both"(Optional) Specify the interrupt generating edge or edges for the switch
debounceTimeoutnumber(Optional) Specify the number of milliseconds for delaying a callback
reconfigureDirectionboolean(Optional) Specify whether the direction foir the GPIO should be reconfigured even though the direction is configured correctly. See here

Notes

Each component has a function called cleanUp which should only be called when the program is terminated or if the component is no longer expected to be interacted with. Generally, you'd use it like this:

const { LED } = require('gpio-components');

const led = new LED({ pin: 1 });

// ...

process.on('SIGINT', _ => {
  led.cleanUp();
});

Special Thanks

Many thanks to Brian Cooke for his excellent library onoff which without would have made this library not possible -- or at least much more difficult. Also thanks for my friend Gene Crocetti for his help with the hardware-side of this project. Without him as well, this would not have been possible.

1.0.4

3 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago