Adafruit Library
Adafruit_CPlay_NeoPixel.h
1 
34 #ifndef ADAFRUIT_CPLAY_NEOPIXEL_H
35 #define ADAFRUIT_CPLAY_NEOPIXEL_H
36 
37 #ifdef ARDUINO
38  #if (ARDUINO >= 100)
39  #include <Arduino.h>
40  #else
41  #include <WProgram.h>
42  #include <pins_arduino.h>
43  #endif
44 #endif
45 
46 #ifdef TARGET_LPC1768
47  #include <Arduino.h>
48 #endif
49 
50 // The order of primary colors in the NeoPixel data stream can vary among
51 // device types, manufacturers and even different revisions of the same
52 // item. The third parameter to the Adafruit_NeoPixel constructor encodes
53 // the per-pixel byte offsets of the red, green and blue primaries (plus
54 // white, if present) in the data stream -- the following #defines provide
55 // an easier-to-use named version for each permutation. e.g. NEO_GRB
56 // indicates a NeoPixel-compatible device expecting three bytes per pixel,
57 // with the first byte transmitted containing the green value, second
58 // containing red and third containing blue. The in-memory representation
59 // of a chain of NeoPixels is the same as the data-stream order; no
60 // re-ordering of bytes is required when issuing data to the chain.
61 // Most of these values won't exist in real-world devices, but it's done
62 // this way so we're ready for it (also, if using the WS2811 driver IC,
63 // one might have their pixels set up in any weird permutation).
64 
65 // Bits 5,4 of this value are the offset (0-3) from the first byte of a
66 // pixel to the location of the red color byte. Bits 3,2 are the green
67 // offset and 1,0 are the blue offset. If it is an RGBW-type device
68 // (supporting a white primary in addition to R,G,B), bits 7,6 are the
69 // offset to the white byte...otherwise, bits 7,6 are set to the same value
70 // as 5,4 (red) to indicate an RGB (not RGBW) device.
71 // i.e. binary representation:
72 // 0bWWRRGGBB for RGBW devices
73 // 0bRRRRGGBB for RGB
74 
75 // RGB NeoPixel permutations; white and red offsets are always same
76 // Offset: W R G B
77 #define NEO_RGB ((0<<6) | (0<<4) | (1<<2) | (2))
78 #define NEO_RBG ((0<<6) | (0<<4) | (2<<2) | (1))
79 #define NEO_GRB ((1<<6) | (1<<4) | (0<<2) | (2))
80 #define NEO_GBR ((2<<6) | (2<<4) | (0<<2) | (1))
81 #define NEO_BRG ((1<<6) | (1<<4) | (2<<2) | (0))
82 #define NEO_BGR ((2<<6) | (2<<4) | (1<<2) | (0))
83 
84 // RGBW NeoPixel permutations; all 4 offsets are distinct
85 // Offset: W R G B
86 #define NEO_WRGB ((0<<6) | (1<<4) | (2<<2) | (3))
87 #define NEO_WRBG ((0<<6) | (1<<4) | (3<<2) | (2))
88 #define NEO_WGRB ((0<<6) | (2<<4) | (1<<2) | (3))
89 #define NEO_WGBR ((0<<6) | (3<<4) | (1<<2) | (2))
90 #define NEO_WBRG ((0<<6) | (2<<4) | (3<<2) | (1))
91 #define NEO_WBGR ((0<<6) | (3<<4) | (2<<2) | (1))
92 
93 #define NEO_RWGB ((1<<6) | (0<<4) | (2<<2) | (3))
94 #define NEO_RWBG ((1<<6) | (0<<4) | (3<<2) | (2))
95 #define NEO_RGWB ((2<<6) | (0<<4) | (1<<2) | (3))
96 #define NEO_RGBW ((3<<6) | (0<<4) | (1<<2) | (2))
97 #define NEO_RBWG ((2<<6) | (0<<4) | (3<<2) | (1))
98 #define NEO_RBGW ((3<<6) | (0<<4) | (2<<2) | (1))
99 
100 #define NEO_GWRB ((1<<6) | (2<<4) | (0<<2) | (3))
101 #define NEO_GWBR ((1<<6) | (3<<4) | (0<<2) | (2))
102 #define NEO_GRWB ((2<<6) | (1<<4) | (0<<2) | (3))
103 #define NEO_GRBW ((3<<6) | (1<<4) | (0<<2) | (2))
104 #define NEO_GBWR ((2<<6) | (3<<4) | (0<<2) | (1))
105 #define NEO_GBRW ((3<<6) | (2<<4) | (0<<2) | (1))
106 
107 #define NEO_BWRG ((1<<6) | (2<<4) | (3<<2) | (0))
108 #define NEO_BWGR ((1<<6) | (3<<4) | (2<<2) | (0))
109 #define NEO_BRWG ((2<<6) | (1<<4) | (3<<2) | (0))
110 #define NEO_BRGW ((3<<6) | (1<<4) | (2<<2) | (0))
111 #define NEO_BGWR ((2<<6) | (3<<4) | (1<<2) | (0))
112 #define NEO_BGRW ((3<<6) | (2<<4) | (1<<2) | (0))
113 
114 // Add NEO_KHZ400 to the color order value to indicate a 400 KHz device.
115 // All but the earliest v1 NeoPixels expect an 800 KHz data stream, this is
116 // the default if unspecified. Because flash space is very limited on ATtiny
117 // devices (e.g. Trinket, Gemma), v1 NeoPixels aren't handled by default on
118 // those chips, though it can be enabled by removing the ifndef/endif below,
119 // but code will be bigger. Conversely, can disable the NEO_KHZ400 line on
120 // other MCUs to remove v1 support and save a little space.
121 
122 #define NEO_KHZ800 0x0000
123 #ifndef __AVR_ATtiny85__
124 #define NEO_KHZ400 0x0100
125 #endif
126 
127 // If 400 KHz support is enabled, the third parameter to the constructor
128 // requires a 16-bit value (in order to select 400 vs 800 KHz speed).
129 // If only 800 KHz is enabled (as is default on ATtiny), an 8-bit value
130 // is sufficient to encode pixel color order, saving some space.
131 
132 #ifdef NEO_KHZ400
133 typedef uint16_t neoPixelType;
134 #else
135 typedef uint8_t neoPixelType;
136 #endif
137 
138 // These two tables are declared outside the Adafruit_NeoPixel class
139 // because some boards may require oldschool compilers that don't
140 // handle the C++11 constexpr keyword.
141 
142 /* A PROGMEM (flash mem) table containing 8-bit unsigned sine wave (0-255).
143  Copy & paste this snippet into a Python REPL to regenerate:
144 import math
145 for x in range(256):
146  print("{:3},".format(int((math.sin(x/128.0*math.pi)+1.0)*127.5+0.5))),
147  if x&15 == 15: print
148 */
149 
150 #if !defined(ADAFRUIT_NEOPIXEL_H)
151 static const uint8_t PROGMEM _NeoPixelSineTable[256] = {
152  128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173,
153  176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215,
154  218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244,
155  245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255,
156  255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,246,
157  245,244,243,241,240,238,237,235,234,232,230,228,226,224,222,220,
158  218,215,213,211,208,206,203,201,198,196,193,190,188,185,182,179,
159  176,173,170,167,165,162,158,155,152,149,146,143,140,137,134,131,
160  128,124,121,118,115,112,109,106,103,100, 97, 93, 90, 88, 85, 82,
161  79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40,
162  37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11,
163  10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0,
164  0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9,
165  10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35,
166  37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
167  79, 82, 85, 88, 90, 93, 97,100,103,106,109,112,115,118,121,124};
168 
169 /* Similar to above, but for an 8-bit gamma-correction table.
170  Copy & paste this snippet into a Python REPL to regenerate:
171 import math
172 gamma=2.6
173 for x in range(256):
174  print("{:3},".format(int(math.pow((x)/255.0,gamma)*255.0+0.5))),
175  if x&15 == 15: print
176 */
177 static const uint8_t PROGMEM _NeoPixelGammaTable[256] = {
178  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179  0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
180  1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
181  3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7,
182  7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12,
183  13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20,
184  20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, 29, 29,
185  30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42,
186  42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
187  58, 59, 60, 61, 62, 63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 75,
188  76, 77, 78, 80, 81, 82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96,
189  97, 99,100,102,103,105,106,108,109,111,112,114,115,117,119,120,
190  122,124,125,127,129,130,132,134,136,137,139,141,143,145,146,148,
191  150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180,
192  182,184,186,188,191,193,195,197,199,202,204,206,209,211,213,215,
193  218,220,223,225,227,230,232,235,237,240,242,245,247,250,252,255};
194 #endif
195 
201 
202  public:
203 
204  // Constructor: number of LEDs, pin number, LED type
205  Adafruit_CPlay_NeoPixel(uint16_t n, uint16_t pin=6,
206  neoPixelType type=NEO_GRB + NEO_KHZ800);
209 
210  void begin(void);
211  void show(void);
212  void setPin(uint16_t p);
213  void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b);
214  void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b,
215  uint8_t w);
216  void setPixelColor(uint16_t n, uint32_t c);
217  void fill(uint32_t c=0, uint16_t first=0, uint16_t count=0);
218  void setBrightness(uint8_t);
219  void clear(void);
220  void updateLength(uint16_t n);
221  void updateType(neoPixelType t);
235  boolean canShow(void) const { return (micros()-endTime) >= 300L; }
250  uint8_t *getPixels(void) const { return pixels; };
251  uint8_t getBrightness(void) const;
256  int16_t getPin(void) const { return pin; };
261  uint16_t numPixels(void) const { return numLEDs; }
262  uint32_t getPixelColor(uint16_t n) const;
275  static uint8_t sine8(uint8_t x) {
276  return pgm_read_byte(&_NeoPixelSineTable[x]); // 0-255 in, 0-255 out
277  }
289  static uint8_t gamma8(uint8_t x) {
290  return pgm_read_byte(&_NeoPixelGammaTable[x]); // 0-255 in, 0-255 out
291  }
303  static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
304  return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
305  }
318  static uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
319  return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
320  }
321  static uint32_t ColorHSV(uint16_t hue, uint8_t sat=255, uint8_t val=255);
334  static uint32_t gamma32(uint32_t x);
335 
336  protected:
337 
338 #ifdef NEO_KHZ400 // If 400 KHz NeoPixel support enabled...
339  boolean is800KHz;
340 #endif
341  boolean begun;
342  uint16_t numLEDs;
343  uint16_t numBytes;
344  int16_t pin;
345  uint8_t brightness;
346  uint8_t *pixels;
347  uint8_t rOffset;
348  uint8_t gOffset;
349  uint8_t bOffset;
350  uint8_t wOffset;
351  uint32_t endTime;
352 #ifdef __AVR__
353  volatile uint8_t *port;
354  uint8_t pinMask;
355 #endif
356 #ifdef ARDUINO_ARCH_STM32
357  GPIO_TypeDef *gpioPort;
358  uint32_t gpioPin;
359 #endif
360 };
361 
362 #endif // ADAFRUIT_NEOPIXEL_H
void setBrightness(uint8_t)
Adjust output brightness. Does not immediately affect what&#39;s currently displayed on the LEDs...
Definition: Adafruit_CPlay_NeoPixel.cpp:2509
boolean is800KHz
true if 800 KHz pixels
Definition: Adafruit_CPlay_NeoPixel.h:339
void updateType(neoPixelType t)
Change the pixel format of a previously-declared Adafruit_CPlay_NeoPixel strip object. If format changes from one of the RGB variants to an RGBW variant (or RGBW to RGB), the old data will be deallocated and new data is cleared. Otherwise, the old data will remain in RAM and is not reordered to the new format, so it&#39;s advisable to follow up with clear().
Definition: Adafruit_CPlay_NeoPixel.cpp:145
uint16_t numPixels(void) const
Return the number of pixels in an Adafruit_NeoPixel strip object.
Definition: Adafruit_CPlay_NeoPixel.h:261
uint8_t getBrightness(void) const
Retrieve the last-set brightness value for the strip.
Definition: Adafruit_CPlay_NeoPixel.cpp:2547
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w)
Convert separate red, green, blue and white values into a single "packed" 32-bit WRGB color...
Definition: Adafruit_CPlay_NeoPixel.h:318
boolean begun
true if begin() previously called
Definition: Adafruit_CPlay_NeoPixel.h:341
uint8_t bOffset
Index of blue byte.
Definition: Adafruit_CPlay_NeoPixel.h:349
uint32_t endTime
Latch timing reference.
Definition: Adafruit_CPlay_NeoPixel.h:351
static uint8_t gamma8(uint8_t x)
An 8-bit gamma-correction function for basic pixel brightness adjustment. Makes color transitions app...
Definition: Adafruit_CPlay_NeoPixel.h:289
int16_t pin
Output pin number (-1 if not yet set)
Definition: Adafruit_CPlay_NeoPixel.h:344
uint8_t * getPixels(void) const
Get a pointer directly to the NeoPixel data buffer in RAM. Pixel data is stored in a device-native fo...
Definition: Adafruit_CPlay_NeoPixel.h:250
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b)
Convert separate red, green and blue values into a single "packed" 32-bit RGB color.
Definition: Adafruit_CPlay_NeoPixel.h:303
int16_t getPin(void) const
Retrieve the pin number used for NeoPixel data output.
Definition: Adafruit_CPlay_NeoPixel.h:256
uint16_t numLEDs
Number of RGB LEDs in strip.
Definition: Adafruit_CPlay_NeoPixel.h:342
uint8_t rOffset
Red index within each 3- or 4-byte pixel.
Definition: Adafruit_CPlay_NeoPixel.h:347
static uint32_t gamma32(uint32_t x)
A gamma-correction function for 32-bit packed RGB or WRGB colors. Makes color transitions appear more...
Definition: Adafruit_CPlay_NeoPixel.cpp:2560
void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b)
Set a pixel&#39;s color using separate red, green and blue components. If using RGBW pixels, white will be set to 0.
Definition: Adafruit_CPlay_NeoPixel.cpp:2226
void clear(void)
Fill the whole NeoPixel strip with 0 / black / off.
Definition: Adafruit_CPlay_NeoPixel.cpp:2554
void show(void)
Transmit pixel data in RAM to NeoPixels.
Definition: Adafruit_CPlay_NeoPixel.cpp:184
uint8_t * pixels
Holds LED color values (3 or 4 bytes each)
Definition: Adafruit_CPlay_NeoPixel.h:346
void setPin(uint16_t p)
Set/change the NeoPixel output pin number. Previous pin, if any, is set to INPUT and the new pin is s...
Definition: Adafruit_CPlay_NeoPixel.cpp:2201
static uint8_t sine8(uint8_t x)
An 8-bit integer sine wave function, not directly compatible with standard trigonometric units like r...
Definition: Adafruit_CPlay_NeoPixel.h:275
~Adafruit_CPlay_NeoPixel()
Deallocate Adafruit_CPlay_NeoPixel object, set data pin back to INPUT.
Definition: Adafruit_CPlay_NeoPixel.cpp:89
uint32_t getPixelColor(uint16_t n) const
Query the color of a previously-set pixel.
Definition: Adafruit_CPlay_NeoPixel.cpp:2455
uint8_t wOffset
Index of white (==rOffset if no white)
Definition: Adafruit_CPlay_NeoPixel.h:350
uint16_t numBytes
Size of &#39;pixels&#39; buffer below.
Definition: Adafruit_CPlay_NeoPixel.h:343
boolean canShow(void) const
Check whether a call to show() will start sending data immediately or will &#39;block&#39; for a required int...
Definition: Adafruit_CPlay_NeoPixel.h:235
void begin(void)
Configure NeoPixel pin for output.
Definition: Adafruit_CPlay_NeoPixel.cpp:97
static uint32_t ColorHSV(uint16_t hue, uint8_t sat=255, uint8_t val=255)
Convert hue, saturation and value into a packed 32-bit RGB color that can be passed to setPixelColor(...
Definition: Adafruit_CPlay_NeoPixel.cpp:2368
uint8_t brightness
Strip brightness 0-255 (stored as +1)
Definition: Adafruit_CPlay_NeoPixel.h:345
Class that stores state and functions for interacting with Adafruit NeoPixels and compatible devices...
Definition: Adafruit_CPlay_NeoPixel.h:200
void fill(uint32_t c=0, uint16_t first=0, uint16_t count=0)
Fill all or part of the NeoPixel strip with a color.
Definition: Adafruit_CPlay_NeoPixel.cpp:2323
Adafruit_CPlay_NeoPixel(void)
"Empty" NeoPixel constructor when length, pin and/or pixel type are not known at compile-time, and must be initialized later with updateType(), updateLength() and setPin().
Definition: Adafruit_CPlay_NeoPixel.cpp:78
void updateLength(uint16_t n)
Change the length of a previously-declared Adafruit_CPlay_NeoPixel strip object. Old data is dealloca...
Definition: Adafruit_CPlay_NeoPixel.cpp:115
uint8_t gOffset
Index of green byte.
Definition: Adafruit_CPlay_NeoPixel.h:348