Advertisement
dale3h

[Arduino] Adafruit_NeoPixel.cpp

Nov 21st, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 39.07 KB | None | 0 0
  1. /*-------------------------------------------------------------------------
  2.   Arduino library to control a wide variety of WS2811- and WS2812-based RGB
  3.   LED devices such as Adafruit FLORA RGB Smart Pixels and NeoPixel strips.
  4.   Currently handles 400 and 800 KHz bitstreams on 8, 12 and 16 MHz ATmega
  5.   MCUs, with LEDs wired for RGB or GRB color order.  8 MHz MCUs provide
  6.   output on PORTB and PORTD, while 16 MHz chips can handle most output pins
  7.   (possible exception with upper PORT registers on the Arduino Mega).
  8.  
  9.   Written by Phil Burgess / Paint Your Dragon for Adafruit Industries,
  10.   contributions by PJRC and other members of the open source community.
  11.  
  12.   Adafruit invests time and resources providing this open source code,
  13.   please support Adafruit and open-source hardware by purchasing products
  14.   from Adafruit!
  15.  
  16.   -------------------------------------------------------------------------
  17.   This file is part of the Adafruit NeoPixel library.
  18.  
  19.   NeoPixel is free software: you can redistribute it and/or modify
  20.   it under the terms of the GNU Lesser General Public License as
  21.   published by the Free Software Foundation, either version 3 of
  22.   the License, or (at your option) any later version.
  23.  
  24.   NeoPixel is distributed in the hope that it will be useful,
  25.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27.   GNU Lesser General Public License for more details.
  28.  
  29.   You should have received a copy of the GNU Lesser General Public
  30.   License along with NeoPixel.  If not, see
  31.   <http://www.gnu.org/licenses/>.
  32.   -------------------------------------------------------------------------*/
  33.  
  34. #include "Adafruit_NeoPixel.h"
  35.  
  36. Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, uint8_t p, uint8_t t) : numLEDs(n), numBytes(n * 3), pin(p), pixels(NULL)
  37. #if defined(NEO_RGB) || defined(NEO_KHZ400)
  38.   ,type(t)
  39. #endif
  40. #ifdef __AVR__
  41.   ,port(portOutputRegister(digitalPinToPort(p))),
  42.    pinMask(digitalPinToBitMask(p))
  43. #endif
  44. {
  45.   if((pixels = (uint8_t *)malloc(numBytes))) {
  46.     memset(pixels, 0, numBytes);
  47.   }
  48. }
  49.  
  50. Adafruit_NeoPixel::~Adafruit_NeoPixel() {
  51.   if(pixels) free(pixels);
  52.   pinMode(pin, INPUT);
  53. }
  54.  
  55. #ifdef __MK20DX128__ // Teensy 3.0
  56. static inline void delayShort(uint32_t) __attribute__((always_inline, unused));
  57. static inline void delayShort(uint32_t num) {
  58.   asm volatile(
  59.     "L_%=_delay:"       "\n\t"
  60.     "subs   %0, #1"     "\n\t"
  61.     "bne    L_%=_delay" "\n"
  62.     : "+r" (num) :
  63.   );
  64. }
  65. #endif // __arm__
  66.  
  67. void Adafruit_NeoPixel::begin(void) {
  68.   pinMode(pin, OUTPUT);
  69.   digitalWrite(pin, LOW);
  70. }
  71.  
  72. void Adafruit_NeoPixel::show(void) {
  73.  
  74.   if(!pixels) return;
  75.  
  76.   // Data latch = 50+ microsecond pause in the output stream.  Rather than
  77.   // put a delay at the end of the function, the ending time is noted and
  78.   // the function will simply hold off (if needed) on issuing the
  79.   // subsequent round of data until the latch time has elapsed.  This
  80.   // allows the mainline code to start generating the next frame of data
  81.   // rather than stalling for the latch.
  82.   while((micros() - endTime) < 50L);
  83.   // endTime is a private member (rather than global var) so that mutliple
  84.   // instances on different pins can be quickly issued in succession (each
  85.   // instance doesn't delay the next).
  86.  
  87.   // In order to make this code runtime-configurable to work with any pin,
  88.   // SBI/CBI instructions are eschewed in favor of full PORT writes via the
  89.   // OUT or ST instructions.  It relies on two facts: that peripheral
  90.   // functions (such as PWM) take precedence on output pins, so our PORT-
  91.   // wide writes won't interfere, and that interrupts are globally disabled
  92.   // while data is being issued to the LEDs, so no other code will be
  93.   // accessing the PORT.  The code takes an initial 'snapshot' of the PORT
  94.   // state, computes 'pin high' and 'pin low' values, and writes these back
  95.   // to the PORT register as needed.
  96.  
  97.   noInterrupts(); // Need 100% focus on instruction timing
  98.  
  99. #ifdef __AVR__
  100.  
  101.   volatile uint16_t
  102.     i   = numBytes; // Loop counter
  103.   volatile uint8_t
  104.    *ptr = pixels,   // Pointer to next byte
  105.     b   = *ptr++,   // Current byte value
  106.     hi,             // PORT w/output bit set high
  107.     lo;             // PORT w/output bit set low
  108.  
  109.   // Hand-tuned assembly code issues data to the LED drivers at a specific
  110.   // rate.  There's separate code for different CPU speeds (8, 12, 16 MHz)
  111.   // for both the WS2811 (400 KHz) and WS2812 (800 KHz) drivers.  The
  112.   // datastream timing for the LED drivers allows a little wiggle room each
  113.   // way (listed in the datasheets), so the conditions for compiling each
  114.   // case are set up for a range of frequencies rather than just the exact
  115.   // 8, 12 or 16 MHz values, permitting use with some close-but-not-spot-on
  116.   // devices (e.g. 16.5 MHz DigiSpark).  The ranges were arrived at based
  117.   // on the datasheet figures and have not been extensively tested outside
  118.   // the canonical 8/12/16 MHz speeds; there's no guarantee these will work
  119.   // close to the extremes (or possibly they could be pushed further).
  120.   // Keep in mind only one CPU speed case actually gets compiled; the
  121.   // resulting program isn't as massive as it might look from source here.
  122.  
  123. // 8 MHz(ish) AVR ---------------------------------------------------------
  124. #if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL)
  125.  
  126. #ifdef NEO_KHZ400
  127.   if((type & NEO_SPDMASK) == NEO_KHZ800) { // 800 KHz bitstream
  128. #endif
  129.  
  130.     volatile uint8_t n1, n2 = 0;  // First, next bits out
  131.  
  132.     // Squeezing an 800 KHz stream out of an 8 MHz chip requires code
  133.     // specific to each PORT register.  At present this is only written
  134.     // to work with pins on PORTD or PORTB, the most likely use case --
  135.     // this covers all the pins on the Adafruit Flora and the bulk of
  136.     // digital pins on the Arduino Pro 8 MHz (keep in mind, this code
  137.     // doesn't even get compiled for 16 MHz boards like the Uno, Mega,
  138.     // Leonardo, etc., so don't bother extending this out of hand).
  139.     // Additional PORTs could be added if you really need them, just
  140.     // duplicate the else and loop and change the PORT.  Each add'l
  141.     // PORT will require about 150(ish) bytes of program space.
  142.  
  143.     // 10 instruction clocks per bit: HHxxxxxLLL
  144.     // OUT instructions:              ^ ^    ^   (T=0,2,7)
  145.  
  146. #ifdef PORTD // PORTD isn't present on ATtiny85, etc.
  147.  
  148.     if(port == &PORTD) {
  149.  
  150.       hi = PORTD |  pinMask;
  151.       lo = PORTD & ~pinMask;
  152.       n1 = lo;
  153.       if(b & 0x80) n1 = hi;
  154.  
  155.       // Dirty trick: RJMPs proceeding to the next instruction are used
  156.       // to delay two clock cycles in one instruction word (rather than
  157.       // using two NOPs).  This was necessary in order to squeeze the
  158.       // loop down to exactly 64 words -- the maximum possible for a
  159.       // relative branch.
  160.  
  161.       asm volatile(
  162.        "headD:"                   "\n\t" // Clk  Pseudocode
  163.         // Bit 7:
  164.         "out  %[port] , %[hi]"    "\n\t" // 1    PORT = hi
  165.         "mov  %[n2]   , %[lo]"    "\n\t" // 1    n2   = lo
  166.         "out  %[port] , %[n1]"    "\n\t" // 1    PORT = n1
  167.         "rjmp .+0"                "\n\t" // 2    nop nop
  168.         "sbrc %[byte] , 6"        "\n\t" // 1-2  if(b & 0x40)
  169.          "mov %[n2]   , %[hi]"    "\n\t" // 0-1   n2 = hi
  170.         "out  %[port] , %[lo]"    "\n\t" // 1    PORT = lo
  171.         "rjmp .+0"                "\n\t" // 2    nop nop
  172.         // Bit 6:
  173.         "out  %[port] , %[hi]"    "\n\t" // 1    PORT = hi
  174.         "mov  %[n1]   , %[lo]"    "\n\t" // 1    n1   = lo
  175.         "out  %[port] , %[n2]"    "\n\t" // 1    PORT = n2
  176.         "rjmp .+0"                "\n\t" // 2    nop nop
  177.         "sbrc %[byte] , 5"        "\n\t" // 1-2  if(b & 0x20)
  178.          "mov %[n1]   , %[hi]"    "\n\t" // 0-1   n1 = hi
  179.         "out  %[port] , %[lo]"    "\n\t" // 1    PORT = lo
  180.         "rjmp .+0"                "\n\t" // 2    nop nop
  181.         // Bit 5:
  182.         "out  %[port] , %[hi]"    "\n\t" // 1    PORT = hi
  183.         "mov  %[n2]   , %[lo]"    "\n\t" // 1    n2   = lo
  184.         "out  %[port] , %[n1]"    "\n\t" // 1    PORT = n1
  185.         "rjmp .+0"                "\n\t" // 2    nop nop
  186.         "sbrc %[byte] , 4"        "\n\t" // 1-2  if(b & 0x10)
  187.          "mov %[n2]   , %[hi]"    "\n\t" // 0-1   n2 = hi
  188.         "out  %[port] , %[lo]"    "\n\t" // 1    PORT = lo
  189.         "rjmp .+0"                "\n\t" // 2    nop nop
  190.         // Bit 4:
  191.         "out  %[port] , %[hi]"    "\n\t" // 1    PORT = hi
  192.         "mov  %[n1]   , %[lo]"    "\n\t" // 1    n1   = lo
  193.         "out  %[port] , %[n2]"    "\n\t" // 1    PORT = n2
  194.         "rjmp .+0"                "\n\t" // 2    nop nop
  195.         "sbrc %[byte] , 3"        "\n\t" // 1-2  if(b & 0x08)
  196.          "mov %[n1]   , %[hi]"    "\n\t" // 0-1   n1 = hi
  197.         "out  %[port] , %[lo]"    "\n\t" // 1    PORT = lo
  198.         "rjmp .+0"                "\n\t" // 2    nop nop
  199.         // Bit 3:
  200.         "out  %[port] , %[hi]"    "\n\t" // 1    PORT = hi
  201.         "mov  %[n2]   , %[lo]"    "\n\t" // 1    n2   = lo
  202.         "out  %[port] , %[n1]"    "\n\t" // 1    PORT = n1
  203.         "rjmp .+0"                "\n\t" // 2    nop nop
  204.         "sbrc %[byte] , 2"        "\n\t" // 1-2  if(b & 0x04)
  205.          "mov %[n2]   , %[hi]"    "\n\t" // 0-1   n2 = hi
  206.         "out  %[port] , %[lo]"    "\n\t" // 1    PORT = lo
  207.         "rjmp .+0"                "\n\t" // 2    nop nop
  208.         // Bit 2:
  209.         "out  %[port] , %[hi]"    "\n\t" // 1    PORT = hi
  210.         "mov  %[n1]   , %[lo]"    "\n\t" // 1    n1   = lo
  211.         "out  %[port] , %[n2]"    "\n\t" // 1    PORT = n2
  212.         "rjmp .+0"                "\n\t" // 2    nop nop
  213.         "sbrc %[byte] , 1"        "\n\t" // 1-2  if(b & 0x02)
  214.          "mov %[n1]   , %[hi]"    "\n\t" // 0-1   n1 = hi
  215.         "out  %[port] , %[lo]"    "\n\t" // 1    PORT = lo
  216.         "rjmp .+0"                "\n\t" // 2    nop nop
  217.         // Bit 1:
  218.         "out  %[port] , %[hi]"    "\n\t" // 1    PORT = hi
  219.         "mov  %[n2]   , %[lo]"    "\n\t" // 1    n2   = lo
  220.         "out  %[port] , %[n1]"    "\n\t" // 1    PORT = n1
  221.         "rjmp .+0"                "\n\t" // 2    nop nop
  222.         "sbrc %[byte] , 0"        "\n\t" // 1-2  if(b & 0x01)
  223.          "mov %[n2]   , %[hi]"    "\n\t" // 0-1   n2 = hi
  224.         "out  %[port] , %[lo]"    "\n\t" // 1    PORT = lo
  225.         "sbiw %[count], 1"        "\n\t" // 2    i-- (don't act on Z flag yet)
  226.         // Bit 0:
  227.         "out  %[port] , %[hi]"    "\n\t" // 1    PORT = hi
  228.         "mov  %[n1]   , %[lo]"    "\n\t" // 1    n1   = lo
  229.         "out  %[port] , %[n2]"    "\n\t" // 1    PORT = n2
  230.         "ld   %[byte] , %a[ptr]+" "\n\t" // 2    b = *ptr++
  231.         "sbrc %[byte] , 7"        "\n\t" // 1-2  if(b & 0x80)
  232.          "mov %[n1]   , %[hi]"    "\n\t" // 0-1   n1 = hi
  233.         "out  %[port] , %[lo]"    "\n\t" // 1    PORT = lo
  234.         "brne headD"              "\n"   // 2    while(i) (Z flag set above)
  235.       : [byte]  "+r" (b),
  236.         [n1]    "+r" (n1),
  237.         [n2]    "+r" (n2),
  238.         [count] "+w" (i)
  239.       : [port]   "I" (_SFR_IO_ADDR(PORTD)),
  240.         [ptr]    "e" (ptr),
  241.         [hi]     "r" (hi),
  242.         [lo]     "r" (lo));
  243.  
  244.     } else if(port == &PORTB) {
  245.  
  246. #endif // PORTD
  247.  
  248.       // Same as above, just switched to PORTB and stripped of comments.
  249.       hi = PORTB |  pinMask;
  250.       lo = PORTB & ~pinMask;
  251.       n1 = lo;
  252.       if(b & 0x80) n1 = hi;
  253.  
  254.       asm volatile(
  255.        "headB:"                   "\n\t"
  256.         "out  %[port] , %[hi]"    "\n\t"
  257.         "mov  %[n2]   , %[lo]"    "\n\t"
  258.         "out  %[port] , %[n1]"    "\n\t"
  259.         "rjmp .+0"                "\n\t"
  260.         "sbrc %[byte] , 6"        "\n\t"
  261.          "mov %[n2]   , %[hi]"    "\n\t"
  262.         "out  %[port] , %[lo]"    "\n\t"
  263.         "rjmp .+0"                "\n\t"
  264.         "out  %[port] , %[hi]"    "\n\t"
  265.         "mov  %[n1]   , %[lo]"    "\n\t"
  266.         "out  %[port] , %[n2]"    "\n\t"
  267.         "rjmp .+0"                "\n\t"
  268.         "sbrc %[byte] , 5"        "\n\t"
  269.          "mov %[n1]   , %[hi]"    "\n\t"
  270.         "out  %[port] , %[lo]"    "\n\t"
  271.         "rjmp .+0"                "\n\t"
  272.         "out  %[port] , %[hi]"    "\n\t"
  273.         "mov  %[n2]   , %[lo]"    "\n\t"
  274.         "out  %[port] , %[n1]"    "\n\t"
  275.         "rjmp .+0"                "\n\t"
  276.         "sbrc %[byte] , 4"        "\n\t"
  277.          "mov %[n2]   , %[hi]"    "\n\t"
  278.         "out  %[port] , %[lo]"    "\n\t"
  279.         "rjmp .+0"                "\n\t"
  280.         "out  %[port] , %[hi]"    "\n\t"
  281.         "mov  %[n1]   , %[lo]"    "\n\t"
  282.         "out  %[port] , %[n2]"    "\n\t"
  283.         "rjmp .+0"                "\n\t"
  284.         "sbrc %[byte] , 3"        "\n\t"
  285.          "mov %[n1]   , %[hi]"    "\n\t"
  286.         "out  %[port] , %[lo]"    "\n\t"
  287.         "rjmp .+0"                "\n\t"
  288.         "out  %[port] , %[hi]"    "\n\t"
  289.         "mov  %[n2]   , %[lo]"    "\n\t"
  290.         "out  %[port] , %[n1]"    "\n\t"
  291.         "rjmp .+0"                "\n\t"
  292.         "sbrc %[byte] , 2"        "\n\t"
  293.          "mov %[n2]   , %[hi]"    "\n\t"
  294.         "out  %[port] , %[lo]"    "\n\t"
  295.         "rjmp .+0"                "\n\t"
  296.         "out  %[port] , %[hi]"    "\n\t"
  297.         "mov  %[n1]   , %[lo]"    "\n\t"
  298.         "out  %[port] , %[n2]"    "\n\t"
  299.         "rjmp .+0"                "\n\t"
  300.         "sbrc %[byte] , 1"        "\n\t"
  301.          "mov %[n1]   , %[hi]"    "\n\t"
  302.         "out  %[port] , %[lo]"    "\n\t"
  303.         "rjmp .+0"                "\n\t"
  304.         "out  %[port] , %[hi]"    "\n\t"
  305.         "mov  %[n2]   , %[lo]"    "\n\t"
  306.         "out  %[port] , %[n1]"    "\n\t"
  307.         "rjmp .+0"                "\n\t"
  308.         "sbrc %[byte] , 0"        "\n\t"
  309.          "mov %[n2]   , %[hi]"    "\n\t"
  310.         "out  %[port] , %[lo]"    "\n\t"
  311.         "sbiw %[count], 1"        "\n\t"
  312.         "out  %[port] , %[hi]"    "\n\t"
  313.         "mov  %[n1]   , %[lo]"    "\n\t"
  314.         "out  %[port] , %[n2]"    "\n\t"
  315.         "ld   %[byte] , %a[ptr]+" "\n\t"
  316.         "sbrc %[byte] , 7"        "\n\t"
  317.          "mov %[n1]   , %[hi]"    "\n\t"
  318.         "out  %[port] , %[lo]"    "\n\t"
  319.         "brne headB"              "\n"
  320.       : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i)
  321.       : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi),
  322.         [lo] "r" (lo));
  323.  
  324. #ifdef PORTD
  325.     }    // endif PORTB
  326. #endif
  327.  
  328. #ifdef NEO_KHZ400
  329.   } else { // end 800 KHz, do 400 KHz
  330.  
  331.     // Timing is more relaxed; unrolling the inner loop for each bit is
  332.     // not necessary.  Still using the peculiar RJMPs as 2X NOPs, not out
  333.     // of need but just to trim the code size down a little.
  334.     // This 400-KHz-datastream-on-8-MHz-CPU code is not quite identical
  335.     // to the 800-on-16 code later -- the hi/lo timing between WS2811 and
  336.     // WS2812 is not simply a 2:1 scale!
  337.  
  338.     // 20 inst. clocks per bit: HHHHxxxxxxLLLLLLLLLL
  339.     // ST instructions:         ^   ^     ^          (T=0,4,10)
  340.  
  341.     volatile uint8_t next, bit;
  342.  
  343.     hi   = *port |  pinMask;
  344.     lo   = *port & ~pinMask;
  345.     next = lo;
  346.     bit  = 8;
  347.  
  348.     asm volatile(
  349.      "head20:"                  "\n\t" // Clk  Pseudocode    (T =  0)
  350.       "st   %a[port], %[hi]"    "\n\t" // 2    PORT = hi     (T =  2)
  351.       "sbrc %[byte] , 7"        "\n\t" // 1-2  if(b & 128)
  352.        "mov  %[next], %[hi]"    "\n\t" // 0-1   next = hi    (T =  4)
  353.       "st   %a[port], %[next]"  "\n\t" // 2    PORT = next   (T =  6)
  354.       "mov  %[next] , %[lo]"    "\n\t" // 1    next = lo     (T =  7)
  355.       "dec  %[bit]"             "\n\t" // 1    bit--         (T =  8)
  356.       "breq nextbyte20"         "\n\t" // 1-2  if(bit == 0)
  357.       "rol  %[byte]"            "\n\t" // 1    b <<= 1       (T = 10)
  358.       "st   %a[port], %[lo]"    "\n\t" // 2    PORT = lo     (T = 12)
  359.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 14)
  360.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 16)
  361.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 18)
  362.       "rjmp head20"             "\n\t" // 2    -> head20 (next bit out)
  363.      "nextbyte20:"              "\n\t" //                    (T = 10)
  364.       "st   %a[port], %[lo]"    "\n\t" // 2    PORT = lo     (T = 12)
  365.       "nop"                     "\n\t" // 1    nop           (T = 13)
  366.       "ldi  %[bit]  , 8"        "\n\t" // 1    bit = 8       (T = 14)
  367.       "ld   %[byte] , %a[ptr]+" "\n\t" // 2    b = *ptr++    (T = 16)
  368.       "sbiw %[count], 1"        "\n\t" // 2    i--           (T = 18)
  369.       "brne head20"             "\n"   // 2    if(i != 0) -> (next byte)
  370.       : [port]  "+e" (port),
  371.         [byte]  "+r" (b),
  372.         [bit]   "+r" (bit),
  373.         [next]  "+r" (next),
  374.         [count] "+w" (i)
  375.       : [hi]    "r" (hi),
  376.         [lo]    "r" (lo),
  377.         [ptr]   "e" (ptr));
  378.   }
  379. #endif
  380.  
  381. // 12 MHz(ish) AVR --------------------------------------------------------
  382. #elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL)
  383.  
  384. #ifdef NEO_KHZ400
  385.   if((type & NEO_SPDMASK) == NEO_KHZ800) { // 800 KHz bitstream
  386. #endif
  387.  
  388.     // In the 12 MHz case, an optimized 800 KHz datastream (no dead time
  389.     // between bytes) requires a PORT-specific loop similar to the 8 MHz
  390.     // code (but a little more relaxed in this case).
  391.  
  392.     // 15 instruction clocks per bit: HHHHxxxxxxLLLLL
  393.     // OUT instructions:              ^   ^     ^     (T=0,4,10)
  394.  
  395.     volatile uint8_t next;
  396.  
  397. #ifdef PORTD
  398.  
  399.     if(port == &PORTD) {
  400.  
  401.       hi   = PORTD |  pinMask;
  402.       lo   = PORTD & ~pinMask;
  403.       next = lo;
  404.       if(b & 0x80) next = hi;
  405.  
  406.       // Don't "optimize" the OUT calls into the bitTime subroutine;
  407.       // we're exploiting the RCALL and RET as 3- and 4-cycle NOPs!
  408.       asm volatile(
  409.        "headD:"                   "\n\t" //        (T =  0)
  410.         "out   %[port], %[hi]"    "\n\t" //        (T =  1)
  411.         "rcall bitTimeD"          "\n\t" // Bit 7  (T = 15)
  412.         "out   %[port], %[hi]"    "\n\t"
  413.         "rcall bitTimeD"          "\n\t" // Bit 6
  414.         "out   %[port], %[hi]"    "\n\t"
  415.         "rcall bitTimeD"          "\n\t" // Bit 5
  416.         "out   %[port], %[hi]"    "\n\t"
  417.         "rcall bitTimeD"          "\n\t" // Bit 4
  418.         "out   %[port], %[hi]"    "\n\t"
  419.         "rcall bitTimeD"          "\n\t" // Bit 3
  420.         "out   %[port], %[hi]"    "\n\t"
  421.         "rcall bitTimeD"          "\n\t" // Bit 2
  422.         "out   %[port], %[hi]"    "\n\t"
  423.         "rcall bitTimeD"          "\n\t" // Bit 1
  424.         // Bit 0:
  425.         "out  %[port] , %[hi]"    "\n\t" // 1    PORT = hi    (T =  1)
  426.         "rjmp .+0"                "\n\t" // 2    nop nop      (T =  3)
  427.         "ld   %[byte] , %a[ptr]+" "\n\t" // 2    b = *ptr++   (T =  5)
  428.         "out  %[port] , %[next]"  "\n\t" // 1    PORT = next  (T =  6)
  429.         "mov  %[next] , %[lo]"    "\n\t" // 1    next = lo    (T =  7)
  430.         "sbrc %[byte] , 7"        "\n\t" // 1-2  if(b & 0x80) (T =  8)
  431.          "mov %[next] , %[hi]"    "\n\t" // 0-1    next = hi  (T =  9)
  432.         "nop"                     "\n\t" // 1                 (T = 10)
  433.         "out  %[port] , %[lo]"    "\n\t" // 1    PORT = lo    (T = 11)
  434.         "sbiw %[count], 1"        "\n\t" // 2    i--          (T = 13)
  435.         "brne headD"              "\n\t" // 2    if(i != 0) -> (next byte)
  436.          "rjmp doneD"             "\n\t"
  437.         "bitTimeD:"               "\n\t" //      nop nop nop     (T =  4)
  438.          "out  %[port], %[next]"  "\n\t" // 1    PORT = next     (T =  5)
  439.          "mov  %[next], %[lo]"    "\n\t" // 1    next = lo       (T =  6)
  440.          "rol  %[byte]"           "\n\t" // 1    b <<= 1         (T =  7)
  441.          "sbrc %[byte], 7"        "\n\t" // 1-2  if(b & 0x80)    (T =  8)
  442.           "mov %[next], %[hi]"    "\n\t" // 0-1   next = hi      (T =  9)
  443.          "nop"                    "\n\t" // 1                    (T = 10)
  444.          "out  %[port], %[lo]"    "\n\t" // 1    PORT = lo       (T = 11)
  445.          "ret"                    "\n\t" // 4    nop nop nop nop (T = 15)
  446.          "doneD:"                 "\n"
  447.         : [byte]  "+r" (b),
  448.           [next]  "+r" (next),
  449.           [count] "+w" (i)
  450.         : [port]   "I" (_SFR_IO_ADDR(PORTD)),
  451.           [ptr]    "e" (ptr),
  452.           [hi]     "r" (hi),
  453.           [lo]     "r" (lo));
  454.  
  455.     } else if(port == &PORTB) {
  456.  
  457. #endif // PORTD
  458.  
  459.       hi   = PORTB |  pinMask;
  460.       lo   = PORTB & ~pinMask;
  461.       next = lo;
  462.       if(b & 0x80) next = hi;
  463.  
  464.       // Same as above, just set for PORTB & stripped of comments
  465.       asm volatile(
  466.        "headB:"                   "\n\t"
  467.         "out   %[port], %[hi]"    "\n\t"
  468.         "rcall bitTimeB"          "\n\t"
  469.         "out   %[port], %[hi]"    "\n\t"
  470.         "rcall bitTimeB"          "\n\t"
  471.         "out   %[port], %[hi]"    "\n\t"
  472.         "rcall bitTimeB"          "\n\t"
  473.         "out   %[port], %[hi]"    "\n\t"
  474.         "rcall bitTimeB"          "\n\t"
  475.         "out   %[port], %[hi]"    "\n\t"
  476.         "rcall bitTimeB"          "\n\t"
  477.         "out   %[port], %[hi]"    "\n\t"
  478.         "rcall bitTimeB"          "\n\t"
  479.         "out   %[port], %[hi]"    "\n\t"
  480.         "rcall bitTimeB"          "\n\t"
  481.         "out  %[port] , %[hi]"    "\n\t"
  482.         "rjmp .+0"                "\n\t"
  483.         "ld   %[byte] , %a[ptr]+" "\n\t"
  484.         "out  %[port] , %[next]"  "\n\t"
  485.         "mov  %[next] , %[lo]"    "\n\t"
  486.         "sbrc %[byte] , 7"        "\n\t"
  487.          "mov %[next] , %[hi]"    "\n\t"
  488.         "nop"                     "\n\t"
  489.         "out  %[port] , %[lo]"    "\n\t"
  490.         "sbiw %[count], 1"        "\n\t"
  491.         "brne headB"              "\n\t"
  492.          "rjmp doneB"             "\n\t"
  493.         "bitTimeB:"               "\n\t"
  494.          "out  %[port], %[next]"  "\n\t"
  495.          "mov  %[next], %[lo]"    "\n\t"
  496.          "rol  %[byte]"           "\n\t"
  497.          "sbrc %[byte], 7"        "\n\t"
  498.           "mov %[next], %[hi]"    "\n\t"
  499.          "nop"                    "\n\t"
  500.          "out  %[port], %[lo]"    "\n\t"
  501.          "ret"                    "\n\t"
  502.          "doneB:"                 "\n"
  503.         : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i)
  504.         : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi),
  505.           [lo] "r" (lo));
  506.  
  507. #ifdef PORTD
  508.     }
  509. #endif
  510.  
  511. #ifdef NEO_KHZ400
  512.   } else { // 400 KHz
  513.  
  514.     // 30 instruction clocks per bit: HHHHHHxxxxxxxxxLLLLLLLLLLLLLLL
  515.     // ST instructions:               ^     ^        ^    (T=0,6,15)
  516.  
  517.     volatile uint8_t next, bit;
  518.  
  519.     hi   = *port |  pinMask;
  520.     lo   = *port & ~pinMask;
  521.     next = lo;
  522.     bit  = 8;
  523.  
  524.     asm volatile(
  525.      "head30:"                  "\n\t" // Clk  Pseudocode    (T =  0)
  526.       "st   %a[port], %[hi]"    "\n\t" // 2    PORT = hi     (T =  2)
  527.       "sbrc %[byte] , 7"        "\n\t" // 1-2  if(b & 128)
  528.        "mov  %[next], %[hi]"    "\n\t" // 0-1   next = hi    (T =  4)
  529.       "rjmp .+0"                "\n\t" // 2    nop nop       (T =  6)
  530.       "st   %a[port], %[next]"  "\n\t" // 2    PORT = next   (T =  8)
  531.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 10)
  532.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 12)
  533.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 14)
  534.       "nop"                     "\n\t" // 1    nop           (T = 15)
  535.       "st   %a[port], %[lo]"    "\n\t" // 2    PORT = lo     (T = 17)
  536.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 19)
  537.       "dec  %[bit]"             "\n\t" // 1    bit--         (T = 20)
  538.       "breq nextbyte30"         "\n\t" // 1-2  if(bit == 0)
  539.       "rol  %[byte]"            "\n\t" // 1    b <<= 1       (T = 22)
  540.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 24)
  541.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 26)
  542.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 28)
  543.       "rjmp head30"             "\n\t" // 2    -> head30 (next bit out)
  544.      "nextbyte30:"              "\n\t" //                    (T = 22)
  545.       "nop"                     "\n\t" // 1    nop           (T = 23)
  546.       "ldi  %[bit]  , 8"        "\n\t" // 1    bit = 8       (T = 24)
  547.       "ld   %[byte] , %a[ptr]+" "\n\t" // 2    b = *ptr++    (T = 26)
  548.       "sbiw %[count], 1"        "\n\t" // 2    i--           (T = 28)
  549.       "brne head30"             "\n"   // 1-2  if(i != 0) -> (next byte)
  550.       : [port]  "+e" (port),
  551.         [byte]  "+r" (b),
  552.         [bit]   "+r" (bit),
  553.         [next]  "+r" (next),
  554.         [count] "+w" (i)
  555.       : [hi]     "r" (hi),
  556.         [lo]     "r" (lo),
  557.         [ptr]    "e" (ptr));
  558.   }
  559. #endif
  560.  
  561. // 16 MHz(ish) AVR --------------------------------------------------------
  562. #elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L)
  563.  
  564. #ifdef NEO_KHZ400
  565.   if((type & NEO_SPDMASK) == NEO_KHZ800) { // 800 KHz bitstream
  566. #endif
  567.  
  568.     // WS2811 and WS2812 have different hi/lo duty cycles; this is
  569.     // similar but NOT an exact copy of the prior 400-on-8 code.
  570.  
  571.     // 20 inst. clocks per bit: HHHHHxxxxxxxxLLLLLLL
  572.     // ST instructions:         ^   ^        ^       (T=0,5,13)
  573.  
  574.     volatile uint8_t next, bit;
  575.  
  576.     hi   = *port |  pinMask;
  577.     lo   = *port & ~pinMask;
  578.     next = lo;
  579.     bit  = 8;
  580.  
  581.     asm volatile(
  582.      "head20:"                   "\n\t" // Clk  Pseudocode    (T =  0)
  583.       "st   %a[port],  %[hi]"    "\n\t" // 2    PORT = hi     (T =  2)
  584.       "sbrc %[byte],  7"         "\n\t" // 1-2  if(b & 128)
  585.        "mov  %[next], %[hi]"     "\n\t" // 0-1   next = hi    (T =  4)
  586.       "dec  %[bit]"              "\n\t" // 1    bit--         (T =  5)
  587.       "st   %a[port],  %[next]"  "\n\t" // 2    PORT = next   (T =  7)
  588.       "mov  %[next] ,  %[lo]"    "\n\t" // 1    next = lo     (T =  8)
  589.       "breq nextbyte20"          "\n\t" // 1-2  if(bit == 0) (from dec above)
  590.       "rol  %[byte]"             "\n\t" // 1    b <<= 1       (T = 10)
  591.       "rjmp .+0"                 "\n\t" // 2    nop nop       (T = 12)
  592.       "nop"                      "\n\t" // 1    nop           (T = 13)
  593.       "st   %a[port],  %[lo]"    "\n\t" // 2    PORT = lo     (T = 15)
  594.       "nop"                      "\n\t" // 1    nop           (T = 16)
  595.       "rjmp .+0"                 "\n\t" // 2    nop nop       (T = 18)
  596.       "rjmp head20"              "\n\t" // 2    -> head20 (next bit out)
  597.      "nextbyte20:"               "\n\t" //                    (T = 10)
  598.       "ldi  %[bit]  ,  8"        "\n\t" // 1    bit = 8       (T = 11)
  599.       "ld   %[byte] ,  %a[ptr]+" "\n\t" // 2    b = *ptr++    (T = 13)
  600.       "st   %a[port], %[lo]"     "\n\t" // 2    PORT = lo     (T = 15)
  601.       "nop"                      "\n\t" // 1    nop           (T = 16)
  602.       "sbiw %[count], 1"         "\n\t" // 2    i--           (T = 18)
  603.        "brne head20"             "\n"   // 2    if(i != 0) -> (next byte)
  604.       : [port]  "+e" (port),
  605.         [byte]  "+r" (b),
  606.         [bit]   "+r" (bit),
  607.         [next]  "+r" (next),
  608.         [count] "+w" (i)
  609.       : [ptr]    "e" (ptr),
  610.         [hi]     "r" (hi),
  611.         [lo]     "r" (lo));
  612.  
  613. #ifdef NEO_KHZ400
  614.   } else { // 400 KHz
  615.  
  616.     // The 400 KHz clock on 16 MHz MCU is the most 'relaxed' version.
  617.  
  618.     // 40 inst. clocks per bit: HHHHHHHHxxxxxxxxxxxxLLLLLLLLLLLLLLLLLLLL
  619.     // ST instructions:         ^       ^           ^         (T=0,8,20)
  620.  
  621.     volatile uint8_t next, bit;
  622.  
  623.     hi   = *port |  pinMask;
  624.     lo   = *port & ~pinMask;
  625.     next = lo;
  626.     bit  = 8;
  627.  
  628.     asm volatile(
  629.      "head40:"                  "\n\t" // Clk  Pseudocode    (T =  0)
  630.       "st   %a[port], %[hi]"    "\n\t" // 2    PORT = hi     (T =  2)
  631.       "sbrc %[byte] , 7"        "\n\t" // 1-2  if(b & 128)
  632.        "mov  %[next] , %[hi]"   "\n\t" // 0-1   next = hi    (T =  4)
  633.       "rjmp .+0"                "\n\t" // 2    nop nop       (T =  6)
  634.       "rjmp .+0"                "\n\t" // 2    nop nop       (T =  8)
  635.       "st   %a[port], %[next]"  "\n\t" // 2    PORT = next   (T = 10)
  636.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 12)
  637.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 14)
  638.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 16)
  639.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 18)
  640.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 20)
  641.       "st   %a[port], %[lo]"    "\n\t" // 2    PORT = lo     (T = 22)
  642.       "nop"                     "\n\t" // 1    nop           (T = 23)
  643.       "mov  %[next] , %[lo]"    "\n\t" // 1    next = lo     (T = 24)
  644.       "dec  %[bit]"             "\n\t" // 1    bit--         (T = 25)
  645.       "breq nextbyte40"         "\n\t" // 1-2  if(bit == 0)
  646.       "rol  %[byte]"            "\n\t" // 1    b <<= 1       (T = 27)
  647.       "nop"                     "\n\t" // 1    nop           (T = 28)
  648.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 30)
  649.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 32)
  650.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 34)
  651.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 36)
  652.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 38)
  653.       "rjmp head40"             "\n\t" // 2    -> head40 (next bit out)
  654.      "nextbyte40:"              "\n\t" //                    (T = 27)
  655.       "ldi  %[bit]  , 8"        "\n\t" // 1    bit = 8       (T = 28)
  656.       "ld   %[byte] , %a[ptr]+" "\n\t" // 2    b = *ptr++    (T = 30)
  657.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 32)
  658.       "st   %a[port], %[lo]"    "\n\t" // 2    PORT = lo     (T = 34)
  659.       "rjmp .+0"                "\n\t" // 2    nop nop       (T = 36)
  660.       "sbiw %[count], 1"        "\n\t" // 2    i--           (T = 38)
  661.       "brne head40"             "\n"   // 1-2  if(i != 0) -> (next byte)
  662.       : [port]  "+e" (port),
  663.         [byte]  "+r" (b),
  664.         [bit]   "+r" (bit),
  665.         [next]  "+r" (next),
  666.         [count] "+w" (i)
  667.       : [ptr]    "e" (ptr),
  668.         [hi]     "r" (hi),
  669.         [lo]     "r" (lo));
  670.   }
  671. #endif
  672.  
  673. #else
  674.  #error "CPU SPEED NOT SUPPORTED"
  675. #endif
  676.  
  677. #elif defined(__arm__)
  678.  
  679.   // Paul Stoffregen: "This implementation may not be quite perfect, but
  680.   // it seems to work reasonably well with an actual 20 LED WS2811 strip.
  681.   // The timing at 48 MHz is off a bit, perhaps due to flash cache misses?
  682.   // Ideally this code should execute from RAM to eliminate slight timing
  683.   // differences between flash caches hits and misses.  But it seems to
  684.   // [run] quite well.  More testing is needed with longer strips."
  685.  
  686. /* If timing can be stabilized, something like this should work:
  687.  #define DELAY_800_T0H (0.40 * F_CPU / 1000000L / DCYC + 0.5)
  688.  #define DELAY_800_T0L (0.85 * F_CPU / 1000000L / DCYC + 0.5)
  689.  #define DELAY_800_T1H (0.80 * F_CPU / 1000000L / DCYC + 0.5)
  690.  #define DELAY_800_T1L (0.45 * F_CPU / 1000000L / DCYC + 0.5)
  691.  #define DELAY_400_T0H (0.50 * F_CPU / 1000000L / DCYC + 0.5)
  692.  #define DELAY_400_T0L (2.00 * F_CPU / 1000000L / DCYC + 0.5)
  693.  #define DELAY_400_T1H (1.20 * F_CPU / 1000000L / DCYC + 0.5)
  694.  #define DELAY_400_T1L (1.30 * F_CPU / 1000000L / DCYC + 0.5)
  695.   But in the meantime, a fixed set of tables is used:
  696. */
  697.  
  698. #ifdef __MK20DX128__ // Teensy 3.0
  699.  
  700. #if (F_CPU == 24000000)
  701.  #define DELAY_800_T0H  2
  702.  #define DELAY_800_T0L  4
  703.  #define DELAY_800_T1H  5
  704.  #define DELAY_800_T1L  1
  705.  #define DELAY_400_T0H  3
  706.  #define DELAY_400_T0L 10
  707.  #define DELAY_400_T1H  9
  708.  #define DELAY_400_T1L  5
  709. #elif (F_CPU == 48000000)
  710.  #define DELAY_800_T0H  4
  711.  #define DELAY_800_T0L  9
  712.  #define DELAY_800_T1H 12
  713.  #define DELAY_800_T1L  1
  714.  #define DELAY_400_T0H  6
  715.  #define DELAY_400_T0L 20
  716.  #define DELAY_400_T1H 18
  717.  #define DELAY_400_T1L 11
  718. #elif (F_CPU == 96000000)
  719.  #define DELAY_800_T0H  7
  720.  #define DELAY_800_T0L 17
  721.  #define DELAY_800_T1H 22
  722.  #define DELAY_800_T1L  2
  723.  #define DELAY_400_T0H 12
  724.  #define DELAY_400_T0L 40
  725.  #define DELAY_400_T1H 36
  726.  #define DELAY_400_T1L 22
  727. #else
  728.  #error "CPU SPEED NOT SUPPORTED"
  729. #endif
  730.  
  731.   volatile uint8_t *set = portSetRegister(pin);
  732.   volatile uint8_t *clr = portClearRegister(pin);
  733.   #define SET_HI   *set = 1;
  734.   #define SET_LO   *clr = 1;
  735.   uint8_t *p   = pixels,
  736.           *end = p + numBytes, pix, mask;
  737.  
  738. #ifdef NEO_KHZ400
  739.   if((type & NEO_SPDMASK) == NEO_KHZ800) { // 800 KHz bitstream
  740. #endif
  741.     while(p < end) {
  742.       pix = *p++;
  743.       for(mask = 0x80; mask; mask >>= 1) {
  744.         SET_HI
  745.         if(pix & mask) {
  746.           delayShort(DELAY_800_T1H);
  747.           SET_LO
  748.           delayShort(DELAY_800_T1L);
  749.         } else {
  750.           delayShort(DELAY_800_T0H);
  751.           SET_LO
  752.           delayShort(DELAY_800_T0L);
  753.         }
  754.       }
  755.     }
  756. #ifdef NEO_KHZ400
  757.   } else { // 400 kHz bitstream
  758.     while(p < end) {
  759.       pix = *p++;
  760.       for(mask = 0x80; mask; mask >>= 1) {
  761.         SET_HI
  762.         if(pix & mask) {
  763.           delayShort(DELAY_400_T1H);
  764.           SET_LO
  765.           delayShort(DELAY_400_T1L);
  766.         } else {
  767.           delayShort(DELAY_400_T0H);
  768.           SET_LO
  769.           delayShort(DELAY_400_T0L);
  770.         }
  771.       }
  772.     }
  773.   }
  774. #endif
  775.  
  776. #else // Arduino Due
  777.  
  778.   #define SCALE      VARIANT_MCK / 2UL / 1000000UL
  779.   #define INST       (2UL * F_CPU / VARIANT_MCK)
  780.   #define TIME_800_L ((int)(0.40 * SCALE + 0.5) - (5 * INST))
  781.   #define TIME_800_H ((int)(0.80 * SCALE + 0.5) - (5 * INST))
  782.   #define PERIOD_800 ((int)(1.25 * SCALE + 0.5) - (5 * INST))
  783.   #define TIME_400_L ((int)(0.50 * SCALE + 0.5) - (5 * INST))
  784.   #define TIME_400_H ((int)(1.20 * SCALE + 0.5) - (5 * INST))
  785.   #define PERIOD_400 ((int)(2.50 * SCALE + 0.5) - (5 * INST))
  786.  
  787.   int             pinMask, timeLo, timeHi, period, t;
  788.   Pio            *port;
  789.   volatile WoReg *portSet, *portClear, *timeValue, *timeReset;
  790.   uint8_t        *p, *end, pix, mask;
  791.  
  792.   pmc_set_writeprotect(false);
  793.   pmc_enable_periph_clk((uint32_t)TC3_IRQn);
  794.   TC_Configure(TC1, 0,
  795.     TC_CMR_WAVE | TC_CMR_WAVSEL_UP | TC_CMR_TCCLKS_TIMER_CLOCK1);
  796.   TC_Start(TC1, 0);
  797.  
  798.   pinMask   = g_APinDescription[pin].ulPin; // Don't 'optimize' these into
  799.   port      = g_APinDescription[pin].pPort; // declarations above.  Want to
  800.   portSet   = &(port->PIO_SODR);            // burn a few cycles after
  801.   portClear = &(port->PIO_CODR);            // starting timer to minimize
  802.   timeValue = &(TC1->TC_CHANNEL[0].TC_CV);  // the initial 'while'.
  803.   timeReset = &(TC1->TC_CHANNEL[0].TC_CCR);
  804.   p         =  pixels;
  805.   end       =  p + numBytes;
  806.   pix       = *p++;
  807.   mask      = 0x80;
  808.  
  809. #ifdef NEO_KHZ400
  810.   if((type & NEO_SPDMASK) == NEO_KHZ800) { // 800 KHz bitstream
  811. #endif
  812.     timeLo = TIME_800_L;
  813.     timeHi = TIME_800_H;
  814.     period = PERIOD_800;
  815. #ifdef NEO_KHZ400
  816.   } else { // 400 KHz bitstream
  817.     timeLo = TIME_400_L;
  818.     timeHi = TIME_400_H;
  819.     period = PERIOD_400;
  820.   }
  821. #endif
  822.  
  823.   for(t = timeLo;; t = timeLo) {
  824.     if(pix & mask) t = timeHi;
  825.     while(*timeValue < period);
  826.     *portSet   = pinMask;
  827.     *timeReset = TC_CCR_CLKEN | TC_CCR_SWTRG;
  828.     while(*timeValue < t);
  829.     *portClear = pinMask;
  830.     if(!(mask >>= 1)) {   // This 'inside-out' loop logic utilizes
  831.       if(p >= end) break; // idle time to minimize inter-byte delays.
  832.       pix = *p++;
  833.       mask = 0x80;
  834.     }
  835.   }
  836.   while(*timeValue < period); // Wait for last bit
  837.   TC_Stop(TC1, 0);
  838.  
  839. #endif // end Arduino Due
  840.  
  841. #endif // end Architecture select
  842.  
  843.   interrupts();
  844.   endTime = micros(); // Save EOD time for latch on next call
  845. }
  846.  
  847. // Set the output pin number
  848. void Adafruit_NeoPixel::setPin(uint8_t p) {
  849.   pinMode(pin, INPUT);
  850.   pin = p;
  851.   pinMode(p, OUTPUT);
  852.   digitalWrite(p, LOW);
  853. #ifdef __AVR__
  854.   port    = portOutputRegister(digitalPinToPort(p));
  855.   pinMask = digitalPinToBitMask(p);
  856. #endif
  857. }
  858.  
  859. // Set pixel color from separate R,G,B components:
  860. void Adafruit_NeoPixel::setPixelColor(
  861.  uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
  862.   if(n < numLEDs) {
  863.     if(brightness) { // See notes in setBrightness()
  864.       r = (r * brightness) >> 8;
  865.       g = (g * brightness) >> 8;
  866.       b = (b * brightness) >> 8;
  867.     }
  868.     uint8_t *p = &pixels[n * 3];
  869. #ifdef NEO_RGB
  870.     if((type & NEO_COLMASK) == NEO_GRB) {
  871. #endif
  872.       *p++ = g;
  873.       *p++ = r;
  874. #ifdef NEO_RGB
  875.     } else {
  876.       *p++ = r;
  877.       *p++ = g;
  878.     }
  879. #endif
  880.     *p = b;
  881.   }
  882. }
  883.  
  884. // Set pixel color from 'packed' 32-bit RGB color:
  885. void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) {
  886.   if(n < numLEDs) {
  887.     uint8_t
  888.       r = (uint8_t)(c >> 16),
  889.       g = (uint8_t)(c >>  8),
  890.       b = (uint8_t)c;
  891.     if(brightness) { // See notes in setBrightness()
  892.       r = (r * brightness) >> 8;
  893.       g = (g * brightness) >> 8;
  894.       b = (b * brightness) >> 8;
  895.     }
  896.     uint8_t *p = &pixels[n * 3];
  897. #ifdef NEO_RGB
  898.     if((type & NEO_COLMASK) == NEO_GRB) {
  899. #endif
  900.       *p++ = g;
  901.       *p++ = r;
  902. #ifdef NEO_RGB
  903.     } else {
  904.       *p++ = r;
  905.       *p++ = g;
  906.     }
  907. #endif
  908.     *p = b;
  909.   }
  910. }
  911.  
  912. // Convert separate R,G,B into packed 32-bit RGB color.
  913. // Packed format is always RGB, regardless of LED strand color order.
  914. uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) {
  915.   return ((uint32_t)r << 16) | ((uint32_t)g <<  8) | b;
  916. }
  917.  
  918. // Query color from previously-set pixel (returns packed 32-bit RGB value)
  919. uint32_t Adafruit_NeoPixel::getPixelColor(uint16_t n) const {
  920.  
  921.   if(n < numLEDs) {
  922.     uint16_t ofs = n * 3;
  923.     return (uint32_t)(pixels[ofs + 2]) |
  924. #ifdef NEO_RGB
  925.       (((type & NEO_COLMASK) == NEO_GRB) ?
  926. #endif
  927.         ((uint32_t)(pixels[ofs    ]) <<  8) |
  928.         ((uint32_t)(pixels[ofs + 1]) << 16)
  929. #ifdef NEO_RGB
  930.       :
  931.         ((uint32_t)(pixels[ofs    ]) << 16) |
  932.         ((uint32_t)(pixels[ofs + 1]) <<  8) )
  933. #endif
  934.       ;
  935.   }
  936.  
  937.   return 0; // Pixel # is out of bounds
  938. }
  939.  
  940. uint8_t *Adafruit_NeoPixel::getPixels(void) const {
  941.   return pixels;
  942. }
  943.  
  944. uint16_t Adafruit_NeoPixel::numPixels(void) const {
  945.   return numLEDs;
  946. }
  947.  
  948. // Adjust output brightness; 0=darkest (off), 255=brightest.  This does
  949. // NOT immediately affect what's currently displayed on the LEDs.  The
  950. // next call to show() will refresh the LEDs at this level.  However,
  951. // this process is potentially "lossy," especially when increasing
  952. // brightness.  The tight timing in the WS2811/WS2812 code means there
  953. // aren't enough free cycles to perform this scaling on the fly as data
  954. // is issued.  So we make a pass through the existing color data in RAM
  955. // and scale it (subsequent graphics commands also work at this
  956. // brightness level).  If there's a significant step up in brightness,
  957. // the limited number of steps (quantization) in the old data will be
  958. // quite visible in the re-scaled version.  For a non-destructive
  959. // change, you'll need to re-render the full strip data.  C'est la vie.
  960. void Adafruit_NeoPixel::setBrightness(uint8_t b) {
  961.   // Stored brightness value is different than what's passed.
  962.   // This simplifies the actual scaling math later, allowing a fast
  963.   // 8x8-bit multiply and taking the MSB.  'brightness' is a uint8_t,
  964.   // adding 1 here may (intentionally) roll over...so 0 = max brightness
  965.   // (color values are interpreted literally; no scaling), 1 = min
  966.   // brightness (off), 255 = just below max brightness.
  967.   uint8_t newBrightness = b + 1;
  968.   if(newBrightness != brightness) { // Compare against prior value
  969.     // Brightness has changed -- re-scale existing data in RAM
  970.     uint8_t  c,
  971.             *ptr           = pixels,
  972.              oldBrightness = brightness - 1; // De-wrap old brightness value
  973.     uint16_t scale;
  974.     if(oldBrightness == 0) scale = 0; // Avoid /0
  975.     else if(b == 255) scale = 65535 / oldBrightness;
  976.     else scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness;
  977.     for(uint16_t i=0; i<numBytes; i++) {
  978.       c      = *ptr;
  979.       *ptr++ = (c * scale) >> 8;
  980.     }
  981.     brightness = newBrightness;
  982.   }
  983. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement