npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ws2801-connect

v1.2.2

Published

A javascript class to control a WS2801 led stripe

Downloads

27

Readme

WS2801-Connect

Codacy Badge Coverage Status npm npm npm

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 = require("ws2801-connect")

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 communication
var leds = new WS2801({ 
   count: 32, 
   spiWrite: (data) => { spi.write(data) }
})

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:

leds.clear()

Fill

Fill all led lights with one color:

leds.fill("#FF0000") //fill with red
leds.fill(0, 255, 0) //fill with green
leds.fill([0x00, 0x00, 0xff]) //fill with blue

Set single light

Set the color of a single led light by index (starting with 0):

leds.setLight(0, "#FF0000") //set first light to red
leds.setLight(1, 0, 255, 0) //set second light to green
leds.setLight(2, [0x00, 0x00, 0xff]) //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 blue
leds.setAll([
   [ 255, 0, 0 ],
   "#00ff00",
   "rgb(0, 0, 255)"
])

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.show()

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 = new WS2801({
   count: 32, 
   spiWrite: (data) => { spi.write(data) },
   rgbIndex: [0,2,1]
})

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 npm

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 = require("sleep")
var SoftSPI = require("rpi-softspi")
var WS2801 = require("ws2801-connect")

/* 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 = new SoftSPI({
   clock: 5, // GPIO 3 - SCL
   mosi: 3 // GPIO 2 - SDA
})
spi.open()

// the led stripe has 32 lights; supply callback as lambda
var leds = new WS2801({
   count: 32, 
   spiWrite: (data) => { spi.write(data) })
})

// first make all lights black
leds.clear().show()
sleep(1) // wait a second
// next fill red
leds.fill("#FF0000").show()
sleep(1)
// fill green
leds.fill(0, 255, 0).show()
sleep(1)
// fill blue
leds.fill([0x00, 0x00, 0xff]).show()
sleep(1)
// and black again
leds.clear().show()

// end SPI communication
spi.close()

rpio NPM version

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 = require("rpio")
var WS2801 = require("ws2801-connect")

// initiate SPI and begin communication
rpio.spiBegin()
// max. 25 MHz
rpio.spiSetClockDivider(10)
rpio.spiSetDataMode(0)

// the led stripe has 32 lights; supply callback as lambda
var leds = new WS2801({
   count: 32, 
   spiWrite: (data) => { 
                           let buf = Buffer.from(data)
                           rpio.spiWrite(buf, buf.length)
                        }
})

// first make all lights black
leds.clear().show()
rpio.sleep(1) // wait a second
// next fill red
leds.fill("#FF0000").show()
rpio.sleep(1)
// fill green
leds.fill(0, 255, 0).show()
rpio.sleep(1)
// fill blue
leds.fill([0x00, 0x00, 0xff]).show()
rpio.sleep(1)
// and black again
leds.clear().show()

// release SPI
rpio.spiEnd()

API

Check out the documentation for details.

License

MIT