Turn on specific LED using NeoPixel module for Espruino?

212 Views Asked by At

I am trying to turn on a specific LED using the NeoPixel module. How it works is really easy: Parse it a 2D array of RGB colors. Here's an example:

require("neopixel").write(NodeMCU.B2, [[255, 0, 0], [0, 255, 0], [0, 0, 255]]);

That will turn on the first three LEDs with red, green, and blue. I want to have a function, where I can do something like:

function single(number, color) {
    require("neopixel").write(NodeMCU.B2, number, color);
}

single(0, [255, 0, 0]);
single(1, [0, 255, 0]);
single(2, [0, 0, 255]);

Which would do the exact same as above. Now you might ask: Why would you want that? Well:

  1. I want it to remember the last "configuration" of LEDs, so I can update it over time
  2. If I want to turn off all my 100+ LEDs and just turn on the last few (or the ones in the middle), I wouldn't have to parse the write() function 100+ LEDs, where most of them are black

Is something like that possible, or would I have to do some magic in order to remember the last LED configuration?

1

There are 1 best solutions below

3
On BEST ANSWER

Yes, totally - it's worth looking at the neopixel docs at http://www.espruino.com/WS2811 as they suggest you use an array to store the current state.

Once you have that array - called arr here - you can use the .set method to set the 3 elements at the right position (3x the number, because RGB), and then can resend the whole array.

var arr = new Uint8ClampedArray(NUM_OF_LEDS*3);

function single(number, color) {
  arr.set(color, number*3);
  require("neopixel").write(NodeMCU.B2, arr);
}