WS2801-Connect
This library contains a javascript class, which represents a WS2801 led stripe connected via SPI. Whereby the SPI implementation is open and not necessarely coupled to the hardware SPI of your controler device of choice.
Precondition
The library is implemented in ECMAScript 2015, so your project should support at least this version.
Installation
Install it via npm:
$ npm install ws2801-connect
Usage
Start with importing the module via:
var WS2801 =
Create an instance providing the number of led lights and a callback function for the communication with the SPI:
// 32 led lights, callback with SPI communicationvar leds = count: 32 { spi }
The callback gets an array as parameter containing the data to be sent to the SPI and finally to the led stripe. See Examples to get an idea how it works.
Clear
Make all led lights black with:
ledsclear
Fill
Fill all led lights with one color:
leds //fill with redleds //fill with greenleds //fill with blue
Set single light
Set the color of a single led light by index (starting with 0):
leds //set first light to redleds //set second light to greenleds //set third light to blue
Set all lights
Set possibly all led lights by provided an array with color information:
// set first three lights with red, green and blueleds
Consider
- If you supply less colors as led lights you have, the rest lights will be cleared
- If you supply more colors as led lights you have, the superfluous are ignored
Show lights on hardware
Send finally your color configuration to the led stripe:
leds
Consider: After you have changed your color configuration through any of the above modifying methods, you have to call
show()
to send the new configuration to the led stripe i.e. to call the callback function you specified at construction.
Change RGB order
If your led stripe has another order as red, green, blue (mine have red, blue, green) you can specify on construction another option to define the RGB index:
var leds = count: 32 { spi } rgbIndex: 021
In this example I used my configuration: first red, second blue, third green. This has only an impact for the resulting data you will get at the callback function.
Hint
On specifying colors you can use any format, which the color library supports with its constructors.
Examples
rpi-softspi
Software implementation of SPI to use any pin for the interface on Raspberry Pi instead of using the hardware SPI of the RPi.
First you have to instantiate the SPI representation and open the communication.
In the callback for the WS2801 you have to call the write()
method.
var sleep = var SoftSPI = var WS2801 = /* we only need clock and mosi for the WS2801 led stripe * for all other options of SoftSPI we can use the default * begin SPI communication immediately */var spi = clock: 5 // GPIO 3 - SCL mosi: 3 // GPIO 2 - SDAspi // the led stripe has 32 lights; supply callback as lambdavar leds = count: 32 { spi }) // first make all lights blackledsclear // wait a second// next fill redleds// fill greenleds// fill blueleds// and black againledsclear // end SPI communicationspi
rpio
High performance node.js addon which provides access to the Raspberry Pi GPIO interface including SPI
First you have to initiate the SPI communication with rpio.
In the callback for the WS2801 you have to call the spiWrite()
method.
var rpio = var WS2801 = // initiate SPI and begin communicationrpio// max. 25 MHzrpiorpio // the led stripe has 32 lights; supply callback as lambdavar leds = count: 32 { let buf = Buffer rpio } // first make all lights blackledsclearrpio // wait a second// next fill redledsrpio// fill greenledsrpio// fill blueledsrpio// and black againledsclear // release SPIrpio
API
Check out the documentation for details.
License
MIT