mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
Let blinkstick handle strings of neopixels
This commit is contained in:
parent
24394c4b18
commit
63abf0a8c3
@ -97,7 +97,7 @@ module.exports = function(RED) {
|
|||||||
node.led = device;
|
node.led = device;
|
||||||
|
|
||||||
if (Object.size(node.led) === 0) {
|
if (Object.size(node.led) === 0) {
|
||||||
node.status({fill:"red",shape:"ring",text:"disconnected"});
|
node.status({fill:"red",shape:"ring",text:"not found"});
|
||||||
node.error("BlinkStick with serial number " + node.serial + " not found");
|
node.error("BlinkStick with serial number " + node.serial + " not found");
|
||||||
} else {
|
} else {
|
||||||
node.status({fill:"green",shape:"dot",text:"connected"});
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
||||||
@ -108,7 +108,7 @@ module.exports = function(RED) {
|
|||||||
node.led = blinkstick.findFirst();
|
node.led = blinkstick.findFirst();
|
||||||
|
|
||||||
if (Object.size(node.led) === 0) {
|
if (Object.size(node.led) === 0) {
|
||||||
node.status({fill:"red",shape:"ring",text:"disconnected"});
|
node.status({fill:"red",shape:"ring",text:"not found"});
|
||||||
node.error("No BlinkStick found");
|
node.error("No BlinkStick found");
|
||||||
} else {
|
} else {
|
||||||
node.status({fill:"green",shape:"dot",text:"connected"});
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
||||||
@ -172,7 +172,7 @@ module.exports = function(RED) {
|
|||||||
else {
|
else {
|
||||||
node.warn("BlinkStick missing ? " + err);
|
node.warn("BlinkStick missing ? " + err);
|
||||||
}
|
}
|
||||||
console.log(err);
|
node.log(err);
|
||||||
//Reset animation
|
//Reset animation
|
||||||
animationComplete = true;
|
animationComplete = true;
|
||||||
//Clear color
|
//Clear color
|
||||||
@ -191,81 +191,86 @@ module.exports = function(RED) {
|
|||||||
findBlinkStick();
|
findBlinkStick();
|
||||||
|
|
||||||
this.on("input", function(msg) {
|
this.on("input", function(msg) {
|
||||||
if (typeof(msg.payload) === 'object' ) {
|
if (typeof(node.led) !== "undefined") {
|
||||||
// if it's an array then hopefully it's r,g,b,r,g,b or name,name,name
|
if (typeof(msg.payload) === 'object' ) {
|
||||||
if (Array.isArray(msg.payload)) {
|
// if it's an array then hopefully it's r,g,b,r,g,b or name,name,name
|
||||||
if (Object.size(node.led) !== 0) {
|
if (Array.isArray(msg.payload)) {
|
||||||
node.led.setMode(2); // put it into ws2812B LED mode
|
if (Object.size(node.led) !== 0) {
|
||||||
var data = [];
|
node.led.setMode(2); // put it into ws2812B LED mode
|
||||||
for (var i = 0; i < msg.payload.length; i++) {
|
var data = [];
|
||||||
if (typeof msg.payload[i] === "string") { // if string then assume must be colour names
|
for (var i = 0; i < msg.payload.length; i++) {
|
||||||
var params = node.led.interpretParameters(msg.payload[i]); // lookup colour code from name
|
if (typeof msg.payload[i] === "string") { // if string then assume must be colour names
|
||||||
if (params) {
|
var params = node.led.interpretParameters(msg.payload[i]); // lookup colour code from name
|
||||||
data.push(params.green);
|
if (params) {
|
||||||
data.push(params.red);
|
data.push(params.green);
|
||||||
data.push(params.blue);
|
data.push(params.red);
|
||||||
|
data.push(params.blue);
|
||||||
|
}
|
||||||
|
else { node.warn("invalid colour: "+msg.payload[i]); }
|
||||||
|
}
|
||||||
|
else { // otherwise lets use numbers 0-255
|
||||||
|
data.push(msg.payload[i+1]);
|
||||||
|
data.push(msg.payload[i]);
|
||||||
|
data.push(msg.payload[i+2]);
|
||||||
|
i += 2;
|
||||||
}
|
}
|
||||||
else { node.warn("invalid colour: "+msg.payload[i]); }
|
|
||||||
}
|
}
|
||||||
else { // otherwise lets use numbers 0-255
|
if ((data.length % 3) === 0) { // by now length must be a multiple of 3
|
||||||
data.push(msg.payload[i+1]);
|
node.led.setColors(0, data, function(err) {
|
||||||
data.push(msg.payload[i]);
|
if (err) { node.log(err); }
|
||||||
data.push(msg.payload[i+2]);
|
});
|
||||||
i += 2;
|
|
||||||
}
|
}
|
||||||
}
|
else {
|
||||||
if ((data.length % 3) === 0) { // by now length must be a multiple of 3
|
node.warn("Colour array length not / 3");
|
||||||
node.led.setColors(0, data, function(err) {
|
}
|
||||||
if (err) { node.log(err); }
|
return;
|
||||||
});
|
} // else if no blinkstick let it get caught below
|
||||||
}
|
|
||||||
else {
|
|
||||||
node.warn("Colour array length not / 3");
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
} // else if no blinkstick let it get caught below
|
|
||||||
}
|
|
||||||
// not an array - must be the "normal" object....
|
|
||||||
else {
|
|
||||||
var data = validatePayloadObject(msg.payload);
|
|
||||||
if (typeof(data) === 'object') {
|
|
||||||
node.task = data.task ? data.task : node.task;
|
|
||||||
node.delay = data.delay ? data.delay : node.delay;
|
|
||||||
node.repeats = data.repeats ? data.repeats : node.repeats;
|
|
||||||
node.duration = data.duration ? data.duration : node.duration;
|
|
||||||
node.steps = data.steps ? data.steps : node.steps;
|
|
||||||
node.repeat = data.repeat ? data.repeat : node.repeat;
|
|
||||||
node.color = data.color ? data.color : node.color;
|
|
||||||
} else {
|
|
||||||
node.error(data);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
// not an array - must be the "normal" object....
|
||||||
|
else {
|
||||||
|
var data = validatePayloadObject(msg.payload);
|
||||||
|
if (typeof(data) === 'object') {
|
||||||
|
node.task = data.task ? data.task : node.task;
|
||||||
|
node.delay = data.delay ? data.delay : node.delay;
|
||||||
|
node.repeats = data.repeats ? data.repeats : node.repeats;
|
||||||
|
node.duration = data.duration ? data.duration : node.duration;
|
||||||
|
node.steps = data.steps ? data.steps : node.steps;
|
||||||
|
node.repeat = data.repeat ? data.repeat : node.repeat;
|
||||||
|
node.color = data.color ? data.color : node.color;
|
||||||
|
} else {
|
||||||
|
node.error(data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (p1.test(msg.payload)) {
|
||||||
|
//Color value is represented as "red,green,blue" string of bytes
|
||||||
|
var rgb = msg.payload.split(",");
|
||||||
|
|
||||||
|
//Convert color value back to HEX string for easier implementation
|
||||||
|
node.color = "#" + decimalToHex(parseInt(rgb[0])&255) +
|
||||||
|
decimalToHex(parseInt(rgb[1])&255) + decimalToHex(parseInt(rgb[2])&255);
|
||||||
|
} else {
|
||||||
|
//Sanitize color value
|
||||||
|
node.color = msg.payload.toLowerCase().replace(/\s+/g,'');
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (p1.test(msg.payload)) {
|
if (Object.size(node.led) !== 0) {
|
||||||
//Color value is represented as "red,green,blue" string of bytes
|
//Start color animation, otherwise the color is queued until current animation completes
|
||||||
var rgb = msg.payload.split(",");
|
|
||||||
|
|
||||||
//Convert color value back to HEX string for easier implementation
|
|
||||||
node.color = "#" + decimalToHex(parseInt(rgb[0])&255) +
|
|
||||||
decimalToHex(parseInt(rgb[1])&255) + decimalToHex(parseInt(rgb[2])&255);
|
|
||||||
} else {
|
|
||||||
//Sanitize color value
|
|
||||||
node.color = msg.payload.toLowerCase().replace(/\s+/g,'');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Object.size(node.led) !== 0) {
|
|
||||||
//Start color animation, otherwise the color is queued until current animation completes
|
|
||||||
if (animationComplete) {
|
|
||||||
applyColor();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//Attempt to find BlinkStick and start animation if it's found
|
|
||||||
findBlinkStick(function() {
|
|
||||||
if (animationComplete) {
|
if (animationComplete) {
|
||||||
applyColor();
|
applyColor();
|
||||||
}
|
}
|
||||||
});
|
} else {
|
||||||
|
//Attempt to find BlinkStick and start animation if it's found
|
||||||
|
findBlinkStick(function() {
|
||||||
|
if (animationComplete) {
|
||||||
|
applyColor();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
node.status({fill:"red",shape:"ring",text:"not found"});
|
||||||
|
node.error("BlinkStick not found",msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name" : "node-red-node-blinkstick",
|
"name" : "node-red-node-blinkstick",
|
||||||
"version" : "0.1.3",
|
"version" : "0.1.5",
|
||||||
"description" : "A Node-RED node to control a Blinkstick",
|
"description" : "A Node-RED node to control a Blinkstick",
|
||||||
"dependencies" : {
|
"dependencies" : {
|
||||||
"blinkstick" : "1.1.*"
|
"blinkstick" : "1.1.*"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user