1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Better detection of BlinkStick - yet to do text colour values

This commit is contained in:
Dave C-J 2013-09-13 12:23:46 +01:00
parent f7bdece081
commit b7179fe373
2 changed files with 22 additions and 5 deletions

View File

@ -31,13 +31,13 @@
<script type="text/javascript">
RED.nodes.registerType('blinkstick',{
category: 'advanced-output',
color:"cornsilk",
color:"GoldenRod",
defaults: {
name: {value:""}
},
inputs:1,
outputs:0,
icon: "arrow-in.png",
icon: "light.png",
align: "right",
label: function() {
return this.name||"blinkstick";

View File

@ -17,24 +17,41 @@
var RED = require("../../red/red");
var blinkstick = require("blinkstick");
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
function BlinkStick(n) {
RED.nodes.createNode(this,n);
var p1 = /^#.*/
var p1 = /^\#[A-Za-z0-9]{6}$/
var p2 = /[0-9]+,[0-9]+,[0-9]+/
this.led = new blinkstick.findFirst();
this.led = blinkstick.findFirst(); // maybe try findAll() ?
var node = this;
node.log("started");
this.on("input", function(msg) {
if (msg != null) {
if (Object.size(blinkstick.findFirst()) !== 0) {
if (p2.test(msg.payload)) {
var rgb = msg.payload.split(",");
node.led.setColor(parseInt(rgb[0]), parseInt(rgb[1]), parseInt(rgb[2]));
}
if ((p1.test(msg.payload))|(p2.test(msg.payload))) {
node.led.setColor(msg.payload);
}
// TODO - need to handle colour strings...
else {
node.error("Incorrect format: "+msg.payload);
}
}
else {
node.error("No BlinkStick found");
}
}
});
}