:pager: Raspberry-Pi PWM GPIO adapter with ES6 Promises.
Control Raspberry Pi GPIO pins with Node.js and ES6 Promises.
This module can then be installed with npm:
# npm install @jdes/gpio # unavailable during beta
npm install git://github.com/jeandesravines/gpio
!! IMPORTANT !! You must run your application as root to use the Raspberry’s GPIO.
Import module:
/**
* @class {Gpio}
*/
const Gpio = require('@jdes/gpio');
Instantiate:
/**
* @type {Gpio}
*/
const gpio = new Gpio();
Before all operations on channel, you have to open it with gpio.open(channel: number, direction: string): Promise
Example:
// Import
const Gpio = require('@jdes/gpio');
// Instantiate
const gpio = new Gpio();
// Open the channel 7 on write mode
// and write an analog value
gpio.open(7, Gpio.direction.out)
.then(() => gpio.setAnalogValue(7, 0.75));
// Open the channel 3 on read mode
// and read an analog value during 500ms
gpio.open(3, Gpio.direction.in)
.then(() => gpio.getAnalogValue(3, 500))
.then((value) => {
console.log(`Current value: ${value}`);
});
// Close after 5s
setTimeout(() => {
gpio.close(7);
gpio.close(5);
}, 5000);
Object representing the available directions
{
"in": "in",
"out": "in"
}
Object representing the available signals
{
"low": 0,
"high": 1
}
channel
: Reference to the pin in the current mode’s schema.direction
: The pin direction, pass either Gpio.direction.in
for read mode or Gpio.direction.out
for write mode.Promise
resolved if the channel was opened or rejected if an error occursSets up a channel for read or write. Must be done before the channel can be used.
Example:
gpio.open(7, Gpio.direction.out)
.then(() => {
console.log('channel 7 opened');
});
channel
: Reference to the pin in the current mode’s schema.Promise
resolved if channel was closed or rejected if an error occursClose a channel
Example:
gpio.close(7)
.then(() => {
console.log('Channel 7 closed');
});
channel
: Reference to the pin in the current mode’s schema.Promise.<number>
resolvde with the current value or rejected if an error occursReads a digital value of a channel.
Example:
gpio.getValue(7)
.then((value) => {
console.log(`Channel 7's value: ${value}`);
});
channel
: Reference to the pin in the current mode’s schema.value
: Boolean value to specify whether the channel will set to Gpio.signal.low
or Gpio.signal.high
.Promise
resolved if the value was set or rejected if an error occursWrites a digital value to a channel.
Example:
gpio.setValue(7, Gpio.signal.high);
channel
: Reference to the pin in the current mode’s schema.duration
: The duration (in ms) of computingPromise.<number>
resolved with the current value or rejected if an error occursReads an analog value (float) from a channel.
Example:
gpio.getAnalogValue(7)
.then((value) => {
console.log(`Channel 7 value: ${value}`);
});
channel
: Reference to the pin in the current mode’s schema.value
: The analog value. A float number value ∈ [0, 1]
Promise
resolved if the value was set or rejected if an error occursWrites an analog value (float) to a channel.
Example:
gpio.setAnalogValue(7, 0.75);
channel
: Reference to the pin in the current mode’s schema.Gets the direction of a channel.
The resolved Promise.<string>
will be returned with the current direction (a Gpio.direction
value)
Example:
gpio.getDirection(7)
.then((direction) => {
console.log(direction === Gpio.direction.out);
});
channel
: Reference to the pin in the current mode’s schema.direction
: The pin direction, pass either Gpio.direction.in
for read mode or Gpio.direction.out
for write mode.Promise
resolved if the direction was set or rejected if an error occursChanges the direction of a channel for read or write.
Example:
gpio.setDirection(7, Gpio.direction.out)
.then(() => {
console.log('Direction set`on channel 7');
});
const Gpio = require('@jdes/gpio');
const gpio = new Gpio();
// Open the channel 3 in read mode
gpio.open(3, Gpio.direction.in)
.then(() => {
// Reads an analog value every seconds
const interval = setInterval(() => {
gpio.getAnalogValue(3)
.then((value) => {
console.log(`Current value: ${value}`);
});
}, 1000);
// After 10 seconds, stop reading and close channel
setTimeout(() => {
clearInterval(interval);
gpio.close(3);
}, 10000);
});
Environment variables can be passed to override the default configuration.
Time in ms for computing an analog read.
If PI_GPIO_FRAME = 1
, at each ms à digital read is perform and at the end, an average value of each digital read will be produce.
If the process has to be executed during 200ms, 200 reads will be performed and the analog value will be equal to countOn / 200
.
PI_GPIO_FRAME
Number
1
The default Rapsberry Pi revision if none match.
It’s useful to determine the GPIO mapping.
PI_GPIO_REVISION
Number
3
Frequency in Hz for execute an analog write.
If PI_GPIO_FREQUENCY = 50
, during a write, 20 (1000 / 50) on-off process will be performed.
PI_GPIO_FREQUENCY
Number
120
If PI_GPIO_FREQUENCY = 50
and the value to write is 0.75
, the on-off process will be performed 20 times.
Every 20ms it writes 1
to the pin and after 15ms (20 * 0.75) it will writes 0
.