diff --git a/README.md b/README.md index 837c2f0..c5e83a4 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,38 @@ -LPD8806-node -============ +# lpd8806-async -Install. +Asynchronous Node.js library that implements the LPD8806 protocol. - npm install lpd8806 +## Install - var LPD8806 = require('LPD8806'); - LPD8806 = new LPD8806(32, '/dev/spidev0.0'); - LPD8806.fillRGB(0, 0, 255); + npm install lpd8806-async - Available Funtions: +## Run + +``` + var LPD8806 = require('lpd8806-async'); + var ledstrip = new LPD8806(32, '/dev/spidev1.0'); + + ledstrip.fillRGB(200,200,200); + + //Available Funtions: + + ledstrip.updateBrightness(0.5); //Value must be between 0.0 and 1.0 + ledstrip.update(); //To write the new Buffer + ledstrip.fillRGB(0, 0, 255); //RED, GREEN, BLUE + ledstrip.fillHSV(0.0, 100.0, 100.0); //HSV Values + ledstrip.allOFF(); + ledstrip.setPixel(new Color({r: 0, g: 0: b: 255})); + ledstrip.setPixelRGB(0, 0, 255); //RED, GREEN, BLUE + ledstrip.setPixelHSV(0.0, 100.0, 100.0) //HSV Values + ledstrip.setPixelOff(pixelNumber); +``` + +Check /examples for usage examples. +Feel free to contribute. + + +Special thanks to [pmdroid](https://github.com/pmdroid) - LPD8806.updateBrightness(0.5); //Value must Between 0.0 and 1.0 - LPD8806.update(); //To write the new Buffer - LPD8806.fillRGB(0, 0, 255); //ROT, GREEN, BLUE - LPD8806.fillHSV(0.0, 100.0, 100.0); //HSV Values - LPD8806.allOFF(); - LPD8806.setPixel(new Color({r: 0, g: 0: b: 255})); - LPD8806.setPixelRGB(0, 0, 255); //ROT, GREEN, BLUE - LPD8806.setPixelHSV(0.0, 100.0, 100.0) //HSV Values - LPD8806.setPixelOff(pixelNumber); The MIT License (MIT) diff --git a/example.js b/example.js deleted file mode 100644 index 5775dd6..0000000 --- a/example.js +++ /dev/null @@ -1,9 +0,0 @@ -var LPD8806 = require('./lib/LPD8806'), - Animations = require('./lib/Animations') - -LPD8806 = new LPD8806(32, '/dev/spidev0.0'); -Animations = new Animations(32); - -Animations.Colors(LPD8806, 0.01); -Animations.Rainbow(LPD8806); -LPD8806.allOFF(); \ No newline at end of file diff --git a/examples/flash.js b/examples/flash.js new file mode 100644 index 0000000..6244442 --- /dev/null +++ b/examples/flash.js @@ -0,0 +1,43 @@ +var LPD8806 = require('LPD8806'); + +// npm install async +var async = require("async"); + +var leds = new LPD8806(96, '/dev/spidev1.0'); + +// Flash ledstrip by manipulation of the color brightness +function flash(r, g, b, speed){ + var i = 0; + var step = speed; + + function performStep(){ + var level = 0.01, + dir = step; + + async.whilst(function(){ + return (level >= 0.0); + },function (callback) { + setTimeout(function(){ + leds.setMasterBrightness(level); + leds.fillRGB(r,g,b); + if(level >= 0.99){ + dir =- step; + } + level += dir; + callback(); + },4); + }, function (err) { + return; + }); + } + performStep(); +} + + +// Fade color +flash(130,16,233, 0.01); + +// Flash color +setTimeout(function(){ + flash(130,16,233, 0.09); +}, 20000); \ No newline at end of file diff --git a/examples/rainbow.js b/examples/rainbow.js new file mode 100644 index 0000000..0aa62dd --- /dev/null +++ b/examples/rainbow.js @@ -0,0 +1,36 @@ +var LPD8806 = require('LPD8806'); +var ledCount = 96; +var leds = new LPD8806(ledCount, '/dev/spidev1.0'); + +/** process.nextTick to avoid blocking it all the time */ +function rainbow(brightness){ + + var _step = 0; + var start = 0; + + brightness = brightness || 1.0; + leds.setMasterBrightness(brightness); + var i = 0; + + function performStep() { + var amt = 1; + for(var p = 0; p < ledCount; p++){ + var color = (p + _step) % 384; + leds.setPixel(start + p, leds.wheel_color(color)); + } + leds.update(); + _step += amt; + var overflow = _step - 384; + if(overflow >= 0){ + _step = overflow; + } + + if (++i >= 384) { + return; + } + setTimeout(performStep, 80); + } + performStep(); +} + +rainbow(0.8); \ No newline at end of file diff --git a/lib/Animations.js b/lib/Animations.js deleted file mode 100644 index a7ae987..0000000 --- a/lib/Animations.js +++ /dev/null @@ -1,51 +0,0 @@ -var Color = require("color"), - sleep = require('sleep'); - -var ledCount = 32; - _step = 0, - start = 0; - -var Animations = function(leds){ - ledCount = leds || ledCount; -} - -Animations.prototype.Colors = function(LPD8806, step){ - var colors = [ - {r:255,g:0,b:0}, - {r:0,g:255,b:0}, - {r:0,g:0,b:255} - ]; - - for(var i =0; i < 4; i++){ - var level = 0.01, - dir = step; - while(level >= 0.0){ - LPD8806.setMasterBrightness(level); - LPD8806.fill(new Color(colors[i])); - if(level >= 0.99){ - dir =- step; - } - level += dir; - sleep.usleep(10000); - } - } -} - -Animations.prototype.Rainbow = function(LPD8806){ - for(var i = 0; i < 384; i++){ - var amt = 1; - for(var p = 0; p < ledCount; p++){ - var color = (p + _step) % 384; - LPD8806.setPixel(start + p, LPD8806.wheel_color(color)); - } - LPD8806.update(); - sleep.usleep(20000); - _step += amt; - var overflow = _step - 384; - if(overflow >= 0){ - _step = overflow; - } - } -} - -module.exports = Animations; \ No newline at end of file diff --git a/lib/LPD8806.js b/lib/LPD8806.js index 93872c4..05c97db 100644 --- a/lib/LPD8806.js +++ b/lib/LPD8806.js @@ -13,20 +13,18 @@ var ledCount = 32, c_order = ChannelOrder.GRB, masterBrightness = 1.0, buffer = [], - gamma = new Buffer(256), - spi = new SPI.Spi(device, { - 'mode': SPI.MODE.MODE_0, - 'chipSelect': SPI.CS.none, - 'maxSpeed': 20000000 - }, function(s){ - s.open(); - }); - -var LPD8806 = function(leds, dev){ + gamma = new Buffer(256); + +var LPD8806 = function(leds, dev, options){ + options = options || {}; ledCount = leds || ledCount; device = dev || device; + this.spi = new SPI.Spi(device, options, function(s){ + s.open(); + }); + for(var i = 0; i < ledCount; i++){ buffer[i] = new Buffer([0x80, 0x80, 0x80]); } @@ -77,7 +75,7 @@ LPD8806.prototype.update = function(){ var _buffer = buffer.slice(0, buffer.length); _buffer.push(new Buffer([0x00, 0x00, 0x00])); _buffer.push(new Buffer([0x00])); - spi.write(Buffer.concat(_buffer)); + this.spi.write(Buffer.concat(_buffer)); }; @@ -158,4 +156,4 @@ LPD8806.prototype.wheel_color = function(wheelpos){ return new Color({r: r, g: g, b: b}); }; -module.exports = LPD8806; \ No newline at end of file +module.exports = LPD8806; diff --git a/package.json b/package.json index 57dd41e..75c1028 100644 --- a/package.json +++ b/package.json @@ -1,22 +1,17 @@ { - "name": "lpd8806", - "version": "0.2.0", + "name": "lpd8806-async", + "version": "0.2.2", "private": false, - "scripts": { - "start": "node example.js" - }, "dependencies": { "spi": "*", - "color": "*", - "sleep": "*", - "socket.io": "*" + "color": "*" }, - "description": "LPD8806 Node library for the Raspberry Pi", + "description": "Non-blocking LPD8806 NodeJS general library", "main": "./lib/LPD8806", "devDependencies": {}, "repository": { "type": "git", - "url": "https://github.com/pmdroid/LPD8806-node" + "url": "https://github.com/muzzley/LPD8806-node" }, "keywords": [ "SPI", @@ -28,9 +23,9 @@ "Raspberry", "PI" ], - "author": "pmdroid", + "author": "pmdroid, v0od0oChild", "license": "MIT License", "bugs": { - "url": "https://github.com/pmdroid/LPD8806-node/issues" + "url": "https://github.com/muzzley/LPD8806-node/issues" } } diff --git a/server.js b/server.js deleted file mode 100644 index c84e25c..0000000 --- a/server.js +++ /dev/null @@ -1,14 +0,0 @@ -var io = require('socket.io').listen(80); -var LPD8806 = require('LPD8806'), - Color = require('color'); - -LPD8806 = new LPD8806(); - -io.sockets.on('connection', function (socket) { - socket.on('setColor', function (data) { - LPD8806.fill(new Color({r: data.red, g: data.green, b: data.blue})); - }); - socket.on('setBrightness', function (data) { - LPD8806.updateBrightness(parseFloat(data.brightness)); - }); -}); \ No newline at end of file