|
| 1 | +/*-------------------------------------------------------------------- |
| 2 | + This file is part of the Adafruit NeoPixel library. |
| 3 | +
|
| 4 | + NeoPixel is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU Lesser General Public License as |
| 6 | + published by the Free Software Foundation, either version 3 of |
| 7 | + the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + NeoPixel is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with NeoPixel. If not, see |
| 16 | + <http://www.gnu.org/licenses/>. |
| 17 | + --------------------------------------------------------------------*/ |
| 18 | + |
| 19 | +#ifndef ADAFRUIT_NEOPIXEL_H |
| 20 | +#define ADAFRUIT_NEOPIXEL_H |
| 21 | + |
| 22 | +#if (ARDUINO >= 100) |
| 23 | + #include <Arduino.h> |
| 24 | +#else |
| 25 | + #include <WProgram.h> |
| 26 | + #include <pins_arduino.h> |
| 27 | +#endif |
| 28 | + |
| 29 | +// The order of primary colors in the NeoPixel data stream can vary |
| 30 | +// among device types, manufacturers and even different revisions of |
| 31 | +// the same item. The third parameter to the Adafruit_NeoPixel |
| 32 | +// constructor encodes the per-pixel byte offsets of the red, green |
| 33 | +// and blue primaries (plus white, if present) in the data stream -- |
| 34 | +// the following #defines provide an easier-to-use named version for |
| 35 | +// each permutation. e.g. NEO_GRB indicates a NeoPixel-compatible |
| 36 | +// device expecting three bytes per pixel, with the first byte |
| 37 | +// containing the green value, second containing red and third |
| 38 | +// containing blue. The in-memory representation of a chain of |
| 39 | +// NeoPixels is the same as the data-stream order; no re-ordering of |
| 40 | +// bytes is required when issuing data to the chain. |
| 41 | + |
| 42 | +// Bits 5,4 of this value are the offset (0-3) from the first byte of |
| 43 | +// a pixel to the location of the red color byte. Bits 3,2 are the |
| 44 | +// green offset and 1,0 are the blue offset. If it is an RGBW-type |
| 45 | +// device (supporting a white primary in addition to R,G,B), bits 7,6 |
| 46 | +// are the offset to the white byte...otherwise, bits 7,6 are set to |
| 47 | +// the same value as 5,4 (red) to indicate an RGB (not RGBW) device. |
| 48 | +// i.e. binary representation: |
| 49 | +// 0bWWRRGGBB for RGBW devices |
| 50 | +// 0bRRRRGGBB for RGB |
| 51 | + |
| 52 | +// RGB NeoPixel permutations; white and red offsets are always same |
| 53 | +// Offset: W R G B |
| 54 | +#define NEO_RGB ((0 << 6) | (0 << 4) | (1 << 2) | (2)) |
| 55 | +#define NEO_RBG ((0 << 6) | (0 << 4) | (2 << 2) | (1)) |
| 56 | +#define NEO_GRB ((1 << 6) | (1 << 4) | (0 << 2) | (2)) |
| 57 | +#define NEO_GBR ((2 << 6) | (2 << 4) | (0 << 2) | (1)) |
| 58 | +#define NEO_BRG ((1 << 6) | (1 << 4) | (2 << 2) | (0)) |
| 59 | +#define NEO_BGR ((2 << 6) | (2 << 4) | (1 << 2) | (0)) |
| 60 | + |
| 61 | +// RGBW NeoPixel permutations; all 4 offsets are distinct |
| 62 | +// Offset: W R G B |
| 63 | +#define NEO_WRGB ((0 << 6) | (1 << 4) | (2 << 2) | (3)) |
| 64 | +#define NEO_WRBG ((0 << 6) | (1 << 4) | (3 << 2) | (2)) |
| 65 | +#define NEO_WGRB ((0 << 6) | (2 << 4) | (1 << 2) | (3)) |
| 66 | +#define NEO_WGBR ((0 << 6) | (3 << 4) | (1 << 2) | (2)) |
| 67 | +#define NEO_WBRG ((0 << 6) | (2 << 4) | (3 << 2) | (1)) |
| 68 | +#define NEO_WBGR ((0 << 6) | (3 << 4) | (2 << 2) | (1)) |
| 69 | + |
| 70 | +#define NEO_RWGB ((1 << 6) | (0 << 4) | (2 << 2) | (3)) |
| 71 | +#define NEO_RWBG ((1 << 6) | (0 << 4) | (3 << 2) | (2)) |
| 72 | +#define NEO_RGWB ((2 << 6) | (0 << 4) | (1 << 2) | (3)) |
| 73 | +#define NEO_RGBW ((3 << 6) | (0 << 4) | (1 << 2) | (2)) |
| 74 | +#define NEO_RBWG ((2 << 6) | (0 << 4) | (3 << 2) | (1)) |
| 75 | +#define NEO_RBGW ((3 << 6) | (0 << 4) | (2 << 2) | (1)) |
| 76 | + |
| 77 | +#define NEO_GWRB ((1 << 6) | (2 << 4) | (0 << 2) | (3)) |
| 78 | +#define NEO_GWBR ((1 << 6) | (3 << 4) | (0 << 2) | (2)) |
| 79 | +#define NEO_GRWB ((2 << 6) | (1 << 4) | (0 << 2) | (3)) |
| 80 | +#define NEO_GRBW ((3 << 6) | (1 << 4) | (0 << 2) | (2)) |
| 81 | +#define NEO_GBWR ((2 << 6) | (3 << 4) | (0 << 2) | (1)) |
| 82 | +#define NEO_GBRW ((3 << 6) | (2 << 4) | (0 << 2) | (1)) |
| 83 | + |
| 84 | +#define NEO_BWRG ((1 << 6) | (2 << 4) | (3 << 2) | (0)) |
| 85 | +#define NEO_BWGR ((1 << 6) | (3 << 4) | (2 << 2) | (0)) |
| 86 | +#define NEO_BRWG ((2 << 6) | (1 << 4) | (3 << 2) | (0)) |
| 87 | +#define NEO_BRGW ((3 << 6) | (1 << 4) | (2 << 2) | (0)) |
| 88 | +#define NEO_BGWR ((2 << 6) | (3 << 4) | (1 << 2) | (0)) |
| 89 | +#define NEO_BGRW ((3 << 6) | (2 << 4) | (1 << 2) | (0)) |
| 90 | + |
| 91 | +// Add NEO_KHZ400 to the color order value to indicate a 400 KHz |
| 92 | +// device. All but the earliest v1 NeoPixels expect an 800 KHz data |
| 93 | +// stream, this is the default if unspecified. Because flash space |
| 94 | +// is very limited on ATtiny devices (e.g. Trinket, Gemma), v1 |
| 95 | +// NeoPixels aren't handled by default on those chips, though it can |
| 96 | +// be enabled by removing the ifndef/endif below -- but code will be |
| 97 | +// bigger. Conversely, can disable the NEO_KHZ400 line on other MCUs |
| 98 | +// to remove v1 support and save a little space. |
| 99 | + |
| 100 | +#define NEO_KHZ800 0x0000 // 800 KHz datastream |
| 101 | +#ifndef __AVR_ATtiny85__ |
| 102 | +#define NEO_KHZ400 0x0100 // 400 KHz datastream |
| 103 | +#endif |
| 104 | + |
| 105 | +// If 400 KHz support is enabled, the third parameter to the constructor |
| 106 | +// requires a 16-bit value (in order to select 400 vs 800 KHz speed). |
| 107 | +// If only 800 KHz is enabled (as is default on ATtiny), an 8-bit value |
| 108 | +// is sufficient to encode pixel color order, saving some space. |
| 109 | + |
| 110 | +#ifdef NEO_KHZ400 |
| 111 | +typedef uint8_t neoPixelType; |
| 112 | +#else |
| 113 | +typedef uint16_t neoPixelType; |
| 114 | +#endif |
| 115 | + |
| 116 | +class Adafruit_NeoPixel { |
| 117 | + |
| 118 | + public: |
| 119 | + |
| 120 | + // Constructor: number of LEDs, pin number, LED type |
| 121 | + Adafruit_NeoPixel(uint16_t n, uint8_t p=6, neoPixelType t=NEO_GRB + NEO_KHZ800); |
| 122 | + Adafruit_NeoPixel(void); |
| 123 | + ~Adafruit_NeoPixel(); |
| 124 | + |
| 125 | + void |
| 126 | + begin(void), |
| 127 | + show(void), |
| 128 | + setPin(uint8_t p), |
| 129 | + setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b), |
| 130 | + setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w), |
| 131 | + setPixelColor(uint16_t n, uint32_t c), |
| 132 | + setBrightness(uint8_t), |
| 133 | + clear(), |
| 134 | + updateLength(uint16_t n), |
| 135 | + updateType(neoPixelType t); |
| 136 | + uint8_t |
| 137 | + *getPixels(void) const, |
| 138 | + getBrightness(void) const; |
| 139 | + uint16_t |
| 140 | + numPixels(void) const; |
| 141 | + static uint32_t |
| 142 | + Color(uint8_t r, uint8_t g, uint8_t b), |
| 143 | + Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w); |
| 144 | + uint32_t |
| 145 | + getPixelColor(uint16_t n) const; |
| 146 | + inline bool |
| 147 | + canShow(void) { return (micros() - endTime) >= 50L; } |
| 148 | + |
| 149 | + private: |
| 150 | + |
| 151 | + boolean |
| 152 | +#ifdef NEO_KHZ400 // If 400 KHz NeoPixel support enabled... |
| 153 | + is800KHz, // ...true if 800 KHz pixels |
| 154 | +#endif |
| 155 | + begun; // true if begin() previously called |
| 156 | + uint16_t |
| 157 | + numLEDs, // Number of RGB LEDs in strip |
| 158 | + numBytes; // Size of 'pixels' buffer below (3 or 4 bytes/pixel) |
| 159 | + int8_t |
| 160 | + pin; // Output pin number (-1 if not yet set) |
| 161 | + uint8_t |
| 162 | + brightness, |
| 163 | + *pixels, // Holds LED color values (3 or 4 bytes each) |
| 164 | + rOffset, // Index of red byte within each 3- or 4-byte pixel |
| 165 | + gOffset, // Index of green byte |
| 166 | + bOffset, // Index of blue byte |
| 167 | + wOffset; // Index of white byte (same as rOffset if no white) |
| 168 | + uint32_t |
| 169 | + endTime; // Latch timing reference |
| 170 | +#ifdef __AVR__ |
| 171 | + volatile uint8_t |
| 172 | + *port; // Output PORT register |
| 173 | + uint8_t |
| 174 | + pinMask; // Output PORT bitmask |
| 175 | +#endif |
| 176 | + |
| 177 | +}; |
| 178 | + |
| 179 | +#endif // ADAFRUIT_NEOPIXEL_H |
0 commit comments