diff options
author | Daniel Friesel <derf@finalrewind.org> | 2019-09-02 20:02:01 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2019-09-02 20:02:01 +0200 |
commit | 7cb855d4c47a9d75abd60a80aab0ca6630bc1549 (patch) | |
tree | ee2bf1d1f07072b71300582e7d1d63eb184b4d63 | |
parent | 35a62370fb5d9d44d6e7ce24a76472ebc6573a15 (diff) |
Add Adafruit NeoPixel library
-rw-r--r-- | include/arch/arduino-nano/driver/neopixel.h | 347 | ||||
-rw-r--r-- | src/arch/arduino-nano/Makefile.inc | 4 | ||||
-rw-r--r-- | src/arch/arduino-nano/driver/neopixel.cc | 1489 |
3 files changed, 1840 insertions, 0 deletions
diff --git a/include/arch/arduino-nano/driver/neopixel.h b/include/arch/arduino-nano/driver/neopixel.h new file mode 100644 index 0000000..6416124 --- /dev/null +++ b/include/arch/arduino-nano/driver/neopixel.h @@ -0,0 +1,347 @@ +/*! + * @file Adafruit_NeoPixel.h + * + * This is part of Adafruit's NeoPixel library for the Arduino platform, + * allowing a broad range of microcontroller boards (most AVR boards, + * many ARM devices, ESP8266 and ESP32, among others) to control Adafruit + * NeoPixels, FLORA RGB Smart Pixels and compatible devices -- WS2811, + * WS2812, WS2812B, SK6812, etc. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing products + * from Adafruit! + * + * Written by Phil "Paint Your Dragon" Burgess for Adafruit Industries, + * with contributions by PJRC, Michael Miller and other members of the + * open source community. + * + * This file is part of the Adafruit_NeoPixel library. + * + * Adafruit_NeoPixel is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * Adafruit_NeoPixel is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with NeoPixel. If not, see + * <http://www.gnu.org/licenses/>. + * + */ + +#ifndef ADAFRUIT_NEOPIXEL_H +#define ADAFRUIT_NEOPIXEL_H + +#include <avr/pgmspace.h> +#include "driver/gpio.h" + +// The order of primary colors in the NeoPixel data stream can vary among +// device types, manufacturers and even different revisions of the same +// item. The third parameter to the Adafruit_NeoPixel constructor encodes +// the per-pixel byte offsets of the red, green and blue primaries (plus +// white, if present) in the data stream -- the following #defines provide +// an easier-to-use named version for each permutation. e.g. NEO_GRB +// indicates a NeoPixel-compatible device expecting three bytes per pixel, +// with the first byte transmitted containing the green value, second +// containing red and third containing blue. The in-memory representation +// of a chain of NeoPixels is the same as the data-stream order; no +// re-ordering of bytes is required when issuing data to the chain. +// Most of these values won't exist in real-world devices, but it's done +// this way so we're ready for it (also, if using the WS2811 driver IC, +// one might have their pixels set up in any weird permutation). + +// Bits 5,4 of this value are the offset (0-3) from the first byte of a +// pixel to the location of the red color byte. Bits 3,2 are the green +// offset and 1,0 are the blue offset. If it is an RGBW-type device +// (supporting a white primary in addition to R,G,B), bits 7,6 are the +// offset to the white byte...otherwise, bits 7,6 are set to the same value +// as 5,4 (red) to indicate an RGB (not RGBW) device. +// i.e. binary representation: +// 0bWWRRGGBB for RGBW devices +// 0bRRRRGGBB for RGB + +// RGB NeoPixel permutations; white and red offsets are always same +// Offset: W R G B +#define NEO_RGB ((0<<6) | (0<<4) | (1<<2) | (2)) ///< Transmit as R,G,B +#define NEO_RBG ((0<<6) | (0<<4) | (2<<2) | (1)) ///< Transmit as R,B,G +#define NEO_GRB ((1<<6) | (1<<4) | (0<<2) | (2)) ///< Transmit as G,R,B +#define NEO_GBR ((2<<6) | (2<<4) | (0<<2) | (1)) ///< Transmit as G,B,R +#define NEO_BRG ((1<<6) | (1<<4) | (2<<2) | (0)) ///< Transmit as B,R,G +#define NEO_BGR ((2<<6) | (2<<4) | (1<<2) | (0)) ///< Transmit as B,G,R + +// RGBW NeoPixel permutations; all 4 offsets are distinct +// Offset: W R G B +#define NEO_WRGB ((0<<6) | (1<<4) | (2<<2) | (3)) ///< Transmit as W,R,G,B +#define NEO_WRBG ((0<<6) | (1<<4) | (3<<2) | (2)) ///< Transmit as W,R,B,G +#define NEO_WGRB ((0<<6) | (2<<4) | (1<<2) | (3)) ///< Transmit as W,G,R,B +#define NEO_WGBR ((0<<6) | (3<<4) | (1<<2) | (2)) ///< Transmit as W,G,B,R +#define NEO_WBRG ((0<<6) | (2<<4) | (3<<2) | (1)) ///< Transmit as W,B,R,G +#define NEO_WBGR ((0<<6) | (3<<4) | (2<<2) | (1)) ///< Transmit as W,B,G,R + +#define NEO_RWGB ((1<<6) | (0<<4) | (2<<2) | (3)) ///< Transmit as R,W,G,B +#define NEO_RWBG ((1<<6) | (0<<4) | (3<<2) | (2)) ///< Transmit as R,W,B,G +#define NEO_RGWB ((2<<6) | (0<<4) | (1<<2) | (3)) ///< Transmit as R,G,W,B +#define NEO_RGBW ((3<<6) | (0<<4) | (1<<2) | (2)) ///< Transmit as R,G,B,W +#define NEO_RBWG ((2<<6) | (0<<4) | (3<<2) | (1)) ///< Transmit as R,B,W,G +#define NEO_RBGW ((3<<6) | (0<<4) | (2<<2) | (1)) ///< Transmit as R,B,G,W + +#define NEO_GWRB ((1<<6) | (2<<4) | (0<<2) | (3)) ///< Transmit as G,W,R,B +#define NEO_GWBR ((1<<6) | (3<<4) | (0<<2) | (2)) ///< Transmit as G,W,B,R +#define NEO_GRWB ((2<<6) | (1<<4) | (0<<2) | (3)) ///< Transmit as G,R,W,B +#define NEO_GRBW ((3<<6) | (1<<4) | (0<<2) | (2)) ///< Transmit as G,R,B,W +#define NEO_GBWR ((2<<6) | (3<<4) | (0<<2) | (1)) ///< Transmit as G,B,W,R +#define NEO_GBRW ((3<<6) | (2<<4) | (0<<2) | (1)) ///< Transmit as G,B,R,W + +#define NEO_BWRG ((1<<6) | (2<<4) | (3<<2) | (0)) ///< Transmit as B,W,R,G +#define NEO_BWGR ((1<<6) | (3<<4) | (2<<2) | (0)) ///< Transmit as B,W,G,R +#define NEO_BRWG ((2<<6) | (1<<4) | (3<<2) | (0)) ///< Transmit as B,R,W,G +#define NEO_BRGW ((3<<6) | (1<<4) | (2<<2) | (0)) ///< Transmit as B,R,G,W +#define NEO_BGWR ((2<<6) | (3<<4) | (1<<2) | (0)) ///< Transmit as B,G,W,R +#define NEO_BGRW ((3<<6) | (2<<4) | (1<<2) | (0)) ///< Transmit as B,G,R,W + +// Add NEO_KHZ400 to the color order value to indicate a 400 KHz device. +// All but the earliest v1 NeoPixels expect an 800 KHz data stream, this is +// the default if unspecified. Because flash space is very limited on ATtiny +// devices (e.g. Trinket, Gemma), v1 NeoPixels aren't handled by default on +// those chips, though it can be enabled by removing the ifndef/endif below, +// but code will be bigger. Conversely, can disable the NEO_KHZ400 line on +// other MCUs to remove v1 support and save a little space. + +#define NEO_KHZ800 0x0000 ///< 800 KHz data transmission +#ifndef __AVR_ATtiny85__ +#define NEO_KHZ400 0x0100 ///< 400 KHz data transmission +#endif + +// If 400 KHz support is enabled, the third parameter to the constructor +// requires a 16-bit value (in order to select 400 vs 800 KHz speed). +// If only 800 KHz is enabled (as is default on ATtiny), an 8-bit value +// is sufficient to encode pixel color order, saving some space. + +#ifdef NEO_KHZ400 +typedef uint16_t neoPixelType; ///< 3rd arg to Adafruit_NeoPixel constructor +#else +typedef uint8_t neoPixelType; ///< 3rd arg to Adafruit_NeoPixel constructor +#endif + +// These two tables are declared outside the Adafruit_NeoPixel class +// because some boards may require oldschool compilers that don't +// handle the C++11 constexpr keyword. + +/* A PROGMEM (flash mem) table containing 8-bit unsigned sine wave (0-255). + Copy & paste this snippet into a Python REPL to regenerate: +import math +for x in range(256): + print("{:3},".format(int((math.sin(x/128.0*math.pi)+1.0)*127.5+0.5))), + if x&15 == 15: print +*/ +static const uint8_t PROGMEM _NeoPixelSineTable[256] = { + 128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173, + 176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215, + 218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244, + 245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255, + 255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,246, + 245,244,243,241,240,238,237,235,234,232,230,228,226,224,222,220, + 218,215,213,211,208,206,203,201,198,196,193,190,188,185,182,179, + 176,173,170,167,165,162,158,155,152,149,146,143,140,137,134,131, + 128,124,121,118,115,112,109,106,103,100, 97, 93, 90, 88, 85, 82, + 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40, + 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, + 10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, + 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, + 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76, + 79, 82, 85, 88, 90, 93, 97,100,103,106,109,112,115,118,121,124}; + +/* Similar to above, but for an 8-bit gamma-correction table. + Copy & paste this snippet into a Python REPL to regenerate: +import math +gamma=2.6 +for x in range(256): + print("{:3},".format(int(math.pow((x)/255.0,gamma)*255.0+0.5))), + if x&15 == 15: print +*/ +static const uint8_t PROGMEM _NeoPixelGammaTable[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, + 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, + 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, + 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, + 20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, 29, 29, + 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, + 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 75, + 76, 77, 78, 80, 81, 82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96, + 97, 99,100,102,103,105,106,108,109,111,112,114,115,117,119,120, + 122,124,125,127,129,130,132,134,136,137,139,141,143,145,146,148, + 150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180, + 182,184,186,188,191,193,195,197,199,202,204,206,209,211,213,215, + 218,220,223,225,227,230,232,235,237,240,242,245,247,250,252,255}; + +/*! + @brief Class that stores state and functions for interacting with + Adafruit NeoPixels and compatible devices. +*/ +class Adafruit_NeoPixel { + + public: + + // Constructor: number of LEDs, pin number, LED type + Adafruit_NeoPixel(uint16_t n, uint16_t pin=6, + neoPixelType type=NEO_GRB + NEO_KHZ800); + Adafruit_NeoPixel(void); + ~Adafruit_NeoPixel(); + + void setup(void); + void show(void); + void setPin(uint16_t p); + void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b); + void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, + uint8_t w); + void setPixelColor(uint16_t n, uint32_t c); + void fill(uint32_t c=0, uint16_t first=0, uint16_t count=0); + void setBrightness(uint8_t); + void clear(void); + void updateLength(uint16_t n); + void updateType(neoPixelType t); + /*! + @brief Check whether a call to show() will start sending data + immediately or will 'block' for a required interval. NeoPixels + require a short quiet time (about 300 microseconds) after the + last bit is received before the data 'latches' and new data can + start being received. Usually one's sketch is implicitly using + this time to generate a new frame of animation...but if it + finishes very quickly, this function could be used to see if + there's some idle time available for some low-priority + concurrent task. + @return 1 or true if show() will start sending immediately, 0 or false + if show() would block (meaning some idle time is available). + */ + bool canShow(void) const { return 1; /*(micros()-endTime) >= 300L;*/ } + /*! + @brief Get a pointer directly to the NeoPixel data buffer in RAM. + Pixel data is stored in a device-native format (a la the NEO_* + constants) and is not translated here. Applications that access + this buffer will need to be aware of the specific data format + and handle colors appropriately. + @return Pointer to NeoPixel buffer (uint8_t* array). + @note This is for high-performance applications where calling + setPixelColor() on every single pixel would be too slow (e.g. + POV or light-painting projects). There is no bounds checking + on the array, creating tremendous potential for mayhem if one + writes past the ends of the buffer. Great power, great + responsibility and all that. + */ + uint8_t *getPixels(void) const { return pixels; }; + uint8_t getBrightness(void) const; + /*! + @brief Retrieve the pin number used for NeoPixel data output. + @return Arduino pin number (-1 if not set). + */ + int16_t getPin(void) const { return pin; }; + /*! + @brief Return the number of pixels in an Adafruit_NeoPixel strip object. + @return Pixel count (0 if not set). + */ + uint16_t numPixels(void) const { return numLEDs; } + uint32_t getPixelColor(uint16_t n) const; + /*! + @brief An 8-bit integer sine wave function, not directly compatible + with standard trigonometric units like radians or degrees. + @param x Input angle, 0-255; 256 would loop back to zero, completing + the circle (equivalent to 360 degrees or 2 pi radians). + One can therefore use an unsigned 8-bit variable and simply + add or subtract, allowing it to overflow/underflow and it + still does the expected contiguous thing. + @return Sine result, 0 to 255, or -128 to +127 if type-converted to + a signed int8_t, but you'll most likely want unsigned as this + output is often used for pixel brightness in animation effects. + */ + static uint8_t sine8(uint8_t x) { + return pgm_read_byte(&_NeoPixelSineTable[x]); // 0-255 in, 0-255 out + } + /*! + @brief An 8-bit gamma-correction function for basic pixel brightness + adjustment. Makes color transitions appear more perceptially + correct. + @param x Input brightness, 0 (minimum or off/black) to 255 (maximum). + @return Gamma-adjusted brightness, can then be passed to one of the + setPixelColor() functions. This uses a fixed gamma correction + exponent of 2.6, which seems reasonably okay for average + NeoPixels in average tasks. If you need finer control you'll + need to provide your own gamma-correction function instead. + */ + static uint8_t gamma8(uint8_t x) { + return pgm_read_byte(&_NeoPixelGammaTable[x]); // 0-255 in, 0-255 out + } + /*! + @brief Convert separate red, green and blue values into a single + "packed" 32-bit RGB color. + @param r Red brightness, 0 to 255. + @param g Green brightness, 0 to 255. + @param b Blue brightness, 0 to 255. + @return 32-bit packed RGB value, which can then be assigned to a + variable for later use or passed to the setPixelColor() + function. Packed RGB format is predictable, regardless of + LED strand color order. + */ + static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) { + return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; + } + /*! + @brief Convert separate red, green, blue and white values into a + single "packed" 32-bit WRGB color. + @param r Red brightness, 0 to 255. + @param g Green brightness, 0 to 255. + @param b Blue brightness, 0 to 255. + @param w White brightness, 0 to 255. + @return 32-bit packed WRGB value, which can then be assigned to a + variable for later use or passed to the setPixelColor() + function. Packed WRGB format is predictable, regardless of + LED strand color order. + */ + static uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { + return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; + } + static uint32_t ColorHSV(uint16_t hue, uint8_t sat=255, uint8_t val=255); + /*! + @brief A gamma-correction function for 32-bit packed RGB or WRGB + colors. Makes color transitions appear more perceptially + correct. + @param x 32-bit packed RGB or WRGB color. + @return Gamma-adjusted packed color, can then be passed in one of the + setPixelColor() functions. Like gamma8(), this uses a fixed + gamma correction exponent of 2.6, which seems reasonably okay + for average NeoPixels in average tasks. If you need finer + control you'll need to provide your own gamma-correction + function instead. + */ + static uint32_t gamma32(uint32_t x); + + protected: + +#ifdef NEO_KHZ400 // If 400 KHz NeoPixel support enabled... + bool is800KHz; ///< true if 800 KHz pixels +#endif + bool begun; ///< true if setup() previously called + uint16_t numLEDs; ///< Number of RGB LEDs in strip + uint16_t numBytes; ///< Size of 'pixels' buffer below + int16_t pin; ///< Output pin number (-1 if not yet set) + uint8_t brightness; ///< Strip brightness 0-255 (stored as +1) + uint8_t *pixels; ///< Holds LED color values (3 or 4 bytes each) + uint8_t rOffset; ///< Red index within each 3- or 4-byte pixel + uint8_t gOffset; ///< Index of green byte + uint8_t bOffset; ///< Index of blue byte + uint8_t wOffset; ///< Index of white (==rOffset if no white) + uint32_t endTime; ///< Latch timing reference +#ifdef __AVR__ + volatile uint8_t *port; ///< Output PORT register + uint8_t pinMask; ///< Output PORT bitmask +#endif +}; + +#endif // ADAFRUIT_NEOPIXEL_H diff --git a/src/arch/arduino-nano/Makefile.inc b/src/arch/arduino-nano/Makefile.inc index 367a4ba..9e149ac 100644 --- a/src/arch/arduino-nano/Makefile.inc +++ b/src/arch/arduino-nano/Makefile.inc @@ -49,6 +49,10 @@ ifneq ($(findstring counter,${arch_drivers}), ) CXX_TARGETS += src/arch/arduino-nano/driver/counter.cc endif +ifneq ($(findstring neopixel,${arch_drivers}), ) + CXX_TARGETS += src/arch/arduino-nano/driver/neopixel.cc +endif + ifeq (${cpu_freq}, 16000000) uart_baud = 57600 else ifeq (${cpu_freq}, 8000000) diff --git a/src/arch/arduino-nano/driver/neopixel.cc b/src/arch/arduino-nano/driver/neopixel.cc new file mode 100644 index 0000000..458bae0 --- /dev/null +++ b/src/arch/arduino-nano/driver/neopixel.cc @@ -0,0 +1,1489 @@ +/*!
+ * @file Adafruit_NeoPixel.cpp
+ *
+ * @mainpage Arduino Library for driving Adafruit NeoPixel addressable LEDs,
+ * FLORA RGB Smart Pixels and compatible devicess -- WS2811, WS2812, WS2812B,
+ * SK6812, etc.
+ *
+ * @section intro_sec Introduction
+ *
+ * This is the documentation for Adafruit's NeoPixel library for the
+ * Arduino platform, allowing a broad range of microcontroller boards
+ * (most AVR boards, many ARM devices, ESP8266 and ESP32, among others)
+ * to control Adafruit NeoPixels, FLORA RGB Smart Pixels and compatible
+ * devices -- WS2811, WS2812, WS2812B, SK6812, etc.
+ *
+ * Adafruit invests time and resources providing this open source code,
+ * please support Adafruit and open-source hardware by purchasing products
+ * from Adafruit!
+ *
+ * @section author Author
+ *
+ * Written by Phil "Paint Your Dragon" Burgess for Adafruit Industries,
+ * with contributions by PJRC, Michael Miller and other members of the
+ * open source community.
+ *
+ * @section license License
+ *
+ * This file is part of the Adafruit_NeoPixel library.
+ *
+ * Adafruit_NeoPixel is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * Adafruit_NeoPixel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with NeoPixel. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <avr/interrupt.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include "driver/neopixel.h"
+
+/*!
+ @brief NeoPixel constructor when length, pin and pixel type are known
+ at compile-time.
+ @param n Number of NeoPixels in strand.
+ @param p Arduino pin number which will drive the NeoPixel data in.
+ @param t Pixel type -- add together NEO_* constants defined in
+ Adafruit_NeoPixel.h, for example NEO_GRB+NEO_KHZ800 for
+ NeoPixels expecting an 800 KHz (vs 400 KHz) data stream
+ with color bytes expressed in green, red, blue order per
+ pixel.
+ @return Adafruit_NeoPixel object. Call the setup() function before use.
+*/
+Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, uint16_t p, neoPixelType t) :
+ begun(false), brightness(0), pixels(NULL), endTime(0) {
+ updateType(t);
+ updateLength(n);
+ setPin(p);
+}
+
+/*!
+ @brief "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().
+ @return Adafruit_NeoPixel object. Call the setup() function before use.
+ @note This function is deprecated, here only for old projects that
+ may still be calling it. New projects should instead use the
+ 'new' keyword with the first constructor syntax (length, pin,
+ type).
+*/
+Adafruit_NeoPixel::Adafruit_NeoPixel() :
+#ifdef NEO_KHZ400
+ is800KHz(true),
+#endif
+ begun(false), numLEDs(0), numBytes(0), pin(-1), brightness(0), pixels(NULL),
+ rOffset(1), gOffset(0), bOffset(2), wOffset(1), endTime(0) {
+}
+
+/*!
+ @brief Deallocate Adafruit_NeoPixel object, set data pin back to INPUT.
+*/
+Adafruit_NeoPixel::~Adafruit_NeoPixel() {
+ free(pixels);
+ if(pin >= 0) {
+ gpio.input(pin);
+ }
+}
+
+/*!
+ @brief Configure NeoPixel pin for output.
+*/
+void Adafruit_NeoPixel::setup(void) {
+ if(pin >= 0) {
+ gpio.output(pin, 0);
+ }
+ begun = true;
+}
+
+/*!
+ @brief Change the length of a previously-declared Adafruit_NeoPixel
+ strip object. Old data is deallocated and new data is cleared.
+ Pin number and pixel format are unchanged.
+ @param n New length of strip, in pixels.
+ @note This function is deprecated, here only for old projects that
+ may still be calling it. New projects should instead use the
+ 'new' keyword with the first constructor syntax (length, pin,
+ type).
+*/
+void Adafruit_NeoPixel::updateLength(uint16_t n) {
+ free(pixels); // Free existing data (if any)
+
+ // Allocate new data -- note: ALL PIXELS ARE CLEARED
+ numBytes = n * ((wOffset == rOffset) ? 3 : 4);
+ if((pixels = (uint8_t *)malloc(numBytes))) {
+ memset(pixels, 0, numBytes);
+ numLEDs = n;
+ } else {
+ numLEDs = numBytes = 0;
+ }
+}
+
+/*!
+ @brief Change the pixel format of a previously-declared
+ Adafruit_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's advisable to follow up with clear().
+ @param t Pixel type -- add together NEO_* constants defined in
+ Adafruit_NeoPixel.h, for example NEO_GRB+NEO_KHZ800 for
+ NeoPixels expecting an 800 KHz (vs 400 KHz) data stream
+ with color bytes expressed in green, red, blue order per
+ pixel.
+ @note This function is deprecated, here only for old projects that
+ may still be calling it. New projects should instead use the
+ 'new' keyword with the first constructor syntax
+ (length, pin, type).
+*/
+void Adafruit_NeoPixel::updateType(neoPixelType t) {
+ bool oldThreeBytesPerPixel = (wOffset == rOffset); // false if RGBW
+
+ wOffset = (t >> 6) & 0b11; // See notes in header file
+ rOffset = (t >> 4) & 0b11; // regarding R/G/B/W offsets
+ gOffset = (t >> 2) & 0b11;
+ bOffset = t & 0b11;
+#ifdef NEO_KHZ400
+ is800KHz = (t < 256); // 400 KHz flag is 1<<8
+#endif
+
+ // If bytes-per-pixel has changed (and pixel data was previously
+ // allocated), re-allocate to new size. Will clear any data.
+ if(pixels) {
+ bool newThreeBytesPerPixel = (wOffset == rOffset);
+ if(newThreeBytesPerPixel != oldThreeBytesPerPixel) updateLength(numLEDs);
+ }
+}
+
+/*!
+ @brief Transmit pixel data in RAM to NeoPixels.
+ @note On most architectures, interrupts are temporarily disabled in
+ order to achieve the correct NeoPixel signal timing. This means
+ that the Arduino millis() and micros() functions, which require
+ interrupts, will lose small intervals of time whenever this
+ function is called (about 30 microseconds per RGB pixel, 40 for
+ RGBW pixels). There's no easy fix for this, but a few
+ specialized alternative or companion libraries exist that use
+ very device-specific peripherals to work around it.
+*/
+void Adafruit_NeoPixel::show(void) {
+
+ if(!pixels) return;
+
+ // Data latch = 300+ microsecond pause in the output stream. Rather than
+ // put a delay at the end of the function, the ending time is noted and
+ // the function will simply hold off (if needed) on issuing the
+ // subsequent round of data until the latch time has elapsed. This
+ // allows the mainline code to start generating the next frame of data
+ // rather than stalling for the latch.
+ while(!canShow());
+ // endTime is a private member (rather than global var) so that multiple
+ // instances on different pins can be quickly issued in succession (each
+ // instance doesn't delay the next).
+
+ // In order to make this code runtime-configurable to work with any pin,
+ // SBI/CBI instructions are eschewed in favor of full PORT writes via the
+ // OUT or ST instructions. It relies on two facts: that peripheral
+ // functions (such as PWM) take precedence on output pins, so our PORT-
+ // wide writes won't interfere, and that interrupts are globally disabled
+ // while data is being issued to the LEDs, so no other code will be
+ // accessing the PORT. The code takes an initial 'snapshot' of the PORT
+ // state, computes 'pin high' and 'pin low' values, and writes these back
+ // to the PORT register as needed.
+
+ cli();
+
+// AVR MCUs -- ATmega & ATtiny (no XMEGA) ---------------------------------
+
+ volatile uint16_t
+ i = numBytes; // Loop counter
+ volatile uint8_t
+ *ptr = pixels, // Pointer to next byte
+ b = *ptr++, // Current byte value
+ hi, // PORT w/output bit set high
+ lo; // PORT w/output bit set low
+
+ // Hand-tuned assembly code issues data to the LED drivers at a specific
+ // rate. There's separate code for different CPU speeds (8, 12, 16 MHz)
+ // for both the WS2811 (400 KHz) and WS2812 (800 KHz) drivers. The
+ // datastream timing for the LED drivers allows a little wiggle room each
+ // way (listed in the datasheets), so the conditions for compiling each
+ // case are set up for a range of frequencies rather than just the exact
+ // 8, 12 or 16 MHz values, permitting use with some close-but-not-spot-on
+ // devices (e.g. 16.5 MHz DigiSpark). The ranges were arrived at based
+ // on the datasheet figures and have not been extensively tested outside
+ // the canonical 8/12/16 MHz speeds; there's no guarantee these will work
+ // close to the extremes (or possibly they could be pushed further).
+ // Keep in mind only one CPU speed case actually gets compiled; the
+ // resulting program isn't as massive as it might look from source here.
+
+// 8 MHz(ish) AVR ---------------------------------------------------------
+#if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL)
+
+#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
+ if(is800KHz) {
+#endif
+
+ volatile uint8_t n1, n2 = 0; // First, next bits out
+
+ // Squeezing an 800 KHz stream out of an 8 MHz chip requires code
+ // specific to each PORT register.
+
+ // 10 instruction clocks per bit: HHxxxxxLLL
+ // OUT instructions: ^ ^ ^ (T=0,2,7)
+
+ // PORTD OUTPUT ----------------------------------------------------
+
+#if defined(PORTD)
+ #if defined(PORTB) || defined(PORTC) || defined(PORTF)
+ if(port == &PORTD) {
+ #endif // defined(PORTB/C/F)
+
+ hi = PORTD | pinMask;
+ lo = PORTD & ~pinMask;
+ n1 = lo;
+ if(b & 0x80) n1 = hi;
+
+ // Dirty trick: RJMPs proceeding to the next instruction are used
+ // to delay two clock cycles in one instruction word (rather than
+ // using two NOPs). This was necessary in order to squeeze the
+ // loop down to exactly 64 words -- the maximum possible for a
+ // relative branch.
+
+ asm volatile(
+ "headD:" "\n\t" // Clk Pseudocode
+ // Bit 7:
+ "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
+ "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
+ "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
+ "rjmp .+0" "\n\t" // 2 nop nop
+ "sbrc %[byte] , 6" "\n\t" // 1-2 if(b & 0x40)
+ "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
+ "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
+ "rjmp .+0" "\n\t" // 2 nop nop
+ // Bit 6:
+ "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
+ "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
+ "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
+ "rjmp .+0" "\n\t" // 2 nop nop
+ "sbrc %[byte] , 5" "\n\t" // 1-2 if(b & 0x20)
+ "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
+ "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
+ "rjmp .+0" "\n\t" // 2 nop nop
+ // Bit 5:
+ "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
+ "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
+ "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
+ "rjmp .+0" "\n\t" // 2 nop nop
+ "sbrc %[byte] , 4" "\n\t" // 1-2 if(b & 0x10)
+ "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
+ "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
+ "rjmp .+0" "\n\t" // 2 nop nop
+ // Bit 4:
+ "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
+ "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
+ "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
+ "rjmp .+0" "\n\t" // 2 nop nop
+ "sbrc %[byte] , 3" "\n\t" // 1-2 if(b & 0x08)
+ "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
+ "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
+ "rjmp .+0" "\n\t" // 2 nop nop
+ // Bit 3:
+ "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
+ "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
+ "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
+ "rjmp .+0" "\n\t" // 2 nop nop
+ "sbrc %[byte] , 2" "\n\t" // 1-2 if(b & 0x04)
+ "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
+ "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
+ "rjmp .+0" "\n\t" // 2 nop nop
+ // Bit 2:
+ "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
+ "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
+ "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
+ "rjmp .+0" "\n\t" // 2 nop nop
+ "sbrc %[byte] , 1" "\n\t" // 1-2 if(b & 0x02)
+ "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
+ "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
+ "rjmp .+0" "\n\t" // 2 nop nop
+ // Bit 1:
+ "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
+ "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
+ "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
+ "rjmp .+0" "\n\t" // 2 nop nop
+ "sbrc %[byte] , 0" "\n\t" // 1-2 if(b & 0x01)
+ "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
+ "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
+ "sbiw %[count], 1" "\n\t" // 2 i-- (don't act on Z flag yet)
+ // Bit 0:
+ "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
+ "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
+ "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
+ "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++
+ "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80)
+ "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
+ "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
+ "brne headD" "\n" // 2 while(i) (Z flag set above)
+ : [byte] "+r" (b),
+ [n1] "+r" (n1),
+ [n2] "+r" (n2),
+ [count] "+w" (i)
+ : [port] "I" (_SFR_IO_ADDR(PORTD)),
+ [ptr] "e" (ptr),
+ [hi] "r" (hi),
+ [lo] "r" (lo));
+
+ #if defined(PORTB) || defined(PORTC) || defined(PORTF)
+ } else // other PORT(s)
+ #endif // defined(PORTB/C/F)
+#endif // defined(PORTD)
+
+ // PORTB OUTPUT ----------------------------------------------------
+
+#if defined(PORTB)
+ #if defined(PORTD) || defined(PORTC) || defined(PORTF)
+ if(port == &PORTB) {
+ #endif // defined(PORTD/C/F)
+
+ // Same as above, just switched to PORTB and stripped of comments.
+ hi = PORTB | pinMask;
+ lo = PORTB & ~pinMask;
+ n1 = lo;
+ if(b & 0x80) n1 = hi;
+
+ asm volatile(
+ "headB:" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 6" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 5" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 4" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 3" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 2" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 1" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 0" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "sbiw %[count], 1" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "ld %[byte] , %a[ptr]+" "\n\t"
+ "sbrc %[byte] , 7" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "brne headB" "\n"
+ : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i)
+ : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi),
+ [lo] "r" (lo));
+
+ #if defined(PORTD) || defined(PORTC) || defined(PORTF)
+ }
+ #endif
+ #if defined(PORTC) || defined(PORTF)
+ else
+ #endif // defined(PORTC/F)
+#endif // defined(PORTB)
+
+ // PORTC OUTPUT ----------------------------------------------------
+
+#if defined(PORTC)
+ #if defined(PORTD) || defined(PORTB) || defined(PORTF)
+ if(port == &PORTC) {
+ #endif // defined(PORTD/B/F)
+
+ // Same as above, just switched to PORTC and stripped of comments.
+ hi = PORTC | pinMask;
+ lo = PORTC & ~pinMask;
+ n1 = lo;
+ if(b & 0x80) n1 = hi;
+
+ asm volatile(
+ "headC:" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 6" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 5" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 4" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 3" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 2" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 1" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 0" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "sbiw %[count], 1" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "ld %[byte] , %a[ptr]+" "\n\t"
+ "sbrc %[byte] , 7" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "brne headC" "\n"
+ : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i)
+ : [port] "I" (_SFR_IO_ADDR(PORTC)), [ptr] "e" (ptr), [hi] "r" (hi),
+ [lo] "r" (lo));
+
+ #if defined(PORTD) || defined(PORTB) || defined(PORTF)
+ }
+ #endif // defined(PORTD/B/F)
+ #if defined(PORTF)
+ else
+ #endif
+#endif // defined(PORTC)
+
+ // PORTF OUTPUT ----------------------------------------------------
+
+#if defined(PORTF)
+ #if defined(PORTD) || defined(PORTB) || defined(PORTC)
+ if(port == &PORTF) {
+ #endif // defined(PORTD/B/C)
+
+ hi = PORTF | pinMask;
+ lo = PORTF & ~pinMask;
+ n1 = lo;
+ if(b & 0x80) n1 = hi;
+
+ asm volatile(
+ "headF:" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 6" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 5" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 4" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 3" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 2" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 1" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n2] , %[lo]" "\n\t"
+ "out %[port] , %[n1]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "sbrc %[byte] , 0" "\n\t"
+ "mov %[n2] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "sbiw %[count], 1" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "mov %[n1] , %[lo]" "\n\t"
+ "out %[port] , %[n2]" "\n\t"
+ "ld %[byte] , %a[ptr]+" "\n\t"
+ "sbrc %[byte] , 7" "\n\t"
+ "mov %[n1] , %[hi]" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "brne headF" "\n"
+ : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i)
+ : [port] "I" (_SFR_IO_ADDR(PORTF)), [ptr] "e" (ptr), [hi] "r" (hi),
+ [lo] "r" (lo));
+
+ #if defined(PORTD) || defined(PORTB) || defined(PORTC)
+ }
+ #endif // defined(PORTD/B/C)
+#endif // defined(PORTF)
+
+#ifdef NEO_KHZ400
+ } else { // end 800 KHz, do 400 KHz
+
+ // Timing is more relaxed; unrolling the inner loop for each bit is
+ // not necessary. Still using the peculiar RJMPs as 2X NOPs, not out
+ // of need but just to trim the code size down a little.
+ // This 400-KHz-datastream-on-8-MHz-CPU code is not quite identical
+ // to the 800-on-16 code later -- the hi/lo timing between WS2811 and
+ // WS2812 is not simply a 2:1 scale!
+
+ // 20 inst. clocks per bit: HHHHxxxxxxLLLLLLLLLL
+ // ST instructions: ^ ^ ^ (T=0,4,10)
+
+ volatile uint8_t next, bit;
+
+ hi = *port | pinMask;
+ lo = *port & ~pinMask;
+ next = lo;
+ bit = 8;
+
+ asm volatile(
+ "head20:" "\n\t" // Clk Pseudocode (T = 0)
+ "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
+ "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128)
+ "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4)
+ "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 6)
+ "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7)
+ "dec %[bit]" "\n\t" // 1 bit-- (T = 8)
+ "breq nextbyte20" "\n\t" // 1-2 if(bit == 0)
+ "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10)
+ "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 12)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 14)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 16)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 18)
+ "rjmp head20" "\n\t" // 2 -> head20 (next bit out)
+ "nextbyte20:" "\n\t" // (T = 10)
+ "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 12)
+ "nop" "\n\t" // 1 nop (T = 13)
+ "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 14)
+ "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 16)
+ "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18)
+ "brne head20" "\n" // 2 if(i != 0) -> (next byte)
+ : [port] "+e" (port),
+ [byte] "+r" (b),
+ [bit] "+r" (bit),
+ [next] "+r" (next),
+ [count] "+w" (i)
+ : [hi] "r" (hi),
+ [lo] "r" (lo),
+ [ptr] "e" (ptr));
+ }
+#endif // NEO_KHZ400
+
+// 12 MHz(ish) AVR --------------------------------------------------------
+#elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL)
+
+#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
+ if(is800KHz) {
+#endif
+
+ // In the 12 MHz case, an optimized 800 KHz datastream (no dead time
+ // between bytes) requires a PORT-specific loop similar to the 8 MHz
+ // code (but a little more relaxed in this case).
+
+ // 15 instruction clocks per bit: HHHHxxxxxxLLLLL
+ // OUT instructions: ^ ^ ^ (T=0,4,10)
+
+ volatile uint8_t next;
+
+ // PORTD OUTPUT ----------------------------------------------------
+
+#if defined(PORTD)
+ #if defined(PORTB) || defined(PORTC) || defined(PORTF)
+ if(port == &PORTD) {
+ #endif // defined(PORTB/C/F)
+
+ hi = PORTD | pinMask;
+ lo = PORTD & ~pinMask;
+ next = lo;
+ if(b & 0x80) next = hi;
+
+ // Don't "optimize" the OUT calls into the bitTime subroutine;
+ // we're exploiting the RCALL and RET as 3- and 4-cycle NOPs!
+ asm volatile(
+ "headD:" "\n\t" // (T = 0)
+ "out %[port], %[hi]" "\n\t" // (T = 1)
+ "rcall bitTimeD" "\n\t" // Bit 7 (T = 15)
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeD" "\n\t" // Bit 6
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeD" "\n\t" // Bit 5
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeD" "\n\t" // Bit 4
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeD" "\n\t" // Bit 3
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeD" "\n\t" // Bit 2
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeD" "\n\t" // Bit 1
+ // Bit 0:
+ "out %[port] , %[hi]" "\n\t" // 1 PORT = hi (T = 1)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 3)
+ "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 5)
+ "out %[port] , %[next]" "\n\t" // 1 PORT = next (T = 6)
+ "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7)
+ "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80) (T = 8)
+ "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 9)
+ "nop" "\n\t" // 1 (T = 10)
+ "out %[port] , %[lo]" "\n\t" // 1 PORT = lo (T = 11)
+ "sbiw %[count], 1" "\n\t" // 2 i-- (T = 13)
+ "brne headD" "\n\t" // 2 if(i != 0) -> (next byte)
+ "rjmp doneD" "\n\t"
+ "bitTimeD:" "\n\t" // nop nop nop (T = 4)
+ "out %[port], %[next]" "\n\t" // 1 PORT = next (T = 5)
+ "mov %[next], %[lo]" "\n\t" // 1 next = lo (T = 6)
+ "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 7)
+ "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 0x80) (T = 8)
+ "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 9)
+ "nop" "\n\t" // 1 (T = 10)
+ "out %[port], %[lo]" "\n\t" // 1 PORT = lo (T = 11)
+ "ret" "\n\t" // 4 nop nop nop nop (T = 15)
+ "doneD:" "\n"
+ : [byte] "+r" (b),
+ [next] "+r" (next),
+ [count] "+w" (i)
+ : [port] "I" (_SFR_IO_ADDR(PORTD)),
+ [ptr] "e" (ptr),
+ [hi] "r" (hi),
+ [lo] "r" (lo));
+
+ #if defined(PORTB) || defined(PORTC) || defined(PORTF)
+ } else // other PORT(s)
+ #endif // defined(PORTB/C/F)
+#endif // defined(PORTD)
+
+ // PORTB OUTPUT ----------------------------------------------------
+
+#if defined(PORTB)
+ #if defined(PORTD) || defined(PORTC) || defined(PORTF)
+ if(port == &PORTB) {
+ #endif // defined(PORTD/C/F)
+
+ hi = PORTB | pinMask;
+ lo = PORTB & ~pinMask;
+ next = lo;
+ if(b & 0x80) next = hi;
+
+ // Same as above, just set for PORTB & stripped of comments
+ asm volatile(
+ "headB:" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeB" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeB" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeB" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeB" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeB" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeB" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeB" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "ld %[byte] , %a[ptr]+" "\n\t"
+ "out %[port] , %[next]" "\n\t"
+ "mov %[next] , %[lo]" "\n\t"
+ "sbrc %[byte] , 7" "\n\t"
+ "mov %[next] , %[hi]" "\n\t"
+ "nop" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "sbiw %[count], 1" "\n\t"
+ "brne headB" "\n\t"
+ "rjmp doneB" "\n\t"
+ "bitTimeB:" "\n\t"
+ "out %[port], %[next]" "\n\t"
+ "mov %[next], %[lo]" "\n\t"
+ "rol %[byte]" "\n\t"
+ "sbrc %[byte], 7" "\n\t"
+ "mov %[next], %[hi]" "\n\t"
+ "nop" "\n\t"
+ "out %[port], %[lo]" "\n\t"
+ "ret" "\n\t"
+ "doneB:" "\n"
+ : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i)
+ : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi),
+ [lo] "r" (lo));
+
+ #if defined(PORTD) || defined(PORTC) || defined(PORTF)
+ }
+ #endif
+ #if defined(PORTC) || defined(PORTF)
+ else
+ #endif // defined(PORTC/F)
+#endif // defined(PORTB)
+
+ // PORTC OUTPUT ----------------------------------------------------
+
+#if defined(PORTC)
+ #if defined(PORTD) || defined(PORTB) || defined(PORTF)
+ if(port == &PORTC) {
+ #endif // defined(PORTD/B/F)
+
+ hi = PORTC | pinMask;
+ lo = PORTC & ~pinMask;
+ next = lo;
+ if(b & 0x80) next = hi;
+
+ // Same as above, just set for PORTC & stripped of comments
+ asm volatile(
+ "headC:" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "ld %[byte] , %a[ptr]+" "\n\t"
+ "out %[port] , %[next]" "\n\t"
+ "mov %[next] , %[lo]" "\n\t"
+ "sbrc %[byte] , 7" "\n\t"
+ "mov %[next] , %[hi]" "\n\t"
+ "nop" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "sbiw %[count], 1" "\n\t"
+ "brne headC" "\n\t"
+ "rjmp doneC" "\n\t"
+ "bitTimeC:" "\n\t"
+ "out %[port], %[next]" "\n\t"
+ "mov %[next], %[lo]" "\n\t"
+ "rol %[byte]" "\n\t"
+ "sbrc %[byte], 7" "\n\t"
+ "mov %[next], %[hi]" "\n\t"
+ "nop" "\n\t"
+ "out %[port], %[lo]" "\n\t"
+ "ret" "\n\t"
+ "doneC:" "\n"
+ : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i)
+ : [port] "I" (_SFR_IO_ADDR(PORTC)), [ptr] "e" (ptr), [hi] "r" (hi),
+ [lo] "r" (lo));
+
+ #if defined(PORTD) || defined(PORTB) || defined(PORTF)
+ }
+ #endif // defined(PORTD/B/F)
+ #if defined(PORTF)
+ else
+ #endif
+#endif // defined(PORTC)
+
+ // PORTF OUTPUT ----------------------------------------------------
+
+#if defined(PORTF)
+ #if defined(PORTD) || defined(PORTB) || defined(PORTC)
+ if(port == &PORTF) {
+ #endif // defined(PORTD/B/C)
+
+ hi = PORTF | pinMask;
+ lo = PORTF & ~pinMask;
+ next = lo;
+ if(b & 0x80) next = hi;
+
+ // Same as above, just set for PORTF & stripped of comments
+ asm volatile(
+ "headF:" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port], %[hi]" "\n\t"
+ "rcall bitTimeC" "\n\t"
+ "out %[port] , %[hi]" "\n\t"
+ "rjmp .+0" "\n\t"
+ "ld %[byte] , %a[ptr]+" "\n\t"
+ "out %[port] , %[next]" "\n\t"
+ "mov %[next] , %[lo]" "\n\t"
+ "sbrc %[byte] , 7" "\n\t"
+ "mov %[next] , %[hi]" "\n\t"
+ "nop" "\n\t"
+ "out %[port] , %[lo]" "\n\t"
+ "sbiw %[count], 1" "\n\t"
+ "brne headF" "\n\t"
+ "rjmp doneC" "\n\t"
+ "bitTimeC:" "\n\t"
+ "out %[port], %[next]" "\n\t"
+ "mov %[next], %[lo]" "\n\t"
+ "rol %[byte]" "\n\t"
+ "sbrc %[byte], 7" "\n\t"
+ "mov %[next], %[hi]" "\n\t"
+ "nop" "\n\t"
+ "out %[port], %[lo]" "\n\t"
+ "ret" "\n\t"
+ "doneC:" "\n"
+ : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i)
+ : [port] "I" (_SFR_IO_ADDR(PORTF)), [ptr] "e" (ptr), [hi] "r" (hi),
+ [lo] "r" (lo));
+
+ #if defined(PORTD) || defined(PORTB) || defined(PORTC)
+ }
+ #endif // defined(PORTD/B/C)
+#endif // defined(PORTF)
+
+#ifdef NEO_KHZ400
+ } else { // 400 KHz
+
+ // 30 instruction clocks per bit: HHHHHHxxxxxxxxxLLLLLLLLLLLLLLL
+ // ST instructions: ^ ^ ^ (T=0,6,15)
+
+ volatile uint8_t next, bit;
+
+ hi = *port | pinMask;
+ lo = *port & ~pinMask;
+ next = lo;
+ bit = 8;
+
+ asm volatile(
+ "head30:" "\n\t" // Clk Pseudocode (T = 0)
+ "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
+ "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128)
+ "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 6)
+ "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 8)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 10)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 12)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 14)
+ "nop" "\n\t" // 1 nop (T = 15)
+ "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 17)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 19)
+ "dec %[bit]" "\n\t" // 1 bit-- (T = 20)
+ "breq nextbyte30" "\n\t" // 1-2 if(bit == 0)
+ "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 22)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 24)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 26)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 28)
+ "rjmp head30" "\n\t" // 2 -> head30 (next bit out)
+ "nextbyte30:" "\n\t" // (T = 22)
+ "nop" "\n\t" // 1 nop (T = 23)
+ "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 24)
+ "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 26)
+ "sbiw %[count], 1" "\n\t" // 2 i-- (T = 28)
+ "brne head30" "\n" // 1-2 if(i != 0) -> (next byte)
+ : [port] "+e" (port),
+ [byte] "+r" (b),
+ [bit] "+r" (bit),
+ [next] "+r" (next),
+ [count] "+w" (i)
+ : [hi] "r" (hi),
+ [lo] "r" (lo),
+ [ptr] "e" (ptr));
+ }
+#endif // NEO_KHZ400
+
+// 16 MHz(ish) AVR --------------------------------------------------------
+#elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L)
+
+#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
+ if(is800KHz) {
+#endif
+
+ // WS2811 and WS2812 have different hi/lo duty cycles; this is
+ // similar but NOT an exact copy of the prior 400-on-8 code.
+
+ // 20 inst. clocks per bit: HHHHHxxxxxxxxLLLLLLL
+ // ST instructions: ^ ^ ^ (T=0,5,13)
+
+ volatile uint8_t next, bit;
+
+ hi = *port | pinMask;
+ lo = *port & ~pinMask;
+ next = lo;
+ bit = 8;
+
+ asm volatile(
+ "head20:" "\n\t" // Clk Pseudocode (T = 0)
+ "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
+ "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 128)
+ "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4)
+ "dec %[bit]" "\n\t" // 1 bit-- (T = 5)
+ "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 7)
+ "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 8)
+ "breq nextbyte20" "\n\t" // 1-2 if(bit == 0) (from dec above)
+ "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 12)
+ "nop" "\n\t" // 1 nop (T = 13)
+ "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 15)
+ "nop" "\n\t" // 1 nop (T = 16)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 18)
+ "rjmp head20" "\n\t" // 2 -> head20 (next bit out)
+ "nextbyte20:" "\n\t" // (T = 10)
+ "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 11)
+ "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 13)
+ "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 15)
+ "nop" "\n\t" // 1 nop (T = 16)
+ "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18)
+ "brne head20" "\n" // 2 if(i != 0) -> (next byte)
+ : [port] "+e" (port),
+ [byte] "+r" (b),
+ [bit] "+r" (bit),
+ [next] "+r" (next),
+ [count] "+w" (i)
+ : [ptr] "e" (ptr),
+ [hi] "r" (hi),
+ [lo] "r" (lo));
+
+#ifdef NEO_KHZ400
+ } else { // 400 KHz
+
+ // The 400 KHz clock on 16 MHz MCU is the most 'relaxed' version.
+
+ // 40 inst. clocks per bit: HHHHHHHHxxxxxxxxxxxxLLLLLLLLLLLLLLLLLLLL
+ // ST instructions: ^ ^ ^ (T=0,8,20)
+
+ volatile uint8_t next, bit;
+
+ hi = *port | pinMask;
+ lo = *port & ~pinMask;
+ next = lo;
+ bit = 8;
+
+ asm volatile(
+ "head40:" "\n\t" // Clk Pseudocode (T = 0)
+ "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
+ "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128)
+ "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 4)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 6)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 8)
+ "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 10)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 12)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 14)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 16)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 18)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 20)
+ "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 22)
+ "nop" "\n\t" // 1 nop (T = 23)
+ "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 24)
+ "dec %[bit]" "\n\t" // 1 bit-- (T = 25)
+ "breq nextbyte40" "\n\t" // 1-2 if(bit == 0)
+ "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 27)
+ "nop" "\n\t" // 1 nop (T = 28)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 30)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 32)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 34)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 36)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 38)
+ "rjmp head40" "\n\t" // 2 -> head40 (next bit out)
+ "nextbyte40:" "\n\t" // (T = 27)
+ "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 28)
+ "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 30)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 32)
+ "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 34)
+ "rjmp .+0" "\n\t" // 2 nop nop (T = 36)
+ "sbiw %[count], 1" "\n\t" // 2 i-- (T = 38)
+ "brne head40" "\n" // 1-2 if(i != 0) -> (next byte)
+ : [port] "+e" (port),
+ [byte] "+r" (b),
+ [bit] "+r" (bit),
+ [next] "+r" (next),
+ [count] "+w" (i)
+ : [ptr] "e" (ptr),
+ [hi] "r" (hi),
+ [lo] "r" (lo));
+ }
+#endif // NEO_KHZ400
+
+#else
+ #error "CPU SPEED NOT SUPPORTED"
+#endif // end F_CPU ifdefs on __AVR__
+
+// END AVR ----------------------------------------------------------------
+
+
+// END ARCHITECTURE SELECT ------------------------------------------------
+
+ sei();
+
+ //endTime = micros(); // Save EOD time for latch on next call
+}
+
+/*!
+ @brief Set/change the NeoPixel output pin number. Previous pin,
+ if any, is set to INPUT and the new pin is set to OUTPUT.
+ @param p Arduino pin number (-1 = no pin).
+*/
+void Adafruit_NeoPixel::setPin(uint16_t p) {
+ if(begun && (pin >= 0)) gpio.input(pin);
+ pin = p;
+ if(begun) {
+ gpio.output(pin, 0);
+ }
+#ifdef __AVR__
+ port = gpio.pinToPort(p);
+ pinMask = gpio.pinToBitmask(p);
+#endif
+}
+
+/*!
+ @brief Set a pixel's color using separate red, green and blue
+ components. If using RGBW pixels, white will be set to 0.
+ @param n Pixel index, starting from 0.
+ @param r Red brightness, 0 = minimum (off), 255 = maximum.
+ @param g Green brightness, 0 = minimum (off), 255 = maximum.
+ @param b Blue brightness, 0 = minimum (off), 255 = maximum.
+*/
+void Adafruit_NeoPixel::setPixelColor(
+ uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
+
+ if(n < numLEDs) {
+ if(brightness) { // See notes in setBrightness()
+ r = (r * brightness) >> 8;
+ g = (g * brightness) >> 8;
+ b = (b * brightness) >> 8;
+ }
+ uint8_t *p;
+ if(wOffset == rOffset) { // Is an RGB-type strip
+ p = &pixels[n * 3]; // 3 bytes per pixel
+ } else { // Is a WRGB-type strip
+ p = &pixels[n * 4]; // 4 bytes per pixel
+ p[wOffset] = 0; // But only R,G,B passed -- set W to 0
+ }
+ p[rOffset] = r; // R,G,B always stored
+ p[gOffset] = g;
+ p[bOffset] = b;
+ }
+}
+
+/*!
+ @brief Set a pixel's color using separate red, green, blue and white
+ components (for RGBW NeoPixels only).
+ @param n Pixel index, starting from 0.
+ @param r Red brightness, 0 = minimum (off), 255 = maximum.
+ @param g Green brightness, 0 = minimum (off), 255 = maximum.
+ @param b Blue brightness, 0 = minimum (off), 255 = maximum.
+ @param w White brightness, 0 = minimum (off), 255 = maximum, ignored
+ if using RGB pixels.
+*/
+void Adafruit_NeoPixel::setPixelColor(
+ uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
+
+ if(n < numLEDs) {
+ if(brightness) { // See notes in setBrightness()
+ r = (r * brightness) >> 8;
+ g = (g * brightness) >> 8;
+ b = (b * brightness) >> 8;
+ w = (w * brightness) >> 8;
+ }
+ uint8_t *p;
+ if(wOffset == rOffset) { // Is an RGB-type strip
+ p = &pixels[n * 3]; // 3 bytes per pixel (ignore W)
+ } else { // Is a WRGB-type strip
+ p = &pixels[n * 4]; // 4 bytes per pixel
+ p[wOffset] = w; // Store W
+ }
+ p[rOffset] = r; // Store R,G,B
+ p[gOffset] = g;
+ p[bOffset] = b;
+ }
+}
+
+/*!
+ @brief Set a pixel's color using a 32-bit 'packed' RGB or RGBW value.
+ @param n Pixel index, starting from 0.
+ @param c 32-bit color value. Most significant byte is white (for RGBW
+ pixels) or ignored (for RGB pixels), next is red, then green,
+ and least significant byte is blue.
+*/
+void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) {
+ if(n < numLEDs) {
+ uint8_t *p,
+ r = (uint8_t)(c >> 16),
+ g = (uint8_t)(c >> 8),
+ b = (uint8_t)c;
+ if(brightness) { // See notes in setBrightness()
+ r = (r * brightness) >> 8;
+ g = (g * brightness) >> 8;
+ b = (b * brightness) >> 8;
+ }
+ if(wOffset == rOffset) {
+ p = &pixels[n * 3];
+ } else {
+ p = &pixels[n * 4];
+ uint8_t w = (uint8_t)(c >> 24);
+ p[wOffset] = brightness ? ((w * brightness) >> 8) : w;
+ }
+ p[rOffset] = r;
+ p[gOffset] = g;
+ p[bOffset] = b;
+ }
+}
+
+/*!
+ @brief Fill all or part of the NeoPixel strip with a color.
+ @param c 32-bit color value. Most significant byte is white (for
+ RGBW pixels) or ignored (for RGB pixels), next is red,
+ then green, and least significant byte is blue. If all
+ arguments are unspecified, this will be 0 (off).
+ @param first Index of first pixel to fill, starting from 0. Must be
+ in-bounds, no clipping is performed. 0 if unspecified.
+ @param count Number of pixels to fill, as a positive value. Passing
+ 0 or leaving unspecified will fill to end of strip.
+*/
+void Adafruit_NeoPixel::fill(uint32_t c, uint16_t first, uint16_t count) {
+ uint16_t i, end;
+
+ if(first >= numLEDs) {
+ return; // If first LED is past end of strip, nothing to do
+ }
+
+ // Calculate the index ONE AFTER the last pixel to fill
+ if(count == 0) {
+ // Fill to end of strip
+ end = numLEDs;
+ } else {
+ // Ensure that the loop won't go past the last pixel
+ end = first + count;
+ if(end > numLEDs) end = numLEDs;
+ }
+
+ for(i = first; i < end; i++) {
+ this->setPixelColor(i, c);
+ }
+}
+
+/*!
+ @brief Convert hue, saturation and value into a packed 32-bit RGB color
+ that can be passed to setPixelColor() or other RGB-compatible
+ functions.
+ @param hue An unsigned 16-bit value, 0 to 65535, representing one full
+ loop of the color wheel, which allows 16-bit hues to "roll
+ over" while still doing the expected thing (and allowing
+ more precision than the wheel() function that was common to
+ prior NeoPixel examples).
+ @param sat Saturation, 8-bit value, 0 (min or pure grayscale) to 255
+ (max or pure hue). Default of 255 if unspecified.
+ @param val Value (brightness), 8-bit value, 0 (min / black / off) to
+ 255 (max or full brightness). Default of 255 if unspecified.
+ @return Packed 32-bit RGB with the most significant byte set to 0 -- the
+ white element of WRGB pixels is NOT utilized. Result is linearly
+ but not perceptually correct, so you may want to pass the result
+ through the gamma32() function (or your own gamma-correction
+ operation) else colors may appear washed out. This is not done
+ automatically by this function because coders may desire a more
+ refined gamma-correction function than the simplified
+ one-size-fits-all operation of gamma32(). Diffusing the LEDs also
+ really seems to help when using low-saturation colors.
+*/
+uint32_t Adafruit_NeoPixel::ColorHSV(uint16_t hue, uint8_t sat, uint8_t val) {
+
+ uint8_t r, g, b;
+
+ // Remap 0-65535 to 0-1529. Pure red is CENTERED on the 64K rollover;
+ // 0 is not the start of pure red, but the midpoint...a few values above
+ // zero and a few below 65536 all yield pure red (similarly, 32768 is the
+ // midpoint, not start, of pure cyan). The 8-bit RGB hexcone (256 values
+ // each for red, green, blue) really only allows for 1530 distinct hues
+ // (not 1536, more on that below), but the full unsigned 16-bit type was
+ // chosen for hue so that one's code can easily handle a contiguous color
+ // wheel by allowing hue to roll over in either direction.
+ hue = (hue * 1530L + 32768) / 65536;
+ // Because red is centered on the rollover point (the +32768 above,
+ // essentially a fixed-point +0.5), the above actually yields 0 to 1530,
+ // where 0 and 1530 would yield the same thing. Rather than apply a
+ // costly modulo operator, 1530 is handled as a special case below.
+
+ // So you'd think that the color "hexcone" (the thing that ramps from
+ // pure red, to pure yellow, to pure green and so forth back to red,
+ // yielding six slices), and with each color component having 256
+ // possible values (0-255), might have 1536 possible items (6*256),
+ // but in reality there's 1530. This is because the last element in
+ // each 256-element slice is equal to the first element of the next
+ // slice, and keeping those in there this would create small
+ // discontinuities in the color wheel. So the last element of each
+ // slice is dropped...we regard only elements 0-254, with item 255
+ // being picked up as element 0 of the next slice. Like this:
+ // Red to not-quite-pure-yellow is: 255, 0, 0 to 255, 254, 0
+ // Pure yellow to not-quite-pure-green is: 255, 255, 0 to 1, 255, 0
+ // Pure green to not-quite-pure-cyan is: 0, 255, 0 to 0, 255, 254
+ // and so forth. Hence, 1530 distinct hues (0 to 1529), and hence why
+ // the constants below are not the multiples of 256 you might expect.
+
+ // Convert hue to R,G,B (nested ifs faster than divide+mod+switch):
+ if(hue < 510) { // Red to Green-1
+ b = 0;
+ if(hue < 255) { // Red to Yellow-1
+ r = 255;
+ g = hue; // g = 0 to 254
+ } else { // Yellow to Green-1
+ r = 510 - hue; // r = 255 to 1
+ g = 255;
+ }
+ } else if(hue < 1020) { // Green to Blue-1
+ r = 0;
+ if(hue < 765) { // Green to Cyan-1
+ g = 255;
+ b = hue - 510; // b = 0 to 254
+ } else { // Cyan to Blue-1
+ g = 1020 - hue; // g = 255 to 1
+ b = 255;
+ }
+ } else if(hue < 1530) { // Blue to Red-1
+ g = 0;
+ if(hue < 1275) { // Blue to Magenta-1
+ r = hue - 1020; // r = 0 to 254
+ b = 255;
+ } else { // Magenta to Red-1
+ r = 255;
+ b = 1530 - hue; // b = 255 to 1
+ }
+ } else { // Last 0.5 Red (quicker than % operator)
+ r = 255;
+ g = b = 0;
+ }
+
+ // Apply saturation and value to R,G,B, pack into 32-bit result:
+ uint32_t v1 = 1 + val; // 1 to 256; allows >>8 instead of /255
+ uint16_t s1 = 1 + sat; // 1 to 256; same reason
+ uint8_t s2 = 255 - sat; // 255 to 0
+ return ((((((r * s1) >> 8) + s2) * v1) & 0xff00) << 8) |
+ (((((g * s1) >> 8) + s2) * v1) & 0xff00) |
+ ( ((((b * s1) >> 8) + s2) * v1) >> 8);
+}
+
+/*!
+ @brief Query the color of a previously-set pixel.
+ @param n Index of pixel to read (0 = first).
+ @return 'Packed' 32-bit RGB or WRGB value. Most significant byte is white
+ (for RGBW pixels) or 0 (for RGB pixels), next is red, then green,
+ and least significant byte is blue.
+ @note If the strip brightness has been changed from the default value
+ of 255, the color read from a pixel may not exactly match what
+ was previously written with one of the setPixelColor() functions.
+ This gets more pronounced at lower brightness levels.
+*/
+uint32_t Adafruit_NeoPixel::getPixelColor(uint16_t n) const {
+ if(n >= numLEDs) return 0; // Out of bounds, return no color.
+
+ uint8_t *p;
+
+ if(wOffset == rOffset) { // Is RGB-type device
+ p = &pixels[n * 3];
+ if(brightness) {
+ // Stored color was decimated by setBrightness(). Returned value
+ // attempts to scale back to an approximation of the original 24-bit
+ // value used when setting the pixel color, but there will always be
+ // some error -- those bits are simply gone. Issue is most
+ // pronounced at low brightness levels.
+ return (((uint32_t)(p[rOffset] << 8) / brightness) << 16) |
+ (((uint32_t)(p[gOffset] << 8) / brightness) << 8) |
+ ( (uint32_t)(p[bOffset] << 8) / brightness );
+ } else {
+ // No brightness adjustment has been made -- return 'raw' color
+ return ((uint32_t)p[rOffset] << 16) |
+ ((uint32_t)p[gOffset] << 8) |
+ (uint32_t)p[bOffset];
+ }
+ } else { // Is RGBW-type device
+ p = &pixels[n * 4];
+ if(brightness) { // Return scaled color
+ return (((uint32_t)(p[wOffset] << 8) / brightness) << 24) |
+ (((uint32_t)(p[rOffset] << 8) / brightness) << 16) |
+ (((uint32_t)(p[gOffset] << 8) / brightness) << 8) |
+ ( (uint32_t)(p[bOffset] << 8) / brightness );
+ } else { // Return raw color
+ return ((uint32_t)p[wOffset] << 24) |
+ ((uint32_t)p[rOffset] << 16) |
+ ((uint32_t)p[gOffset] << 8) |
+ (uint32_t)p[bOffset];
+ }
+ }
+}
+
+
+/*!
+ @brief Adjust output brightness. Does not immediately affect what's
+ currently displayed on the LEDs. The next call to show() will
+ refresh the LEDs at this level.
+ @param b Brightness setting, 0=minimum (off), 255=brightest.
+ @note This was intended for one-time use in one's setup() function,
+ not as an animation effect in itself. Because of the way this
+ library "pre-multiplies" LED colors in RAM, changing the
+ brightness is often a "lossy" operation -- what you write to
+ pixels isn't necessary the same as what you'll read back.
+ Repeated brightness changes using this function exacerbate the
+ problem. Smart programs therefore treat the strip as a
+ write-only resource, maintaining their own state to render each
+ frame of an animation, not relying on read-modify-write.
+*/
+void Adafruit_NeoPixel::setBrightness(uint8_t b) {
+ // Stored brightness value is different than what's passed.
+ // This simplifies the actual scaling math later, allowing a fast
+ // 8x8-bit multiply and taking the MSB. 'brightness' is a uint8_t,
+ // adding 1 here may (intentionally) roll over...so 0 = max brightness
+ // (color values are interpreted literally; no scaling), 1 = min
+ // brightness (off), 255 = just below max brightness.
+ uint8_t newBrightness = b + 1;
+ if(newBrightness != brightness) { // Compare against prior value
+ // Brightness has changed -- re-scale existing data in RAM,
+ // This process is potentially "lossy," especially when increasing
+ // brightness. The tight timing in the WS2811/WS2812 code means there
+ // aren't enough free cycles to perform this scaling on the fly as data
+ // is issued. So we make a pass through the existing color data in RAM
+ // and scale it (subsequent graphics commands also work at this
+ // brightness level). If there's a significant step up in brightness,
+ // the limited number of steps (quantization) in the old data will be
+ // quite visible in the re-scaled version. For a non-destructive
+ // change, you'll need to re-render the full strip data. C'est la vie.
+ uint8_t c,
+ *ptr = pixels,
+ oldBrightness = brightness - 1; // De-wrap old brightness value
+ uint16_t scale;
+ if(oldBrightness == 0) scale = 0; // Avoid /0
+ else if(b == 255) scale = 65535 / oldBrightness;
+ else scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness;
+ for(uint16_t i=0; i<numBytes; i++) {
+ c = *ptr;
+ *ptr++ = (c * scale) >> 8;
+ }
+ brightness = newBrightness;
+ }
+}
+
+/*!
+ @brief Retrieve the last-set brightness value for the strip.
+ @return Brightness value: 0 = minimum (off), 255 = maximum.
+*/
+uint8_t Adafruit_NeoPixel::getBrightness(void) const {
+ return brightness - 1;
+}
+
+/*!
+ @brief Fill the whole NeoPixel strip with 0 / black / off.
+*/
+void Adafruit_NeoPixel::clear(void) {
+ memset(pixels, 0, numBytes);
+}
+
+// A 32-bit variant of gamma8() that applies the same function
+// to all components of a packed RGB or WRGB value.
+uint32_t Adafruit_NeoPixel::gamma32(uint32_t x) {
+ uint8_t *y = (uint8_t *)&x;
+ // All four bytes of a 32-bit value are filtered even if RGB (not WRGB),
+ // to avoid a bunch of shifting and masking that would be necessary for
+ // properly handling different endianisms (and each byte is a fairly
+ // trivial operation, so it might not even be wasting cycles vs a check
+ // and branch for the RGB case). In theory this might cause trouble *if*
+ // someone's storing information in the unused most significant byte
+ // of an RGB value, but this seems exceedingly rare and if it's
+ // encountered in reality they can mask values going in or coming out.
+ for(uint8_t i=0; i<4; i++) y[i] = gamma8(y[i]);
+ return x; // Packed 32-bit return
+}
|