mirror of
				https://github.com/node-red/node-red-nodes.git
				synced 2025-03-01 10:37:43 +00:00 
			
		
		
		
	Let neopixel node support GRB as well as RGB
This commit is contained in:
		@@ -169,15 +169,22 @@ var colours = {
 | 
			
		||||
 | 
			
		||||
var p1 = /^\#[A-Fa-f0-9]{6}$/
 | 
			
		||||
 | 
			
		||||
module.exports.getRGB = function(col) {
 | 
			
		||||
module.exports.getRGB = function(col,rgb) {
 | 
			
		||||
    col = col.toString().toLowerCase();
 | 
			
		||||
    if (col in colours) {
 | 
			
		||||
        col = colours[col];
 | 
			
		||||
    }
 | 
			
		||||
    if (p1.test(col)) {
 | 
			
		||||
        r = parseInt(col.slice(1,3),16);
 | 
			
		||||
        g = parseInt(col.slice(3,5),16);
 | 
			
		||||
        b = parseInt(col.slice(5),16);
 | 
			
		||||
        if (rgb === "grb") {
 | 
			
		||||
            g = parseInt(col.slice(1,3),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;
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
 
 | 
			
		||||
@@ -40,7 +40,11 @@
 | 
			
		||||
</div>
 | 
			
		||||
<div class="form-row">
 | 
			
		||||
    <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        Pixel order
 | 
			
		||||
    <select id="node-input-rgb" style="width:80px">
 | 
			
		||||
        <option value="rgb">RGB</option>
 | 
			
		||||
        <option value="grb">GRB</option>
 | 
			
		||||
    </select>
 | 
			
		||||
</div>
 | 
			
		||||
<br/>
 | 
			
		||||
<div class="form-row">
 | 
			
		||||
@@ -80,7 +84,8 @@
 | 
			
		||||
            bgnd: { value:"" },
 | 
			
		||||
            fgnd: { value:"" },
 | 
			
		||||
            wipe: { value:"40", required:true, validate:RED.validators.number() },
 | 
			
		||||
            mode: { value:"pcent" }
 | 
			
		||||
            mode: { value:"pcent" },
 | 
			
		||||
            rgb: { value:"rgb" }
 | 
			
		||||
        },
 | 
			
		||||
        inputs:1,
 | 
			
		||||
        outputs:0,
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@
 | 
			
		||||
module.exports = function(RED) {
 | 
			
		||||
    "use strict";
 | 
			
		||||
    var spawn = require('child_process').spawn;
 | 
			
		||||
    var fs =  require('fs');
 | 
			
		||||
    var fs = require('fs');
 | 
			
		||||
    var colors = require('./colours.js');
 | 
			
		||||
 | 
			
		||||
    var piCommand = __dirname+'/neopix';
 | 
			
		||||
@@ -46,6 +46,7 @@ module.exports = function(RED) {
 | 
			
		||||
        this.bgnd = n.bgnd || "0,0,0";
 | 
			
		||||
        this.fgnd = n.fgnd || "128,128,128";
 | 
			
		||||
        this.mode = n.mode || "pcent";
 | 
			
		||||
        this.rgb = n.rgb || "rgb";
 | 
			
		||||
        this.wipe = Number(n.wipe || 40);
 | 
			
		||||
        if (this.wipe < 0) { this.wipe = 0; }
 | 
			
		||||
        var node = this;
 | 
			
		||||
@@ -62,25 +63,24 @@ module.exports = function(RED) {
 | 
			
		||||
                if (parts.length <= 2) {
 | 
			
		||||
                    if (parts.length === 2) { // it's a colour and length
 | 
			
		||||
                        if (isNaN(parseInt(parts[1]))) { parts = parts.reverse(); }
 | 
			
		||||
                        if (colors.getRGB(parts[0])) {
 | 
			
		||||
                        if (colors.getRGB(parts[0],node.rgb)) {
 | 
			
		||||
                            var l = parts[1];
 | 
			
		||||
                            if (node.mode.indexOf("pcent") >= 0) { l = parseInt(l / 100 * node.pixels + 0.5); }
 | 
			
		||||
                            l = l - 1;
 | 
			
		||||
                            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;
 | 
			
		||||
                            } 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;
 | 
			
		||||
                            }
 | 
			
		||||
                            console.log(pay);
 | 
			
		||||
                        }
 | 
			
		||||
                        else { node.warn("Invalid payload : "+pay); return; }
 | 
			
		||||
                        else { node.warn("Invalid colour : "+pay); return; }
 | 
			
		||||
                    }
 | 
			
		||||
                    else {
 | 
			
		||||
                        if (isNaN(pay)) { // it's a single colour word so set background
 | 
			
		||||
                            if (colors.getRGB(pay)) {
 | 
			
		||||
                                node.bgnd = colors.getRGB(pay);
 | 
			
		||||
                            if (colors.getRGB(pay,node.rgb)) {
 | 
			
		||||
                                node.bgnd = colors.getRGB(pay,node.rgb);
 | 
			
		||||
                                pay = node.bgnd;
 | 
			
		||||
                            }
 | 
			
		||||
                            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 (parts.length === 3) { node.bgnd = pay; }
 | 
			
		||||
                    node.child.stdin.write(pay+"\n"); // handle 3 parts, 4 part and 5 parts in the python
 | 
			
		||||
                if ( p2.test(pay) || p3.test(pay) || p4.test(pay) ) {
 | 
			
		||||
                    if ((parts.length > 2) && (node.rgb === "grb")) { // swap r and g values
 | 
			
		||||
                        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); }
 | 
			
		||||
            }
 | 
			
		||||
@@ -145,7 +152,7 @@ module.exports = function(RED) {
 | 
			
		||||
 | 
			
		||||
        if (node.bgnd) {
 | 
			
		||||
            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) {
 | 
			
		||||
                node.child.stdin.write(node.bgnd+"\n");
 | 
			
		||||
@@ -154,7 +161,7 @@ module.exports = function(RED) {
 | 
			
		||||
 | 
			
		||||
        if (node.fgnd) {
 | 
			
		||||
            if (node.fgnd.split(',').length === 1) {
 | 
			
		||||
                node.fgnd = colors.getRGB(node.fgnd);
 | 
			
		||||
                node.fgnd = colors.getRGB(node.fgnd,node.rgb);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
{
 | 
			
		||||
    "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.",
 | 
			
		||||
    "dependencies"  : {
 | 
			
		||||
    },
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user