A library for manipulating NeoPixel RGB LEDs for the:
Particle Core, Photon, P1, Electron, Argon, Boron, Xenon and RedBear Duo
Implementation based on Adafruit's NeoPixel Library.
The most common kinds are WS2812/WS2813 (6-pin part), WS2812B (4-pin part) and SK6812RGBW (3 colors + white).
PIXEL_TYPE
)Set up the hardware:
Flash the rainbow example. With the
Particle CLI
do particle flash <my_device> examples/a-rainbow
Adapt it to your needs while keeping this general structure:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void setup() {
strip.begin();
strip.show();
}
void loop() {
// change your pixel colors and call strip.show() again
}
Adafruit_NeoPixel
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 10
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
Creates an object to interact wth a NeoPixel strip.
PIXEL_COUNT
is the number of pixels in strip.
Note: for some stripes like those with the TM1829, you need to count the number of segments, i.e. the number of controllers in your stripe, not the number of individual LEDs!
PIXEL_PIN
is the pin number where your NeoPixel are connected (A0-A7, D0-D7, etc). If omitted, D2 is used.
On Photon, Electron, P1, Core and Duo, any pin can be used for Neopixel.
On the Argon, Boron and Xenon, only these pins can be used for Neopixel:
In addition on the Argon/Boron/Xenon, only one pin per group can be used at a time. So it's OK to have one Adafruit_NeoPixel instance on pin D2 and another one on pin A2, but it's not possible to have one on pin A0 and another one on pin A1.
PIXEL_TYPE
is the type of LED, one of WS2811, WS2812, WS2812B, WS2812B2, WS2813, TM1803, TM1829, SK6812RGBW. If omitted, WS2812B is used.
Note: For legacy 50us reset pulse timing on WS2812/WS2812B or WS2812B2, select WS2812B_FAST or WS2812B2_FAST respectively. Otherwise, 300us timing will be used.
Note: RGB order is automatically applied to WS2811, WS2812/WS2812B/WS2812B2/WS2813/TM1803 is GRB order.
begin
strip.begin();
Sets up the pin used for the NeoPixel strip.
setPixelColor
setColor
strip.setPixelColor(num, red, green, blue);
strip.setPixelColor(num, red, green, blue, white);
strip.setPixelColor(num, color);
strip.setColor(num, red, green, blue);
strip.setColor(num, red, green, blue, white);
Set the color of LED number num
(0 to PIXEL_COUNT-1
). red
,
green
, blue
, white
are between 0 and 255. White is only used for
RGBW type pixels. color
is a color returned from Color
.
The brightness set with setBrightness
will modify the color before it
is applied to the LED.
show
strip.show();
Displays the colors on the NeoPixel strip that were set with setPixelColor
and other calls that change the color of LEDs.
This function takes some time to run (more time the more LEDs you have) and disables interrupts while running.
clear
strip.clear();
Set all LED color to off. Will take effect on next show()
.
setBrightness
strip.setBrightness(brightness);
Make the LED less bright. brightness
is from 0 (off) to 255 (max brightness) and defaults to 255.
This factor is not linear: 128 is not visibly half as bright as 255 but almost as bright.
getBrightness
uint8_t brightness = strip.getBrightness();
Get the current brightness.
setColorScaled
strip.setColorScaled(num, red, green, blue, scaling);
strip.setColorScaled(num, red, green, blue, white, scaling);
Set the color of LED number num
and scale that color non-linearly according to the scaling
parameter (0 to 255).
setColorDimmed
strip.setColorDimmed(num, red, green, blue, brightness);
strip.setColorDimmed(num, red, green, blue, white, brightness);
Set the color of LED number num
and dim that color linearly according to the brightness
parameter (0 to 255). In this case 128 should look half as bright as 255.
Color
uint32_t color = strip.Color(red, green, blue);
uint32_t color = strip.Color(red, green, blue, white);
Make a color from component colors. Useful if you want to store colors in a variable or pass them as function arguments.
getPixelColor
uint32_t color = strip.getPixelColor();
Get the current color of an LED in the same format as Color
.
setPin
strip.setPin(pinNumber);
Change the pin used for the NeoPixel strip.
updateLength
strip.updateLength(n);
Change the number of LEDs in the NeoPixel strip.
getPixels
uint8_t *pixels = strip.getPixels();
Get the raw color data for the LEDs.
getNumLeds
numPixels
uint16_t n = strip.getNumLeds();
uint16_t n = strip.numPixels();
Get the number of LEDs in the NeoPixel strip. numPixels
is an alias for getNumLeds
.
getPixelColor()
to move pixel data around when you are also using setBrightness()
. When the brightness is set, all setPixelColor()
calls will end up scaling colors to dim them before they are stored in memory. When using getPixelColor()
the stored dimmed color is rescaled back up to the original color. However, due to some loss of precision with the math, it is not possible to recreate this color data perfectly. This is especially true with low brightness values. If you get
and set
color data repeatedly with a dimmed pixel, it will eventually continue to decrease in value until it is equal to zero.setPixelColor()
first with fresh un-dimmed color data, then call setBrightness()
, and finally show()
.Copyright 2014-2018 Technobly, Julien Vanier, Cullen Shane, Phil Burgess
Released under the LGPL license