RPiTD Library
npm install rpitd
Pin numbers used by this library are the Broadcom numbers; that is, those used by Linux (NOT the physical pin numbers).
Note that the LEDArray and I2C classes have not been tested and thus are not guaranteed to work.
Future versions of this library will support SPI communication and may offer improved support for interrupts.
This JavaScript file was compiled from the original TypeScript source file using http://www.typescriptlang.org/Playground
Author: Rebecca Horzewski
Date: 2015-12-04
API:
DigitalInput A digital input which can have state 0 or 1 and handle interrupts on rising and/or falling edge
constructor: var dev = new DigitalInput(pin) //pin: pin number of the gpio
read() //returns the number (0 or 1) read from the device
readAsync(callback) //callback(error, value): function to call when the read completes, receives an error object (undefined if no error) and the value read (0 or 1)
getDirection() //returns the direction of this gpio pin; that is, “in”
getEdge() //returns the edge that interrupts are detected on; may be "rising", "falling", "both", or "none"
setEdge(edge) //set the edge that interrupts are detected on; may be "rising", "falling", "both", or "none"
addCallback(callback) //add a callback function so it will be called when an interrupt occurs //callback(error, state): the function to add, receives an error object (undefined if no error) and the current state of the gpio (0 or 1)
removeCallback(callback) //remove a callback function so it will no longer be called when an interrupt occurs //callback(error, state): the function to remove
removeAllCallbacks() //remove all callback functions so nothing is called on any edge
close() //close this gpio pin; attempting to use the pin after closing it may crash the program
DigitalOutput A digital output that can have states 0 or 1
constructor var dev = new DigitalOutput(pin, state); //pin: pin number the device is on //state: initial state of the device (0 or 1)
high() //set the output high
low() { //set the output low
highAsync(callback) //asynchronously set the output high //callback(error): function to call when the write completes, receives an error object (undefined if no error)
lowAsync(callback) //asynchronously set the output low //callback(error): function to call when the write completes, receives an error object (undefined if no error)
read() //read from the gpio //returns the state of the gpio (0 or 1)
readAsync(callback) //asynchronously read function //callback(error, value): function to call when the read completes, receives an error object (undefined if no error), and the value read (0 or 1)
getDirection() //check the current direction of this device //should always return "out"
close() //close this pin (unexport the gpio) //trying to use the gpio after closing it may crash the program
LED A digital output that can have states 0 or 1 Inherits all methods from DigitalOutput
constructor var dev = new LED(pin, state); //pin: pin number the LED is on //state: initial state of the LED (0 or 1)
on() //turn the LED on (set the output high)
off() //turn the LED off (set the output low)
onAsync(callback) //asynchronously turn the LED on (set the output high) //callback(error): function to call when the write completes, receives an error object (undefined if no error)
offAsync(callback = undefined) //asynchronously turn the LED off (set the output low) //callback(error): function to call when the write completes, receives an error object (undefined if no error)
LEDArray (NOT TESTED) An array of digital outputs that can have states 0 or 1
constructor var dev = new LEDArray(numLEDs, pins) //numLEDs: the number of LEDs in the array //pins: an array holding the pin numbers of the LEDs in this array, in the order they should appear in the array
startLarsonScanner(milliseconds) //start a larson scanner with the given number of milliseconds delay between steps
BinaryDevice A general object for a gpio pin that can handle switching between input and output Note that the device can only be written to while it is an output, and interrupts will only be detected while the device is an input However, callbacks can be registered and removed at any time, and all currently registered callbacks will be possible again when the device is set back to an input
constructor var dev = new BinaryDevice(pin, direction) //pin: pin number the gpio is on //direction: "in" or "out"
getDirection() //check the direction of this gpio pin //returns "in" or "out"
setDirection(direction) //change the direction of this gpio pin //direction: "in" or "out" //returns 0 on success or -1 on error
setEdge(edge) //set the edge that interrupts on this pin are detected on //edge: "rising", "falling", "both", or "none"
write(value) //write to the gpio (only valid while the device is an output) //value: the value to write (0 or 1) //returns 0 on success, -1 on error
writeAsync(value, callback) //asynchronous write to the gpio (only valid while the device is an output) //value: the value to write (0 or 1) //callback(error): function to call when the write completes, receives an error object (undefined if no error)
PWM A class for pwm output
setDutyCycle(dutyCycle) //duty cycle: 0-100, duty cycle in percent of full period
getDutyCycle() //returns 0-100, duty cycle in percent of full period
Motor A specialized PWM output for controlling a motor
constructor var dev = new Motor(pwmPin,, forwardPin, backwardPin) //pwmPin: pin the pwm output is on //forwardPin: pin the "forward" output is on //backwardPin: pin the "backward" output is on
getDirection() //get the direction this motor is set to turn in //returns "forward" or "backward"
setDirection(direction) //direction: "forward" or "backward" //returns 0 upon success and -1 for error
getSpeed() //returns speed as percent of max
setSpeed(speed) //speed: 0-100, a percent of max
write(direction, speed) //set both direction and speed at the same time; if direction fails to set, speed will not be set //direction: "forward" or "backward" //speed: 0-100, a percent of max speed
I2C (NOT TESTED) An I2C bus that allows communication with any device on the bus
constructor var dev = new I2C(busNumber) //busNumber: the number of the I2C bus/adapter to open, 0 for /dev/i2c-0, 1 for /dev/i2c-1, etc
scan() //returns an array of numbers where each number represents the I2C address of a device which was detected
scanAsync(callback) //callback(error, devices) where devices is the number array that scan would return
read(address, length, buffer) //address: address of the device to be read from //length: number of bytes to be read //buffer: data buffer where the read data will be stored //returns the number of bytes read
readAsync(address, length, buffer, callback) //address: address of the device to be read from //length: number of bytes to be read //buffer: data buffer where the read data will be stored //callback(error, numBytesRead, buffer)
write(address, length, buffer) //address: address of the device to be written to //length: number of bytes to be written //buffer: data buffer where the data to write can be found //returns the number of bytes written
writeAsync(address, length, buffer, callback) //address: address of the device to be written to //length: number of bytes to be written //buffer: data buffer where the data to write can be found //callback(error, numBytesWritten, buffer)