MAXREFDES117# Code Documentation  V01.00
Heart Rate / SpO2 Monitor
 All Data Structures Files Functions Variables Typedefs Macros Pages
Adafruit_NeoPixel.cpp
Go to the documentation of this file.
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 various color orders. 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, Michael Miller and other members of the open
11  source community.
12 
13  Adafruit invests time and resources providing this open source code,
14  please support Adafruit and open-source hardware by purchasing products
15  from Adafruit!
16 
17  -------------------------------------------------------------------------
18  This file is part of the Adafruit NeoPixel library.
19 
20  NeoPixel is free software: you can redistribute it and/or modify
21  it under the terms of the GNU Lesser General Public License as
22  published by the Free Software Foundation, either version 3 of
23  the License, or (at your option) any later version.
24 
25  NeoPixel is distributed in the hope that it will be useful,
26  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  GNU Lesser General Public License for more details.
29 
30  You should have received a copy of the GNU Lesser General Public
31  License along with NeoPixel. If not, see
32  <http://www.gnu.org/licenses/>.
33  -------------------------------------------------------------------------*/
34 
35 #include "Adafruit_NeoPixel.h"
36 
37 // Constructor when length, pin and type are known at compile-time:
39  brightness(0), pixels(NULL), endTime(0), begun(false)
40 {
41  updateType(t);
42  updateLength(n);
43  setPin(p);
44 }
45 
46 // via Michael Vogt/neophob: empty constructor is used when strand length
47 // isn't known at compile-time; situations where program config might be
48 // read from internal flash memory or an SD card, or arrive via serial
49 // command. If using this constructor, MUST follow up with updateType(),
50 // updateLength(), etc. to establish the strand type, length and pin number!
52  pin(-1), brightness(0), pixels(NULL), endTime(0), begun(false),
53  numLEDs(0), numBytes(0), rOffset(1), gOffset(0), bOffset(2), wOffset(1)
54 #ifdef NEO_KHZ400
55  , is800KHz(true)
56 #endif
57 {
58 }
59 
61  if(pixels) free(pixels);
62  if(pin >= 0) pinMode(pin, INPUT);
63 }
64 
66  if(pin >= 0) {
67  pinMode(pin, OUTPUT);
68  digitalWrite(pin, LOW);
69  }
70  begun = true;
71 }
72 
74  if(pixels) free(pixels); // Free existing data (if any)
75 
76  // Allocate new data -- note: ALL PIXELS ARE CLEARED
77  numBytes = n * ((wOffset == rOffset) ? 3 : 4);
78  if((pixels = (uint8_t *)malloc(numBytes))) {
79  memset(pixels, 0, numBytes);
80  numLEDs = n;
81  } else {
82  numLEDs = numBytes = 0;
83  }
84 }
85 
87  boolean oldThreeBytesPerPixel = (wOffset == rOffset); // false if RGBW
88 
89  wOffset = (t >> 6) & 0b11; // See notes in header file
90  rOffset = (t >> 4) & 0b11; // regarding R/G/B/W offsets
91  gOffset = (t >> 2) & 0b11;
92  bOffset = t & 0b11;
93 #ifdef NEO_KHZ400
94  is800KHz = (t < 256); // 400 KHz flag is 1<<8
95 #endif
96 
97  // If bytes-per-pixel has changed (and pixel data was previously
98  // allocated), re-allocate to new size. Will clear any data.
99  if(pixels) {
100  boolean newThreeBytesPerPixel = (wOffset == rOffset);
101  if(newThreeBytesPerPixel != oldThreeBytesPerPixel) updateLength(numLEDs);
102  }
103 }
104 
105 #ifdef ESP8266
106 // ESP8266 show() is external to enforce ICACHE_RAM_ATTR execution
107 extern "C" void ICACHE_RAM_ATTR espShow(
108  uint8_t pin, uint8_t *pixels, uint32_t numBytes, uint8_t type);
109 #endif // ESP8266
110 
112 
113  if(!pixels) return;
114 
115  // Data latch = 50+ microsecond pause in the output stream. Rather than
116  // put a delay at the end of the function, the ending time is noted and
117  // the function will simply hold off (if needed) on issuing the
118  // subsequent round of data until the latch time has elapsed. This
119  // allows the mainline code to start generating the next frame of data
120  // rather than stalling for the latch.
121  while(!canShow());
122  // endTime is a private member (rather than global var) so that mutliple
123  // instances on different pins can be quickly issued in succession (each
124  // instance doesn't delay the next).
125 
126  // In order to make this code runtime-configurable to work with any pin,
127  // SBI/CBI instructions are eschewed in favor of full PORT writes via the
128  // OUT or ST instructions. It relies on two facts: that peripheral
129  // functions (such as PWM) take precedence on output pins, so our PORT-
130  // wide writes won't interfere, and that interrupts are globally disabled
131  // while data is being issued to the LEDs, so no other code will be
132  // accessing the PORT. The code takes an initial 'snapshot' of the PORT
133  // state, computes 'pin high' and 'pin low' values, and writes these back
134  // to the PORT register as needed.
135 
136  noInterrupts(); // Need 100% focus on instruction timing
137 
138 
139 #ifdef __AVR__
140 // AVR MCUs -- ATmega & ATtiny (no XMEGA) ---------------------------------
141 
142  volatile uint16_t
143  i = numBytes; // Loop counter
144  volatile uint8_t
145  *ptr = pixels, // Pointer to next byte
146  b = *ptr++, // Current byte value
147  hi, // PORT w/output bit set high
148  lo; // PORT w/output bit set low
149 
150  // Hand-tuned assembly code issues data to the LED drivers at a specific
151  // rate. There's separate code for different CPU speeds (8, 12, 16 MHz)
152  // for both the WS2811 (400 KHz) and WS2812 (800 KHz) drivers. The
153  // datastream timing for the LED drivers allows a little wiggle room each
154  // way (listed in the datasheets), so the conditions for compiling each
155  // case are set up for a range of frequencies rather than just the exact
156  // 8, 12 or 16 MHz values, permitting use with some close-but-not-spot-on
157  // devices (e.g. 16.5 MHz DigiSpark). The ranges were arrived at based
158  // on the datasheet figures and have not been extensively tested outside
159  // the canonical 8/12/16 MHz speeds; there's no guarantee these will work
160  // close to the extremes (or possibly they could be pushed further).
161  // Keep in mind only one CPU speed case actually gets compiled; the
162  // resulting program isn't as massive as it might look from source here.
163 
164 // 8 MHz(ish) AVR ---------------------------------------------------------
165 #if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL)
166 
167 #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
168  if(is800KHz) {
169 #endif
170 
171  volatile uint8_t n1, n2 = 0; // First, next bits out
172 
173  // Squeezing an 800 KHz stream out of an 8 MHz chip requires code
174  // specific to each PORT register. At present this is only written
175  // to work with pins on PORTD or PORTB, the most likely use case --
176  // this covers all the pins on the Adafruit Flora and the bulk of
177  // digital pins on the Arduino Pro 8 MHz (keep in mind, this code
178  // doesn't even get compiled for 16 MHz boards like the Uno, Mega,
179  // Leonardo, etc., so don't bother extending this out of hand).
180  // Additional PORTs could be added if you really need them, just
181  // duplicate the else and loop and change the PORT. Each add'l
182  // PORT will require about 150(ish) bytes of program space.
183 
184  // 10 instruction clocks per bit: HHxxxxxLLL
185  // OUT instructions: ^ ^ ^ (T=0,2,7)
186 
187 #ifdef PORTD // PORTD isn't present on ATtiny85, etc.
188 
189  if(port == &PORTD) {
190 
191  hi = PORTD | pinMask;
192  lo = PORTD & ~pinMask;
193  n1 = lo;
194  if(b & 0x80) n1 = hi;
195 
196  // Dirty trick: RJMPs proceeding to the next instruction are used
197  // to delay two clock cycles in one instruction word (rather than
198  // using two NOPs). This was necessary in order to squeeze the
199  // loop down to exactly 64 words -- the maximum possible for a
200  // relative branch.
201 
202  asm volatile(
203  "headD:" "\n\t" // Clk Pseudocode
204  // Bit 7:
205  "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
206  "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
207  "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
208  "rjmp .+0" "\n\t" // 2 nop nop
209  "sbrc %[byte] , 6" "\n\t" // 1-2 if(b & 0x40)
210  "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
211  "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
212  "rjmp .+0" "\n\t" // 2 nop nop
213  // Bit 6:
214  "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
215  "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
216  "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
217  "rjmp .+0" "\n\t" // 2 nop nop
218  "sbrc %[byte] , 5" "\n\t" // 1-2 if(b & 0x20)
219  "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
220  "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
221  "rjmp .+0" "\n\t" // 2 nop nop
222  // Bit 5:
223  "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
224  "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
225  "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
226  "rjmp .+0" "\n\t" // 2 nop nop
227  "sbrc %[byte] , 4" "\n\t" // 1-2 if(b & 0x10)
228  "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
229  "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
230  "rjmp .+0" "\n\t" // 2 nop nop
231  // Bit 4:
232  "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
233  "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
234  "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
235  "rjmp .+0" "\n\t" // 2 nop nop
236  "sbrc %[byte] , 3" "\n\t" // 1-2 if(b & 0x08)
237  "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
238  "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
239  "rjmp .+0" "\n\t" // 2 nop nop
240  // Bit 3:
241  "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
242  "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
243  "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
244  "rjmp .+0" "\n\t" // 2 nop nop
245  "sbrc %[byte] , 2" "\n\t" // 1-2 if(b & 0x04)
246  "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
247  "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
248  "rjmp .+0" "\n\t" // 2 nop nop
249  // Bit 2:
250  "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
251  "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
252  "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
253  "rjmp .+0" "\n\t" // 2 nop nop
254  "sbrc %[byte] , 1" "\n\t" // 1-2 if(b & 0x02)
255  "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
256  "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
257  "rjmp .+0" "\n\t" // 2 nop nop
258  // Bit 1:
259  "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
260  "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
261  "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
262  "rjmp .+0" "\n\t" // 2 nop nop
263  "sbrc %[byte] , 0" "\n\t" // 1-2 if(b & 0x01)
264  "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
265  "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
266  "sbiw %[count], 1" "\n\t" // 2 i-- (don't act on Z flag yet)
267  // Bit 0:
268  "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
269  "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
270  "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
271  "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++
272  "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80)
273  "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
274  "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
275  "brne headD" "\n" // 2 while(i) (Z flag set above)
276  : [byte] "+r" (b),
277  [n1] "+r" (n1),
278  [n2] "+r" (n2),
279  [count] "+w" (i)
280  : [port] "I" (_SFR_IO_ADDR(PORTD)),
281  [ptr] "e" (ptr),
282  [hi] "r" (hi),
283  [lo] "r" (lo));
284 
285  } else if(port == &PORTB) {
286 
287 #endif // PORTD
288 
289  // Same as above, just switched to PORTB and stripped of comments.
290  hi = PORTB | pinMask;
291  lo = PORTB & ~pinMask;
292  n1 = lo;
293  if(b & 0x80) n1 = hi;
294 
295  asm volatile(
296  "headB:" "\n\t"
297  "out %[port] , %[hi]" "\n\t"
298  "mov %[n2] , %[lo]" "\n\t"
299  "out %[port] , %[n1]" "\n\t"
300  "rjmp .+0" "\n\t"
301  "sbrc %[byte] , 6" "\n\t"
302  "mov %[n2] , %[hi]" "\n\t"
303  "out %[port] , %[lo]" "\n\t"
304  "rjmp .+0" "\n\t"
305  "out %[port] , %[hi]" "\n\t"
306  "mov %[n1] , %[lo]" "\n\t"
307  "out %[port] , %[n2]" "\n\t"
308  "rjmp .+0" "\n\t"
309  "sbrc %[byte] , 5" "\n\t"
310  "mov %[n1] , %[hi]" "\n\t"
311  "out %[port] , %[lo]" "\n\t"
312  "rjmp .+0" "\n\t"
313  "out %[port] , %[hi]" "\n\t"
314  "mov %[n2] , %[lo]" "\n\t"
315  "out %[port] , %[n1]" "\n\t"
316  "rjmp .+0" "\n\t"
317  "sbrc %[byte] , 4" "\n\t"
318  "mov %[n2] , %[hi]" "\n\t"
319  "out %[port] , %[lo]" "\n\t"
320  "rjmp .+0" "\n\t"
321  "out %[port] , %[hi]" "\n\t"
322  "mov %[n1] , %[lo]" "\n\t"
323  "out %[port] , %[n2]" "\n\t"
324  "rjmp .+0" "\n\t"
325  "sbrc %[byte] , 3" "\n\t"
326  "mov %[n1] , %[hi]" "\n\t"
327  "out %[port] , %[lo]" "\n\t"
328  "rjmp .+0" "\n\t"
329  "out %[port] , %[hi]" "\n\t"
330  "mov %[n2] , %[lo]" "\n\t"
331  "out %[port] , %[n1]" "\n\t"
332  "rjmp .+0" "\n\t"
333  "sbrc %[byte] , 2" "\n\t"
334  "mov %[n2] , %[hi]" "\n\t"
335  "out %[port] , %[lo]" "\n\t"
336  "rjmp .+0" "\n\t"
337  "out %[port] , %[hi]" "\n\t"
338  "mov %[n1] , %[lo]" "\n\t"
339  "out %[port] , %[n2]" "\n\t"
340  "rjmp .+0" "\n\t"
341  "sbrc %[byte] , 1" "\n\t"
342  "mov %[n1] , %[hi]" "\n\t"
343  "out %[port] , %[lo]" "\n\t"
344  "rjmp .+0" "\n\t"
345  "out %[port] , %[hi]" "\n\t"
346  "mov %[n2] , %[lo]" "\n\t"
347  "out %[port] , %[n1]" "\n\t"
348  "rjmp .+0" "\n\t"
349  "sbrc %[byte] , 0" "\n\t"
350  "mov %[n2] , %[hi]" "\n\t"
351  "out %[port] , %[lo]" "\n\t"
352  "sbiw %[count], 1" "\n\t"
353  "out %[port] , %[hi]" "\n\t"
354  "mov %[n1] , %[lo]" "\n\t"
355  "out %[port] , %[n2]" "\n\t"
356  "ld %[byte] , %a[ptr]+" "\n\t"
357  "sbrc %[byte] , 7" "\n\t"
358  "mov %[n1] , %[hi]" "\n\t"
359  "out %[port] , %[lo]" "\n\t"
360  "brne headB" "\n"
361  : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i)
362  : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi),
363  [lo] "r" (lo));
364 
365 #ifdef PORTD
366  } // endif PORTB
367 #endif
368 
369 #ifdef NEO_KHZ400
370  } else { // end 800 KHz, do 400 KHz
371 
372  // Timing is more relaxed; unrolling the inner loop for each bit is
373  // not necessary. Still using the peculiar RJMPs as 2X NOPs, not out
374  // of need but just to trim the code size down a little.
375  // This 400-KHz-datastream-on-8-MHz-CPU code is not quite identical
376  // to the 800-on-16 code later -- the hi/lo timing between WS2811 and
377  // WS2812 is not simply a 2:1 scale!
378 
379  // 20 inst. clocks per bit: HHHHxxxxxxLLLLLLLLLL
380  // ST instructions: ^ ^ ^ (T=0,4,10)
381 
382  volatile uint8_t next, bit;
383 
384  hi = *port | pinMask;
385  lo = *port & ~pinMask;
386  next = lo;
387  bit = 8;
388 
389  asm volatile(
390  "head20:" "\n\t" // Clk Pseudocode (T = 0)
391  "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
392  "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128)
393  "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4)
394  "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 6)
395  "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7)
396  "dec %[bit]" "\n\t" // 1 bit-- (T = 8)
397  "breq nextbyte20" "\n\t" // 1-2 if(bit == 0)
398  "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10)
399  "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 12)
400  "rjmp .+0" "\n\t" // 2 nop nop (T = 14)
401  "rjmp .+0" "\n\t" // 2 nop nop (T = 16)
402  "rjmp .+0" "\n\t" // 2 nop nop (T = 18)
403  "rjmp head20" "\n\t" // 2 -> head20 (next bit out)
404  "nextbyte20:" "\n\t" // (T = 10)
405  "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 12)
406  "nop" "\n\t" // 1 nop (T = 13)
407  "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 14)
408  "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 16)
409  "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18)
410  "brne head20" "\n" // 2 if(i != 0) -> (next byte)
411  : [port] "+e" (port),
412  [byte] "+r" (b),
413  [bit] "+r" (bit),
414  [next] "+r" (next),
415  [count] "+w" (i)
416  : [hi] "r" (hi),
417  [lo] "r" (lo),
418  [ptr] "e" (ptr));
419  }
420 #endif // NEO_KHZ400
421 
422 // 12 MHz(ish) AVR --------------------------------------------------------
423 #elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL)
424 
425 #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
426  if(is800KHz) {
427 #endif
428 
429  // In the 12 MHz case, an optimized 800 KHz datastream (no dead time
430  // between bytes) requires a PORT-specific loop similar to the 8 MHz
431  // code (but a little more relaxed in this case).
432 
433  // 15 instruction clocks per bit: HHHHxxxxxxLLLLL
434  // OUT instructions: ^ ^ ^ (T=0,4,10)
435 
436  volatile uint8_t next;
437 
438 #ifdef PORTD
439 
440  if(port == &PORTD) {
441 
442  hi = PORTD | pinMask;
443  lo = PORTD & ~pinMask;
444  next = lo;
445  if(b & 0x80) next = hi;
446 
447  // Don't "optimize" the OUT calls into the bitTime subroutine;
448  // we're exploiting the RCALL and RET as 3- and 4-cycle NOPs!
449  asm volatile(
450  "headD:" "\n\t" // (T = 0)
451  "out %[port], %[hi]" "\n\t" // (T = 1)
452  "rcall bitTimeD" "\n\t" // Bit 7 (T = 15)
453  "out %[port], %[hi]" "\n\t"
454  "rcall bitTimeD" "\n\t" // Bit 6
455  "out %[port], %[hi]" "\n\t"
456  "rcall bitTimeD" "\n\t" // Bit 5
457  "out %[port], %[hi]" "\n\t"
458  "rcall bitTimeD" "\n\t" // Bit 4
459  "out %[port], %[hi]" "\n\t"
460  "rcall bitTimeD" "\n\t" // Bit 3
461  "out %[port], %[hi]" "\n\t"
462  "rcall bitTimeD" "\n\t" // Bit 2
463  "out %[port], %[hi]" "\n\t"
464  "rcall bitTimeD" "\n\t" // Bit 1
465  // Bit 0:
466  "out %[port] , %[hi]" "\n\t" // 1 PORT = hi (T = 1)
467  "rjmp .+0" "\n\t" // 2 nop nop (T = 3)
468  "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 5)
469  "out %[port] , %[next]" "\n\t" // 1 PORT = next (T = 6)
470  "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7)
471  "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80) (T = 8)
472  "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 9)
473  "nop" "\n\t" // 1 (T = 10)
474  "out %[port] , %[lo]" "\n\t" // 1 PORT = lo (T = 11)
475  "sbiw %[count], 1" "\n\t" // 2 i-- (T = 13)
476  "brne headD" "\n\t" // 2 if(i != 0) -> (next byte)
477  "rjmp doneD" "\n\t"
478  "bitTimeD:" "\n\t" // nop nop nop (T = 4)
479  "out %[port], %[next]" "\n\t" // 1 PORT = next (T = 5)
480  "mov %[next], %[lo]" "\n\t" // 1 next = lo (T = 6)
481  "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 7)
482  "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 0x80) (T = 8)
483  "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 9)
484  "nop" "\n\t" // 1 (T = 10)
485  "out %[port], %[lo]" "\n\t" // 1 PORT = lo (T = 11)
486  "ret" "\n\t" // 4 nop nop nop nop (T = 15)
487  "doneD:" "\n"
488  : [byte] "+r" (b),
489  [next] "+r" (next),
490  [count] "+w" (i)
491  : [port] "I" (_SFR_IO_ADDR(PORTD)),
492  [ptr] "e" (ptr),
493  [hi] "r" (hi),
494  [lo] "r" (lo));
495 
496  } else if(port == &PORTB) {
497 
498 #endif // PORTD
499 
500  hi = PORTB | pinMask;
501  lo = PORTB & ~pinMask;
502  next = lo;
503  if(b & 0x80) next = hi;
504 
505  // Same as above, just set for PORTB & stripped of comments
506  asm volatile(
507  "headB:" "\n\t"
508  "out %[port], %[hi]" "\n\t"
509  "rcall bitTimeB" "\n\t"
510  "out %[port], %[hi]" "\n\t"
511  "rcall bitTimeB" "\n\t"
512  "out %[port], %[hi]" "\n\t"
513  "rcall bitTimeB" "\n\t"
514  "out %[port], %[hi]" "\n\t"
515  "rcall bitTimeB" "\n\t"
516  "out %[port], %[hi]" "\n\t"
517  "rcall bitTimeB" "\n\t"
518  "out %[port], %[hi]" "\n\t"
519  "rcall bitTimeB" "\n\t"
520  "out %[port], %[hi]" "\n\t"
521  "rcall bitTimeB" "\n\t"
522  "out %[port] , %[hi]" "\n\t"
523  "rjmp .+0" "\n\t"
524  "ld %[byte] , %a[ptr]+" "\n\t"
525  "out %[port] , %[next]" "\n\t"
526  "mov %[next] , %[lo]" "\n\t"
527  "sbrc %[byte] , 7" "\n\t"
528  "mov %[next] , %[hi]" "\n\t"
529  "nop" "\n\t"
530  "out %[port] , %[lo]" "\n\t"
531  "sbiw %[count], 1" "\n\t"
532  "brne headB" "\n\t"
533  "rjmp doneB" "\n\t"
534  "bitTimeB:" "\n\t"
535  "out %[port], %[next]" "\n\t"
536  "mov %[next], %[lo]" "\n\t"
537  "rol %[byte]" "\n\t"
538  "sbrc %[byte], 7" "\n\t"
539  "mov %[next], %[hi]" "\n\t"
540  "nop" "\n\t"
541  "out %[port], %[lo]" "\n\t"
542  "ret" "\n\t"
543  "doneB:" "\n"
544  : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i)
545  : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi),
546  [lo] "r" (lo));
547 
548 #ifdef PORTD
549  }
550 #endif
551 
552 #ifdef NEO_KHZ400
553  } else { // 400 KHz
554 
555  // 30 instruction clocks per bit: HHHHHHxxxxxxxxxLLLLLLLLLLLLLLL
556  // ST instructions: ^ ^ ^ (T=0,6,15)
557 
558  volatile uint8_t next, bit;
559 
560  hi = *port | pinMask;
561  lo = *port & ~pinMask;
562  next = lo;
563  bit = 8;
564 
565  asm volatile(
566  "head30:" "\n\t" // Clk Pseudocode (T = 0)
567  "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
568  "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128)
569  "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4)
570  "rjmp .+0" "\n\t" // 2 nop nop (T = 6)
571  "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 8)
572  "rjmp .+0" "\n\t" // 2 nop nop (T = 10)
573  "rjmp .+0" "\n\t" // 2 nop nop (T = 12)
574  "rjmp .+0" "\n\t" // 2 nop nop (T = 14)
575  "nop" "\n\t" // 1 nop (T = 15)
576  "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 17)
577  "rjmp .+0" "\n\t" // 2 nop nop (T = 19)
578  "dec %[bit]" "\n\t" // 1 bit-- (T = 20)
579  "breq nextbyte30" "\n\t" // 1-2 if(bit == 0)
580  "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 22)
581  "rjmp .+0" "\n\t" // 2 nop nop (T = 24)
582  "rjmp .+0" "\n\t" // 2 nop nop (T = 26)
583  "rjmp .+0" "\n\t" // 2 nop nop (T = 28)
584  "rjmp head30" "\n\t" // 2 -> head30 (next bit out)
585  "nextbyte30:" "\n\t" // (T = 22)
586  "nop" "\n\t" // 1 nop (T = 23)
587  "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 24)
588  "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 26)
589  "sbiw %[count], 1" "\n\t" // 2 i-- (T = 28)
590  "brne head30" "\n" // 1-2 if(i != 0) -> (next byte)
591  : [port] "+e" (port),
592  [byte] "+r" (b),
593  [bit] "+r" (bit),
594  [next] "+r" (next),
595  [count] "+w" (i)
596  : [hi] "r" (hi),
597  [lo] "r" (lo),
598  [ptr] "e" (ptr));
599  }
600 #endif // NEO_KHZ400
601 
602 // 16 MHz(ish) AVR --------------------------------------------------------
603 #elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L)
604 
605 #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
606  if(is800KHz) {
607 #endif
608 
609  // WS2811 and WS2812 have different hi/lo duty cycles; this is
610  // similar but NOT an exact copy of the prior 400-on-8 code.
611 
612  // 20 inst. clocks per bit: HHHHHxxxxxxxxLLLLLLL
613  // ST instructions: ^ ^ ^ (T=0,5,13)
614 
615  volatile uint8_t next, bit;
616 
617  hi = *port | pinMask;
618  lo = *port & ~pinMask;
619  next = lo;
620  bit = 8;
621 
622  asm volatile(
623  "head20:" "\n\t" // Clk Pseudocode (T = 0)
624  "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
625  "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 128)
626  "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4)
627  "dec %[bit]" "\n\t" // 1 bit-- (T = 5)
628  "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 7)
629  "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 8)
630  "breq nextbyte20" "\n\t" // 1-2 if(bit == 0) (from dec above)
631  "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10)
632  "rjmp .+0" "\n\t" // 2 nop nop (T = 12)
633  "nop" "\n\t" // 1 nop (T = 13)
634  "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 15)
635  "nop" "\n\t" // 1 nop (T = 16)
636  "rjmp .+0" "\n\t" // 2 nop nop (T = 18)
637  "rjmp head20" "\n\t" // 2 -> head20 (next bit out)
638  "nextbyte20:" "\n\t" // (T = 10)
639  "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 11)
640  "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 13)
641  "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 15)
642  "nop" "\n\t" // 1 nop (T = 16)
643  "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18)
644  "brne head20" "\n" // 2 if(i != 0) -> (next byte)
645  : [port] "+e" (port),
646  [byte] "+r" (b),
647  [bit] "+r" (bit),
648  [next] "+r" (next),
649  [count] "+w" (i)
650  : [ptr] "e" (ptr),
651  [hi] "r" (hi),
652  [lo] "r" (lo));
653 
654 #ifdef NEO_KHZ400
655  } else { // 400 KHz
656 
657  // The 400 KHz clock on 16 MHz MCU is the most 'relaxed' version.
658 
659  // 40 inst. clocks per bit: HHHHHHHHxxxxxxxxxxxxLLLLLLLLLLLLLLLLLLLL
660  // ST instructions: ^ ^ ^ (T=0,8,20)
661 
662  volatile uint8_t next, bit;
663 
664  hi = *port | pinMask;
665  lo = *port & ~pinMask;
666  next = lo;
667  bit = 8;
668 
669  asm volatile(
670  "head40:" "\n\t" // Clk Pseudocode (T = 0)
671  "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
672  "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128)
673  "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 4)
674  "rjmp .+0" "\n\t" // 2 nop nop (T = 6)
675  "rjmp .+0" "\n\t" // 2 nop nop (T = 8)
676  "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 10)
677  "rjmp .+0" "\n\t" // 2 nop nop (T = 12)
678  "rjmp .+0" "\n\t" // 2 nop nop (T = 14)
679  "rjmp .+0" "\n\t" // 2 nop nop (T = 16)
680  "rjmp .+0" "\n\t" // 2 nop nop (T = 18)
681  "rjmp .+0" "\n\t" // 2 nop nop (T = 20)
682  "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 22)
683  "nop" "\n\t" // 1 nop (T = 23)
684  "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 24)
685  "dec %[bit]" "\n\t" // 1 bit-- (T = 25)
686  "breq nextbyte40" "\n\t" // 1-2 if(bit == 0)
687  "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 27)
688  "nop" "\n\t" // 1 nop (T = 28)
689  "rjmp .+0" "\n\t" // 2 nop nop (T = 30)
690  "rjmp .+0" "\n\t" // 2 nop nop (T = 32)
691  "rjmp .+0" "\n\t" // 2 nop nop (T = 34)
692  "rjmp .+0" "\n\t" // 2 nop nop (T = 36)
693  "rjmp .+0" "\n\t" // 2 nop nop (T = 38)
694  "rjmp head40" "\n\t" // 2 -> head40 (next bit out)
695  "nextbyte40:" "\n\t" // (T = 27)
696  "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 28)
697  "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 30)
698  "rjmp .+0" "\n\t" // 2 nop nop (T = 32)
699  "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 34)
700  "rjmp .+0" "\n\t" // 2 nop nop (T = 36)
701  "sbiw %[count], 1" "\n\t" // 2 i-- (T = 38)
702  "brne head40" "\n" // 1-2 if(i != 0) -> (next byte)
703  : [port] "+e" (port),
704  [byte] "+r" (b),
705  [bit] "+r" (bit),
706  [next] "+r" (next),
707  [count] "+w" (i)
708  : [ptr] "e" (ptr),
709  [hi] "r" (hi),
710  [lo] "r" (lo));
711  }
712 #endif // NEO_KHZ400
713 
714 #else
715  #error "CPU SPEED NOT SUPPORTED"
716 #endif // end F_CPU ifdefs on __AVR__
717 
718 // END AVR ----------------------------------------------------------------
719 
720 
721 #elif defined(__arm__)
722 
723 // ARM MCUs -- Teensy 3.0, 3.1, LC, Arduino Due ---------------------------
724 
725 #if defined(__MK20DX128__) || defined(__MK20DX256__) // Teensy 3.0 & 3.1
726 #define CYCLES_800_T0H (F_CPU / 4000000)
727 #define CYCLES_800_T1H (F_CPU / 1250000)
728 #define CYCLES_800 (F_CPU / 800000)
729 #define CYCLES_400_T0H (F_CPU / 2000000)
730 #define CYCLES_400_T1H (F_CPU / 833333)
731 #define CYCLES_400 (F_CPU / 400000)
732 
733  uint8_t *p = pixels,
734  *end = p + numBytes, pix, mask;
735  volatile uint8_t *set = portSetRegister(pin),
736  *clr = portClearRegister(pin);
737  uint32_t cyc;
738 
739  ARM_DEMCR |= ARM_DEMCR_TRCENA;
740  ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
741 
742 #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
743  if(is800KHz) {
744 #endif
745  cyc = ARM_DWT_CYCCNT + CYCLES_800;
746  while(p < end) {
747  pix = *p++;
748  for(mask = 0x80; mask; mask >>= 1) {
749  while(ARM_DWT_CYCCNT - cyc < CYCLES_800);
750  cyc = ARM_DWT_CYCCNT;
751  *set = 1;
752  if(pix & mask) {
753  while(ARM_DWT_CYCCNT - cyc < CYCLES_800_T1H);
754  } else {
755  while(ARM_DWT_CYCCNT - cyc < CYCLES_800_T0H);
756  }
757  *clr = 1;
758  }
759  }
760  while(ARM_DWT_CYCCNT - cyc < CYCLES_800);
761 #ifdef NEO_KHZ400
762  } else { // 400 kHz bitstream
763  cyc = ARM_DWT_CYCCNT + CYCLES_400;
764  while(p < end) {
765  pix = *p++;
766  for(mask = 0x80; mask; mask >>= 1) {
767  while(ARM_DWT_CYCCNT - cyc < CYCLES_400);
768  cyc = ARM_DWT_CYCCNT;
769  *set = 1;
770  if(pix & mask) {
771  while(ARM_DWT_CYCCNT - cyc < CYCLES_400_T1H);
772  } else {
773  while(ARM_DWT_CYCCNT - cyc < CYCLES_400_T0H);
774  }
775  *clr = 1;
776  }
777  }
778  while(ARM_DWT_CYCCNT - cyc < CYCLES_400);
779  }
780 #endif // NEO_KHZ400
781 
782 #elif defined(__MKL26Z64__) // Teensy-LC
783 
784 #if F_CPU == 48000000
785  uint8_t *p = pixels,
786  pix, count, dly,
787  bitmask = digitalPinToBitMask(pin);
788  volatile uint8_t *reg = portSetRegister(pin);
789  uint32_t num = numBytes;
790  asm volatile(
791  "L%=_begin:" "\n\t"
792  "ldrb %[pix], [%[p], #0]" "\n\t"
793  "lsl %[pix], #24" "\n\t"
794  "movs %[count], #7" "\n\t"
795  "L%=_loop:" "\n\t"
796  "lsl %[pix], #1" "\n\t"
797  "bcs L%=_loop_one" "\n\t"
798  "L%=_loop_zero:"
799  "strb %[bitmask], [%[reg], #0]" "\n\t"
800  "movs %[dly], #4" "\n\t"
801  "L%=_loop_delay_T0H:" "\n\t"
802  "sub %[dly], #1" "\n\t"
803  "bne L%=_loop_delay_T0H" "\n\t"
804  "strb %[bitmask], [%[reg], #4]" "\n\t"
805  "movs %[dly], #13" "\n\t"
806  "L%=_loop_delay_T0L:" "\n\t"
807  "sub %[dly], #1" "\n\t"
808  "bne L%=_loop_delay_T0L" "\n\t"
809  "b L%=_next" "\n\t"
810  "L%=_loop_one:"
811  "strb %[bitmask], [%[reg], #0]" "\n\t"
812  "movs %[dly], #13" "\n\t"
813  "L%=_loop_delay_T1H:" "\n\t"
814  "sub %[dly], #1" "\n\t"
815  "bne L%=_loop_delay_T1H" "\n\t"
816  "strb %[bitmask], [%[reg], #4]" "\n\t"
817  "movs %[dly], #4" "\n\t"
818  "L%=_loop_delay_T1L:" "\n\t"
819  "sub %[dly], #1" "\n\t"
820  "bne L%=_loop_delay_T1L" "\n\t"
821  "nop" "\n\t"
822  "L%=_next:" "\n\t"
823  "sub %[count], #1" "\n\t"
824  "bne L%=_loop" "\n\t"
825  "lsl %[pix], #1" "\n\t"
826  "bcs L%=_last_one" "\n\t"
827  "L%=_last_zero:"
828  "strb %[bitmask], [%[reg], #0]" "\n\t"
829  "movs %[dly], #4" "\n\t"
830  "L%=_last_delay_T0H:" "\n\t"
831  "sub %[dly], #1" "\n\t"
832  "bne L%=_last_delay_T0H" "\n\t"
833  "strb %[bitmask], [%[reg], #4]" "\n\t"
834  "movs %[dly], #10" "\n\t"
835  "L%=_last_delay_T0L:" "\n\t"
836  "sub %[dly], #1" "\n\t"
837  "bne L%=_last_delay_T0L" "\n\t"
838  "b L%=_repeat" "\n\t"
839  "L%=_last_one:"
840  "strb %[bitmask], [%[reg], #0]" "\n\t"
841  "movs %[dly], #13" "\n\t"
842  "L%=_last_delay_T1H:" "\n\t"
843  "sub %[dly], #1" "\n\t"
844  "bne L%=_last_delay_T1H" "\n\t"
845  "strb %[bitmask], [%[reg], #4]" "\n\t"
846  "movs %[dly], #1" "\n\t"
847  "L%=_last_delay_T1L:" "\n\t"
848  "sub %[dly], #1" "\n\t"
849  "bne L%=_last_delay_T1L" "\n\t"
850  "nop" "\n\t"
851  "L%=_repeat:" "\n\t"
852  "add %[p], #1" "\n\t"
853  "sub %[num], #1" "\n\t"
854  "bne L%=_begin" "\n\t"
855  "L%=_done:" "\n\t"
856  : [p] "+r" (p),
857  [pix] "=&r" (pix),
858  [count] "=&r" (count),
859  [dly] "=&r" (dly),
860  [num] "+r" (num)
861  : [bitmask] "r" (bitmask),
862  [reg] "r" (reg)
863  );
864 #else
865 #error "Sorry, only 48 MHz is supported, please set Tools > CPU Speed to 48 MHz"
866 #endif // F_CPU == 48000000
867 
868 #elif defined(__SAMD21G18A__) // Arduino Zero
869 
870  // Tried this with a timer/counter, couldn't quite get adequate
871  // resolution. So yay, you get a load of goofball NOPs...
872 
873  uint8_t *ptr, *end, p, bitMask, portNum;
874  uint32_t pinMask;
875 
876  portNum = g_APinDescription[pin].ulPort;
877  pinMask = 1ul << g_APinDescription[pin].ulPin;
878  ptr = pixels;
879  end = ptr + numBytes;
880  p = *ptr++;
881  bitMask = 0x80;
882 
883  volatile uint32_t *set = &(PORT->Group[portNum].OUTSET.reg),
884  *clr = &(PORT->Group[portNum].OUTCLR.reg);
885 
886 #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
887  if(is800KHz) {
888 #endif
889  for(;;) {
890  *set = pinMask;
891  asm("nop; nop; nop; nop; nop; nop; nop; nop;");
892  if(p & bitMask) {
893  asm("nop; nop; nop; nop; nop; nop; nop; nop;"
894  "nop; nop; nop; nop; nop; nop; nop; nop;"
895  "nop; nop; nop; nop;");
896  *clr = pinMask;
897  } else {
898  *clr = pinMask;
899  asm("nop; nop; nop; nop; nop; nop; nop; nop;"
900  "nop; nop; nop; nop; nop; nop; nop; nop;"
901  "nop; nop; nop; nop;");
902  }
903  if(bitMask >>= 1) {
904  asm("nop; nop; nop; nop; nop; nop; nop; nop; nop;");
905  } else {
906  if(ptr >= end) break;
907  p = *ptr++;
908  bitMask = 0x80;
909  }
910  }
911 #ifdef NEO_KHZ400
912  } else { // 400 KHz bitstream
913  for(;;) {
914  *set = pinMask;
915  asm("nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop;");
916  if(p & bitMask) {
917  asm("nop; nop; nop; nop; nop; nop; nop; nop;"
918  "nop; nop; nop; nop; nop; nop; nop; nop;"
919  "nop; nop; nop; nop; nop; nop; nop; nop;"
920  "nop; nop; nop;");
921  *clr = pinMask;
922  } else {
923  *clr = pinMask;
924  asm("nop; nop; nop; nop; nop; nop; nop; nop;"
925  "nop; nop; nop; nop; nop; nop; nop; nop;"
926  "nop; nop; nop; nop; nop; nop; nop; nop;"
927  "nop; nop; nop;");
928  }
929  asm("nop; nop; nop; nop; nop; nop; nop; nop;"
930  "nop; nop; nop; nop; nop; nop; nop; nop;"
931  "nop; nop; nop; nop; nop; nop; nop; nop;"
932  "nop; nop; nop; nop; nop; nop; nop; nop;");
933  if(bitMask >>= 1) {
934  asm("nop; nop; nop; nop; nop; nop; nop;");
935  } else {
936  if(ptr >= end) break;
937  p = *ptr++;
938  bitMask = 0x80;
939  }
940  }
941  }
942 #endif
943 
944 
945 #else // Other ARM architecture -- Presumed Arduino Due
946 
947  #define SCALE VARIANT_MCK / 2UL / 1000000UL
948  #define INST (2UL * F_CPU / VARIANT_MCK)
949  #define TIME_800_0 ((int)(0.40 * SCALE + 0.5) - (5 * INST))
950  #define TIME_800_1 ((int)(0.80 * SCALE + 0.5) - (5 * INST))
951  #define PERIOD_800 ((int)(1.25 * SCALE + 0.5) - (5 * INST))
952  #define TIME_400_0 ((int)(0.50 * SCALE + 0.5) - (5 * INST))
953  #define TIME_400_1 ((int)(1.20 * SCALE + 0.5) - (5 * INST))
954  #define PERIOD_400 ((int)(2.50 * SCALE + 0.5) - (5 * INST))
955 
956  int pinMask, time0, time1, period, t;
957  Pio *port;
958  volatile WoReg *portSet, *portClear, *timeValue, *timeReset;
959  uint8_t *p, *end, pix, mask;
960 
961  pmc_set_writeprotect(false);
962  pmc_enable_periph_clk((uint32_t)TC3_IRQn);
963  TC_Configure(TC1, 0,
964  TC_CMR_WAVE | TC_CMR_WAVSEL_UP | TC_CMR_TCCLKS_TIMER_CLOCK1);
965  TC_Start(TC1, 0);
966 
967  pinMask = g_APinDescription[pin].ulPin; // Don't 'optimize' these into
968  port = g_APinDescription[pin].pPort; // declarations above. Want to
969  portSet = &(port->PIO_SODR); // burn a few cycles after
970  portClear = &(port->PIO_CODR); // starting timer to minimize
971  timeValue = &(TC1->TC_CHANNEL[0].TC_CV); // the initial 'while'.
972  timeReset = &(TC1->TC_CHANNEL[0].TC_CCR);
973  p = pixels;
974  end = p + numBytes;
975  pix = *p++;
976  mask = 0x80;
977 
978 #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
979  if(is800KHz) {
980 #endif
981  time0 = TIME_800_0;
982  time1 = TIME_800_1;
983  period = PERIOD_800;
984 #ifdef NEO_KHZ400
985  } else { // 400 KHz bitstream
986  time0 = TIME_400_0;
987  time1 = TIME_400_1;
988  period = PERIOD_400;
989  }
990 #endif
991 
992  for(t = time0;; t = time0) {
993  if(pix & mask) t = time1;
994  while(*timeValue < period);
995  *portSet = pinMask;
996  *timeReset = TC_CCR_CLKEN | TC_CCR_SWTRG;
997  while(*timeValue < t);
998  *portClear = pinMask;
999  if(!(mask >>= 1)) { // This 'inside-out' loop logic utilizes
1000  if(p >= end) break; // idle time to minimize inter-byte delays.
1001  pix = *p++;
1002  mask = 0x80;
1003  }
1004  }
1005  while(*timeValue < period); // Wait for last bit
1006  TC_Stop(TC1, 0);
1007 
1008 #endif // end Due
1009 
1010 // END ARM ----------------------------------------------------------------
1011 
1012 
1013 #elif defined(ESP8266)
1014 
1015 // ESP8266 ----------------------------------------------------------------
1016 
1017  // ESP8266 show() is external to enforce ICACHE_RAM_ATTR execution
1018  espShow(pin, pixels, numBytes, is800KHz);
1019 
1020 #endif // ESP8266
1021 
1022 
1023 // END ARCHITECTURE SELECT ------------------------------------------------
1024 
1025 
1026  interrupts();
1027  endTime = micros(); // Save EOD time for latch on next call
1028 }
1029 
1030 // Set the output pin number
1031 void Adafruit_NeoPixel::setPin(uint8_t p) {
1032  if(begun && (pin >= 0)) pinMode(pin, INPUT);
1033  if(p >= 0) {
1034  pin = p;
1035  if(begun) {
1036  pinMode(p, OUTPUT);
1037  digitalWrite(p, LOW);
1038  }
1039 #ifdef __AVR__
1040  port = portOutputRegister(digitalPinToPort(p));
1041  pinMask = digitalPinToBitMask(p);
1042 #endif
1043  }
1044 }
1045 
1046 // Set pixel color from separate R,G,B components:
1048  uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
1049 
1050  if(n < numLEDs) {
1051  if(brightness) { // See notes in setBrightness()
1052  r = (r * brightness) >> 8;
1053  g = (g * brightness) >> 8;
1054  b = (b * brightness) >> 8;
1055  }
1056  uint8_t *p;
1057  if(wOffset == rOffset) { // Is an RGB-type strip
1058  p = &pixels[n * 3]; // 3 bytes per pixel
1059  } else { // Is a WRGB-type strip
1060  p = &pixels[n * 4]; // 4 bytes per pixel
1061  p[wOffset] = 0; // But only R,G,B passed -- set W to 0
1062  }
1063  p[rOffset] = r; // R,G,B always stored
1064  p[gOffset] = g;
1065  p[bOffset] = b;
1066  }
1067 }
1068 
1069 // Set pixel color from 'packed' 32-bit RGB color:
1070 void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) {
1071  if(n < numLEDs) {
1072  uint8_t *p,
1073  r = (uint8_t)(c >> 16),
1074  g = (uint8_t)(c >> 8),
1075  b = (uint8_t)c;
1076  if(brightness) { // See notes in setBrightness()
1077  r = (r * brightness) >> 8;
1078  g = (g * brightness) >> 8;
1079  b = (b * brightness) >> 8;
1080  }
1081  if(wOffset == rOffset) {
1082  p = &pixels[n * 3];
1083  } else {
1084  p = &pixels[n * 4];
1085  uint8_t w = (uint8_t)(c >> 24);
1086  p[wOffset] = brightness ? ((w * brightness) >> 8) : w;
1087  }
1088  p[rOffset] = r;
1089  p[gOffset] = g;
1090  p[bOffset] = b;
1091  }
1092 }
1093 
1094 // Convert separate R,G,B into packed 32-bit RGB color.
1095 // Packed format is always RGB, regardless of LED strand color order.
1096 uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) {
1097  return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
1098 }
1099 
1100 // Convert separate R,G,B,W into packed 32-bit WRGB color.
1101 // Packed format is always WRGB, regardless of LED strand color order.
1102 uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
1103  return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
1104 }
1105 
1106 // Query color from previously-set pixel (returns packed 32-bit RGB value)
1107 uint32_t Adafruit_NeoPixel::getPixelColor(uint16_t n) const {
1108  if(n >= numLEDs) return 0; // Out of bounds, return no color.
1109 
1110  uint8_t *p;
1111 
1112  if(wOffset == rOffset) { // Is RGB-type device
1113  p = &pixels[n * 3];
1114  if(brightness) {
1115  // Stored color was decimated by setBrightness(). Returned value
1116  // attempts to scale back to an approximation of the original 24-bit
1117  // value used when setting the pixel color, but there will always be
1118  // some error -- those bits are simply gone. Issue is most
1119  // pronounced at low brightness levels.
1120  return (((uint32_t)(p[rOffset] << 8) / brightness) << 16) |
1121  (((uint32_t)(p[gOffset] << 8) / brightness) << 8) |
1122  ( (uint32_t)(p[bOffset] << 8) / brightness );
1123  } else {
1124  // No brightness adjustment has been made -- return 'raw' color
1125  return ((uint32_t)p[rOffset] << 16) |
1126  ((uint32_t)p[gOffset] << 8) |
1127  (uint32_t)p[bOffset];
1128  }
1129  } else { // Is RGBW-type device
1130  p = &pixels[n * 4];
1131  if(brightness) { // Return scaled color
1132  return (((uint32_t)(p[wOffset] << 8) / brightness) << 24) |
1133  (((uint32_t)(p[rOffset] << 8) / brightness) << 16) |
1134  (((uint32_t)(p[gOffset] << 8) / brightness) << 8) |
1135  ( (uint32_t)(p[bOffset] << 8) / brightness );
1136  } else { // Return raw color
1137  return ((uint32_t)p[wOffset] << 24) |
1138  ((uint32_t)p[rOffset] << 16) |
1139  ((uint32_t)p[gOffset] << 8) |
1140  (uint32_t)p[bOffset];
1141  }
1142  }
1143 }
1144 
1145 // Returns pointer to pixels[] array. Pixel data is stored in device-
1146 // native format and is not translated here. Application will need to be
1147 // aware of specific pixel data format and handle colors appropriately.
1148 uint8_t *Adafruit_NeoPixel::getPixels(void) const {
1149  return pixels;
1150 }
1151 
1152 uint16_t Adafruit_NeoPixel::numPixels(void) const {
1153  return numLEDs;
1154 }
1155 
1156 // Adjust output brightness; 0=darkest (off), 255=brightest. This does
1157 // NOT immediately affect what's currently displayed on the LEDs. The
1158 // next call to show() will refresh the LEDs at this level. However,
1159 // this process is potentially "lossy," especially when increasing
1160 // brightness. The tight timing in the WS2811/WS2812 code means there
1161 // aren't enough free cycles to perform this scaling on the fly as data
1162 // is issued. So we make a pass through the existing color data in RAM
1163 // and scale it (subsequent graphics commands also work at this
1164 // brightness level). If there's a significant step up in brightness,
1165 // the limited number of steps (quantization) in the old data will be
1166 // quite visible in the re-scaled version. For a non-destructive
1167 // change, you'll need to re-render the full strip data. C'est la vie.
1169  // Stored brightness value is different than what's passed.
1170  // This simplifies the actual scaling math later, allowing a fast
1171  // 8x8-bit multiply and taking the MSB. 'brightness' is a uint8_t,
1172  // adding 1 here may (intentionally) roll over...so 0 = max brightness
1173  // (color values are interpreted literally; no scaling), 1 = min
1174  // brightness (off), 255 = just below max brightness.
1175  uint8_t newBrightness = b + 1;
1176  if(newBrightness != brightness) { // Compare against prior value
1177  // Brightness has changed -- re-scale existing data in RAM
1178  uint8_t c,
1179  *ptr = pixels,
1180  oldBrightness = brightness - 1; // De-wrap old brightness value
1181  uint16_t scale;
1182  if(oldBrightness == 0) scale = 0; // Avoid /0
1183  else if(b == 255) scale = 65535 / oldBrightness;
1184  else scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness;
1185  for(uint16_t i=0; i<numBytes; i++) {
1186  c = *ptr;
1187  *ptr++ = (c * scale) >> 8;
1188  }
1189  brightness = newBrightness;
1190  }
1191 }
1192 
1193 //Return the brightness value
1195  return brightness - 1;
1196 }
1197 
1199  memset(pixels, 0, numBytes);
1200 }