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

Let Blinkstick accept upper case colours

This commit is contained in:
Dave C-J 2013-10-20 20:37:01 +01:00
parent 778e0d2086
commit e775346946
2 changed files with 55 additions and 54 deletions

View File

@ -23,27 +23,28 @@
</script>
<script type="text/x-red" data-help-name="blinkstick">
<p>BlinkStick output node. Expects a <b>msg.payload</b> with either a hex string #rrggbb triple or red,green,blue as three 0-255 values.</p>
<p><b>NOTE:</b> currently only works with a single BlinkStick. (As it uses the findFirst() function to attach).</p>
<p>For more info see the <i><a href="http://blinkstick.com/" target="_new">BlinkStick website</a></i> or the <i><a href="https://github.com/arvydas/blinkstick-node" target="_new">node module</a></i> documentation.</p>
<p>BlinkStick output node. Expects a <b>msg.payload</b> with either a hex string #rrggbb triple or red,green,blue as three 0-255 values.
It can also accept <i><a href="http://www.w3schools.com/html/html_colornames.asp" target="_new">standard HTML colour</a></i> names</p>
<p><b>NOTE:</b> currently only works with a single BlinkStick. (As it uses the findFirst() function to attach).</p>
<p>For more info see the <i><a href="http://blinkstick.com/" target="_new">BlinkStick website</a></i> or the <i><a href="https://github.com/arvydas/blinkstick-node" target="_new">node module</a></i> documentation.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('blinkstick',{
category: 'output',
color:"GoldenRod",
defaults: {
name: {value:""}
},
inputs:1,
outputs:0,
icon: "light.png",
align: "right",
label: function() {
return this.name||"blinkstick";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
RED.nodes.registerType('blinkstick',{
category: 'output',
color:"GoldenRod",
defaults: {
name: {value:""}
},
inputs:1,
outputs:0,
icon: "light.png",
align: "right",
label: function() {
return this.name||"blinkstick";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

View File

@ -18,44 +18,44 @@ 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;
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 = /^\#[A-Fa-f0-9]{6}$/
var p2 = /[0-9]+,[0-9]+,[0-9]+/
this.led = blinkstick.findFirst(); // maybe try findAll() (one day)
var node = this;
RED.nodes.createNode(this,n);
var p1 = /^\#[A-Fa-f0-9]{6}$/
var p2 = /[0-9]+,[0-9]+,[0-9]+/
this.led = blinkstick.findFirst(); // maybe try findAll() (one day)
var node = this;
this.on("input", function(msg) {
if (msg != null) {
if (Object.size(node.led) !== 0) {
try {
if (p2.test(msg.payload)) {
var rgb = msg.payload.split(",");
node.led.setColor(parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255);
}
else {
node.led.setColor(msg.payload);
}
}
catch (err) {
node.warn("BlinkStick missing ?");
node.led = blinkstick.findFirst();
}
}
else {
//node.warn("No BlinkStick found");
node.led = blinkstick.findFirst();
}
}
});
if (Object.size(node.led) === 0) {
node.error("No BlinkStick found");
}
this.on("input", function(msg) {
if (msg != null) {
if (Object.size(node.led) !== 0) {
try {
if (p2.test(msg.payload)) {
var rgb = msg.payload.split(",");
node.led.setColor(parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255);
}
else {
node.led.setColor(msg.payload.toLowerCase().replace(/\s+/g,''));
}
}
catch (err) {
node.warn("BlinkStick missing ?");
node.led = blinkstick.findFirst();
}
}
else {
//node.warn("No BlinkStick found");
node.led = blinkstick.findFirst();
}
}
});
if (Object.size(node.led) === 0) {
node.error("No BlinkStick found");
}
}