diff --git a/hardware/Pi/37-rpi-piface.js b/hardware/Pi/37-rpi-piface.js index 17a1482f..6616d8da 100644 --- a/hardware/Pi/37-rpi-piface.js +++ b/hardware/Pi/37-rpi-piface.js @@ -14,138 +14,140 @@ * limitations under the License. **/ -var RED = require(process.env.NODE_RED_HOME+"/red/red"); -var util = require("util"); -var exec = require('child_process').exec; -var fs = require('fs'); +module.exports = function(RED) { + "use strict"; + var util = require("util"); + var exec = require('child_process').exec; + var fs = require('fs'); -if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi - throw "Info : Ignoring Raspberry Pi specific node."; -} + if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi + throw "Info : Ignoring Raspberry Pi specific node."; + } -if (!fs.existsSync("/usr/local/bin/gpio")) { // gpio command not installed - throw "Info : Can't find Raspberry Pi wiringPi gpio command."; -} + if (!fs.existsSync("/usr/local/bin/gpio")) { // gpio command not installed + throw "Info : Can't find Raspberry Pi wiringPi gpio command."; + } -// Map names of pins to Gordon's gpio PiFace pin numbers -var pintable = { -// Physical : WiringPi - "Button S1":"200", - "Button S2":"201", - "Button S3":"202", - "Button S4":"203", - "Input 5":"204", - "Input 6":"205", - "Input 7":"206", - "Input 8":"207", - "Output0":"208", - "Output1":"209", - "Output2":"210", - "Output3":"211", - "Output4":"212", - "Output5":"213", - "Output6":"214", - "Output7":"215", - "LED 0 / Relay 0":"200", - "LED 1 / Relay 1":"201", - "LED 2":"202", - "LED 3":"203", - "LED 4":"204", - "LED 5":"205", - "LED 6":"206", - "LED 7":"207" -} -var tablepin = { -// WiringPi : Physical - "200":"S1", - "201":"S2", - "202":"S3", - "203":"S4", - "204":"I5", - "205":"I6", - "206":"I7", - "207":"I8", - "208":"O0", - "209":"O1", - "210":"O2", - "211":"O3", - "212":"O4", - "213":"O5", - "214":"O6", - "215":"O7", - "200":"L0", - "201":"L1", - "202":"L2", - "203":"L3", - "204":"L4", - "205":"L5", - "206":"L6", - "207":"L7" -} + // Map names of pins to Gordon's gpio PiFace pin numbers + var pintable = { + // Physical : WiringPi + "Button S1":"200", + "Button S2":"201", + "Button S3":"202", + "Button S4":"203", + "Input 5":"204", + "Input 6":"205", + "Input 7":"206", + "Input 8":"207", + "Output0":"208", + "Output1":"209", + "Output2":"210", + "Output3":"211", + "Output4":"212", + "Output5":"213", + "Output6":"214", + "Output7":"215", + "LED 0 / Relay 0":"200", + "LED 1 / Relay 1":"201", + "LED 2":"202", + "LED 3":"203", + "LED 4":"204", + "LED 5":"205", + "LED 6":"206", + "LED 7":"207" + } + var tablepin = { + // WiringPi : Physical + "200":"S1", + "201":"S2", + "202":"S3", + "203":"S4", + "204":"I5", + "205":"I6", + "206":"I7", + "207":"I8", + "208":"O0", + "209":"O1", + "210":"O2", + "211":"O3", + "212":"O4", + "213":"O5", + "214":"O6", + "215":"O7", + "200":"L0", + "201":"L1", + "202":"L2", + "203":"L3", + "204":"L4", + "205":"L5", + "206":"L6", + "207":"L7" + } -function PiFACEInNode(n) { - RED.nodes.createNode(this,n); - this.buttonState = -1; - this.pin = pintable[n.pin]; - this.intype = n.intype; - var node = this; - if (node.pin) { - exec("gpio -p mode "+node.pin+" "+node.intype, function(err,stdout,stderr) { - if (err) node.error(err); - else { - node._interval = setInterval( function() { - exec("gpio -p read "+node.pin, function(err,stdout,stderr) { - if (err) node.error(err); - else { - if (node.buttonState !== Number(stdout)) { - var previousState = node.buttonState; - node.buttonState = Number(stdout); - if (previousState !== -1) { - var msg = {topic:"piface/"+tablepin[node.pin], payload:node.buttonState}; - node.send(msg); + function PiFACEInNode(n) { + RED.nodes.createNode(this,n); + this.buttonState = -1; + this.pin = pintable[n.pin]; + this.intype = n.intype; + var node = this; + if (node.pin) { + exec("gpio -p mode "+node.pin+" "+node.intype, function(err,stdout,stderr) { + if (err) { node.error(err); } + else { + node._interval = setInterval( function() { + exec("gpio -p read "+node.pin, function(err,stdout,stderr) { + if (err) { node.error(err); } + else { + if (node.buttonState !== Number(stdout)) { + var previousState = node.buttonState; + node.buttonState = Number(stdout); + if (previousState !== -1) { + var msg = {topic:"piface/"+tablepin[node.pin], payload:node.buttonState}; + node.send(msg); + } } } - } - }); - }, 200); - } + }); + }, 200); + } + }); + } + else { + node.error("Invalid PiFACE pin: "+node.pin); + } + node.on("close", function() { + clearInterval(node._interval); }); } - else { - node.error("Invalid PiFACE pin: "+node.pin); + + function PiFACEOutNode(n) { + RED.nodes.createNode(this,n); + this.pin = pintable[n.pin]; + var node = this; + if (node.pin) { + node.on("input", function(msg) { + if (msg.payload === "true") { msg.payload = true; } + if (msg.payload === "false") { msg.payload = false; } + var out = Number(msg.payload); + if ((out === 0)|(out === 1)) { + exec("gpio -p write "+node.pin+" "+out, function(err,stdout,stderr) { + if (err) { node.error(err); } + }); + } + else { node.warn("Invalid input - not 0 or 1"); } + }); + } + else { + node.error("Invalid PiFACE pin: "+node.pin); + } } - node.on("close", function() { - clearInterval(node._interval); + + + exec("gpio load spi",function(err,stdout,stderr) { + if (err) { + util.log('[37-rpi-piface.js] Error: "gpio load spi" command failed for some reason.'); + } + RED.nodes.registerType("rpi-piface in",PiFACEInNode); + RED.nodes.registerType("rpi-piface out",PiFACEOutNode); }); } - -function PiFACEOutNode(n) { - RED.nodes.createNode(this,n); - this.pin = pintable[n.pin]; - var node = this; - if (node.pin) { - node.on("input", function(msg) { - if (msg.payload === "true") msg.payload = true; - if (msg.payload === "false") msg.payload = false; - var out = Number(msg.payload); - if ((out == 0)|(out == 1)) { - exec("gpio -p write "+node.pin+" "+out, function(err,stdout,stderr) { - if (err) node.error(err); - }); - } - else node.warn("Invalid input - not 0 or 1"); - }); - } - else { - node.error("Invalid PiFACE pin: "+node.pin); - } -} - - -exec("gpio load spi",function(err,stdout,stderr) { - if (err) { - util.log('[37-rpi-piface.js] Error: "gpio load spi" command failed for some reason.'); - } - RED.nodes.registerType("rpi-piface in",PiFACEInNode); - RED.nodes.registerType("rpi-piface out",PiFACEOutNode); -}); diff --git a/hardware/Pi/38-rpi-pibrella.js b/hardware/Pi/38-rpi-pibrella.js index ad2c3339..eaa8c96e 100644 --- a/hardware/Pi/38-rpi-pibrella.js +++ b/hardware/Pi/38-rpi-pibrella.js @@ -14,159 +14,161 @@ * limitations under the License. **/ -var RED = require(process.env.NODE_RED_HOME+"/red/red"); -var util = require("util"); -var exec = require('child_process').exec; -var fs = require('fs'); +module.exports = function(RED) { + "use strict"; + var util = require("util"); + var exec = require('child_process').exec; + var fs = require('fs'); -if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi - throw "Info : Ignoring Raspberry Pi specific node."; -} + if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi + throw "Info : Ignoring Raspberry Pi specific node."; + } -if (!fs.existsSync("/usr/local/bin/gpio")) { // gpio command not installed - throw "Info : Can't find Raspberry Pi wiringPi gpio command."; -} + if (!fs.existsSync("/usr/local/bin/gpio")) { // gpio command not installed + throw "Info : Can't find Raspberry Pi wiringPi gpio command."; + } -// Map physical P1 pins to Gordon's Wiring-Pi Pins (as they should be V1/V2 tolerant) -var pintable = { -// Physical : WiringPi - "Amber LED":"0", - "Buzzer ":"1", - "Red LED":"2", - "Out E":"3", - "Out F":"4", - "Out G":"5", - "Out H":"6", - "Green LED":"7", - "In C":"10", - "In B":"11", - "In D":"12", - "In A":"13", - "Red Button":"14", -} -var tablepin = { -// WiringPi : Physical - "0":"Amber", - "1":"Buzzer", - "2":"Red", - "3":"E", - "4":"F", - "5":"G", - "6":"H", - "7":"Green", - "10":"C", - "11":"B", - "12":"D", - "13":"A", - "14":"R", -} + // Map physical P1 pins to Gordon's Wiring-Pi Pins (as they should be V1/V2 tolerant) + var pintable = { + // Physical : WiringPi + "Amber LED":"0", + "Buzzer ":"1", + "Red LED":"2", + "Out E":"3", + "Out F":"4", + "Out G":"5", + "Out H":"6", + "Green LED":"7", + "In C":"10", + "In B":"11", + "In D":"12", + "In A":"13", + "Red Button":"14", + } + var tablepin = { + // WiringPi : Physical + "0":"Amber", + "1":"Buzzer", + "2":"Red", + "3":"E", + "4":"F", + "5":"G", + "6":"H", + "7":"Green", + "10":"C", + "11":"B", + "12":"D", + "13":"A", + "14":"R", + } -function PibrellaIn(n) { - RED.nodes.createNode(this,n); - this.buttonState = -1; - this.pin = pintable[n.pin]; - var node = this; + function PibrellaIn(n) { + RED.nodes.createNode(this,n); + this.buttonState = -1; + this.pin = pintable[n.pin]; + var node = this; - if (node.pin) { - exec("gpio mode "+node.pin+" in", function(err,stdout,stderr) { - if (err) node.error(err); - else { - node._interval = setInterval( function() { - exec("gpio read "+node.pin, function(err,stdout,stderr) { - if (err) node.error(err); - else { - if (node.buttonState !== Number(stdout)) { - var previousState = node.buttonState; - node.buttonState = Number(stdout); - if (previousState !== -1) { - var msg = {topic:"pibrella/"+tablepin[node.pin], payload:node.buttonState}; - node.send(msg); + if (node.pin) { + exec("gpio mode "+node.pin+" in", function(err,stdout,stderr) { + if (err) { node.error(err); } + else { + node._interval = setInterval( function() { + exec("gpio read "+node.pin, function(err,stdout,stderr) { + if (err) { node.error(err); } + else { + if (node.buttonState !== Number(stdout)) { + var previousState = node.buttonState; + node.buttonState = Number(stdout); + if (previousState !== -1) { + var msg = {topic:"pibrella/"+tablepin[node.pin], payload:node.buttonState}; + node.send(msg); + } } } - } - }); - }, 200); - } - }); - } - else { - node.error("Invalid GPIO pin: "+node.pin); - } - - node.on("close", function() { - clearInterval(node._interval); - }); -} - -function PibrellaOut(n) { - RED.nodes.createNode(this,n); - this.pin = pintable[n.pin]; - var node = this; - - if (node.pin == "1") { - exec("gpio mode 1 pwm"); - process.nextTick(function() { - exec("gpio pwm-ms"); - node.on("input", function(msg) { - var out = Number(msg.payload); - if (out == 1) { // fixed buzz - exec("gpio pwm 1 511"); - exec("gpio pwmc 100"); - } - else if ((out >= 2) && (out <= 9999)) { // set buzz to a value - exec("gpio pwm 1 511"); - exec("gpio pwmc "+out); - } - else { exec("gpio pwm 1 0"); } // turn it off - }); - }); - } - else if (node.pin) { - process.nextTick(function() { - exec("gpio mode "+node.pin+" out", function(err,stdout,stderr) { - if (err) node.error(err); - else { - node.on("input", function(msg) { - if (msg.payload === "true") msg.payload = true; - if (msg.payload === "false") msg.payload = false; - var out = Number(msg.payload); - if ((out == 0)|(out == 1)) { - exec("gpio write "+node.pin+" "+out, function(err,stdout,stderr) { - if (err) node.error(err); - }); - } - else node.warn("Invalid input - not 0 or 1"); - }); + }); + }, 200); } }); + } + else { + node.error("Invalid GPIO pin: "+node.pin); + } + + node.on("close", function() { + clearInterval(node._interval); }); } - else { - node.error("Invalid GPIO pin: "+node.pin); + + function PibrellaOut(n) { + RED.nodes.createNode(this,n); + this.pin = pintable[n.pin]; + var node = this; + + if (node.pin == "1") { + exec("gpio mode 1 pwm"); + process.nextTick(function() { + exec("gpio pwm-ms"); + node.on("input", function(msg) { + var out = Number(msg.payload); + if (out == 1) { // fixed buzz + exec("gpio pwm 1 511"); + exec("gpio pwmc 100"); + } + else if ((out >= 2) && (out <= 9999)) { // set buzz to a value + exec("gpio pwm 1 511"); + exec("gpio pwmc "+out); + } + else { exec("gpio pwm 1 0"); } // turn it off + }); + }); + } + else if (node.pin) { + process.nextTick(function() { + exec("gpio mode "+node.pin+" out", function(err,stdout,stderr) { + if (err) { node.error(err); } + else { + node.on("input", function(msg) { + if (msg.payload === "true") { msg.payload = true; } + if (msg.payload === "false") { msg.payload = false; } + var out = Number(msg.payload); + if ((out === 0)|(out === 1)) { + exec("gpio write "+node.pin+" "+out, function(err,stdout,stderr) { + if (err) { node.error(err); } + }); + } + else { node.warn("Invalid input - not 0 or 1"); } + }); + } + }); + }); + } + else { + node.error("Invalid GPIO pin: "+node.pin); + } + + node.on("close", function() { + exec("gpio mode "+node.pin+" in"); + }); } - node.on("close", function() { - exec("gpio mode "+node.pin+" in"); - }); + //exec("gpio mode 0 out",function(err,stdout,stderr) { + //if (err) { + //util.log('[36-rpi-gpio.js] Error: "gpio" command failed for some reason.'); + //} + //exec("gpio mode 1 out"); + //exec("gpio mode 2 out"); + //exec("gpio mode 3 out"); + //exec("gpio mode 4 out"); + //exec("gpio mode 5 out"); + //exec("gpio mode 6 out"); + //exec("gpio mode 7 out"); + //exec("gpio mode 10 in"); + //exec("gpio mode 11 in"); + //exec("gpio mode 12 in"); + //exec("gpio mode 13 in"); + //exec("gpio mode 14 in"); + //}); + + RED.nodes.registerType("rpi-pibrella in",PibrellaIn); + RED.nodes.registerType("rpi-pibrella out",PibrellaOut); } - -//exec("gpio mode 0 out",function(err,stdout,stderr) { - //if (err) { - //util.log('[36-rpi-gpio.js] Error: "gpio" command failed for some reason.'); - //} - //exec("gpio mode 1 out"); - //exec("gpio mode 2 out"); - //exec("gpio mode 3 out"); - //exec("gpio mode 4 out"); - //exec("gpio mode 5 out"); - //exec("gpio mode 6 out"); - //exec("gpio mode 7 out"); - //exec("gpio mode 10 in"); - //exec("gpio mode 11 in"); - //exec("gpio mode 12 in"); - //exec("gpio mode 13 in"); - //exec("gpio mode 14 in"); -//}); - -RED.nodes.registerType("rpi-pibrella in",PibrellaIn); -RED.nodes.registerType("rpi-pibrella out",PibrellaOut); diff --git a/hardware/Pi/78-ledborg.js b/hardware/Pi/78-ledborg.js index 9aa6453e..ee43ca9d 100644 --- a/hardware/Pi/78-ledborg.js +++ b/hardware/Pi/78-ledborg.js @@ -14,50 +14,52 @@ * limitations under the License. **/ -var RED = require(process.env.NODE_RED_HOME+"/red/red"); -var util = require('util'); -var fs = require('fs'); +module.exports = function(RED) { + "use strict"; + var util = require('util'); + var fs = require('fs'); -// check if /dev/ledborg exists - if not then don't even show the node. -if (!fs.existsSync("/dev/ledborg")) { - throw "Info : PiBorg hardware : LedBorg not found"; -} + // check if /dev/ledborg exists - if not then don't even show the node. + if (!fs.existsSync("/dev/ledborg")) { + throw "Info : PiBorg hardware : LedBorg not found"; + } -function LedBorgNode(n) { - RED.nodes.createNode(this,n); - var p1 = /^[0-2][0-2][0-2]$/ - var p2 = /^\#[A-Fa-f0-9]{6}$/ - var node = this; + function LedBorgNode(n) { + RED.nodes.createNode(this,n); + var p1 = /^[0-2][0-2][0-2]$/ + var p2 = /^\#[A-Fa-f0-9]{6}$/ + var node = this; - this.on("input", function(msg) { - if (p1.test(msg.payload)) { - fs.writeFile('/dev/ledborg', msg.payload, function (err) { - if (err) node.warn(msg.payload+" : No LedBorg found"); - }); - } - else if (p2.test(msg.payload)) { - var r = Math.floor(parseInt(msg.payload.slice(1,3),16)/88).toString(); - var g = Math.floor(parseInt(msg.payload.slice(3,5),16)/88).toString(); - var b = Math.floor(parseInt(msg.payload.slice(5),16)/88).toString(); - fs.writeFile('/dev/ledborg', r+g+b, function (err) { - if (err) node.warn(r+g+b+" : No LedBorg found"); - }); - } - else { - // you can add fancy colours by name here if you want... - // these are the @cheerlight ones. - var colors = {"red":"200","green":"020","blue":"002","cyan":"022","white":"222","pink":"201", - "warmwhite":"221","purple":"101","magenta":"202","yellow":"220","amber":"220","orange":"210","black":"000"} - if (msg.payload.toLowerCase() in colors) { - var c = colors[msg.payload.toLowerCase()]; - fs.writeFile('/dev/ledborg', c, function (err) { - if (err) node.warn(msg.payload+" : No LedBorg found"); + this.on("input", function(msg) { + if (p1.test(msg.payload)) { + fs.writeFile('/dev/ledborg', msg.payload, function (err) { + if (err) { node.warn(msg.payload+" : No LedBorg found"); } + }); + } + else if (p2.test(msg.payload)) { + var r = Math.floor(parseInt(msg.payload.slice(1,3),16)/88).toString(); + var g = Math.floor(parseInt(msg.payload.slice(3,5),16)/88).toString(); + var b = Math.floor(parseInt(msg.payload.slice(5),16)/88).toString(); + fs.writeFile('/dev/ledborg', r+g+b, function (err) { + if (err) { node.warn(r+g+b+" : No LedBorg found"); } }); } else { - node.warn("Invalid LedBorg colour code"); + // you can add fancy colours by name here if you want... + // these are the @cheerlight ones. + var colors = {"red":"200","green":"020","blue":"002","cyan":"022","white":"222","pink":"201", + "warmwhite":"221","purple":"101","magenta":"202","yellow":"220","amber":"220","orange":"210","black":"000"} + if (msg.payload.toLowerCase() in colors) { + var c = colors[msg.payload.toLowerCase()]; + fs.writeFile('/dev/ledborg', c, function (err) { + if (err) { node.warn(msg.payload+" : No LedBorg found"); } + }); + } + else { + node.warn("Invalid LedBorg colour code"); + } } - } - }); + }); + } + RED.nodes.registerType("ledborg",LedBorgNode); } -RED.nodes.registerType("ledborg",LedBorgNode); diff --git a/hardware/blink/76-blinkstick.js b/hardware/blink/76-blinkstick.js index ebbdacc7..389dd605 100644 --- a/hardware/blink/76-blinkstick.js +++ b/hardware/blink/76-blinkstick.js @@ -14,49 +14,51 @@ * limitations under the License. **/ -var RED = require(process.env.NODE_RED_HOME+"/red/red"); -var blinkstick = require("blinkstick"); +module.exports = function(RED) { + "use strict"; + var blinkstick = require("blinkstick"); -Object.size = function(obj) { - var size = 0, key; - for (key in obj) { if (obj.hasOwnProperty(key)) size++; } - return size; -}; + Object.size = function(obj) { + var size = 0; + for (var 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; + 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; - 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); + 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,'')); + } } - else { - node.led.setColor(msg.payload.toLowerCase().replace(/\s+/g,'')); + catch (err) { + node.warn("BlinkStick missing ?"); + node.led = blinkstick.findFirst(); } } - catch (err) { - node.warn("BlinkStick missing ?"); + else { + //node.warn("No BlinkStick found"); 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"); } - }); - if (Object.size(node.led) === 0) { - node.error("No BlinkStick found"); + } + RED.nodes.registerType("blinkstick",BlinkStick); } - -RED.nodes.registerType("blinkstick",BlinkStick); diff --git a/hardware/blink/77-blink1.js b/hardware/blink/77-blink1.js index 474164d9..dfefd143 100644 --- a/hardware/blink/77-blink1.js +++ b/hardware/blink/77-blink1.js @@ -14,70 +14,73 @@ * limitations under the License. **/ -var RED = require(process.env.NODE_RED_HOME+"/red/red"); -var Blink1 = require("node-blink1"); -// create a single global blink1 object -// all blink1 nodes affect the same (single) led -var blink1 = null; +module.exports = function(RED) { + "use strict"; + var Blink1 = require("node-blink1"); + // create a single global blink1 object + // all blink1 nodes affect the same (single) led + var blink1 = null; -function Blink1Node(n) { - RED.nodes.createNode(this,n); - this.fade = Number(n.fade) || 0; - var node = this; + function Blink1Node(n) { + RED.nodes.createNode(this,n); + this.fade = Number(n.fade) || 0; + var node = this; - try { - var p1 = /^\#[A-Fa-f0-9]{6}$/ - var p2 = /[0-9]+,[0-9]+,[0-9]+/ - this.on("input", function(msg) { - blink1 = blink1 || new Blink1.Blink1(); - if (blink1) { - try { - if (p1.test(msg.payload)) { - // if it is a hex colour string - var r = parseInt(msg.payload.slice(1,3),16); - 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 { - // you can add fancy colours by name here if you want... - // these are the @cheerlight ones. - var colors = {"red":"#FF0000","green":"#008000","blue":"#0000FF","cyan":"#00FFFF","white":"#FFFFFF","warmwhite":"#FDF5E6", - "purple":"#800080","magenta":"#FF00FF","yellow":"#FFFF00","amber":"#FFD200","orange":"#FFA500","black":"#000000"} - if (msg.payload.toLowerCase() in colors) { - var c = colors[msg.payload.toLowerCase()]; - var r = parseInt(c.slice(1,3),16); - var g = parseInt(c.slice(3,5),16); - var b = parseInt(c.slice(5),16); - if (node.fade == 0) { blink1.setRGB( r, g, b ); } + try { + var p1 = /^\#[A-Fa-f0-9]{6}$/ + var p2 = /[0-9]+,[0-9]+,[0-9]+/ + this.on("input", function(msg) { + blink1 = blink1 || new Blink1.Blink1(); + if (blink1) { + var r,g,b; + try { + if (p1.test(msg.payload)) { + // if it is a hex colour string + r = parseInt(msg.payload.slice(1,3),16); + g = parseInt(msg.payload.slice(3,5),16); + 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 { - node.warn("Blink1 : invalid msg : "+msg.payload); + 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 { + // you can add fancy colours by name here if you want... + // these are the @cheerlight ones. + var colors = {"red":"#FF0000","green":"#008000","blue":"#0000FF","cyan":"#00FFFF","white":"#FFFFFF","warmwhite":"#FDF5E6", + "purple":"#800080","magenta":"#FF00FF","yellow":"#FFFF00","amber":"#FFD200","orange":"#FFA500","black":"#000000"} + if (msg.payload.toLowerCase() in colors) { + var c = colors[msg.payload.toLowerCase()]; + r = parseInt(c.slice(1,3),16); + g = parseInt(c.slice(3,5),16); + b = parseInt(c.slice(5),16); + if (node.fade === 0) { blink1.setRGB( r, g, b ); } + else { blink1.fadeToRGB(node.fade, r, g, b ); } + } + else { + node.warn("Blink1 : invalid msg : "+msg.payload); + } + } + } catch (e) { node.warn("Blink1 : error"); blink1 = null; } } - } catch (e) { node.warn("Blink1 : error"); blink1 = null; } - } - else { - node.warn("Blink1 : not found"); - } - }); - this.on("close", function() { - if (blink1 && typeof blink1.close == "function") { - //blink1.close(); //This ought to work but seems to cause more hangs on closing than not... - } - blink1 = null; - }); - } - catch(e) { - node.error("No Blink1 found (" + e + ")"); + else { + node.warn("Blink1 : not found"); + } + }); + this.on("close", function() { + if (blink1 && typeof blink1.close == "function") { + //blink1.close(); //This ought to work but seems to cause more hangs on closing than not... + } + blink1 = null; + }); + } + catch(e) { + node.error("No Blink1 found (" + e + ")"); + } } + RED.nodes.registerType("blink1",Blink1Node); } -RED.nodes.registerType("blink1",Blink1Node); diff --git a/hardware/digiRGB/78-digiRGB.js b/hardware/digiRGB/78-digiRGB.js index cb2cf0cf..3c7b49e8 100644 --- a/hardware/digiRGB/78-digiRGB.js +++ b/hardware/digiRGB/78-digiRGB.js @@ -14,56 +14,58 @@ * limitations under the License. **/ -var RED = require(process.env.NODE_RED_HOME+"/red/red"); -var HID = require('node-hid'); -var device; -var node; +module.exports = function(RED) { + "use strict"; + var HID = require('node-hid'); + var device; + var node; -function DigiRGBNode(n) { - RED.nodes.createNode(this,n); - node=this; + function DigiRGBNode(n) { + RED.nodes.createNode(this,n); + node=this; - var devices = HID.devices(0x16c0,0x05df); - for (var i=0; i< devices.length; i++) { - if (devices[i].product == 'DigiUSB') { - path = devices[i].path; - node.log("found: " + path); - try { - device = new HID.HID(devices[i].path); - break; - } catch (e) { - node.log(e) - } - } - } - - var p1 = /^\#[A-Fa-f0-9]{6}$/ - var p2 = /[0-9]+,[0-9]+,[0-9]+/ - - if (device) { - this.on("input", function(msg) { - if (msg != null) { - if (p1.test(msg.payload)) { - var r = parseInt(msg.payload.slice(1,3),16); - var g = parseInt(msg.payload.slice(3,5),16); - var b = parseInt(msg.payload.slice(5),16); - device.sendFeatureReport([115,r,g,b]); - } else if (p2.test(msg.payload)) { - var args = msg.payload.split(','); - if (args.length == 3) { - device.sendFeatureReport([115,parseInt(args[0]),parseInt(args[1]),parseInt(args[2])]); + var devices = HID.devices(0x16c0,0x05df); + for (var i=0; i< devices.length; i++) { + if (devices[i].product == 'DigiUSB') { + path = devices[i].path; + node.log("found: " + path); + try { + device = new HID.HID(devices[i].path); + break; + } catch (e) { + node.log(e) } - } else { - node.warn("incompatable input - " + msg.payload); } } - }); - } else { - node.warn("no digispark RGB found"); - } - this.on('close', function() { - if (device) { device.close(); } - }); + var p1 = /^\#[A-Fa-f0-9]{6}$/ + var p2 = /[0-9]+,[0-9]+,[0-9]+/ + + if (device) { + this.on("input", function(msg) { + if (msg != null) { + if (p1.test(msg.payload)) { + var r = parseInt(msg.payload.slice(1,3),16); + var g = parseInt(msg.payload.slice(3,5),16); + var b = parseInt(msg.payload.slice(5),16); + device.sendFeatureReport([115,r,g,b]); + } else if (p2.test(msg.payload)) { + var args = msg.payload.split(','); + if (args.length == 3) { + device.sendFeatureReport([115,parseInt(args[0]),parseInt(args[1]),parseInt(args[2])]); + } + } else { + node.warn("incompatable input - " + msg.payload); + } + } + }); + } else { + node.warn("no digispark RGB found"); + } + + this.on('close', function() { + if (device) { device.close(); } + }); + } + RED.nodes.registerType("digiRGB",DigiRGBNode); } -RED.nodes.registerType("digiRGB",DigiRGBNode); diff --git a/hardware/wemo/60-wemo.js b/hardware/wemo/60-wemo.js index 34391dee..c2e89e2c 100644 --- a/hardware/wemo/60-wemo.js +++ b/hardware/wemo/60-wemo.js @@ -14,47 +14,49 @@ * limitations under the License. **/ -var RED = require(process.env.NODE_RED_HOME+"/red/red"); -var WeMo = new require('wemo'); +module.exports = function(RED) { + "use strict"; + var WeMo = new require('wemo'); -function WeMoOut(n) { - RED.nodes.createNode(this,n); - this.ipaddr = n.ipaddr; - this.wemoSwitch = new WeMo(n.ipaddr); - var node = this; + function WeMoOut(n) { + RED.nodes.createNode(this,n); + this.ipaddr = n.ipaddr; + this.wemoSwitch = new WeMo(n.ipaddr); + var node = this; - this.on("input", function(msg) { - if (msg != null) { - var state = 0; - if ( msg.payload == 1 || msg.payload == true || msg.payload == "on" ) { var state = 1; } - node.wemoSwitch.setBinaryState(state, function(err, result) { - if (err) node.warn(err); - //else { node.log(result); } - }); - } - }); -} -RED.nodes.registerType("wemo out",WeMoOut); - -function WeMoIn(n) { - RED.nodes.createNode(this,n); - this.ipaddr = n.ipaddr; - this.wemoSwitch = new WeMo(n.ipaddr); - this.wemoSwitch.state = 0; - var node = this; - - var tick = setInterval(function() { - wemoSwitch.getBinaryState(function(err, result) { - if (err) node.warn(err); - if (parseInt(result) != wemoSwitch.state) { - wemoSwitch.state = parseInt(result); - node.send({payload:wemoSwitch.state,topic:"wemo/"+node.ipaddr}); + this.on("input", function(msg) { + if (msg != null) { + var state = 0; + if ( msg.payload == 1 || msg.payload === true || msg.payload == "on" ) { state = 1; } + node.wemoSwitch.setBinaryState(state, function(err, result) { + if (err) { node.warn(err); } + //else { node.log(result); } + }); } }); - }, 2000); + } + RED.nodes.registerType("wemo out",WeMoOut); - this.on("close", function() { - clearInterval(tick); - }); + function WeMoIn(n) { + RED.nodes.createNode(this,n); + this.ipaddr = n.ipaddr; + this.wemoSwitch = new WeMo(n.ipaddr); + this.wemoSwitch.state = 0; + var node = this; + + var tick = setInterval(function() { + wemoSwitch.getBinaryState(function(err, result) { + if (err) { node.warn(err); } + if (parseInt(result) != wemoSwitch.state) { + wemoSwitch.state = parseInt(result); + node.send({payload:wemoSwitch.state,topic:"wemo/"+node.ipaddr}); + } + }); + }, 2000); + + this.on("close", function() { + clearInterval(tick); + }); + } + RED.nodes.registerType("wemo in",WeMoOut); } -RED.nodes.registerType("wemo in",WeMoOut);