Electric Imp releases major update to its WS2812 RGB LED library

We have just published a major update to our WS2812 library – code you can very easily incorporate into your own to support WS2812 RGB LEDs, which Adafruit calls NeoPixels.

The library moves up to version 2.0.0. As per our version numbering scheme, that means changes to the library’s API which will not work with existing software. Don’t worry if your application uses an earlier version: we’ll continue to provide it so that it will continue to be added to your code when you make a new build, but we think it’ll be worth your while taking a look at the changes and updating your code accordingly.

The most visible change are to the names of the library’s methods, updated to give the class a nomenclature in line with typical graphics libraries, whatever their platform.

As before we have three methods, but they are now set(), fill() and draw(). You can read the full documentation at GitHub, but briefly, set() sets the color of a specific LED; fill() applies a color to a range of LEDs (by default all of them), and draw() writes the stored color data to the LEDs themselves.

In addition, all of these methods return the class instance, this, allowing you to chain methods using dot syntax:

`#require "ws2812.class.nut:2.0.0"

// Configure the SPI bus
spi <- hardware.spi257;
spi.configure(MSB_FIRST, 7500);

// Instantiate LED array with 5 pixels
pixels <- WS2812(spi, 5);

// Set the array’s initial state:
// LED 0 - red
// LED 1 - green
// LEDs 2-4 - blue
pixels.set(0, [255,0,0]).set(1, [0,255,0]).fill([0,0,255], 2, 4).draw();`

The result is a much more streamlined class that is more compatible with wider graphics coding standards. As a bonus, it contains a number of under-the-hood improvements too.

As the code snippet above shows, to add the library to your code, just add the line

`#require "ws2812.class.nut:2.0.0"`

to the top of your own device code.