This is a recent thing I got working. Thanks to all who helped me with the syntax error I made.
The problem now is it is a bit too good at doing what I want it to do.
The idea is the R
G
and B
values may be used for different things and so if the LED is showing a RED event and a BLUE event comes in, it becomes RED AND BLUE.
The problem is that if another RED value comes in: the RED value is added.
This can quickly go over the allowed 255 for maximum value.
I would prefer if the values were anded together.
So the existing RED value is anded. So if the value hasn't changed, the RED value won't change.
Given that all the events that use RED are the same value.
Code:
if (msg.debug != undefined)
{
context.set("DEBUG",msg.debug);
node.warn("Debug set to " + msg.debug);
return;
}
let debug = context.get("DEBUG") || 0;
// Get LED number from `payload`
//let led = parseInt(msg.payload.split(",")[1]);
let payload = msg.payload
let blah = payload.split(",")[1];
let led = parseInt(blah);
if (debug == 1)
node.warn("LED " + led);
if (led > 4)
{
if (debug == 1)
node.warn("LED Colour adjustment happening");
// Get RGB values from `payload`
let n_red = parseInt(payload.split(",")[2])
let n_green = parseInt(payload.split(",")[3])
let n_blue = parseInt(payload.split(",")[4])
if (debug == 1)
{
node.warn("==== New values below ====");
node.warn("R " + n_red + " G " + n_green + " B " + n_blue);
}
let existing_value = msg.existing[led];
// Now get existing RGB values.
if (existing_value == undefined)
{
existing_value = "0,0,0";
}
let x_red = parseInt(existing_value.split(",")[0]);
let x_green = parseInt(existing_value.split(",")[1]);
let x_blue = parseInt(existing_value.split(",")[2]);
if (debug == 1)
{
node.warn("---- Exiting values below ----");
node.warn("R " + x_red + " G " + x_green + " B " + x_blue);
}
let red = n_red + x_red;
let green = n_green + x_green;
let blue = n_blue + x_blue;
msg.payload = "rgb," + led + "," + red + "," + green + "," + blue
}
//if (msg.payload.who == "All clear") // Old code
if (msg.wipe == "WIPE")
{
//
// Wipe the alarm LEDs
//
// work needed
msg.payload = "rgb," + led + ",0,0,0";
}
if (msg.clear == "CLEAR")
{
// `flow.LEDSTRIP`
flow.set("LEDSTRIP["+led+"],0,0,0")
}
return msg;
I'm guessing it will be simple. I just am not sure how to do it.
I'm suspicious it is &&
rather than the +
in the lines:
let red = n_red + x_red;
let green = n_green + x_green;
let blue = n_blue + x_blue;
Correct?
So they would become:
let red = n_red && x_red;
let green = n_green && x_green;
let blue = n_blue && x_blue;
Yeah: dumb question.