ws2801-connect
Version:
A javascript class to control a WS2801 led stripe
214 lines (169 loc) • 8.5 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Home</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Home</h1>
<h3> </h3>
<section>
<article><h1>WS2801-Connect</h1><p><a href="https://www.codacy.com/app/Vertumnus/js-ws2801-connect?utm_source=github.com&utm_medium=referral&utm_content=Vertumnus/js-ws2801-connect&utm_campaign=Badge_Grade"><img src="https://api.codacy.com/project/badge/Grade/ce88ce0410ee4e6cb2182ead6ebd4acf" alt="Codacy Badge"></a>
<a href="https://coveralls.io/github/Vertumnus/js-ws2801-connect?branch=master"><img src="https://coveralls.io/repos/github/Vertumnus/js-ws2801-connect/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="https://www.npmjs.com/package/ws2801-connect"><img src="https://img.shields.io/npm/dt/ws2801-connect.svg" alt="npm"></a>
<a href="https://www.npmjs.com/package/ws2801-connect"><img src="https://img.shields.io/npm/v/ws2801-connect.svg" alt="npm"></a>
<a href="https://www.npmjs.com/package/ws2801-connect"><img src="https://img.shields.io/npm/l/ws2801-connect.svg" alt="npm"></a></p>
<p>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.</p>
<h2>Precondition</h2><p>The library is implemented in ECMAScript 2015, so your project should support
at least this version.</p>
<h2>Installation</h2><p>Install it via npm:</p>
<pre class="prettyprint source lang-shell"><code>$ npm install ws2801-connect</code></pre><h2>Usage</h2><p>Start with importing the module via:</p>
<pre class="prettyprint source lang-js"><code>var WS2801 = require("ws2801-connect")</code></pre><p>Create an instance providing the number of led lights and a callback function
for the communication with the SPI:</p>
<pre class="prettyprint source lang-js"><code>// 32 led lights, callback with SPI communication
var leds = new WS2801({
count: 32,
spiWrite: (data) => { spi.write(data) }
})</code></pre><blockquote>
<p>The callback gets an array as parameter containing the data to be sent to
the SPI and finally to the led stripe.
See <a href="#examples">Examples</a> to get an idea how it works.</p>
</blockquote>
<h3>Clear</h3><p>Make all led lights black with:</p>
<pre class="prettyprint source lang-js"><code>leds.clear()</code></pre><h3>Fill</h3><p>Fill all led lights with one color:</p>
<pre class="prettyprint source lang-js"><code>leds.fill("#FF0000") //fill with red
leds.fill(0, 255, 0) //fill with green
leds.fill([0x00, 0x00, 0xff]) //fill with blue</code></pre><h3>Set single light</h3><p>Set the color of a single led light by index (starting with 0):</p>
<pre class="prettyprint source lang-js"><code>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</code></pre><h3>Set all lights</h3><p>Set possibly all led lights by provided an array with color information:</p>
<pre class="prettyprint source lang-js"><code>// set first three lights with red, green and blue
leds.setAll([
[ 255, 0, 0 ],
"#00ff00",
"rgb(0, 0, 255)"
])</code></pre><blockquote>
<p>Consider</p>
<ul>
<li>If you supply less colors as led lights you have, the rest lights will be cleared</li>
<li>If you supply more colors as led lights you have, the superfluous are ignored</li>
</ul>
</blockquote>
<h3>Show lights on hardware</h3><p>Send finally your color configuration to the led stripe:</p>
<pre class="prettyprint source lang-js"><code>leds.show()</code></pre><blockquote>
<p><strong>Consider:</strong> After you have changed your color configuration through
any of the above modifying methods, you have to call <code>show()</code> to send
the new configuration to the led stripe i.e. to call the callback function
you specified at construction.</p>
</blockquote>
<h3>Change RGB order</h3><p>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:</p>
<pre class="prettyprint source lang-js"><code>var leds = new WS2801({
count: 32,
spiWrite: (data) => { spi.write(data) },
rgbIndex: [0,2,1]
})</code></pre><blockquote>
<p>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.</p>
</blockquote>
<h3>Hint</h3><p>On specifying colors you can use any format, which the <a href="https://www.npmjs.com/package/color">color library</a>
supports with its <a href="https://www.npmjs.com/package/color#constructors">constructors</a>.</p>
<h2>Examples</h2><h3>rpi-softspi <a href="https://www.npmjs.com/package/rpi-softspi"><img src="https://img.shields.io/npm/v/rpi-softspi.svg" alt="npm"></a></h3><blockquote>
<p>Software implementation of SPI to use any pin for the interface on Raspberry Pi
instead of using the hardware SPI of the RPi.</p>
</blockquote>
<p>First you have to instantiate the SPI representation and open the communication.
In the callback for the WS2801 you have to call the <code>write()</code> method.</p>
<pre class="prettyprint source lang-js"><code>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()</code></pre><h3>rpio <a href="http://badge.fury.io/js/rpio"><img src="https://badge.fury.io/js/rpio.svg" alt="NPM version"></a></h3><blockquote>
<p>High performance node.js addon which provides access to the Raspberry Pi GPIO interface
including SPI</p>
</blockquote>
<p>First you have to initiate the SPI communication with rpio.
In the callback for the WS2801 you have to call the <code>spiWrite()</code> method.</p>
<pre class="prettyprint source lang-js"><code>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()</code></pre><h2>API</h2><p>Check out the <a href="doc">documentation</a> for details.</p>
<h2>License</h2><p>MIT</p></article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="WS2801.html">WS2801</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Thu Apr 12 2018 23:52:30 GMT+0200 (Mitteleuropäische Sommerzeit)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>