|
| 1 | +/** |
| 2 | + * Copyright 2013 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | + |
| 17 | +var RED = require("../../red/red"); |
| 18 | +var Blink1 = require("node-blink1"); |
| 19 | + |
| 20 | +function Blink1Node(n) { |
| 21 | + RED.nodes.createNode(this,n); |
| 22 | + this.fade = n.fade||0; |
| 23 | + var node = this; |
| 24 | + |
| 25 | + try { |
| 26 | + var p1 = /^\#[A-Fa-f0-9]{6}$/ |
| 27 | + var p2 = /[0-9]+,[0-9]+,[0-9]+/ |
| 28 | + this.on("input", function(msg) { |
| 29 | + if (blink1) { |
| 30 | + if (p1.test(msg.payload)) { |
| 31 | + // if it is a hex colour string |
| 32 | + var r = parseInt(msg.payload.slice(1,3),16); |
| 33 | + var g = parseInt(msg.payload.slice(3,5),16); |
| 34 | + var b = parseInt(msg.payload.slice(5),16); |
| 35 | + if (node.fade == 0) { blink1.setRGB( r, g, b ); } |
| 36 | + else { blink1.fadeToRGB(node.fade, r, g, b ); } |
| 37 | + } |
| 38 | + else if (p2.test(msg.payload)) { |
| 39 | + // if it is a r,g,b triple |
| 40 | + var rgb = msg.payload.split(','); |
| 41 | + if (node.fade == 0) { blink1.setRGB(parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); } |
| 42 | + else { blink1.fadeToRGB(node.fade, parseInt(rgb[0])&255, parseInt(rgb[1])&255, parseInt(rgb[2])&255); } |
| 43 | + } |
| 44 | + else { |
| 45 | + // you can do fancy colours by name here if you want... |
| 46 | + node.warn("Blink1 : invalid msg : "+msg.payload); |
| 47 | + } |
| 48 | + } |
| 49 | + else { |
| 50 | + node.warn("No Blink1 found"); |
| 51 | + } |
| 52 | + }); |
| 53 | + var blink1 = new Blink1.Blink1(); |
| 54 | + } |
| 55 | + catch(e) { |
| 56 | + node.error("No Blink1 found"); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +RED.nodes.registerType("blink1",Blink1Node); |
0 commit comments