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

Made Blink1 and BlinkStick msg.payloads consistent so both accept hex #rrggbb or decimal rrr,ggg,bbb. (This will also break any existing Blink1 deployments...)

This commit is contained in:
Dave C-J 2013-09-15 11:35:56 +01:00
parent 77eb0f76ce
commit f8988ca583
4 changed files with 43 additions and 27 deletions

View File

@ -30,7 +30,7 @@
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('blinkstick',{ RED.nodes.registerType('blinkstick',{
category: 'advanced-output', category: 'output',
color:"GoldenRod", color:"GoldenRod",
defaults: { defaults: {
name: {value:""} name: {value:""}

View File

@ -19,9 +19,7 @@ var blinkstick = require("blinkstick");
Object.size = function(obj) { Object.size = function(obj) {
var size = 0, key; var size = 0, key;
for (key in obj) { for (key in obj) { if (obj.hasOwnProperty(key)) size++; }
if (obj.hasOwnProperty(key)) size++;
}
return size; return size;
}; };
@ -32,31 +30,30 @@ function BlinkStick(n) {
this.led = blinkstick.findFirst(); // maybe try findAll() (one day) this.led = blinkstick.findFirst(); // maybe try findAll() (one day)
var node = this; var node = this;
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 (Object.size(node.led) !== 0) {
if (p2.test(msg.payload)) { if (p2.test(msg.payload)) {
var rgb = msg.payload.split(","); var rgb = msg.payload.split(",");
node.led.setColor(parseInt(rgb[0]), parseInt(rgb[1]), parseInt(rgb[2])); node.led.setColor(parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255);
}
else if ((p1.test(msg.payload))|(p2.test(msg.payload))) {
node.led.setColor(msg.payload);
} }
else { else {
try { try {
node.led.setColor(msg.payload); node.led.setColor(msg.payload);
} }
catch (err) { catch (err) {
node.error("Incorrect format: "+msg.payload); node.warn("Incorrect format: "+msg.payload);
} }
} }
} }
else { else {
node.error("No BlinkStick found"); node.warn("No BlinkStick found");
} }
} }
}); });
if (Object.size(node.led) === 0) {
node.error("No BlinkStick found");
}
} }

View File

@ -14,7 +14,11 @@
limitations under the License. limitations under the License.
--> -->
<script type="text/x-red" data-template-name="blink"> <script type="text/x-red" data-template-name="blink1">
<div class="form-row">
<label for="node-input-fade"><i class="icon-signal"></i> Fade (mS)</label>
<input type="text" id="node-input-fade" placeholder="0">
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
@ -22,15 +26,16 @@
<div class="form-tips">Expects a msg.payload with three part csv string of r,g,b.</div> <div class="form-tips">Expects a msg.payload with three part csv string of r,g,b.</div>
</script> </script>
<script type="text/x-red" data-help-name="blink"> <script type="text/x-red" data-help-name="blink1">
<p>Thingm Blink1 output node. Expects a msg.payload with a three part csv string of r,g,b.</p> <p>Thingm Blink1 output node. Expects a msg.payload with a three part csv string of r,g,b.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('blink',{ RED.nodes.registerType('blink1',{
category: 'output', category: 'output',
color:"GoldenRod", color:"GoldenRod",
defaults: { defaults: {
fade: {value:"0",required:true,validate:RED.validators.number()},
name: {value:""} name: {value:""}
}, },
inputs:1, inputs:1,

View File

@ -19,28 +19,42 @@ var Blink1 = require("node-blink1");
function Blink1Node(n) { function Blink1Node(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.fade = n.fade||0;
var node = this; var node = this;
try {
var blink1 = new Blink1.Blink1();
node.log("started");
try {
var p1 = /^\#[A-Za-z0-9]{6}$/
var p2 = /[0-9]+,[0-9]+,[0-9]+/
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg != null) { if (blink1) {
var rgb = msg.payload.split(','); if (p1.test(msg.payload)) {
// only do it if three parameters... // if it is a hex colour string
if (rgb.length == 3) { var r = parseInt(msg.payload.slice(1,3),16);
blink1.setRGB( (rgb[0]*1)&255, (rgb[1]*1)&255, (rgb[2]*1)&255 ); var g = parseInt(msg.payload.slice(3,5),16);
var b = parseInt(msg.payload.slice(5),16);
if (node.fade == 0) { blink1.setRGB( r, g, b ); }
else { blink1.fadeToRGB(node.fade, r, g, b ); }
}
else if (p2.test(msg.payload)) {
// if it is a r,g,b triple
var rgb = msg.payload.split(',');
if (node.fade == 0) { blink1.setRGB(parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); }
else { blink1.fadeToRGB(node.fade, parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); }
} }
else { else {
//let Andy do fancy colours by name here if he wants... // you can do fancy colours by name here if you want...
node.log("received "+msg.payload); node.warn("Blink1 : invalid msg : "+msg.payload);
} }
} }
else {
node.warn("No Blink1 found");
}
}); });
var blink1 = new Blink1.Blink1();
} }
catch(e) { catch(e) {
node.error(e); node.error("no Blink1 found");
} }
} }
RED.nodes.registerType("blink",Blink1Node); RED.nodes.registerType("blink1",Blink1Node);