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

View File

@ -17,23 +17,40 @@
var RED = require("../../red/red"); var RED = require("../../red/red");
var blinkstick = require("blinkstick"); 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) { function BlinkStick(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var p1 = /^#.*/ var p1 = /^\#[A-Za-z0-9]{6}$/
var p2 = /[0-9]+,[0-9]+,[0-9]+/ var p2 = /[0-9]+,[0-9]+,[0-9]+/
this.led = blinkstick.findFirst(); // maybe try findAll() ?
this.led = new blinkstick.findFirst();
var node = this; var node = this;
node.log("started"); node.log("started");
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg != null) { 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))) { if ((p1.test(msg.payload))|(p2.test(msg.payload))) {
node.led.setColor(msg.payload); node.led.setColor(msg.payload);
} }
// TODO - need to handle colour strings...
else { else {
node.error("Incorrect format: "+msg.payload); node.error("Incorrect format: "+msg.payload);
} }
}
else {
node.error("No BlinkStick found");
}
} }
}); });