#include "NeoPixel.h" // Constructor when length, pin and type are known at compile-time: Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, uint8_t p, neoPixelType t) : begun(false), brightness(0), pixels(NULL), endTime(0) { updateType(t); updateLength(n); setPin(p); } Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n1, uint16_t n2, uint8_t p1, uint8_t p2, neoPixelType t) : begun(false), brightness(0), pixels(NULL), endTime(0) { updateType(t); updateLength(n1+n2); setPin(p1, p2); } // via Michael Vogt/neophob: empty constructor is used when strand length // isn't known at compile-time; situations where program config might be // read from internal flash memory or an SD card, or arrive via serial // command. If using this constructor, MUST follow up with updateType(), // updateLength(), etc. to establish the strand type, length and pin number! 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) { } Adafruit_NeoPixel::~Adafruit_NeoPixel() { if(pixels) free(pixels); if(pin >= 0) pinMode(pin, INPUT); } void Adafruit_NeoPixel::begin(void) { if(pin >= 0) { pinMode(pin, OUTPUT); digitalWrite(pin, LOW); } begun = true; } void Adafruit_NeoPixel::updateLength(uint16_t n) { if(pixels) 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; } } void Adafruit_NeoPixel::updateType(neoPixelType t) { boolean 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) { boolean newThreeBytesPerPixel = (wOffset == rOffset); if(newThreeBytesPerPixel != oldThreeBytesPerPixel) updateLength(numLEDs); } } 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. // NRF52 may use PWM + DMA (if available), may not need to disable interrupt #ifndef NRF52 noInterrupts(); // Need 100% focus on instruction timing #endif #if defined(__arm__) #if defined (__SAMD21E17A__) || defined(__SAMD21G18A__) || defined(__SAMD21E18A__) || defined(__SAMD21J18A__) // Arduino Zero, Gemma/Trinket M0, SODAQ Autonomo and others // Tried this with a timer/counter, couldn't quite get adequate // resolution. So yay, you get a load of goofball NOPs... uint8_t *ptr, *end, p, bitMask, portNum; uint32_t pinMask; portNum = g_APinDescription[pin].ulPort; pinMask = 1ul << g_APinDescription[pin].ulPin; ptr = pixels; end = ptr + numBytes; p = *ptr++; bitMask = 0x80; volatile uint32_t *set = &(PORT->Group[portNum].OUTSET.reg), *clr = &(PORT->Group[portNum].OUTCLR.reg); #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled if(is800KHz) { #endif for(;;) { *set = pinMask; asm("nop; nop; nop; nop; nop; nop; nop; nop;"); if(p & bitMask) { asm("nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop;"); *clr = pinMask; } else { *clr = pinMask; asm("nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop;"); } if(bitMask >>= 1) { asm("nop; nop; nop; nop; nop; nop; nop; nop; nop;"); } else { if(ptr >= end) break; p = *ptr++; bitMask = 0x80; } } #ifdef NEO_KHZ400 } else { // 400 KHz bitstream for(;;) { *set = pinMask; asm("nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop;"); if(p & bitMask) { asm("nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop;"); *clr = pinMask; } else { *clr = pinMask; asm("nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop;"); } asm("nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;"); if(bitMask >>= 1) { asm("nop; nop; nop; nop; nop; nop; nop;"); } else { if(ptr >= end) break; p = *ptr++; bitMask = 0x80; } } } #endif #endif // END ARM ---------------------------------------------------------------- #else #error Architecture not supported #endif // END ARCHITECTURE SELECT ------------------------------------------------ #ifndef NRF52 interrupts(); #endif endTime = micros(); // Save EOD time for latch on next call } void Adafruit_NeoPixel::show_strips(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. // NRF52 may use PWM + DMA (if available), may not need to disable interrupt #ifndef NRF52 noInterrupts(); // Need 100% focus on instruction timing #endif #if defined(__arm__) #if defined (__SAMD21E17A__) || defined(__SAMD21G18A__) || defined(__SAMD21E18A__) || defined(__SAMD21J18A__) // Arduino Zero, Gemma/Trinket M0, SODAQ Autonomo and others // Tried this with a timer/counter, couldn't quite get adequate // resolution. So yay, you get a load of goofball NOPs... uint8_t temp_pin[3] = {8, 4, 3}; uint8_t *ptr1, *ptr2, *end, p1, p2, bitMask, portNum[3], val1, val2; uint32_t pinMask[3]; for(int i=0; i<2; i++) { portNum[i] = g_APinDescription[temp_pin[i]].ulPort; pinMask[i] = 1ul << g_APinDescription[temp_pin[i]].ulPin; } ptr1 = pixels; ptr2 = pixels; end = ptr1 + numBytes/2; p1 = *ptr1++; p2 = *ptr2++; bitMask = 0x80; uint32_t *set0 = (uint32_t*)(&(PORT->Group[portNum[0]].OUTSET.reg)), *clr0 = (uint32_t*)(&(PORT->Group[portNum[0]].OUTCLR.reg)), *set1 = (uint32_t*)(&(PORT->Group[portNum[1]].OUTSET.reg)), *clr1 = (uint32_t*)(&(PORT->Group[portNum[1]].OUTCLR.reg)); // #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled // if(is800KHz) { // #endif if (p1 & bitMask) val1=1; else val1=0; if (p2 & bitMask) val2=1; else val2=0; for(;;) { // Serial.print(p1); // Serial.print(" "); // Serial.println(p2); *set0 = pinMask[0]; *set1 = pinMask[1]; if((val1)&&(val2)) { asm("nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; "); *clr0 = pinMask[0]; //asm("nop;"); *clr1 = pinMask[1]; } else if((!val1)&&(!val2)) { *clr0 = pinMask[0]; //asm("nop;"); *clr1 = pinMask[1]; asm("nop; nop; nop; nop; nop; nop; nop; nop;" ); } // else if((p1 & bitMask)&&(!(p2 & bitMask))) // { // asm("nop; nop; nop; nop; nop; nop; nop;" // ); // *clr0 = pinMask[0]; // asm("nop;"); // *clr1 = pinMask[1]; // } // else if((!(p1 & bitMask))&&((p2 & bitMask))) // { // *clr0 = pinMask[0]; // asm("nop;"); // *clr1 = pinMask[1]; // asm("nop; nop; nop; nop; nop; nop; nop; nop;" // "nop; nop; nop; nop; nop; nop;"); // } if(bitMask >>= 1) { //asm("nop; nop;"); } else { if(ptr1 >= end) break; p1 = *ptr1++; //p2 = p1; p2 = *ptr2++; bitMask = 0x80; } if (p1 & bitMask) val1=1; else val1=0; if (p2 & bitMask) val2=1; else val2=0; } // #ifdef NEO_KHZ400 // } else { // 400 KHz bitstream // for(;;) { // *set0 = pinMask[0]; //// *set1 = pinMask[1]; //// *set2 = pinMask[2]; // asm("nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop;"); // if(p & bitMask) { // asm("nop; nop; nop; nop; nop; nop; nop; nop;" // "nop; nop; nop; nop; nop; nop; nop; nop;" // "nop; nop; nop; nop; nop; nop; nop; nop;" // "nop; nop; nop;"); // *clr0 = pinMask[0]; //// *clr1 = pinMask[1]; //// *clr2 = pinMask[2]; // } else { // *clr0 = pinMask[0]; //// *clr1 = pinMask[1]; //// *clr2 = pinMask[2]; // asm("nop; nop; nop; nop; nop; nop; nop; nop;" // "nop; nop; nop; nop; nop; nop; nop; nop;" // "nop; nop; nop; nop; nop; nop; nop; nop;" // "nop; nop; nop;"); // } // asm("nop; nop; nop; nop; nop; nop; nop; nop;" // "nop; nop; nop; nop; nop; nop; nop; nop;" // "nop; nop; nop; nop; nop; nop; nop; nop;" // "nop; nop; nop; nop; nop; nop; nop; nop;"); // if(bitMask >>= 1) { // asm("nop; nop; nop; nop; nop; nop; nop;"); // } else { // if(ptr >= end) break; // p = *ptr++; // bitMask = 0x80; // } // } // } // #endif #endif // END ARM ---------------------------------------------------------------- #else #error Architecture not supported #endif // END ARCHITECTURE SELECT ------------------------------------------------ #ifndef NRF52 interrupts(); #endif endTime = micros(); // Save EOD time for latch on next call } // Set the output pin number void Adafruit_NeoPixel::setPin(uint8_t p) { if(begun && (pin >= 0)) pinMode(pin, INPUT); pin = p; if(begun) { pinMode(p, OUTPUT); digitalWrite(p, LOW); } } // Set the output pin number void Adafruit_NeoPixel::setPin(uint8_t p1, uint8_t p2) { pin_strip[0] = p1; pin_strip[1] = p2; if(begun) { pinMode(p1, OUTPUT); pinMode(p2, OUTPUT); digitalWrite(p1, LOW); digitalWrite(p2, LOW); } } // Set pixel color from separate R,G,B components: 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; } } 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; } } // Set pixel color from 'packed' 32-bit RGB color: 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; } } // Convert separate R,G,B into packed 32-bit RGB color. // Packed format is always RGB, regardless of LED strand color order. uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) { return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; } // Convert separate R,G,B,W into packed 32-bit WRGB color. // Packed format is always WRGB, regardless of LED strand color order. uint32_t Adafruit_NeoPixel::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; } // Query color from previously-set pixel (returns packed 32-bit RGB value) 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]; } } } // Returns pointer to pixels[] array. Pixel data is stored in device- // native format and is not translated here. Application will need to be // aware of specific pixel data format and handle colors appropriately. uint8_t *Adafruit_NeoPixel::getPixels(void) const { return pixels; } uint16_t Adafruit_NeoPixel::numPixels(void) const { return numLEDs; } // Adjust output brightness; 0=darkest (off), 255=brightest. This does // NOT immediately affect what's currently displayed on the LEDs. The // next call to show() will refresh the LEDs at this level. However, // 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. 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 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> 8; } brightness = newBrightness; } } //Return the brightness value uint8_t Adafruit_NeoPixel::getBrightness(void) const { return brightness - 1; } void Adafruit_NeoPixel::clear() { memset(pixels, 0, numBytes); } /* 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 _sineTable[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 _gammaTable[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}; uint8_t Adafruit_NeoPixel::sine8(uint8_t x) const { return pgm_read_byte(&_sineTable[x]); // 0-255 in, 0-255 out } uint8_t Adafruit_NeoPixel::gamma8(uint8_t x) const { return pgm_read_byte(&_gammaTable[x]); // 0-255 in, 0-255 out }