├── library.json ├── .gitattributes ├── .gitignore ├── examples ├── NeoPixelTest │ └── NeoPixelTest.pde └── NeoPixelFun │ └── NeoPixelFun.pde ├── RgbColor.cpp ├── RgbColor.h ├── NeoPixelBus.h ├── NeoPixelesp8266.c ├── ReadMe.md ├── COPYING └── NeoPixelBus.cpp /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "NeoPixelBus", 3 | "description": "Adafruit enhanced NeoPixel support library", 4 | "keywords": "WS2811,WS2812,ESP8266", 5 | "frameworks": "*", 6 | "platforms": "*", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/Makuna/NeoPixelBus.git" 10 | }, 11 | "examples": [ 12 | "examples/*/*.pde" 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # ========================= 18 | # Operating System Files 19 | # ========================= 20 | 21 | # OSX 22 | # ========================= 23 | 24 | .DS_Store 25 | .AppleDouble 26 | .LSOverride 27 | 28 | # Icon must ends with two \r. 29 | Icon 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | -------------------------------------------------------------------------------- /examples/NeoPixelTest/NeoPixelTest.pde: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pixelCount 4 4 | #define colorSaturation 128 5 | 6 | NeoPixelBus strip = NeoPixelBus(pixelCount, 8); 7 | 8 | RgbColor red = RgbColor(colorSaturation, 0, 0); 9 | RgbColor green = RgbColor(0, colorSaturation, 0); 10 | RgbColor blue = RgbColor(0, 0, colorSaturation); 11 | RgbColor white = RgbColor(colorSaturation); 12 | RgbColor black = RgbColor(0); 13 | 14 | void setup() 15 | { 16 | // this resets all the neopixels to an off state 17 | strip.Begin(); 18 | strip.Show(); 19 | } 20 | 21 | 22 | void loop() 23 | { 24 | delay(1000); 25 | 26 | // set the colors, 27 | // if they don't match in order, you may need to use NEO_GRB flag 28 | strip.SetPixelColor(0, red); 29 | strip.SetPixelColor(1, green); 30 | strip.SetPixelColor(2, blue); 31 | strip.SetPixelColor(3, white); 32 | strip.Show(); 33 | 34 | delay(3000); 35 | 36 | // turn off the pixels 37 | strip.SetPixelColor(0, black); 38 | strip.SetPixelColor(1, black); 39 | strip.SetPixelColor(2, black); 40 | strip.SetPixelColor(3, black); 41 | strip.Show(); 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /RgbColor.cpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------- 2 | NeoPixel is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU Lesser General Public License as 4 | published by the Free Software Foundation, either version 3 of 5 | the License, or (at your option) any later version. 6 | 7 | NeoPixel is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU Lesser General Public License for more details. 11 | 12 | You should have received a copy of the GNU Lesser General Public 13 | License along with NeoPixel. If not, see 14 | . 15 | --------------------------------------------------------------------*/ 16 | 17 | #include "RgbColor.h" 18 | 19 | uint8_t RgbColor::CalculateBrightness() 20 | { 21 | return (uint8_t)(((uint16_t)R + (uint16_t)G + (uint16_t)B) / 3); 22 | } 23 | 24 | void RgbColor::Darken(uint8_t delta) 25 | { 26 | if (R > delta) 27 | { 28 | R -= delta; 29 | } 30 | else 31 | { 32 | R = 0; 33 | } 34 | 35 | if (G > delta) 36 | { 37 | G -= delta; 38 | } 39 | else 40 | { 41 | G = 0; 42 | } 43 | 44 | if (B > delta) 45 | { 46 | B -= delta; 47 | } 48 | else 49 | { 50 | B = 0; 51 | } 52 | } 53 | 54 | void RgbColor::Lighten(uint8_t delta) 55 | { 56 | if (R < 255 - delta) 57 | { 58 | R += delta; 59 | } 60 | else 61 | { 62 | R = 255; 63 | } 64 | 65 | if (G < 255 - delta) 66 | { 67 | G += delta; 68 | } 69 | else 70 | { 71 | G = 255; 72 | } 73 | 74 | if (B < 255 - delta) 75 | { 76 | B += delta; 77 | } 78 | else 79 | { 80 | B = 255; 81 | } 82 | } 83 | 84 | RgbColor RgbColor::LinearBlend(RgbColor left, RgbColor right, uint8_t progress) 85 | { 86 | return RgbColor( left.R + ((right.R - left.R) * progress / 255), 87 | left.G + ((right.G - left.G) * progress / 255), 88 | left.B + ((right.B - left.B) * progress / 255)); 89 | } -------------------------------------------------------------------------------- /examples/NeoPixelFun/NeoPixelFun.pde: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pixelCount 4 4 | 5 | NeoPixelBus strip = NeoPixelBus(pixelCount, 8); 6 | uint16_t effectState = 0; 7 | 8 | 9 | void setup() 10 | { 11 | strip.Begin(); 12 | strip.Show(); 13 | SetRandomSeed(); 14 | } 15 | 16 | 17 | void loop() 18 | { 19 | // There are three fun functions that implement different effects 20 | // uncomment one at a time and upload to see the effect 21 | 22 | // LoopAround(192, 200); // very interesting on rings of NeoPixels 23 | PickRandom(128); 24 | // FadeInFadeOutRinseRepeat(192); 25 | 26 | // start animating 27 | strip.StartAnimating(); 28 | 29 | // wait until no more animations are running 30 | while (strip.IsAnimating()) 31 | { 32 | strip.UpdateAnimations(); 33 | strip.Show(); 34 | delay(31); // ~30hz change cycle 35 | } 36 | 37 | } 38 | 39 | void FadeInFadeOutRinseRepeat(uint8_t peak) 40 | { 41 | if (effectState == 0) 42 | { 43 | for (uint8_t pixel = 0; pixel < pixelCount; pixel++) 44 | { 45 | uint16_t time = random(800,1000); 46 | strip.LinearFadePixelColor(time, pixel, RgbColor(random(peak), random(peak), random(peak))); 47 | } 48 | } 49 | else if (effectState == 1) 50 | { 51 | for (uint8_t pixel = 0; pixel < pixelCount; pixel++) 52 | { 53 | uint16_t time = random(600,700); 54 | strip.LinearFadePixelColor(time, pixel, RgbColor(0, 0, 0)); 55 | } 56 | } 57 | effectState = (effectState + 1) % 2; // next effectState and keep within the number of effectStates 58 | 59 | } 60 | 61 | void PickRandom(uint8_t peak) 62 | { 63 | 64 | // pick random set of pixels to animate 65 | uint8_t count = random(pixelCount); 66 | while (count > 0) 67 | { 68 | uint8_t pixel = random(pixelCount); 69 | 70 | // configure the animations 71 | RgbColor color; // = strip.getPixelColor(pixel); 72 | 73 | color = RgbColor(random(peak), random(peak), random(peak)); 74 | 75 | 76 | uint16_t time = random(100,400); 77 | strip.LinearFadePixelColor( time, pixel, color); 78 | 79 | count--; 80 | } 81 | } 82 | 83 | void LoopAround(uint8_t peak, uint16_t speed) 84 | { 85 | // Looping around the ring sample 86 | uint16_t prevPixel; 87 | RgbColor prevColor; 88 | 89 | // fade previous one dark 90 | prevPixel = (effectState + (pixelCount - 5)) % pixelCount; 91 | strip.LinearFadePixelColor(speed, prevPixel, RgbColor(0, 0, 0)); 92 | 93 | // fade previous one dark 94 | prevPixel = (effectState + (pixelCount - 4)) % pixelCount; 95 | prevColor = strip.GetPixelColor( prevPixel ); 96 | prevColor.Darken(prevColor.CalculateBrightness() / 2); 97 | strip.LinearFadePixelColor(speed, prevPixel, prevColor); 98 | 99 | // fade previous one dark 100 | prevPixel = (effectState + (pixelCount - 3)) % pixelCount; 101 | prevColor = strip.GetPixelColor( prevPixel ); 102 | prevColor.Darken(prevColor.CalculateBrightness() / 2); 103 | strip.LinearFadePixelColor(speed, prevPixel, prevColor); 104 | 105 | // fade previous one dark 106 | prevPixel = (effectState + (pixelCount - 2)) % pixelCount; 107 | prevColor = strip.GetPixelColor( prevPixel ); 108 | prevColor.Darken(prevColor.CalculateBrightness() / 2); 109 | strip.LinearFadePixelColor(speed, prevPixel, prevColor); 110 | 111 | // fade previous one dark 112 | prevPixel = (effectState + (pixelCount - 1)) % pixelCount; 113 | prevColor = strip.GetPixelColor( prevPixel ); 114 | prevColor.Darken(prevColor.CalculateBrightness() / 2); 115 | strip.LinearFadePixelColor(speed, prevPixel, prevColor); 116 | 117 | // fade current one light 118 | strip.LinearFadePixelColor(speed, effectState, RgbColor(random(peak), random(peak), random(peak))); 119 | effectState = (effectState + 1) % pixelCount; 120 | } 121 | 122 | void SetRandomSeed() 123 | { 124 | uint32_t seed; 125 | 126 | // random works best with a seed that can use 31 bits 127 | // analogRead on a unconnected pin tends toward less than four bits 128 | seed = analogRead(0); 129 | delay(1); 130 | 131 | for (int shifts = 3; shifts < 31; shifts += 3) 132 | { 133 | seed ^= analogRead(0) << shifts; 134 | delay(1); 135 | } 136 | 137 | // Serial.println(seed); 138 | randomSeed(seed); 139 | } 140 | -------------------------------------------------------------------------------- /RgbColor.h: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------- 2 | NeoPixel is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU Lesser General Public License as 4 | published by the Free Software Foundation, either version 3 of 5 | the License, or (at your option) any later version. 6 | 7 | NeoPixel is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU Lesser General Public License for more details. 11 | 12 | You should have received a copy of the GNU Lesser General Public 13 | License along with NeoPixel. If not, see 14 | . 15 | --------------------------------------------------------------------*/ 16 | #pragma once 17 | 18 | #include 19 | 20 | // ------------------------------------------------------------------------ 21 | // RgbColor represents a color object that is represented by Red, Green, Blue 22 | // component values. It contains helpful color routines to manipulate the 23 | // color. 24 | // ------------------------------------------------------------------------ 25 | struct RgbColor 26 | { 27 | // ------------------------------------------------------------------------ 28 | // Construct a RgbColor using R, G, B values (0-255) 29 | // ------------------------------------------------------------------------ 30 | RgbColor(uint8_t r, uint8_t g, uint8_t b) : 31 | R(r), G(g), B(b) 32 | { 33 | }; 34 | 35 | // ------------------------------------------------------------------------ 36 | // Construct a RgbColor using a single brightness value (0-255) 37 | // This works well for creating gray tone colors 38 | // (0) = blakc, (255) = white, (128) = gray 39 | // ------------------------------------------------------------------------ 40 | RgbColor(uint8_t brightness) : 41 | R(brightness), G(brightness), B(brightness) 42 | { 43 | }; 44 | 45 | // ------------------------------------------------------------------------ 46 | // Construct a RgbColor that will have its values set in latter operations 47 | // CAUTION: The R,G,B members are not initialized and may not be consistent 48 | // ------------------------------------------------------------------------ 49 | RgbColor() 50 | { 51 | }; 52 | 53 | // ------------------------------------------------------------------------ 54 | // CalculateBrightness will calculate the overall brightness 55 | // NOTE: This is a simple linear brightness 56 | // ------------------------------------------------------------------------ 57 | uint8_t CalculateBrightness(); 58 | 59 | // ------------------------------------------------------------------------ 60 | // Darken will adjust the color by the given delta toward black 61 | // NOTE: This is a simple linear change 62 | // delta - (0-255) the amount to dim the color 63 | // ------------------------------------------------------------------------ 64 | void Darken(uint8_t delta); 65 | 66 | // ------------------------------------------------------------------------ 67 | // Lighten will adjust the color by the given delta toward white 68 | // NOTE: This is a simple linear change 69 | // delta - (0-255) the amount to lighten the color 70 | // ------------------------------------------------------------------------ 71 | void Lighten(uint8_t delta); 72 | 73 | // ------------------------------------------------------------------------ 74 | // LinearBlend between two colors by the amount defined by progress variable 75 | // left - the color to start the blend at 76 | // right - the color to end the blend at 77 | // progress - (0-255) value where 0 will return left and 255 will return right 78 | // and a value between will blend the color weighted linearly between them 79 | // ------------------------------------------------------------------------ 80 | static RgbColor LinearBlend(RgbColor left, RgbColor right, uint8_t progress); 81 | 82 | // ------------------------------------------------------------------------ 83 | // Red, Green, Blue color members (0-255) where 84 | // (0,0,0) is black and (255,255,255) is white 85 | // ------------------------------------------------------------------------ 86 | uint8_t R; 87 | uint8_t G; 88 | uint8_t B; 89 | }; 90 | 91 | -------------------------------------------------------------------------------- /NeoPixelBus.h: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------- 2 | This file is a modification of the Adafruit NeoPixel library. 3 | 4 | NeoPixel is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as 6 | published by the Free Software Foundation, either version 3 of 7 | the License, or (at your option) any later version. 8 | 9 | NeoPixel is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with NeoPixel. If not, see 16 | . 17 | --------------------------------------------------------------------*/ 18 | #pragma once 19 | 20 | #include 21 | #include "RgbColor.h" 22 | 23 | // '_flagsPixels' flags for LED _pixels (third parameter to constructor): 24 | #define NEO_RGB 0x00 // Wired for RGB data order 25 | #define NEO_GRB 0x01 // Wired for GRB data order 26 | #define NEO_BRG 0x04 27 | #define NEO_COLMASK 0x05 28 | 29 | #define NEO_KHZ400 0x00 // 400 KHz datastream 30 | #define NEO_KHZ800 0x02 // 800 KHz datastream 31 | #define NEO_SPDMASK 0x02 32 | #define NEO_DIRTY 0x80 // a change was made it _pixels that requires a show 33 | 34 | // v1 NeoPixels aren't handled by default, include the following define before the 35 | // NeoPixelBus library include to support the slower bus speeds 36 | //#define INCLUDE_NEO_KHZ400_SUPPORT 37 | 38 | class NeoPixelBus 39 | { 40 | public: 41 | // Constructor: number of LEDs, pin number, LED type 42 | NeoPixelBus(uint16_t n, uint8_t p, uint8_t t = NEO_GRB | NEO_KHZ800); 43 | ~NeoPixelBus(); 44 | 45 | inline uint16_t getPixelCount() 46 | { 47 | return _countPixels; 48 | } 49 | 50 | void Begin(); 51 | void Show(); 52 | inline bool CanShow(void) 53 | { 54 | return (micros() - _endTime) >= 50L; 55 | } 56 | void ClearTo(uint8_t r, uint8_t g, uint8_t b); 57 | void ClearTo(RgbColor c) 58 | { 59 | ClearTo(c.R, c.G, c.B); 60 | } 61 | 62 | bool IsDirty() 63 | { 64 | return (_flagsPixels & NEO_DIRTY); 65 | }; 66 | void Dirty() 67 | { 68 | _flagsPixels |= NEO_DIRTY; 69 | }; 70 | void ResetDirty() 71 | { 72 | _flagsPixels &= ~NEO_DIRTY; 73 | } 74 | 75 | uint8_t* Pixels() const 76 | { 77 | return _pixels; 78 | }; 79 | uint16_t PixelCount() const 80 | { 81 | return _countPixels; 82 | }; 83 | 84 | void SetPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b); 85 | void SetPixelColor(uint16_t n, RgbColor c) 86 | { 87 | SetPixelColor(n, c.R, c.G, c.B); 88 | }; 89 | 90 | RgbColor GetPixelColor(uint16_t n) const; 91 | 92 | void StartAnimating(); 93 | void UpdateAnimations(); 94 | 95 | bool IsAnimating() const 96 | { 97 | return _activeAnimations > 0; 98 | } 99 | void LinearFadePixelColor(uint16_t time, uint16_t n, RgbColor color); 100 | 101 | void FadeTo(uint16_t time, RgbColor color); 102 | 103 | private: 104 | void setPin(uint8_t p); 105 | void UpdatePixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b); 106 | void UpdatePixelColor(uint16_t n, RgbColor c) 107 | { 108 | UpdatePixelColor(n, c.R, c.G, c.B); 109 | }; 110 | 111 | const uint16_t _countPixels; // Number of RGB LEDs in strip 112 | const uint16_t _sizePixels; // Size of '_pixels' buffer below 113 | 114 | uint8_t _flagsPixels; // Pixel flags (400 vs 800 KHz, RGB vs GRB color) 115 | uint8_t _pin; // Output pin number 116 | uint8_t* _pixels; // Holds LED color values (3 bytes each) 117 | uint32_t _endTime; // Latch timing reference 118 | #ifdef __AVR__ 119 | const volatile uint8_t* _port; // Output PORT register 120 | uint8_t _pinMask; // Output PORT bitmask 121 | #endif 122 | 123 | struct FadeAnimation 124 | { 125 | uint16_t time; 126 | uint16_t remaining; 127 | 128 | RgbColor target; 129 | RgbColor origin; 130 | }; 131 | 132 | uint16_t _activeAnimations; 133 | FadeAnimation* _animations; 134 | uint32_t _animationLastTick; 135 | 136 | }; 137 | 138 | -------------------------------------------------------------------------------- /NeoPixelesp8266.c: -------------------------------------------------------------------------------- 1 | /* 2 | NeoPixelEsp8266.h - NeoPixel library helper functions for Esp8266 using cycle count 3 | Copyright (c) 2015 Michael C. Miller. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #if defined(ESP8266) 21 | 22 | #include 23 | #include 24 | 25 | inline uint32_t _getCycleCount() 26 | { 27 | uint32_t ccount; 28 | __asm__ __volatile__("rsr %0,ccount":"=a" (ccount)); 29 | return ccount; 30 | } 31 | 32 | #define CYCLES_800_T0H (F_CPU / 2500000) // 0.4us 33 | #define CYCLES_800_T1H (F_CPU / 1250000) // 0.8us 34 | #define CYCLES_800 (F_CPU / 800000) // 1.25us per bit 35 | #define CYCLES_400_T0H (F_CPU / 2000000) 36 | #define CYCLES_400_T1H (F_CPU / 833333) 37 | #define CYCLES_400 (F_CPU / 400000) 38 | 39 | void ICACHE_RAM_ATTR send_pixels_800(uint8_t* pixels, uint8_t* end, uint8_t pin) 40 | { 41 | const uint32_t pinRegister = _BV(pin); 42 | uint8_t mask; 43 | uint8_t subpix; 44 | uint32_t cyclesStart; 45 | 46 | // trigger emediately 47 | cyclesStart = _getCycleCount() - CYCLES_800; 48 | do 49 | { 50 | subpix = *pixels++; 51 | for (mask = 0x80; mask != 0; mask >>= 1) 52 | { 53 | // do the checks here while we are waiting on time to pass 54 | uint32_t cyclesBit = ((subpix & mask)) ? CYCLES_800_T1H : CYCLES_800_T0H; 55 | uint32_t cyclesNext = cyclesStart; 56 | uint32_t delta; 57 | 58 | // after we have done as much work as needed for this next bit 59 | // now wait for the HIGH 60 | do 61 | { 62 | // cache and use this count so we don't incur another 63 | // instruction before we turn the bit high 64 | cyclesStart = _getCycleCount(); 65 | } while ((cyclesStart - cyclesNext) < CYCLES_800); 66 | 67 | // set high 68 | GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); 69 | 70 | // wait for the LOW 71 | do 72 | { 73 | cyclesNext = _getCycleCount(); 74 | } while ((cyclesNext - cyclesStart) < cyclesBit); 75 | 76 | // set low 77 | GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); 78 | } 79 | } while (pixels < end); 80 | 81 | // while accurate, this isn't needed due to the delays at the 82 | // top of Show() to enforce between update timing 83 | // while ((_getCycleCount() - cyclesStart) < CYCLES_800); 84 | } 85 | 86 | void ICACHE_RAM_ATTR send_pixels_400(uint8_t* pixels, uint8_t* end, uint8_t pin) 87 | { 88 | const uint32_t pinRegister = _BV(pin); 89 | uint8_t mask; 90 | uint8_t subpix; 91 | uint32_t cyclesStart; 92 | 93 | // trigger emediately 94 | cyclesStart = _getCycleCount() - CYCLES_400; 95 | while (pixels < end) 96 | { 97 | subpix = *pixels++; 98 | for (mask = 0x80; mask; mask >>= 1) 99 | { 100 | uint32_t cyclesBit = ((subpix & mask)) ? CYCLES_400_T1H : CYCLES_400_T0H; 101 | uint32_t cyclesNext = cyclesStart; 102 | 103 | // after we have done as much work as needed for this next bit 104 | // now wait for the HIGH 105 | do 106 | { 107 | // cache and use this count so we don't incur another 108 | // instruction before we turn the bit high 109 | cyclesStart = _getCycleCount(); 110 | } while ((cyclesStart - cyclesNext) < CYCLES_400); 111 | 112 | // set high 113 | GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); 114 | 115 | // wait for the LOW 116 | do 117 | { 118 | cyclesNext = _getCycleCount(); 119 | } while ((cyclesNext - cyclesStart) < cyclesBit); 120 | 121 | // set low 122 | GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); 123 | } 124 | } 125 | 126 | // while accurate, this isn't needed due to the delays at the 127 | // top of Show() to enforce between update timing 128 | // while ((_getCycleCount() - cyclesStart) < CYCLES_400); 129 | } 130 | 131 | #endif -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | # NeoPixelBus 2 | 3 | [![Donate](http://img.shields.io/paypal/donate.png?color=yellow)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6AA97KE54UJR4) 4 | 5 | Arduino NeoPixel library 6 | 7 | ESP8266 CUSTOMERS PLEASE READ: While this branch does work with the esp8266, due to the latest SDK releases it will not function reliably when WiFi is being used. Therefore I suggest you use the DmaDriven or UartDriven branches, which both include solutions that will work with WiFi on. Further they contains enhancements that just can't be supported on AVR platform. Including HslColor object and an enhanced animator manager. 8 | 9 | [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Makuna/NeoPixelBus?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) 10 | 11 | Clone this into your Arduino\Library folder 12 | 13 | This library is a modification of the Adafruit NeoPixel library. 14 | The Api is similiar, but it removes the overal brightness feature and adds animation support. 15 | 16 | ## Installing This Library 17 | Create a directory in your Arduino\Library folder named "NeoPixelBus" 18 | Clone (Git) this project into that folder. 19 | It should now show up in the import list. 20 | 21 | ## Samples 22 | ### NeoPixelTest 23 | this is simple example that sets four neopixels to red, green, blue, and then white in order; and then flashes them. If the first pixel is green and the second is red, you need to pass the NEO_RGB flag into the NeoPixelBus constructor. 24 | ### NeoPixelFun 25 | this is a more complex example, that includes code for three effects, and demonstrates animations. 26 | 27 | ## API Documentation 28 | 29 | ### RgbColor object 30 | This represents a color and exposes useful methods to manipulate colors. 31 | 32 | #### RgbColor(uint8_t r, uint8_t g, uint8_t b) 33 | instantiates a RgbColor object with the given r, g, b values. 34 | 35 | #### RgbColor(uint8_t brightness) 36 | instantiates a RgbColor object with the given brightness. 0 is black, 128 is grey, 255 is white. 37 | 38 | #### uint8_t CalculateBrightness() 39 | returns the general brightness of the pixe, averaging color. 40 | 41 | #### void Darken(uint8_t delta) 42 | this will darken the color by the given amount, blending toward black. This method is destructive in that you can't expect to then call lighten and return to the original color. 43 | 44 | #### void Lighten(uint8_t delta) 45 | this will lighten the color by the given amount, blending toward white. This method is destructive in that you can't expect to then call darken and return to the original color. 46 | 47 | #### static RgbColor LinearBlend(RgbColor left, RgbColor right, uint8_t progress) 48 | this will return a color that is a blend between the given colors. The amount to blend is given by the value of progress, 0 will return the left value, 255 will return the right value, 128 will return the value between them. 49 | 50 | NOTE: This is not an accurate "visible light" color blend but is fast and in most cases good enough. 51 | 52 | ### NeoPixelBus object 53 | This represents a single NeoPixel Bus that is connected by a single pin. Please see Adafruit's documentation for details, but the differences are documented below. 54 | 55 | #### NeoPixelBus(uint16_t n, uint8_t p = 6, uint8_t t = NEO_GRB | NEO_KHZ800); 56 | instantiates a NewoPixelBus object, with n number of pixels on the bus, over the p pin, using the defined NeoPixel type. 57 | There are some NeoPixels that address the color values differently, so if you set the green color but it displays as red, use the NEO_RGB type flag. 58 | 59 | ``` 60 | NeoPixelBus strip = NeoPixelBus(4, 8, NEO_RGB | NEO_KHZ800); 61 | ``` 62 | It is rare, but some older NeoPixels require a slower communications speed, to include this support you must include the following define before the NeoPixelBus library include and then include the NEO_KHZ400 type flag to enable this slower speed. 63 | 64 | ``` 65 | #define INCLUDE_NEO_KHZ400_SUPPORT 66 | #include 67 | 68 | NeoPixelBus strip = NeoPixelBus(4, 8, NEO_RGB | NEO_KHZ400); 69 | ``` 70 | 71 | #### void SetPixelColor(uint16_t n, RgbColor c) 72 | This allows setting a pixel on the bus to a color as defined by a color object. If an animation is actively running on a pixel, it will be stopped. 73 | 74 | #### RgbColor GetPixelColor(uint16_t n) const 75 | this allows retrieving the current pixel color 76 | 77 | #### void LinearFadePixelColor(uint16_t time, uint16_t n, RgbColor color) 78 | this will setup an animation for a pixel to linear fade between the current color and the given color over the time given. The time is in milliseconds. 79 | 80 | #### void StartAnimating() 81 | this method will initialize the animation state. This should be called only if there are no active animations and new animations are started. 82 | 83 | #### void UpdateAnimations() 84 | this method will allow the animations to be processed and update the pixel color state. 85 | 86 | NOTE: Show must still be called to push the color state to the physical NeoPixels. 87 | 88 | #### bool IsAnimating() const 89 | this method will return the current animation state. It will return false if there are no active animations. 90 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | 2 | GNU GENERAL PUBLIC LICENSE 3 | Version 3, 29 June 2007 4 | 5 | Copyright (C) 2007 Free Software Foundation, Inc. 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The GNU General Public License is a free, copyleft license for 12 | software and other kinds of works. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | the GNU General Public License is intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. We, the Free Software Foundation, use the 19 | GNU General Public License for most of our software; it applies also to 20 | any other work released this way by its authors. You can apply it to 21 | your programs, too. 22 | 23 | When we speak of free software, we are referring to freedom, not 24 | price. Our General Public Licenses are designed to make sure that you 25 | have the freedom to distribute copies of free software (and charge for 26 | them if you wish), that you receive source code or can get it if you 27 | want it, that you can change the software or use pieces of it in new 28 | free programs, and that you know you can do these things. 29 | 30 | To protect your rights, we need to prevent others from denying you 31 | these rights or asking you to surrender the rights. Therefore, you have 32 | certain responsibilities if you distribute copies of the software, or if 33 | you modify it: responsibilities to respect the freedom of others. 34 | 35 | For example, if you distribute copies of such a program, whether 36 | gratis or for a fee, you must pass on to the recipients the same 37 | freedoms that you received. You must make sure that they, too, receive 38 | or can get the source code. And you must show them these terms so they 39 | know their rights. 40 | 41 | Developers that use the GNU GPL protect your rights with two steps: 42 | (1) assert copyright on the software, and (2) offer you this License 43 | giving you legal permission to copy, distribute and/or modify it. 44 | 45 | For the developers' and authors' protection, the GPL clearly explains 46 | that there is no warranty for this free software. For both users' and 47 | authors' sake, the GPL requires that modified versions be marked as 48 | changed, so that their problems will not be attributed erroneously to 49 | authors of previous versions. 50 | 51 | Some devices are designed to deny users access to install or run 52 | modified versions of the software inside them, although the manufacturer 53 | can do so. This is fundamentally incompatible with the aim of 54 | protecting users' freedom to change the software. The systematic 55 | pattern of such abuse occurs in the area of products for individuals to 56 | use, which is precisely where it is most unacceptable. Therefore, we 57 | have designed this version of the GPL to prohibit the practice for those 58 | products. If such problems arise substantially in other domains, we 59 | stand ready to extend this provision to those domains in future versions 60 | of the GPL, as needed to protect the freedom of users. 61 | 62 | Finally, every program is threatened constantly by software patents. 63 | States should not allow patents to restrict development and use of 64 | software on general-purpose computers, but in those that do, we wish to 65 | avoid the special danger that patents applied to a free program could 66 | make it effectively proprietary. To prevent this, the GPL assures that 67 | patents cannot be used to render the program non-free. 68 | 69 | The precise terms and conditions for copying, distribution and 70 | modification follow. 71 | 72 | TERMS AND CONDITIONS 73 | 74 | 0. Definitions. 75 | 76 | "This License" refers to version 3 of the GNU General Public License. 77 | 78 | "Copyright" also means copyright-like laws that apply to other kinds of 79 | works, such as semiconductor masks. 80 | 81 | "The Program" refers to any copyrightable work licensed under this 82 | License. Each licensee is addressed as "you". "Licensees" and 83 | "recipients" may be individuals or organizations. 84 | 85 | To "modify" a work means to copy from or adapt all or part of the work 86 | in a fashion requiring copyright permission, other than the making of an 87 | exact copy. The resulting work is called a "modified version" of the 88 | earlier work or a work "based on" the earlier work. 89 | 90 | A "covered work" means either the unmodified Program or a work based 91 | on the Program. 92 | 93 | To "propagate" a work means to do anything with it that, without 94 | permission, would make you directly or secondarily liable for 95 | infringement under applicable copyright law, except executing it on a 96 | computer or modifying a private copy. Propagation includes copying, 97 | distribution (with or without modification), making available to the 98 | public, and in some countries other activities as well. 99 | 100 | To "convey" a work means any kind of propagation that enables other 101 | parties to make or receive copies. Mere interaction with a user through 102 | a computer network, with no transfer of a copy, is not conveying. 103 | 104 | An interactive user interface displays "Appropriate Legal Notices" 105 | to the extent that it includes a convenient and prominently visible 106 | feature that (1) displays an appropriate copyright notice, and (2) 107 | tells the user that there is no warranty for the work (except to the 108 | extent that warranties are provided), that licensees may convey the 109 | work under this License, and how to view a copy of this License. If 110 | the interface presents a list of user commands or options, such as a 111 | menu, a prominent item in the list meets this criterion. 112 | 113 | 1. Source Code. 114 | 115 | The "source code" for a work means the preferred form of the work 116 | for making modifications to it. "Object code" means any non-source 117 | form of a work. 118 | 119 | A "Standard Interface" means an interface that either is an official 120 | standard defined by a recognized standards body, or, in the case of 121 | interfaces specified for a particular programming language, one that 122 | is widely used among developers working in that language. 123 | 124 | The "System Libraries" of an executable work include anything, other 125 | than the work as a whole, that (a) is included in the normal form of 126 | packaging a Major Component, but which is not part of that Major 127 | Component, and (b) serves only to enable use of the work with that 128 | Major Component, or to implement a Standard Interface for which an 129 | implementation is available to the public in source code form. A 130 | "Major Component", in this context, means a major essential component 131 | (kernel, window system, and so on) of the specific operating system 132 | (if any) on which the executable work runs, or a compiler used to 133 | produce the work, or an object code interpreter used to run it. 134 | 135 | The "Corresponding Source" for a work in object code form means all 136 | the source code needed to generate, install, and (for an executable 137 | work) run the object code and to modify the work, including scripts to 138 | control those activities. However, it does not include the work's 139 | System Libraries, or general-purpose tools or generally available free 140 | programs which are used unmodified in performing those activities but 141 | which are not part of the work. For example, Corresponding Source 142 | includes interface definition files associated with source files for 143 | the work, and the source code for shared libraries and dynamically 144 | linked subprograms that the work is specifically designed to require, 145 | such as by intimate data communication or control flow between those 146 | subprograms and other parts of the work. 147 | 148 | The Corresponding Source need not include anything that users 149 | can regenerate automatically from other parts of the Corresponding 150 | Source. 151 | 152 | The Corresponding Source for a work in source code form is that 153 | same work. 154 | 155 | 2. Basic Permissions. 156 | 157 | All rights granted under this License are granted for the term of 158 | copyright on the Program, and are irrevocable provided the stated 159 | conditions are met. This License explicitly affirms your unlimited 160 | permission to run the unmodified Program. The output from running a 161 | covered work is covered by this License only if the output, given its 162 | content, constitutes a covered work. This License acknowledges your 163 | rights of fair use or other equivalent, as provided by copyright law. 164 | 165 | You may make, run and propagate covered works that you do not 166 | convey, without conditions so long as your license otherwise remains 167 | in force. You may convey covered works to others for the sole purpose 168 | of having them make modifications exclusively for you, or provide you 169 | with facilities for running those works, provided that you comply with 170 | the terms of this License in conveying all material for which you do 171 | not control copyright. Those thus making or running the covered works 172 | for you must do so exclusively on your behalf, under your direction 173 | and control, on terms that prohibit them from making any copies of 174 | your copyrighted material outside their relationship with you. 175 | 176 | Conveying under any other circumstances is permitted solely under 177 | the conditions stated below. Sublicensing is not allowed; section 10 178 | makes it unnecessary. 179 | 180 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 181 | 182 | No covered work shall be deemed part of an effective technological 183 | measure under any applicable law fulfilling obligations under article 184 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 185 | similar laws prohibiting or restricting circumvention of such 186 | measures. 187 | 188 | When you convey a covered work, you waive any legal power to forbid 189 | circumvention of technological measures to the extent such circumvention 190 | is effected by exercising rights under this License with respect to 191 | the covered work, and you disclaim any intention to limit operation or 192 | modification of the work as a means of enforcing, against the work's 193 | users, your or third parties' legal rights to forbid circumvention of 194 | technological measures. 195 | 196 | 4. Conveying Verbatim Copies. 197 | 198 | You may convey verbatim copies of the Program's source code as you 199 | receive it, in any medium, provided that you conspicuously and 200 | appropriately publish on each copy an appropriate copyright notice; 201 | keep intact all notices stating that this License and any 202 | non-permissive terms added in accord with section 7 apply to the code; 203 | keep intact all notices of the absence of any warranty; and give all 204 | recipients a copy of this License along with the Program. 205 | 206 | You may charge any price or no price for each copy that you convey, 207 | and you may offer support or warranty protection for a fee. 208 | 209 | 5. Conveying Modified Source Versions. 210 | 211 | You may convey a work based on the Program, or the modifications to 212 | produce it from the Program, in the form of source code under the 213 | terms of section 4, provided that you also meet all of these conditions: 214 | 215 | a) The work must carry prominent notices stating that you modified 216 | it, and giving a relevant date. 217 | 218 | b) The work must carry prominent notices stating that it is 219 | released under this License and any conditions added under section 220 | 7. This requirement modifies the requirement in section 4 to 221 | "keep intact all notices". 222 | 223 | c) You must license the entire work, as a whole, under this 224 | License to anyone who comes into possession of a copy. This 225 | License will therefore apply, along with any applicable section 7 226 | additional terms, to the whole of the work, and all its parts, 227 | regardless of how they are packaged. This License gives no 228 | permission to license the work in any other way, but it does not 229 | invalidate such permission if you have separately received it. 230 | 231 | d) If the work has interactive user interfaces, each must display 232 | Appropriate Legal Notices; however, if the Program has interactive 233 | interfaces that do not display Appropriate Legal Notices, your 234 | work need not make them do so. 235 | 236 | A compilation of a covered work with other separate and independent 237 | works, which are not by their nature extensions of the covered work, 238 | and which are not combined with it such as to form a larger program, 239 | in or on a volume of a storage or distribution medium, is called an 240 | "aggregate" if the compilation and its resulting copyright are not 241 | used to limit the access or legal rights of the compilation's users 242 | beyond what the individual works permit. Inclusion of a covered work 243 | in an aggregate does not cause this License to apply to the other 244 | parts of the aggregate. 245 | 246 | 6. Conveying Non-Source Forms. 247 | 248 | You may convey a covered work in object code form under the terms 249 | of sections 4 and 5, provided that you also convey the 250 | machine-readable Corresponding Source under the terms of this License, 251 | in one of these ways: 252 | 253 | a) Convey the object code in, or embodied in, a physical product 254 | (including a physical distribution medium), accompanied by the 255 | Corresponding Source fixed on a durable physical medium 256 | customarily used for software interchange. 257 | 258 | b) Convey the object code in, or embodied in, a physical product 259 | (including a physical distribution medium), accompanied by a 260 | written offer, valid for at least three years and valid for as 261 | long as you offer spare parts or customer support for that product 262 | model, to give anyone who possesses the object code either (1) a 263 | copy of the Corresponding Source for all the software in the 264 | product that is covered by this License, on a durable physical 265 | medium customarily used for software interchange, for a price no 266 | more than your reasonable cost of physically performing this 267 | conveying of source, or (2) access to copy the 268 | Corresponding Source from a network server at no charge. 269 | 270 | c) Convey individual copies of the object code with a copy of the 271 | written offer to provide the Corresponding Source. This 272 | alternative is allowed only occasionally and noncommercially, and 273 | only if you received the object code with such an offer, in accord 274 | with subsection 6b. 275 | 276 | d) Convey the object code by offering access from a designated 277 | place (gratis or for a charge), and offer equivalent access to the 278 | Corresponding Source in the same way through the same place at no 279 | further charge. You need not require recipients to copy the 280 | Corresponding Source along with the object code. If the place to 281 | copy the object code is a network server, the Corresponding Source 282 | may be on a different server (operated by you or a third party) 283 | that supports equivalent copying facilities, provided you maintain 284 | clear directions next to the object code saying where to find the 285 | Corresponding Source. Regardless of what server hosts the 286 | Corresponding Source, you remain obligated to ensure that it is 287 | available for as long as needed to satisfy these requirements. 288 | 289 | e) Convey the object code using peer-to-peer transmission, provided 290 | you inform other peers where the object code and Corresponding 291 | Source of the work are being offered to the general public at no 292 | charge under subsection 6d. 293 | 294 | A separable portion of the object code, whose source code is excluded 295 | from the Corresponding Source as a System Library, need not be 296 | included in conveying the object code work. 297 | 298 | A "User Product" is either (1) a "consumer product", which means any 299 | tangible personal property which is normally used for personal, family, 300 | or household purposes, or (2) anything designed or sold for incorporation 301 | into a dwelling. In determining whether a product is a consumer product, 302 | doubtful cases shall be resolved in favor of coverage. For a particular 303 | product received by a particular user, "normally used" refers to a 304 | typical or common use of that class of product, regardless of the status 305 | of the particular user or of the way in which the particular user 306 | actually uses, or expects or is expected to use, the product. A product 307 | is a consumer product regardless of whether the product has substantial 308 | commercial, industrial or non-consumer uses, unless such uses represent 309 | the only significant mode of use of the product. 310 | 311 | "Installation Information" for a User Product means any methods, 312 | procedures, authorization keys, or other information required to install 313 | and execute modified versions of a covered work in that User Product from 314 | a modified version of its Corresponding Source. The information must 315 | suffice to ensure that the continued functioning of the modified object 316 | code is in no case prevented or interfered with solely because 317 | modification has been made. 318 | 319 | If you convey an object code work under this section in, or with, or 320 | specifically for use in, a User Product, and the conveying occurs as 321 | part of a transaction in which the right of possession and use of the 322 | User Product is transferred to the recipient in perpetuity or for a 323 | fixed term (regardless of how the transaction is characterized), the 324 | Corresponding Source conveyed under this section must be accompanied 325 | by the Installation Information. But this requirement does not apply 326 | if neither you nor any third party retains the ability to install 327 | modified object code on the User Product (for example, the work has 328 | been installed in ROM). 329 | 330 | The requirement to provide Installation Information does not include a 331 | requirement to continue to provide support service, warranty, or updates 332 | for a work that has been modified or installed by the recipient, or for 333 | the User Product in which it has been modified or installed. Access to a 334 | network may be denied when the modification itself materially and 335 | adversely affects the operation of the network or violates the rules and 336 | protocols for communication across the network. 337 | 338 | Corresponding Source conveyed, and Installation Information provided, 339 | in accord with this section must be in a format that is publicly 340 | documented (and with an implementation available to the public in 341 | source code form), and must require no special password or key for 342 | unpacking, reading or copying. 343 | 344 | 7. Additional Terms. 345 | 346 | "Additional permissions" are terms that supplement the terms of this 347 | License by making exceptions from one or more of its conditions. 348 | Additional permissions that are applicable to the entire Program shall 349 | be treated as though they were included in this License, to the extent 350 | that they are valid under applicable law. If additional permissions 351 | apply only to part of the Program, that part may be used separately 352 | under those permissions, but the entire Program remains governed by 353 | this License without regard to the additional permissions. 354 | 355 | When you convey a copy of a covered work, you may at your option 356 | remove any additional permissions from that copy, or from any part of 357 | it. (Additional permissions may be written to require their own 358 | removal in certain cases when you modify the work.) You may place 359 | additional permissions on material, added by you to a covered work, 360 | for which you have or can give appropriate copyright permission. 361 | 362 | Notwithstanding any other provision of this License, for material you 363 | add to a covered work, you may (if authorized by the copyright holders of 364 | that material) supplement the terms of this License with terms: 365 | 366 | a) Disclaiming warranty or limiting liability differently from the 367 | terms of sections 15 and 16 of this License; or 368 | 369 | b) Requiring preservation of specified reasonable legal notices or 370 | author attributions in that material or in the Appropriate Legal 371 | Notices displayed by works containing it; or 372 | 373 | c) Prohibiting misrepresentation of the origin of that material, or 374 | requiring that modified versions of such material be marked in 375 | reasonable ways as different from the original version; or 376 | 377 | d) Limiting the use for publicity purposes of names of licensors or 378 | authors of the material; or 379 | 380 | e) Declining to grant rights under trademark law for use of some 381 | trade names, trademarks, or service marks; or 382 | 383 | f) Requiring indemnification of licensors and authors of that 384 | material by anyone who conveys the material (or modified versions of 385 | it) with contractual assumptions of liability to the recipient, for 386 | any liability that these contractual assumptions directly impose on 387 | those licensors and authors. 388 | 389 | All other non-permissive additional terms are considered "further 390 | restrictions" within the meaning of section 10. If the Program as you 391 | received it, or any part of it, contains a notice stating that it is 392 | governed by this License along with a term that is a further 393 | restriction, you may remove that term. If a license document contains 394 | a further restriction but permits relicensing or conveying under this 395 | License, you may add to a covered work material governed by the terms 396 | of that license document, provided that the further restriction does 397 | not survive such relicensing or conveying. 398 | 399 | If you add terms to a covered work in accord with this section, you 400 | must place, in the relevant source files, a statement of the 401 | additional terms that apply to those files, or a notice indicating 402 | where to find the applicable terms. 403 | 404 | Additional terms, permissive or non-permissive, may be stated in the 405 | form of a separately written license, or stated as exceptions; 406 | the above requirements apply either way. 407 | 408 | 8. Termination. 409 | 410 | You may not propagate or modify a covered work except as expressly 411 | provided under this License. Any attempt otherwise to propagate or 412 | modify it is void, and will automatically terminate your rights under 413 | this License (including any patent licenses granted under the third 414 | paragraph of section 11). 415 | 416 | However, if you cease all violation of this License, then your 417 | license from a particular copyright holder is reinstated (a) 418 | provisionally, unless and until the copyright holder explicitly and 419 | finally terminates your license, and (b) permanently, if the copyright 420 | holder fails to notify you of the violation by some reasonable means 421 | prior to 60 days after the cessation. 422 | 423 | Moreover, your license from a particular copyright holder is 424 | reinstated permanently if the copyright holder notifies you of the 425 | violation by some reasonable means, this is the first time you have 426 | received notice of violation of this License (for any work) from that 427 | copyright holder, and you cure the violation prior to 30 days after 428 | your receipt of the notice. 429 | 430 | Termination of your rights under this section does not terminate the 431 | licenses of parties who have received copies or rights from you under 432 | this License. If your rights have been terminated and not permanently 433 | reinstated, you do not qualify to receive new licenses for the same 434 | material under section 10. 435 | 436 | 9. Acceptance Not Required for Having Copies. 437 | 438 | You are not required to accept this License in order to receive or 439 | run a copy of the Program. Ancillary propagation of a covered work 440 | occurring solely as a consequence of using peer-to-peer transmission 441 | to receive a copy likewise does not require acceptance. However, 442 | nothing other than this License grants you permission to propagate or 443 | modify any covered work. These actions infringe copyright if you do 444 | not accept this License. Therefore, by modifying or propagating a 445 | covered work, you indicate your acceptance of this License to do so. 446 | 447 | 10. Automatic Licensing of Downstream Recipients. 448 | 449 | Each time you convey a covered work, the recipient automatically 450 | receives a license from the original licensors, to run, modify and 451 | propagate that work, subject to this License. You are not responsible 452 | for enforcing compliance by third parties with this License. 453 | 454 | An "entity transaction" is a transaction transferring control of an 455 | organization, or substantially all assets of one, or subdividing an 456 | organization, or merging organizations. If propagation of a covered 457 | work results from an entity transaction, each party to that 458 | transaction who receives a copy of the work also receives whatever 459 | licenses to the work the party's predecessor in interest had or could 460 | give under the previous paragraph, plus a right to possession of the 461 | Corresponding Source of the work from the predecessor in interest, if 462 | the predecessor has it or can get it with reasonable efforts. 463 | 464 | You may not impose any further restrictions on the exercise of the 465 | rights granted or affirmed under this License. For example, you may 466 | not impose a license fee, royalty, or other charge for exercise of 467 | rights granted under this License, and you may not initiate litigation 468 | (including a cross-claim or counterclaim in a lawsuit) alleging that 469 | any patent claim is infringed by making, using, selling, offering for 470 | sale, or importing the Program or any portion of it. 471 | 472 | 11. Patents. 473 | 474 | A "contributor" is a copyright holder who authorizes use under this 475 | License of the Program or a work on which the Program is based. The 476 | work thus licensed is called the contributor's "contributor version". 477 | 478 | A contributor's "essential patent claims" are all patent claims 479 | owned or controlled by the contributor, whether already acquired or 480 | hereafter acquired, that would be infringed by some manner, permitted 481 | by this License, of making, using, or selling its contributor version, 482 | but do not include claims that would be infringed only as a 483 | consequence of further modification of the contributor version. For 484 | purposes of this definition, "control" includes the right to grant 485 | patent sublicenses in a manner consistent with the requirements of 486 | this License. 487 | 488 | Each contributor grants you a non-exclusive, worldwide, royalty-free 489 | patent license under the contributor's essential patent claims, to 490 | make, use, sell, offer for sale, import and otherwise run, modify and 491 | propagate the contents of its contributor version. 492 | 493 | In the following three paragraphs, a "patent license" is any express 494 | agreement or commitment, however denominated, not to enforce a patent 495 | (such as an express permission to practice a patent or covenant not to 496 | sue for patent infringement). To "grant" such a patent license to a 497 | party means to make such an agreement or commitment not to enforce a 498 | patent against the party. 499 | 500 | If you convey a covered work, knowingly relying on a patent license, 501 | and the Corresponding Source of the work is not available for anyone 502 | to copy, free of charge and under the terms of this License, through a 503 | publicly available network server or other readily accessible means, 504 | then you must either (1) cause the Corresponding Source to be so 505 | available, or (2) arrange to deprive yourself of the benefit of the 506 | patent license for this particular work, or (3) arrange, in a manner 507 | consistent with the requirements of this License, to extend the patent 508 | license to downstream recipients. "Knowingly relying" means you have 509 | actual knowledge that, but for the patent license, your conveying the 510 | covered work in a country, or your recipient's use of the covered work 511 | in a country, would infringe one or more identifiable patents in that 512 | country that you have reason to believe are valid. 513 | 514 | If, pursuant to or in connection with a single transaction or 515 | arrangement, you convey, or propagate by procuring conveyance of, a 516 | covered work, and grant a patent license to some of the parties 517 | receiving the covered work authorizing them to use, propagate, modify 518 | or convey a specific copy of the covered work, then the patent license 519 | you grant is automatically extended to all recipients of the covered 520 | work and works based on it. 521 | 522 | A patent license is "discriminatory" if it does not include within 523 | the scope of its coverage, prohibits the exercise of, or is 524 | conditioned on the non-exercise of one or more of the rights that are 525 | specifically granted under this License. You may not convey a covered 526 | work if you are a party to an arrangement with a third party that is 527 | in the business of distributing software, under which you make payment 528 | to the third party based on the extent of your activity of conveying 529 | the work, and under which the third party grants, to any of the 530 | parties who would receive the covered work from you, a discriminatory 531 | patent license (a) in connection with copies of the covered work 532 | conveyed by you (or copies made from those copies), or (b) primarily 533 | for and in connection with specific products or compilations that 534 | contain the covered work, unless you entered into that arrangement, 535 | or that patent license was granted, prior to 28 March 2007. 536 | 537 | Nothing in this License shall be construed as excluding or limiting 538 | any implied license or other defenses to infringement that may 539 | otherwise be available to you under applicable patent law. 540 | 541 | 12. No Surrender of Others' Freedom. 542 | 543 | If conditions are imposed on you (whether by court order, agreement or 544 | otherwise) that contradict the conditions of this License, they do not 545 | excuse you from the conditions of this License. If you cannot convey a 546 | covered work so as to satisfy simultaneously your obligations under this 547 | License and any other pertinent obligations, then as a consequence you may 548 | not convey it at all. For example, if you agree to terms that obligate you 549 | to collect a royalty for further conveying from those to whom you convey 550 | the Program, the only way you could satisfy both those terms and this 551 | License would be to refrain entirely from conveying the Program. 552 | 553 | 13. Use with the GNU Affero General Public License. 554 | 555 | Notwithstanding any other provision of this License, you have 556 | permission to link or combine any covered work with a work licensed 557 | under version 3 of the GNU Affero General Public License into a single 558 | combined work, and to convey the resulting work. The terms of this 559 | License will continue to apply to the part which is the covered work, 560 | but the special requirements of the GNU Affero General Public License, 561 | section 13, concerning interaction through a network will apply to the 562 | combination as such. 563 | 564 | 14. Revised Versions of this License. 565 | 566 | The Free Software Foundation may publish revised and/or new versions of 567 | the GNU General Public License from time to time. Such new versions will 568 | be similar in spirit to the present version, but may differ in detail to 569 | address new problems or concerns. 570 | 571 | Each version is given a distinguishing version number. If the 572 | Program specifies that a certain numbered version of the GNU General 573 | Public License "or any later version" applies to it, you have the 574 | option of following the terms and conditions either of that numbered 575 | version or of any later version published by the Free Software 576 | Foundation. If the Program does not specify a version number of the 577 | GNU General Public License, you may choose any version ever published 578 | by the Free Software Foundation. 579 | 580 | If the Program specifies that a proxy can decide which future 581 | versions of the GNU General Public License can be used, that proxy's 582 | public statement of acceptance of a version permanently authorizes you 583 | to choose that version for the Program. 584 | 585 | Later license versions may give you additional or different 586 | permissions. However, no additional obligations are imposed on any 587 | author or copyright holder as a result of your choosing to follow a 588 | later version. 589 | 590 | 15. Disclaimer of Warranty. 591 | 592 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 593 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 594 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 595 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 596 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 597 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 598 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 599 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 600 | 601 | 16. Limitation of Liability. 602 | 603 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 604 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 605 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 606 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 607 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 608 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 609 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 610 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 611 | SUCH DAMAGES. 612 | 613 | 17. Interpretation of Sections 15 and 16. 614 | 615 | If the disclaimer of warranty and limitation of liability provided 616 | above cannot be given local legal effect according to their terms, 617 | reviewing courts shall apply local law that most closely approximates 618 | an absolute waiver of all civil liability in connection with the 619 | Program, unless a warranty or assumption of liability accompanies a 620 | copy of the Program in return for a fee. 621 | 622 | END OF TERMS AND CONDITIONS 623 | 624 | 625 | 626 | LGPL ADDENDUM: 627 | 628 | 629 | 630 | GNU LESSER GENERAL PUBLIC LICENSE 631 | Version 3, 29 June 2007 632 | 633 | Copyright (C) 2007 Free Software Foundation, Inc. 634 | Everyone is permitted to copy and distribute verbatim copies 635 | of this license document, but changing it is not allowed. 636 | 637 | 638 | This version of the GNU Lesser General Public License incorporates 639 | the terms and conditions of version 3 of the GNU General Public 640 | License, supplemented by the additional permissions listed below. 641 | 642 | 0. Additional Definitions. 643 | 644 | As used herein, "this License" refers to version 3 of the GNU Lesser 645 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 646 | General Public License. 647 | 648 | "The Library" refers to a covered work governed by this License, 649 | other than an Application or a Combined Work as defined below. 650 | 651 | An "Application" is any work that makes use of an interface provided 652 | by the Library, but which is not otherwise based on the Library. 653 | Defining a subclass of a class defined by the Library is deemed a mode 654 | of using an interface provided by the Library. 655 | 656 | A "Combined Work" is a work produced by combining or linking an 657 | Application with the Library. The particular version of the Library 658 | with which the Combined Work was made is also called the "Linked 659 | Version". 660 | 661 | The "Minimal Corresponding Source" for a Combined Work means the 662 | Corresponding Source for the Combined Work, excluding any source code 663 | for portions of the Combined Work that, considered in isolation, are 664 | based on the Application, and not on the Linked Version. 665 | 666 | The "Corresponding Application Code" for a Combined Work means the 667 | object code and/or source code for the Application, including any data 668 | and utility programs needed for reproducing the Combined Work from the 669 | Application, but excluding the System Libraries of the Combined Work. 670 | 671 | 1. Exception to Section 3 of the GNU GPL. 672 | 673 | You may convey a covered work under sections 3 and 4 of this License 674 | without being bound by section 3 of the GNU GPL. 675 | 676 | 2. Conveying Modified Versions. 677 | 678 | If you modify a copy of the Library, and, in your modifications, a 679 | facility refers to a function or data to be supplied by an Application 680 | that uses the facility (other than as an argument passed when the 681 | facility is invoked), then you may convey a copy of the modified 682 | version: 683 | 684 | a) under this License, provided that you make a good faith effort to 685 | ensure that, in the event an Application does not supply the 686 | function or data, the facility still operates, and performs 687 | whatever part of its purpose remains meaningful, or 688 | 689 | b) under the GNU GPL, with none of the additional permissions of 690 | this License applicable to that copy. 691 | 692 | 3. Object Code Incorporating Material from Library Header Files. 693 | 694 | The object code form of an Application may incorporate material from 695 | a header file that is part of the Library. You may convey such object 696 | code under terms of your choice, provided that, if the incorporated 697 | material is not limited to numerical parameters, data structure 698 | layouts and accessors, or small macros, inline functions and templates 699 | (ten or fewer lines in length), you do both of the following: 700 | 701 | a) Give prominent notice with each copy of the object code that the 702 | Library is used in it and that the Library and its use are 703 | covered by this License. 704 | 705 | b) Accompany the object code with a copy of the GNU GPL and this license 706 | document. 707 | 708 | 4. Combined Works. 709 | 710 | You may convey a Combined Work under terms of your choice that, 711 | taken together, effectively do not restrict modification of the 712 | portions of the Library contained in the Combined Work and reverse 713 | engineering for debugging such modifications, if you also do each of 714 | the following: 715 | 716 | a) Give prominent notice with each copy of the Combined Work that 717 | the Library is used in it and that the Library and its use are 718 | covered by this License. 719 | 720 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 721 | document. 722 | 723 | c) For a Combined Work that displays copyright notices during 724 | execution, include the copyright notice for the Library among 725 | these notices, as well as a reference directing the user to the 726 | copies of the GNU GPL and this license document. 727 | 728 | d) Do one of the following: 729 | 730 | 0) Convey the Minimal Corresponding Source under the terms of this 731 | License, and the Corresponding Application Code in a form 732 | suitable for, and under terms that permit, the user to 733 | recombine or relink the Application with a modified version of 734 | the Linked Version to produce a modified Combined Work, in the 735 | manner specified by section 6 of the GNU GPL for conveying 736 | Corresponding Source. 737 | 738 | 1) Use a suitable shared library mechanism for linking with the 739 | Library. A suitable mechanism is one that (a) uses at run time 740 | a copy of the Library already present on the user's computer 741 | system, and (b) will operate properly with a modified version 742 | of the Library that is interface-compatible with the Linked 743 | Version. 744 | 745 | e) Provide Installation Information, but only if you would otherwise 746 | be required to provide such information under section 6 of the 747 | GNU GPL, and only to the extent that such information is 748 | necessary to install and execute a modified version of the 749 | Combined Work produced by recombining or relinking the 750 | Application with a modified version of the Linked Version. (If 751 | you use option 4d0, the Installation Information must accompany 752 | the Minimal Corresponding Source and Corresponding Application 753 | Code. If you use option 4d1, you must provide the Installation 754 | Information in the manner specified by section 6 of the GNU GPL 755 | for conveying Corresponding Source.) 756 | 757 | 5. Combined Libraries. 758 | 759 | You may place library facilities that are a work based on the 760 | Library side by side in a single library together with other library 761 | facilities that are not Applications and are not covered by this 762 | License, and convey such a combined library under terms of your 763 | choice, if you do both of the following: 764 | 765 | a) Accompany the combined library with a copy of the same work based 766 | on the Library, uncombined with any other library facilities, 767 | conveyed under the terms of this License. 768 | 769 | b) Give prominent notice with the combined library that part of it 770 | is a work based on the Library, and explaining where to find the 771 | accompanying uncombined form of the same work. 772 | 773 | 6. Revised Versions of the GNU Lesser General Public License. 774 | 775 | The Free Software Foundation may publish revised and/or new versions 776 | of the GNU Lesser General Public License from time to time. Such new 777 | versions will be similar in spirit to the present version, but may 778 | differ in detail to address new problems or concerns. 779 | 780 | Each version is given a distinguishing version number. If the 781 | Library as you received it specifies that a certain numbered version 782 | of the GNU Lesser General Public License "or any later version" 783 | applies to it, you have the option of following the terms and 784 | conditions either of that published version or of any later version 785 | published by the Free Software Foundation. If the Library as you 786 | received it does not specify a version number of the GNU Lesser 787 | General Public License, you may choose any version of the GNU Lesser 788 | General Public License ever published by the Free Software Foundation. 789 | 790 | If the Library as you received it specifies that a proxy can decide 791 | whether future versions of the GNU Lesser General Public License shall 792 | apply, that proxy's public statement of acceptance of any version is 793 | permanent authorization for you to choose that version for the 794 | Library. 795 | -------------------------------------------------------------------------------- /NeoPixelBus.cpp: -------------------------------------------------------------------------------- 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 | Originally 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 | NeoPixel is free software: you can redistribute it and/or modify 18 | it under the terms of the GNU Lesser General Public License as 19 | published by the Free Software Foundation, either version 3 of 20 | the License, or (at your option) any later version. 21 | 22 | NeoPixel is distributed in the hope that it will be useful, 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 | GNU Lesser General Public License for more details. 26 | 27 | You should have received a copy of the GNU Lesser General Public 28 | License along with NeoPixel. If not, see 29 | . 30 | -------------------------------------------------------------------------*/ 31 | 32 | #include "NeoPixelBus.h" 33 | 34 | #if defined(ESP8266) 35 | // due to linker overriding the ICACHE_RAM_ATTR for cpp files, these methods are 36 | // moved into a C file so the attribute will be applied correctly 37 | extern "C" void ICACHE_RAM_ATTR send_pixels_800(uint8_t* pixels, uint8_t* end, uint8_t pin); 38 | extern "C" void ICACHE_RAM_ATTR send_pixels_400(uint8_t* pixels, uint8_t* end, uint8_t pin); 39 | #endif 40 | 41 | NeoPixelBus::NeoPixelBus(uint16_t n, uint8_t p, uint8_t t) : 42 | _countPixels(n), 43 | _sizePixels(n * 3), 44 | _pin(p), 45 | _animationLastTick(0), 46 | _activeAnimations(0), 47 | _flagsPixels(t) 48 | { 49 | setPin(p); 50 | 51 | _pixels = (uint8_t *)malloc(_sizePixels); 52 | if (_pixels) 53 | { 54 | memset(_pixels, 0, _sizePixels); 55 | } 56 | 57 | uint16_t animationSize = n * sizeof(FadeAnimation); 58 | _animations = (FadeAnimation*)malloc(animationSize); 59 | if (_animations) 60 | { 61 | memset(_animations, 0, animationSize); 62 | } 63 | } 64 | 65 | NeoPixelBus::~NeoPixelBus() 66 | { 67 | if (_pixels) 68 | free(_pixels); 69 | if (_animations) 70 | free(_animations); 71 | 72 | pinMode(_pin, INPUT); 73 | } 74 | 75 | void NeoPixelBus::Begin(void) 76 | { 77 | pinMode(_pin, OUTPUT); 78 | digitalWrite(_pin, LOW); 79 | 80 | Dirty(); 81 | } 82 | 83 | void NeoPixelBus::Show(void) 84 | { 85 | if (!_pixels) 86 | return; 87 | if (!IsDirty()) 88 | return; 89 | 90 | // Data latch = 50+ microsecond pause in the output stream. Rather than 91 | // put a delay at the end of the function, the ending time is noted and 92 | // the function will simply hold off (if needed) on issuing the 93 | // subsequent round of data until the latch time has elapsed. This 94 | // allows the mainline code to start generating the next frame of data 95 | // rather than stalling for the latch. 96 | while (!CanShow()) 97 | { 98 | delay(0); // allows for system yield if needed 99 | } 100 | // _endTime is a private member (rather than global var) so that mutliple 101 | // instances on different pins can be quickly issued in succession (each 102 | // instance doesn't delay the next). 103 | 104 | // In order to make this code runtime-configurable to work with any pin, 105 | // SBI/CBI instructions are eschewed in favor of full PORT writes via the 106 | // OUT or ST instructions. It relies on two facts: that peripheral 107 | // functions (such as PWM) take precedence on output pins, so our PORT- 108 | // wide writes won't interfere, and that interrupts are globally disabled 109 | // while data is being issued to the LEDs, so no other code will be 110 | // accessing the PORT. The code takes an initial 'snapshot' of the PORT 111 | // state, computes 'pin high' and 'pin low' values, and writes these back 112 | // to the PORT register as needed. 113 | 114 | noInterrupts(); // Need 100% focus on instruction timing 115 | 116 | #ifdef __AVR__ 117 | 118 | volatile uint16_t 119 | i = _sizePixels; // Loop counter 120 | volatile uint8_t 121 | *ptr = _pixels, // Pointer to next byte 122 | b = *ptr++, // Current byte value 123 | hi, // PORT w/output bit set high 124 | lo; // PORT w/output bit set low 125 | 126 | // Hand-tuned assembly code issues data to the LED drivers at a specific 127 | // rate. There's separate code for different CPU speeds (8, 12, 16 MHz) 128 | // for both the WS2811 (400 KHz) and WS2812 (800 KHz) drivers. The 129 | // datastream timing for the LED drivers allows a little wiggle room each 130 | // way (listed in the datasheets), so the conditions for compiling each 131 | // case are set up for a range of frequencies rather than just the exact 132 | // 8, 12 or 16 MHz values, permitting use with some close-but-not-spot-on 133 | // devices (e.g. 16.5 MHz DigiSpark). The ranges were arrived at based 134 | // on the datasheet figures and have not been extensively tested outside 135 | // the canonical 8/12/16 MHz speeds; there's no guarantee these will work 136 | // close to the extremes (or possibly they could be pushed further). 137 | // Keep in mind only one CPU speed case actually gets compiled; the 138 | // resulting program isn't as massive as it might look from source here. 139 | 140 | // 8 MHz(ish) AVR --------------------------------------------------------- 141 | #if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL) 142 | 143 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 144 | if ((_flagsPixels & NEO_SPDMASK) == NEO_KHZ800) 145 | { 146 | // 800 KHz bitstream 147 | #endif 148 | 149 | volatile uint8_t n1, n2 = 0; // First, next bits out 150 | 151 | // Squeezing an 800 KHz stream out of an 8 MHz chip requires code 152 | // specific to each PORT register. At present this is only written 153 | // to work with pins on PORTD or PORTB, the most likely use case -- 154 | // this covers all the pins on the Adafruit Flora and the bulk of 155 | // digital pins on the Arduino Pro 8 MHz (keep in mind, this code 156 | // doesn't even get compiled for 16 MHz boards like the Uno, Mega, 157 | // Leonardo, etc., so don't bother extending this out of hand). 158 | // Additional PORTs could be added if you really need them, just 159 | // duplicate the else and loop and change the PORT. Each add'l 160 | // PORT will require about 150(ish) bytes of program space. 161 | 162 | // 10 instruction clocks per bit: HHxxxxxLLL 163 | // OUT instructions: ^ ^ ^ (T=0,2,7) 164 | 165 | #ifdef PORTD // PORTD isn't present on ATtiny85, etc. 166 | 167 | if (_port == &PORTD) 168 | { 169 | 170 | hi = PORTD | _pinMask; 171 | lo = PORTD & ~_pinMask; 172 | n1 = lo; 173 | if(b & 0x80) n1 = hi; 174 | 175 | // Dirty trick: RJMPs proceeding to the next instruction are used 176 | // to delay two clock cycles in one instruction word (rather than 177 | // using two NOPs). This was necessary in order to squeeze the 178 | // loop down to exactly 64 words -- the maximum possible for a 179 | // relative branch. 180 | 181 | asm volatile( 182 | "headD:" "\n\t" // Clk Pseudocode 183 | // Bit 7: 184 | "out %[_port] , %[hi]" "\n\t" // 1 PORT = hi 185 | "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo 186 | "out %[_port] , %[n1]" "\n\t" // 1 PORT = n1 187 | "rjmp .+0" "\n\t" // 2 nop nop 188 | "sbrc %[byte] , 6" "\n\t" // 1-2 if(b & 0x40) 189 | "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi 190 | "out %[_port] , %[lo]" "\n\t" // 1 PORT = lo 191 | "rjmp .+0" "\n\t" // 2 nop nop 192 | // Bit 6: 193 | "out %[_port] , %[hi]" "\n\t" // 1 PORT = hi 194 | "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo 195 | "out %[_port] , %[n2]" "\n\t" // 1 PORT = n2 196 | "rjmp .+0" "\n\t" // 2 nop nop 197 | "sbrc %[byte] , 5" "\n\t" // 1-2 if(b & 0x20) 198 | "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi 199 | "out %[_port] , %[lo]" "\n\t" // 1 PORT = lo 200 | "rjmp .+0" "\n\t" // 2 nop nop 201 | // Bit 5: 202 | "out %[_port] , %[hi]" "\n\t" // 1 PORT = hi 203 | "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo 204 | "out %[_port] , %[n1]" "\n\t" // 1 PORT = n1 205 | "rjmp .+0" "\n\t" // 2 nop nop 206 | "sbrc %[byte] , 4" "\n\t" // 1-2 if(b & 0x10) 207 | "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi 208 | "out %[_port] , %[lo]" "\n\t" // 1 PORT = lo 209 | "rjmp .+0" "\n\t" // 2 nop nop 210 | // Bit 4: 211 | "out %[_port] , %[hi]" "\n\t" // 1 PORT = hi 212 | "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo 213 | "out %[_port] , %[n2]" "\n\t" // 1 PORT = n2 214 | "rjmp .+0" "\n\t" // 2 nop nop 215 | "sbrc %[byte] , 3" "\n\t" // 1-2 if(b & 0x08) 216 | "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi 217 | "out %[_port] , %[lo]" "\n\t" // 1 PORT = lo 218 | "rjmp .+0" "\n\t" // 2 nop nop 219 | // Bit 3: 220 | "out %[_port] , %[hi]" "\n\t" // 1 PORT = hi 221 | "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo 222 | "out %[_port] , %[n1]" "\n\t" // 1 PORT = n1 223 | "rjmp .+0" "\n\t" // 2 nop nop 224 | "sbrc %[byte] , 2" "\n\t" // 1-2 if(b & 0x04) 225 | "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi 226 | "out %[_port] , %[lo]" "\n\t" // 1 PORT = lo 227 | "rjmp .+0" "\n\t" // 2 nop nop 228 | // Bit 2: 229 | "out %[_port] , %[hi]" "\n\t" // 1 PORT = hi 230 | "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo 231 | "out %[_port] , %[n2]" "\n\t" // 1 PORT = n2 232 | "rjmp .+0" "\n\t" // 2 nop nop 233 | "sbrc %[byte] , 1" "\n\t" // 1-2 if(b & 0x02) 234 | "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi 235 | "out %[_port] , %[lo]" "\n\t" // 1 PORT = lo 236 | "rjmp .+0" "\n\t" // 2 nop nop 237 | // Bit 1: 238 | "out %[_port] , %[hi]" "\n\t" // 1 PORT = hi 239 | "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo 240 | "out %[_port] , %[n1]" "\n\t" // 1 PORT = n1 241 | "rjmp .+0" "\n\t" // 2 nop nop 242 | "sbrc %[byte] , 0" "\n\t" // 1-2 if(b & 0x01) 243 | "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi 244 | "out %[_port] , %[lo]" "\n\t" // 1 PORT = lo 245 | "sbiw %[count], 1" "\n\t" // 2 i-- (don't act on Z flag yet) 246 | // Bit 0: 247 | "out %[_port] , %[hi]" "\n\t" // 1 PORT = hi 248 | "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo 249 | "out %[_port] , %[n2]" "\n\t" // 1 PORT = n2 250 | "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ 251 | "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80) 252 | "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi 253 | "out %[_port] , %[lo]" "\n\t" // 1 PORT = lo 254 | "brne headD" "\n" // 2 while(i) (Z flag set above) 255 | : [byte] "+r" (b), 256 | [n1] "+r" (n1), 257 | [n2] "+r" (n2), 258 | [count] "+w" (i) 259 | : [_port] "I" (_SFR_IO_ADDR(PORTD)), 260 | [ptr] "e" (ptr), 261 | [hi] "r" (hi), 262 | [lo] "r" (lo)); 263 | 264 | } 265 | else if (_port == &PORTB) 266 | { 267 | 268 | #endif // PORTD 269 | 270 | // Same as above, just switched to PORTB and stripped of comments. 271 | hi = PORTB | _pinMask; 272 | lo = PORTB & ~_pinMask; 273 | n1 = lo; 274 | if(b & 0x80) n1 = hi; 275 | 276 | asm volatile( 277 | "headB:" "\n\t" 278 | "out %[_port] , %[hi]" "\n\t" 279 | "mov %[n2] , %[lo]" "\n\t" 280 | "out %[_port] , %[n1]" "\n\t" 281 | "rjmp .+0" "\n\t" 282 | "sbrc %[byte] , 6" "\n\t" 283 | "mov %[n2] , %[hi]" "\n\t" 284 | "out %[_port] , %[lo]" "\n\t" 285 | "rjmp .+0" "\n\t" 286 | "out %[_port] , %[hi]" "\n\t" 287 | "mov %[n1] , %[lo]" "\n\t" 288 | "out %[_port] , %[n2]" "\n\t" 289 | "rjmp .+0" "\n\t" 290 | "sbrc %[byte] , 5" "\n\t" 291 | "mov %[n1] , %[hi]" "\n\t" 292 | "out %[_port] , %[lo]" "\n\t" 293 | "rjmp .+0" "\n\t" 294 | "out %[_port] , %[hi]" "\n\t" 295 | "mov %[n2] , %[lo]" "\n\t" 296 | "out %[_port] , %[n1]" "\n\t" 297 | "rjmp .+0" "\n\t" 298 | "sbrc %[byte] , 4" "\n\t" 299 | "mov %[n2] , %[hi]" "\n\t" 300 | "out %[_port] , %[lo]" "\n\t" 301 | "rjmp .+0" "\n\t" 302 | "out %[_port] , %[hi]" "\n\t" 303 | "mov %[n1] , %[lo]" "\n\t" 304 | "out %[_port] , %[n2]" "\n\t" 305 | "rjmp .+0" "\n\t" 306 | "sbrc %[byte] , 3" "\n\t" 307 | "mov %[n1] , %[hi]" "\n\t" 308 | "out %[_port] , %[lo]" "\n\t" 309 | "rjmp .+0" "\n\t" 310 | "out %[_port] , %[hi]" "\n\t" 311 | "mov %[n2] , %[lo]" "\n\t" 312 | "out %[_port] , %[n1]" "\n\t" 313 | "rjmp .+0" "\n\t" 314 | "sbrc %[byte] , 2" "\n\t" 315 | "mov %[n2] , %[hi]" "\n\t" 316 | "out %[_port] , %[lo]" "\n\t" 317 | "rjmp .+0" "\n\t" 318 | "out %[_port] , %[hi]" "\n\t" 319 | "mov %[n1] , %[lo]" "\n\t" 320 | "out %[_port] , %[n2]" "\n\t" 321 | "rjmp .+0" "\n\t" 322 | "sbrc %[byte] , 1" "\n\t" 323 | "mov %[n1] , %[hi]" "\n\t" 324 | "out %[_port] , %[lo]" "\n\t" 325 | "rjmp .+0" "\n\t" 326 | "out %[_port] , %[hi]" "\n\t" 327 | "mov %[n2] , %[lo]" "\n\t" 328 | "out %[_port] , %[n1]" "\n\t" 329 | "rjmp .+0" "\n\t" 330 | "sbrc %[byte] , 0" "\n\t" 331 | "mov %[n2] , %[hi]" "\n\t" 332 | "out %[_port] , %[lo]" "\n\t" 333 | "sbiw %[count], 1" "\n\t" 334 | "out %[_port] , %[hi]" "\n\t" 335 | "mov %[n1] , %[lo]" "\n\t" 336 | "out %[_port] , %[n2]" "\n\t" 337 | "ld %[byte] , %a[ptr]+" "\n\t" 338 | "sbrc %[byte] , 7" "\n\t" 339 | "mov %[n1] , %[hi]" "\n\t" 340 | "out %[_port] , %[lo]" "\n\t" 341 | "brne headB" "\n" 342 | : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i) 343 | : [_port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi), 344 | [lo] "r" (lo)); 345 | 346 | #ifdef PORTD 347 | } // endif PORTB 348 | #endif 349 | 350 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 351 | } 352 | else 353 | { 354 | // end 800 KHz, do 400 KHz 355 | 356 | // Timing is more relaxed; unrolling the inner loop for each bit is 357 | // not necessary. Still using the peculiar RJMPs as 2X NOPs, not out 358 | // of need but just to trim the code size down a little. 359 | // This 400-KHz-datastream-on-8-MHz-CPU code is not quite identical 360 | // to the 800-on-16 code later -- the hi/lo timing between WS2811 and 361 | // WS2812 is not simply a 2:1 scale! 362 | 363 | // 20 inst. clocks per bit: HHHHxxxxxxLLLLLLLLLL 364 | // ST instructions: ^ ^ ^ (T=0,4,10) 365 | 366 | volatile uint8_t next, bit; 367 | 368 | hi = *_port | _pinMask; 369 | lo = *_port & ~_pinMask; 370 | next = lo; 371 | bit = 8; 372 | 373 | asm volatile( 374 | "head20:" "\n\t" // Clk Pseudocode (T = 0) 375 | "st %a[_port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) 376 | "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128) 377 | "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4) 378 | "st %a[_port], %[next]" "\n\t" // 2 PORT = next (T = 6) 379 | "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7) 380 | "dec %[bit]" "\n\t" // 1 bit-- (T = 8) 381 | "breq nextbyte20" "\n\t" // 1-2 if(bit == 0) 382 | "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10) 383 | "st %a[_port], %[lo]" "\n\t" // 2 PORT = lo (T = 12) 384 | "rjmp .+0" "\n\t" // 2 nop nop (T = 14) 385 | "rjmp .+0" "\n\t" // 2 nop nop (T = 16) 386 | "rjmp .+0" "\n\t" // 2 nop nop (T = 18) 387 | "rjmp head20" "\n\t" // 2 -> head20 (next bit out) 388 | "nextbyte20:" "\n\t" // (T = 10) 389 | "st %a[_port], %[lo]" "\n\t" // 2 PORT = lo (T = 12) 390 | "nop" "\n\t" // 1 nop (T = 13) 391 | "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 14) 392 | "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 16) 393 | "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18) 394 | "brne head20" "\n" // 2 if(i != 0) -> (next byte) 395 | : [_port] "+e" (_port), 396 | [byte] "+r" (b), 397 | [bit] "+r" (bit), 398 | [next] "+r" (next), 399 | [count] "+w" (i) 400 | : [hi] "r" (hi), 401 | [lo] "r" (lo), 402 | [ptr] "e" (ptr)); 403 | } 404 | #endif 405 | 406 | // 12 MHz(ish) AVR -------------------------------------------------------- 407 | #elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL) 408 | 409 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 410 | if ((_flagsPixels & NEO_SPDMASK) == NEO_KHZ800) 411 | { 412 | // 800 KHz bitstream 413 | #endif 414 | 415 | // In the 12 MHz case, an optimized 800 KHz datastream (no dead time 416 | // between bytes) requires a PORT-specific loop similar to the 8 MHz 417 | // code (but a little more relaxed in this case). 418 | 419 | // 15 instruction clocks per bit: HHHHxxxxxxLLLLL 420 | // OUT instructions: ^ ^ ^ (T=0,4,10) 421 | 422 | volatile uint8_t next; 423 | 424 | #ifdef PORTD 425 | 426 | if (_port == &PORTD) 427 | { 428 | 429 | hi = PORTD | _pinMask; 430 | lo = PORTD & ~_pinMask; 431 | next = lo; 432 | if(b & 0x80) next = hi; 433 | 434 | // Don't "optimize" the OUT calls into the bitTime subroutine; 435 | // we're exploiting the RCALL and RET as 3- and 4-cycle NOPs! 436 | asm volatile( 437 | "headD:" "\n\t" // (T = 0) 438 | "out %[_port], %[hi]" "\n\t" // (T = 1) 439 | "rcall bitTimeD" "\n\t" // Bit 7 (T = 15) 440 | "out %[_port], %[hi]" "\n\t" 441 | "rcall bitTimeD" "\n\t" // Bit 6 442 | "out %[_port], %[hi]" "\n\t" 443 | "rcall bitTimeD" "\n\t" // Bit 5 444 | "out %[_port], %[hi]" "\n\t" 445 | "rcall bitTimeD" "\n\t" // Bit 4 446 | "out %[_port], %[hi]" "\n\t" 447 | "rcall bitTimeD" "\n\t" // Bit 3 448 | "out %[_port], %[hi]" "\n\t" 449 | "rcall bitTimeD" "\n\t" // Bit 2 450 | "out %[_port], %[hi]" "\n\t" 451 | "rcall bitTimeD" "\n\t" // Bit 1 452 | // Bit 0: 453 | "out %[_port] , %[hi]" "\n\t" // 1 PORT = hi (T = 1) 454 | "rjmp .+0" "\n\t" // 2 nop nop (T = 3) 455 | "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 5) 456 | "out %[_port] , %[next]" "\n\t" // 1 PORT = next (T = 6) 457 | "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7) 458 | "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80) (T = 8) 459 | "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 9) 460 | "nop" "\n\t" // 1 (T = 10) 461 | "out %[_port] , %[lo]" "\n\t" // 1 PORT = lo (T = 11) 462 | "sbiw %[count], 1" "\n\t" // 2 i-- (T = 13) 463 | "brne headD" "\n\t" // 2 if(i != 0) -> (next byte) 464 | "rjmp doneD" "\n\t" 465 | "bitTimeD:" "\n\t" // nop nop nop (T = 4) 466 | "out %[_port], %[next]" "\n\t" // 1 PORT = next (T = 5) 467 | "mov %[next], %[lo]" "\n\t" // 1 next = lo (T = 6) 468 | "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 7) 469 | "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 0x80) (T = 8) 470 | "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 9) 471 | "nop" "\n\t" // 1 (T = 10) 472 | "out %[_port], %[lo]" "\n\t" // 1 PORT = lo (T = 11) 473 | "ret" "\n\t" // 4 nop nop nop nop (T = 15) 474 | "doneD:" "\n" 475 | : [byte] "+r" (b), 476 | [next] "+r" (next), 477 | [count] "+w" (i) 478 | : [_port] "I" (_SFR_IO_ADDR(PORTD)), 479 | [ptr] "e" (ptr), 480 | [hi] "r" (hi), 481 | [lo] "r" (lo)); 482 | 483 | } 484 | else if (_port == &PORTB) 485 | { 486 | 487 | #endif // PORTD 488 | 489 | hi = PORTB | _pinMask; 490 | lo = PORTB & ~_pinMask; 491 | next = lo; 492 | if(b & 0x80) next = hi; 493 | 494 | // Same as above, just set for PORTB & stripped of comments 495 | asm volatile( 496 | "headB:" "\n\t" 497 | "out %[_port], %[hi]" "\n\t" 498 | "rcall bitTimeB" "\n\t" 499 | "out %[_port], %[hi]" "\n\t" 500 | "rcall bitTimeB" "\n\t" 501 | "out %[_port], %[hi]" "\n\t" 502 | "rcall bitTimeB" "\n\t" 503 | "out %[_port], %[hi]" "\n\t" 504 | "rcall bitTimeB" "\n\t" 505 | "out %[_port], %[hi]" "\n\t" 506 | "rcall bitTimeB" "\n\t" 507 | "out %[_port], %[hi]" "\n\t" 508 | "rcall bitTimeB" "\n\t" 509 | "out %[_port], %[hi]" "\n\t" 510 | "rcall bitTimeB" "\n\t" 511 | "out %[_port] , %[hi]" "\n\t" 512 | "rjmp .+0" "\n\t" 513 | "ld %[byte] , %a[ptr]+" "\n\t" 514 | "out %[_port] , %[next]" "\n\t" 515 | "mov %[next] , %[lo]" "\n\t" 516 | "sbrc %[byte] , 7" "\n\t" 517 | "mov %[next] , %[hi]" "\n\t" 518 | "nop" "\n\t" 519 | "out %[_port] , %[lo]" "\n\t" 520 | "sbiw %[count], 1" "\n\t" 521 | "brne headB" "\n\t" 522 | "rjmp doneB" "\n\t" 523 | "bitTimeB:" "\n\t" 524 | "out %[_port], %[next]" "\n\t" 525 | "mov %[next], %[lo]" "\n\t" 526 | "rol %[byte]" "\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 | "ret" "\n\t" 532 | "doneB:" "\n" 533 | : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i) 534 | : [_port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi), 535 | [lo] "r" (lo)); 536 | 537 | #ifdef PORTD 538 | } 539 | #endif 540 | 541 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 542 | } 543 | else 544 | { 545 | // 400 KHz 546 | 547 | // 30 instruction clocks per bit: HHHHHHxxxxxxxxxLLLLLLLLLLLLLLL 548 | // ST instructions: ^ ^ ^ (T=0,6,15) 549 | 550 | volatile uint8_t next, bit; 551 | 552 | hi = *_port | _pinMask; 553 | lo = *_port & ~_pinMask; 554 | next = lo; 555 | bit = 8; 556 | 557 | asm volatile( 558 | "head30:" "\n\t" // Clk Pseudocode (T = 0) 559 | "st %a[_port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) 560 | "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128) 561 | "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4) 562 | "rjmp .+0" "\n\t" // 2 nop nop (T = 6) 563 | "st %a[_port], %[next]" "\n\t" // 2 PORT = next (T = 8) 564 | "rjmp .+0" "\n\t" // 2 nop nop (T = 10) 565 | "rjmp .+0" "\n\t" // 2 nop nop (T = 12) 566 | "rjmp .+0" "\n\t" // 2 nop nop (T = 14) 567 | "nop" "\n\t" // 1 nop (T = 15) 568 | "st %a[_port], %[lo]" "\n\t" // 2 PORT = lo (T = 17) 569 | "rjmp .+0" "\n\t" // 2 nop nop (T = 19) 570 | "dec %[bit]" "\n\t" // 1 bit-- (T = 20) 571 | "breq nextbyte30" "\n\t" // 1-2 if(bit == 0) 572 | "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 22) 573 | "rjmp .+0" "\n\t" // 2 nop nop (T = 24) 574 | "rjmp .+0" "\n\t" // 2 nop nop (T = 26) 575 | "rjmp .+0" "\n\t" // 2 nop nop (T = 28) 576 | "rjmp head30" "\n\t" // 2 -> head30 (next bit out) 577 | "nextbyte30:" "\n\t" // (T = 22) 578 | "nop" "\n\t" // 1 nop (T = 23) 579 | "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 24) 580 | "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 26) 581 | "sbiw %[count], 1" "\n\t" // 2 i-- (T = 28) 582 | "brne head30" "\n" // 1-2 if(i != 0) -> (next byte) 583 | : [_port] "+e" (_port), 584 | [byte] "+r" (b), 585 | [bit] "+r" (bit), 586 | [next] "+r" (next), 587 | [count] "+w" (i) 588 | : [hi] "r" (hi), 589 | [lo] "r" (lo), 590 | [ptr] "e" (ptr)); 591 | } 592 | #endif 593 | 594 | // 16 MHz(ish) AVR -------------------------------------------------------- 595 | #elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L) 596 | 597 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 598 | if ((_flagsPixels & NEO_SPDMASK) == NEO_KHZ800) 599 | { 600 | // 800 KHz bitstream 601 | #endif 602 | 603 | // WS2811 and WS2812 have different hi/lo duty cycles; this is 604 | // similar but NOT an exact copy of the prior 400-on-8 code. 605 | 606 | // 20 inst. clocks per bit: HHHHHxxxxxxxxLLLLLLL 607 | // ST instructions: ^ ^ ^ (T=0,5,13) 608 | 609 | volatile uint8_t next, bit; 610 | 611 | hi = *_port | _pinMask; 612 | lo = *_port & ~_pinMask; 613 | next = lo; 614 | bit = 8; 615 | 616 | asm volatile( 617 | "head20:" "\n\t" // Clk Pseudocode (T = 0) 618 | "st %a[_port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) 619 | "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 128) 620 | "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4) 621 | "dec %[bit]" "\n\t" // 1 bit-- (T = 5) 622 | "st %a[_port], %[next]" "\n\t" // 2 PORT = next (T = 7) 623 | "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 8) 624 | "breq nextbyte20" "\n\t" // 1-2 if(bit == 0) (from dec above) 625 | "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10) 626 | "rjmp .+0" "\n\t" // 2 nop nop (T = 12) 627 | "nop" "\n\t" // 1 nop (T = 13) 628 | "st %a[_port], %[lo]" "\n\t" // 2 PORT = lo (T = 15) 629 | "nop" "\n\t" // 1 nop (T = 16) 630 | "rjmp .+0" "\n\t" // 2 nop nop (T = 18) 631 | "rjmp head20" "\n\t" // 2 -> head20 (next bit out) 632 | "nextbyte20:" "\n\t" // (T = 10) 633 | "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 11) 634 | "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 13) 635 | "st %a[_port], %[lo]" "\n\t" // 2 PORT = lo (T = 15) 636 | "nop" "\n\t" // 1 nop (T = 16) 637 | "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18) 638 | "brne head20" "\n" // 2 if(i != 0) -> (next byte) 639 | : [_port] "+e" (_port), 640 | [byte] "+r" (b), 641 | [bit] "+r" (bit), 642 | [next] "+r" (next), 643 | [count] "+w" (i) 644 | : [ptr] "e" (ptr), 645 | [hi] "r" (hi), 646 | [lo] "r" (lo)); 647 | 648 | 649 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 650 | } 651 | else 652 | { 653 | // 400 KHz 654 | 655 | // The 400 KHz clock on 16 MHz MCU is the most 'relaxed' version. 656 | 657 | // 40 inst. clocks per bit: HHHHHHHHxxxxxxxxxxxxLLLLLLLLLLLLLLLLLLLL 658 | // ST instructions: ^ ^ ^ (T=0,8,20) 659 | 660 | volatile uint8_t next, bit; 661 | 662 | hi = *_port | _pinMask; 663 | lo = *_port & ~_pinMask; 664 | next = lo; 665 | bit = 8; 666 | 667 | asm volatile( 668 | "head40:" "\n\t" // Clk Pseudocode (T = 0) 669 | "st %a[_port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) 670 | "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128) 671 | "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 4) 672 | "rjmp .+0" "\n\t" // 2 nop nop (T = 6) 673 | "rjmp .+0" "\n\t" // 2 nop nop (T = 8) 674 | "st %a[_port], %[next]" "\n\t" // 2 PORT = next (T = 10) 675 | "rjmp .+0" "\n\t" // 2 nop nop (T = 12) 676 | "rjmp .+0" "\n\t" // 2 nop nop (T = 14) 677 | "rjmp .+0" "\n\t" // 2 nop nop (T = 16) 678 | "rjmp .+0" "\n\t" // 2 nop nop (T = 18) 679 | "rjmp .+0" "\n\t" // 2 nop nop (T = 20) 680 | "st %a[_port], %[lo]" "\n\t" // 2 PORT = lo (T = 22) 681 | "nop" "\n\t" // 1 nop (T = 23) 682 | "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 24) 683 | "dec %[bit]" "\n\t" // 1 bit-- (T = 25) 684 | "breq nextbyte40" "\n\t" // 1-2 if(bit == 0) 685 | "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 27) 686 | "nop" "\n\t" // 1 nop (T = 28) 687 | "rjmp .+0" "\n\t" // 2 nop nop (T = 30) 688 | "rjmp .+0" "\n\t" // 2 nop nop (T = 32) 689 | "rjmp .+0" "\n\t" // 2 nop nop (T = 34) 690 | "rjmp .+0" "\n\t" // 2 nop nop (T = 36) 691 | "rjmp .+0" "\n\t" // 2 nop nop (T = 38) 692 | "rjmp head40" "\n\t" // 2 -> head40 (next bit out) 693 | "nextbyte40:" "\n\t" // (T = 27) 694 | "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 28) 695 | "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 30) 696 | "rjmp .+0" "\n\t" // 2 nop nop (T = 32) 697 | "st %a[_port], %[lo]" "\n\t" // 2 PORT = lo (T = 34) 698 | "rjmp .+0" "\n\t" // 2 nop nop (T = 36) 699 | "sbiw %[count], 1" "\n\t" // 2 i-- (T = 38) 700 | "brne head40" "\n" // 1-2 if(i != 0) -> (next byte) 701 | : [_port] "+e" (_port), 702 | [byte] "+r" (b), 703 | [bit] "+r" (bit), 704 | [next] "+r" (next), 705 | [count] "+w" (i) 706 | : [ptr] "e" (ptr), 707 | [hi] "r" (hi), 708 | [lo] "r" (lo)); 709 | } 710 | #endif 711 | 712 | #else 713 | #error "CPU SPEED NOT SUPPORTED" 714 | #endif 715 | 716 | #elif defined(ESP8266) 717 | 718 | uint8_t* p = _pixels; 719 | uint8_t* end = p + _sizePixels; 720 | 721 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 722 | 723 | 724 | if ((_flagsPixels & NEO_SPDMASK) == NEO_KHZ800) 725 | { 726 | #endif 727 | // 800 KHz bitstream 728 | send_pixels_800(p, end, _pin); 729 | 730 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 731 | } 732 | else 733 | { 734 | // 400 kHz bitstream 735 | send_pixels_400(p, end, _pin); 736 | } 737 | #endif 738 | 739 | #elif defined(__arm__) 740 | 741 | 742 | #if defined(__MK20DX128__) || defined(__MK20DX256__) // Teensy 3.0 & 3.1 743 | #define CYCLES_800_T0H (F_CPU / 4000000) // 0.4us 744 | #define CYCLES_800_T1H (F_CPU / 1250000) // 0.8us 745 | #define CYCLES_800 (F_CPU / 800000) // 1.25us per bit 746 | #define CYCLES_400_T0H (F_CPU / 2000000) 747 | #define CYCLES_400_T1H (F_CPU / 833333) 748 | #define CYCLES_400 (F_CPU / 400000) 749 | 750 | uint8_t *p = _pixels, 751 | *end = p + _sizePixels, pix, mask; 752 | volatile uint8_t *set = portSetRegister(_pin), 753 | *clr = portClearRegister(_pin); 754 | uint32_t cyc; 755 | 756 | ARM_DEMCR |= ARM_DEMCR_TRCENA; 757 | ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; 758 | 759 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 760 | if ((_flagsPixels & NEO_SPDMASK) == NEO_KHZ800) 761 | { 762 | #endif 763 | // 800 KHz bitstream 764 | cyc = ARM_DWT_CYCCNT + CYCLES_800; 765 | while (p < end) 766 | { 767 | pix = *p++; 768 | for (mask = 0x80; mask; mask >>= 1) 769 | { 770 | while (ARM_DWT_CYCCNT - cyc < CYCLES_800); 771 | cyc = ARM_DWT_CYCCNT; 772 | *set = 1; 773 | if (pix & mask) 774 | { 775 | while (ARM_DWT_CYCCNT - cyc < CYCLES_800_T1H); 776 | } 777 | else 778 | { 779 | while (ARM_DWT_CYCCNT - cyc < CYCLES_800_T0H); 780 | } 781 | *clr = 1; 782 | } 783 | } 784 | while (ARM_DWT_CYCCNT - cyc < CYCLES_800); 785 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 786 | } 787 | else 788 | { 789 | // 400 kHz bitstream 790 | cyc = ARM_DWT_CYCCNT + CYCLES_400; 791 | while (p < end) 792 | { 793 | pix = *p++; 794 | for(mask = 0x80; mask; mask >>= 1) 795 | { 796 | while (ARM_DWT_CYCCNT - cyc < CYCLES_400); 797 | cyc = ARM_DWT_CYCCNT; 798 | *set = 1; 799 | if (pix & mask) 800 | { 801 | while (ARM_DWT_CYCCNT - cyc < CYCLES_400_T1H); 802 | } 803 | else 804 | { 805 | while (ARM_DWT_CYCCNT - cyc < CYCLES_400_T0H); 806 | } 807 | *clr = 1; 808 | } 809 | } 810 | while (ARM_DWT_CYCCNT - cyc < CYCLES_400); 811 | } 812 | #endif 813 | 814 | #elif defined(__MKL26Z64__) // Teensy-LC 815 | 816 | #if F_CPU == 48000000 817 | uint8_t *p = pixels, 818 | pix, count, dly, 819 | bitmask = digitalPinToBitMask(pin); 820 | volatile uint8_t *reg = portSetRegister(pin); 821 | uint32_t num = numBytes; 822 | asm volatile( 823 | "L%=_begin:" "\n\t" 824 | "ldrb %[pix], [%[p], #0]" "\n\t" 825 | "lsl %[pix], #24" "\n\t" 826 | "movs %[count], #7" "\n\t" 827 | "L%=_loop:" "\n\t" 828 | "lsl %[pix], #1" "\n\t" 829 | "bcs L%=_loop_one" "\n\t" 830 | "L%=_loop_zero:" 831 | "strb %[bitmask], [%[reg], #0]" "\n\t" 832 | "movs %[dly], #4" "\n\t" 833 | "L%=_loop_delay_T0H:" "\n\t" 834 | "sub %[dly], #1" "\n\t" 835 | "bne L%=_loop_delay_T0H" "\n\t" 836 | "strb %[bitmask], [%[reg], #4]" "\n\t" 837 | "movs %[dly], #13" "\n\t" 838 | "L%=_loop_delay_T0L:" "\n\t" 839 | "sub %[dly], #1" "\n\t" 840 | "bne L%=_loop_delay_T0L" "\n\t" 841 | "b L%=_next" "\n\t" 842 | "L%=_loop_one:" 843 | "strb %[bitmask], [%[reg], #0]" "\n\t" 844 | "movs %[dly], #13" "\n\t" 845 | "L%=_loop_delay_T1H:" "\n\t" 846 | "sub %[dly], #1" "\n\t" 847 | "bne L%=_loop_delay_T1H" "\n\t" 848 | "strb %[bitmask], [%[reg], #4]" "\n\t" 849 | "movs %[dly], #4" "\n\t" 850 | "L%=_loop_delay_T1L:" "\n\t" 851 | "sub %[dly], #1" "\n\t" 852 | "bne L%=_loop_delay_T1L" "\n\t" 853 | "nop" "\n\t" 854 | "L%=_next:" "\n\t" 855 | "sub %[count], #1" "\n\t" 856 | "bne L%=_loop" "\n\t" 857 | "lsl %[pix], #1" "\n\t" 858 | "bcs L%=_last_one" "\n\t" 859 | "L%=_last_zero:" 860 | "strb %[bitmask], [%[reg], #0]" "\n\t" 861 | "movs %[dly], #4" "\n\t" 862 | "L%=_last_delay_T0H:" "\n\t" 863 | "sub %[dly], #1" "\n\t" 864 | "bne L%=_last_delay_T0H" "\n\t" 865 | "strb %[bitmask], [%[reg], #4]" "\n\t" 866 | "movs %[dly], #10" "\n\t" 867 | "L%=_last_delay_T0L:" "\n\t" 868 | "sub %[dly], #1" "\n\t" 869 | "bne L%=_last_delay_T0L" "\n\t" 870 | "b L%=_repeat" "\n\t" 871 | "L%=_last_one:" 872 | "strb %[bitmask], [%[reg], #0]" "\n\t" 873 | "movs %[dly], #13" "\n\t" 874 | "L%=_last_delay_T1H:" "\n\t" 875 | "sub %[dly], #1" "\n\t" 876 | "bne L%=_last_delay_T1H" "\n\t" 877 | "strb %[bitmask], [%[reg], #4]" "\n\t" 878 | "movs %[dly], #1" "\n\t" 879 | "L%=_last_delay_T1L:" "\n\t" 880 | "sub %[dly], #1" "\n\t" 881 | "bne L%=_last_delay_T1L" "\n\t" 882 | "nop" "\n\t" 883 | "L%=_repeat:" "\n\t" 884 | "add %[p], #1" "\n\t" 885 | "sub %[num], #1" "\n\t" 886 | "bne L%=_begin" "\n\t" 887 | "L%=_done:" "\n\t" 888 | : [p] "+r" (p), 889 | [pix] "=&r" (pix), 890 | [count] "=&r" (count), 891 | [dly] "=&r" (dly), 892 | [num] "+r" (num) 893 | : [bitmask] "r" (bitmask), 894 | [reg] "r" (reg) 895 | ); 896 | #else 897 | #error "Sorry, only 48 MHz is supported, please set Tools > CPU Speed to 48 MHz" 898 | #endif 899 | 900 | #else // Arduino Due 901 | 902 | #define SCALE VARIANT_MCK / 2UL / 1000000UL 903 | #define INST (2UL * F_CPU / VARIANT_MCK) 904 | #define TIME_800_0 ((int)(0.40 * SCALE + 0.5) - (5 * INST)) 905 | #define TIME_800_1 ((int)(0.80 * SCALE + 0.5) - (5 * INST)) 906 | #define PERIOD_800 ((int)(1.25 * SCALE + 0.5) - (5 * INST)) 907 | #define TIME_400_0 ((int)(0.50 * SCALE + 0.5) - (5 * INST)) 908 | #define TIME_400_1 ((int)(1.20 * SCALE + 0.5) - (5 * INST)) 909 | #define PERIOD_400 ((int)(2.50 * SCALE + 0.5) - (5 * INST)) 910 | 911 | int pinMask, time0, time1, period, t; 912 | Pio *port; 913 | volatile WoReg *portSet, *portClear, *timeValue, *timeReset; 914 | uint8_t *p, *end, pix, mask; 915 | 916 | pmc_set_writeprotect(false); 917 | pmc_enable_periph_clk((uint32_t)TC3_IRQn); 918 | TC_Configure(TC1, 0, 919 | TC_CMR_WAVE | TC_CMR_WAVSEL_UP | TC_CMR_TCCLKS_TIMER_CLOCK1); 920 | TC_Start(TC1, 0); 921 | 922 | pinMask = g_APinDescription[_pin].ulPin; // Don't 'optimize' these into 923 | port = g_APinDescription[_pin].pPort; // declarations above. Want to 924 | portSet = &(port->PIO_SODR); // burn a few cycles after 925 | portClear = &(port->PIO_CODR); // starting timer to minimize 926 | timeValue = &(TC1->TC_CHANNEL[0].TC_CV); // the initial 'while'. 927 | timeReset = &(TC1->TC_CHANNEL[0].TC_CCR); 928 | p = _pixels; 929 | end = p + _sizePixels; 930 | pix = *p++; 931 | mask = 0x80; 932 | 933 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 934 | if ((_flagsPixels & NEO_SPDMASK) == NEO_KHZ800) 935 | { 936 | #endif 937 | // 800 KHz bitstream 938 | time0 = TIME_800_0; 939 | time1 = TIME_800_1; 940 | period = PERIOD_800; 941 | #ifdef INCLUDE_NEO_KHZ400_SUPPORT 942 | } 943 | else 944 | { 945 | // 400 KHz bitstream 946 | time0 = TIME_400_0; 947 | time1 = TIME_400_1; 948 | period = PERIOD_400; 949 | } 950 | #endif 951 | 952 | for (t = time0;; t = time0) 953 | { 954 | if (pix & mask) 955 | t = time1; 956 | while (*timeValue < period); 957 | *portSet = pinMask; 958 | *timeReset = TC_CCR_CLKEN | TC_CCR_SWTRG; 959 | while (*timeValue < t); 960 | *portClear = pinMask; 961 | if (!(mask >>= 1)) 962 | { // This 'inside-out' loop logic utilizes 963 | if (p >= end) 964 | break; // idle time to minimize inter-byte delays. 965 | pix = *p++; 966 | mask = 0x80; 967 | } 968 | } 969 | while (*timeValue < period); // Wait for last bit 970 | TC_Stop(TC1, 0); 971 | 972 | #endif // end Arduino Due 973 | 974 | #endif // end Architecture select 975 | 976 | interrupts(); 977 | ResetDirty(); 978 | _endTime = micros(); // Save EOD time for latch on next call 979 | } 980 | 981 | 982 | // Set the output pin number 983 | void NeoPixelBus::setPin(uint8_t p) 984 | { 985 | pinMode(_pin, INPUT); 986 | _pin = p; 987 | pinMode(p, OUTPUT); 988 | digitalWrite(p, LOW); 989 | #ifdef __AVR__ 990 | _port = portOutputRegister(digitalPinToPort(p)); 991 | _pinMask = digitalPinToBitMask(p); 992 | #endif 993 | } 994 | 995 | // Set pixel color from separate R,G,B components: 996 | void NeoPixelBus::SetPixelColor( 997 | uint16_t n, 998 | uint8_t r, 999 | uint8_t g, 1000 | uint8_t b) 1001 | { 1002 | if (n < _countPixels) 1003 | { 1004 | // clear any animation 1005 | if (_animations[n].time != 0) 1006 | { 1007 | _activeAnimations--; 1008 | _animations[n].time = 0; 1009 | _animations[n].remaining = 0; 1010 | } 1011 | UpdatePixelColor(n, r, g, b); 1012 | } 1013 | } 1014 | 1015 | void NeoPixelBus::ClearTo(uint8_t r, uint8_t g, uint8_t b) 1016 | { 1017 | for (uint8_t n = 0; n < _countPixels; n++) 1018 | { 1019 | SetPixelColor(n, r, g, b); 1020 | } 1021 | } 1022 | 1023 | // Set pixel color from separate R,G,B components: 1024 | void NeoPixelBus::UpdatePixelColor( 1025 | uint16_t n, 1026 | uint8_t r, 1027 | uint8_t g, 1028 | uint8_t b) 1029 | { 1030 | Dirty(); 1031 | 1032 | uint8_t *p = &_pixels[n * 3]; 1033 | 1034 | uint8_t colorOrder = (_flagsPixels & NEO_COLMASK); 1035 | if (colorOrder == NEO_GRB) 1036 | { 1037 | *p++ = g; 1038 | *p++ = r; 1039 | *p = b; 1040 | } 1041 | else if (colorOrder == NEO_RGB) 1042 | { 1043 | *p++ = r; 1044 | *p++ = g; 1045 | *p = b; 1046 | } 1047 | else 1048 | { 1049 | *p++ = b; 1050 | *p++ = r; 1051 | *p = g; 1052 | } 1053 | } 1054 | 1055 | // Query color from previously-set pixel (returns packed 32-bit RGB value) 1056 | RgbColor NeoPixelBus::GetPixelColor(uint16_t n) const 1057 | { 1058 | if (n < _countPixels) 1059 | { 1060 | RgbColor c; 1061 | uint8_t *p = &_pixels[n * 3]; 1062 | 1063 | uint8_t colorOrder = (_flagsPixels & NEO_COLMASK); 1064 | if (colorOrder == NEO_GRB) 1065 | { 1066 | c.G = *p++; 1067 | c.R = *p++; 1068 | c.B = *p; 1069 | } 1070 | else if (colorOrder == NEO_RGB) 1071 | { 1072 | c.R = *p++; 1073 | c.G = *p++; 1074 | c.B = *p; 1075 | } 1076 | else 1077 | { 1078 | c.B = *p++; 1079 | c.R = *p++; 1080 | c.G = *p; 1081 | } 1082 | 1083 | return c; 1084 | } 1085 | 1086 | return RgbColor(0); // Pixel # is out of bounds 1087 | } 1088 | 1089 | void NeoPixelBus::LinearFadePixelColor(uint16_t time, uint16_t n, RgbColor color) 1090 | { 1091 | if (n >= _countPixels) 1092 | { 1093 | return; 1094 | } 1095 | 1096 | if (_animations[n].time != 0) 1097 | { 1098 | _activeAnimations--; 1099 | } 1100 | 1101 | _animations[n].time = time; 1102 | _animations[n].remaining = time; 1103 | _animations[n].target = color; 1104 | _animations[n].origin = GetPixelColor(n); 1105 | 1106 | if (time > 0) 1107 | { 1108 | _activeAnimations++; 1109 | } 1110 | else 1111 | { 1112 | SetPixelColor(n, _animations[n].target); 1113 | } 1114 | } 1115 | 1116 | void NeoPixelBus::FadeTo(uint16_t time, RgbColor color) 1117 | { 1118 | for (uint8_t n = 0; n < _countPixels; n++) 1119 | { 1120 | LinearFadePixelColor(time, n, color); 1121 | } 1122 | } 1123 | 1124 | void NeoPixelBus::StartAnimating() 1125 | { 1126 | _animationLastTick = millis(); 1127 | } 1128 | 1129 | void NeoPixelBus::UpdateAnimations() 1130 | { 1131 | uint32_t currentTick = millis(); 1132 | 1133 | if (_animationLastTick != 0) 1134 | { 1135 | uint32_t delta = currentTick - _animationLastTick; 1136 | if (delta > 0) 1137 | { 1138 | uint16_t countAnimations = _activeAnimations; 1139 | 1140 | FadeAnimation* pAnim; 1141 | RgbColor color; 1142 | 1143 | for (uint16_t iAnim = 0; iAnim < _countPixels && countAnimations > 0; iAnim++) 1144 | { 1145 | pAnim = &_animations[iAnim]; 1146 | 1147 | if (pAnim->remaining > delta) 1148 | { 1149 | pAnim->remaining -= delta; 1150 | 1151 | uint8_t progress = (pAnim->time - pAnim->remaining) * (uint32_t)256 / pAnim->time; 1152 | 1153 | color = RgbColor::LinearBlend(pAnim->origin, 1154 | pAnim->target, 1155 | progress); 1156 | 1157 | UpdatePixelColor(iAnim, color); 1158 | countAnimations--; 1159 | } 1160 | else if (pAnim->remaining > 0) 1161 | { 1162 | // specifically calling SetPixelColor so it will clear animation state 1163 | SetPixelColor(iAnim, pAnim->target); 1164 | countAnimations--; 1165 | } 1166 | } 1167 | } 1168 | } 1169 | 1170 | _animationLastTick = currentTick; 1171 | } 1172 | 1173 | --------------------------------------------------------------------------------