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

Let neopixel node support GRB as well as RGB

This commit is contained in:
Dave Conway-Jones 2016-01-29 20:08:30 +00:00
parent 9ba84c4908
commit 90878364ac
4 changed files with 39 additions and 20 deletions

View File

@ -169,15 +169,22 @@ var colours = {
var p1 = /^\#[A-Fa-f0-9]{6}$/ var p1 = /^\#[A-Fa-f0-9]{6}$/
module.exports.getRGB = function(col) { module.exports.getRGB = function(col,rgb) {
col = col.toString().toLowerCase(); col = col.toString().toLowerCase();
if (col in colours) { if (col in colours) {
col = colours[col]; col = colours[col];
} }
if (p1.test(col)) { if (p1.test(col)) {
r = parseInt(col.slice(1,3),16); if (rgb === "grb") {
g = parseInt(col.slice(3,5),16); g = parseInt(col.slice(1,3),16);
b = parseInt(col.slice(5),16); r = parseInt(col.slice(3,5),16);
b = parseInt(col.slice(5),16);
}
else {
r = parseInt(col.slice(1,3),16);
g = parseInt(col.slice(3,5),16);
b = parseInt(col.slice(5),16);
}
return r+","+g+","+b; return r+","+g+","+b;
} }
else { else {

View File

@ -40,7 +40,11 @@
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-wipe"><i class="fa fa-clock-o"></i> Wipe time</label> <label for="node-input-wipe"><i class="fa fa-clock-o"></i> Wipe time</label>
<input type="text" id="node-input-wipe" placeholder="number" style="width:60px;"> mS <input type="text" id="node-input-wipe" placeholder="number" style="width:60px;"> mS &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Pixel order
<select id="node-input-rgb" style="width:80px">
<option value="rgb">RGB</option>
<option value="grb">GRB</option>
</select>
</div> </div>
<br/> <br/>
<div class="form-row"> <div class="form-row">
@ -80,7 +84,8 @@
bgnd: { value:"" }, bgnd: { value:"" },
fgnd: { value:"" }, fgnd: { value:"" },
wipe: { value:"40", required:true, validate:RED.validators.number() }, wipe: { value:"40", required:true, validate:RED.validators.number() },
mode: { value:"pcent" } mode: { value:"pcent" },
rgb: { value:"rgb" }
}, },
inputs:1, inputs:1,
outputs:0, outputs:0,

View File

@ -17,7 +17,7 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;
var fs = require('fs'); var fs = require('fs');
var colors = require('./colours.js'); var colors = require('./colours.js');
var piCommand = __dirname+'/neopix'; var piCommand = __dirname+'/neopix';
@ -46,6 +46,7 @@ module.exports = function(RED) {
this.bgnd = n.bgnd || "0,0,0"; this.bgnd = n.bgnd || "0,0,0";
this.fgnd = n.fgnd || "128,128,128"; this.fgnd = n.fgnd || "128,128,128";
this.mode = n.mode || "pcent"; this.mode = n.mode || "pcent";
this.rgb = n.rgb || "rgb";
this.wipe = Number(n.wipe || 40); this.wipe = Number(n.wipe || 40);
if (this.wipe < 0) { this.wipe = 0; } if (this.wipe < 0) { this.wipe = 0; }
var node = this; var node = this;
@ -62,25 +63,24 @@ module.exports = function(RED) {
if (parts.length <= 2) { if (parts.length <= 2) {
if (parts.length === 2) { // it's a colour and length if (parts.length === 2) { // it's a colour and length
if (isNaN(parseInt(parts[1]))) { parts = parts.reverse(); } if (isNaN(parseInt(parts[1]))) { parts = parts.reverse(); }
if (colors.getRGB(parts[0])) { if (colors.getRGB(parts[0],node.rgb)) {
var l = parts[1]; var l = parts[1];
if (node.mode.indexOf("pcent") >= 0) { l = parseInt(l / 100 * node.pixels + 0.5); } if (node.mode.indexOf("pcent") >= 0) { l = parseInt(l / 100 * node.pixels + 0.5); }
l = l - 1; l = l - 1;
if (node.mode.indexOf("need") >= 0) { if (node.mode.indexOf("need") >= 0) {
needle = colors.getRGB(parts[0]); needle = colors.getRGB(parts[0],node.rgb);
pay = "0,"+(l-1)+","+node.fgnd+"\n"+l+","+needle+"\n"+(l+1)+","+(node.pixels-1)+","+node.bgnd; pay = "0,"+(l-1)+","+node.fgnd+"\n"+l+","+needle+"\n"+(l+1)+","+(node.pixels-1)+","+node.bgnd;
} else { } else {
node.fgnd = colors.getRGB(parts[0]); node.fgnd = colors.getRGB(parts[0],node.rgb);
pay = "0,"+l+","+node.fgnd+"\n"+(l+1)+","+(node.pixels-1)+","+node.bgnd; pay = "0,"+l+","+node.fgnd+"\n"+(l+1)+","+(node.pixels-1)+","+node.bgnd;
} }
console.log(pay);
} }
else { node.warn("Invalid payload : "+pay); return; } else { node.warn("Invalid colour : "+pay); return; }
} }
else { else {
if (isNaN(pay)) { // it's a single colour word so set background if (isNaN(pay)) { // it's a single colour word so set background
if (colors.getRGB(pay)) { if (colors.getRGB(pay,node.rgb)) {
node.bgnd = colors.getRGB(pay); node.bgnd = colors.getRGB(pay,node.rgb);
pay = node.bgnd; pay = node.bgnd;
} }
else { node.warn("Invalid payload : "+pay); return; } else { node.warn("Invalid payload : "+pay); return; }
@ -96,10 +96,17 @@ module.exports = function(RED) {
} }
} }
} }
node.child.stdin.write(pay+"\n");
return;
} }
if ((parts.length <= 2) || p2.test(pay) || p3.test(pay) || p4.test(pay) ) { if ( p2.test(pay) || p3.test(pay) || p4.test(pay) ) {
if (parts.length === 3) { node.bgnd = pay; } if ((parts.length > 2) && (node.rgb === "grb")) { // swap r and g values
node.child.stdin.write(pay+"\n"); // handle 3 parts, 4 part and 5 parts in the python var tmp = parts[parts.length-3];
parts[parts.length-3] = parts[parts.length-2];
parts[parts.length-2] = tmp;
}
if (parts.length === 3) { node.bgnd = parts.join(","); }
node.child.stdin.write(parts.join(",")+"\n"); // handle 3 parts, 4 part and 5 parts in the python
} }
else { node.warn("Invalid payload : "+pay); } else { node.warn("Invalid payload : "+pay); }
} }
@ -145,7 +152,7 @@ module.exports = function(RED) {
if (node.bgnd) { if (node.bgnd) {
if (node.bgnd.split(',').length === 1) { if (node.bgnd.split(',').length === 1) {
node.bgnd = colors.getRGB(node.bgnd); node.bgnd = colors.getRGB(node.bgnd,node.rgb);
} }
if (node.mode.indexOf("shift") === -1) { if (node.mode.indexOf("shift") === -1) {
node.child.stdin.write(node.bgnd+"\n"); node.child.stdin.write(node.bgnd+"\n");
@ -154,7 +161,7 @@ module.exports = function(RED) {
if (node.fgnd) { if (node.fgnd) {
if (node.fgnd.split(',').length === 1) { if (node.fgnd.split(',').length === 1) {
node.fgnd = colors.getRGB(node.fgnd); node.fgnd = colors.getRGB(node.fgnd,node.rgb);
} }
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-pi-neopixel", "name" : "node-red-node-pi-neopixel",
"version" : "0.0.6", "version" : "0.0.7",
"description" : "A Node-RED node to output to a neopixel (ws2812) string of LEDS from a Raspberry Pi.", "description" : "A Node-RED node to output to a neopixel (ws2812) string of LEDS from a Raspberry Pi.",
"dependencies" : { "dependencies" : {
}, },