diff --git a/ReadMe.md b/ReadMe.md index 2ccace2d..41c4c46a 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -6,7 +6,7 @@ Arduino NeoPixel library A library to control one wire protocol RGB and RGBW leds like SK6812, WS2811, and WS2812 that are commonly refered to as NeoPixels and two wire protocol RGB like APA102 commonly refered to as DotStars. Supports most Arduino platforms. -This is the most funtional library for the Esp8266 as it provides solutions for all Esp8266 module types even when WiFi is used. +This is the most funtional library for the Esp8266 as it provides solutions for all Esp8266 module types even when WiFi is used. Please read this best practices link before connecting your NeoPixels, it will save you alot of time and effort. diff --git a/examples/NeoPixelAnimation/NeoPixelAnimation.ino b/examples/animations/NeoPixelAnimation/NeoPixelAnimation.ino similarity index 100% rename from examples/NeoPixelAnimation/NeoPixelAnimation.ino rename to examples/animations/NeoPixelAnimation/NeoPixelAnimation.ino diff --git a/examples/NeoPixelCylon/NeoPixelCylon.ino b/examples/animations/NeoPixelCylon/NeoPixelCylon.ino similarity index 100% rename from examples/NeoPixelCylon/NeoPixelCylon.ino rename to examples/animations/NeoPixelCylon/NeoPixelCylon.ino diff --git a/examples/NeoPixelFunFadeInOut/NeoPixelFunFadeInOut.ino b/examples/animations/NeoPixelFunFadeInOut/NeoPixelFunFadeInOut.ino similarity index 100% rename from examples/NeoPixelFunFadeInOut/NeoPixelFunFadeInOut.ino rename to examples/animations/NeoPixelFunFadeInOut/NeoPixelFunFadeInOut.ino diff --git a/examples/NeoPixelFunLoop/NeoPixelFunLoop.ino b/examples/animations/NeoPixelFunLoop/NeoPixelFunLoop.ino similarity index 100% rename from examples/NeoPixelFunLoop/NeoPixelFunLoop.ino rename to examples/animations/NeoPixelFunLoop/NeoPixelFunLoop.ino diff --git a/examples/NeoPixelFunRandomChange/NeoPixelFunRandomChange.ino b/examples/animations/NeoPixelFunRandomChange/NeoPixelFunRandomChange.ino similarity index 100% rename from examples/NeoPixelFunRandomChange/NeoPixelFunRandomChange.ino rename to examples/animations/NeoPixelFunRandomChange/NeoPixelFunRandomChange.ino diff --git a/examples/NeoPixelRotateLoop/NeoPixelRotateLoop.ino b/examples/animations/NeoPixelRotateLoop/NeoPixelRotateLoop.ino similarity index 100% rename from examples/NeoPixelRotateLoop/NeoPixelRotateLoop.ino rename to examples/animations/NeoPixelRotateLoop/NeoPixelRotateLoop.ino diff --git a/examples/NeoPixelBitmap/NeoPixelBitmap.ino b/examples/bitmaps/NeoPixelBitmap/NeoPixelBitmap.ino similarity index 100% rename from examples/NeoPixelBitmap/NeoPixelBitmap.ino rename to examples/bitmaps/NeoPixelBitmap/NeoPixelBitmap.ino diff --git a/examples/NeoPixelBitmap/Strings.bmp b/examples/bitmaps/NeoPixelBitmap/Strings.bmp similarity index 100% rename from examples/NeoPixelBitmap/Strings.bmp rename to examples/bitmaps/NeoPixelBitmap/Strings.bmp diff --git a/examples/NeoPixelBitmap/StringsW.bmp b/examples/bitmaps/NeoPixelBitmap/StringsW.bmp similarity index 100% rename from examples/NeoPixelBitmap/StringsW.bmp rename to examples/bitmaps/NeoPixelBitmap/StringsW.bmp diff --git a/examples/NeoPixelBufferCylon/Cylon.pdn b/examples/bitmaps/NeoPixelBufferCylon/Cylon.pdn similarity index 100% rename from examples/NeoPixelBufferCylon/Cylon.pdn rename to examples/bitmaps/NeoPixelBufferCylon/Cylon.pdn diff --git a/examples/NeoPixelBufferCylon/CylonGrb.h b/examples/bitmaps/NeoPixelBufferCylon/CylonGrb.h similarity index 100% rename from examples/NeoPixelBufferCylon/CylonGrb.h rename to examples/bitmaps/NeoPixelBufferCylon/CylonGrb.h diff --git a/examples/NeoPixelBufferCylon/CylonGrbw.h b/examples/bitmaps/NeoPixelBufferCylon/CylonGrbw.h similarity index 100% rename from examples/NeoPixelBufferCylon/CylonGrbw.h rename to examples/bitmaps/NeoPixelBufferCylon/CylonGrbw.h diff --git a/examples/NeoPixelBufferCylon/NeoPixelBufferCylon.ino b/examples/bitmaps/NeoPixelBufferCylon/NeoPixelBufferCylon.ino similarity index 100% rename from examples/NeoPixelBufferCylon/NeoPixelBufferCylon.ino rename to examples/bitmaps/NeoPixelBufferCylon/NeoPixelBufferCylon.ino diff --git a/examples/bitmaps/NeoPixelBufferShader/NeoPixelBufferShader.ino b/examples/bitmaps/NeoPixelBufferShader/NeoPixelBufferShader.ino new file mode 100644 index 00000000..37a109f4 --- /dev/null +++ b/examples/bitmaps/NeoPixelBufferShader/NeoPixelBufferShader.ino @@ -0,0 +1,167 @@ +// NeoPixelBufferShader +// This example will provide a shader class to the NeoPixelBuffer that will dim and brighten +// the pixels that are in the buffer (a device dependent bitmap) +// + +#include + +const uint16_t PixelCount = 64; // set this to the size of your strip +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 + +// three element GRB pixels, change to your needs +NeoPixelBus strip(PixelCount, PixelPin); + +// the buffer object, +// defined to use memory with the same feature as the strip +// initialized with the same number of pixels as our strip +NeoBuffer> image(8,8,NULL); + +const RgbColor BrightRed(255, 0, 0); +const RgbColor BrightGreen(0, 255, 0); +const RgbColor BrightBlue(0, 0, 255); + +const RgbColor BrightYellow(255, 255, 0); +const RgbColor BrightCyan(0, 255, 255); +const RgbColor BrightPurple(255, 0, 255); + +const RgbColor DarkRed(32, 0, 0); +const RgbColor DarkGreen(0, 32, 0); +const RgbColor DarkBlue(0, 0, 32); + +const RgbColor DarkYellow(32, 32, 0); +const RgbColor DarkCyan(0, 32, 32); +const RgbColor DarkPurple(32, 0, 32); + +const RgbColor White(255); +const RgbColor Black(0); + +// define a custom shader object that provides brightness support +// based upon the NeoBitsBase +template class BrightnessShader : public NeoBitsBase +{ +public: + BrightnessShader(): + NeoBitsBase(), + _brightness(255) // default to full bright + {} + + // required for a shader object, it will be called for + // every pixel + void Apply(uint16_t index, uint8_t* pDest, uint8_t* pSrc) + { + // we don't care what the index is so we ignore it + // + // to apply our brightness shader, + // use the source color, modify, and apply to the destination + + // for every byte in the pixel, + // scale the source value by the brightness and + // store it in the destination byte + const uint8_t* pSrcEnd = pSrc + T_COLOR_FEATURE::PixelSize; + while (pSrc != pSrcEnd) + { + *pDest++ = (*pSrc++ * (uint16_t(_brightness) + 1)) >> 8; + } + } + + // provide an accessor to set brightness + void setBrightness(uint8_t brightness) + { + _brightness = brightness; + Dirty(); // must call dirty when a property changes + } + + // provide an accessor to get brightness + uint8_t getBrightness() + { + return _brightness; + } + +private: + uint8_t _brightness; +}; + +// create an instance of our shader object with the same feature as our buffer +BrightnessShader shader; + +// some dimming tracking variables and settings +int8_t delta; + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + Serial.println(); + Serial.println("Initializing..."); + Serial.flush(); + + // this resets all the neopixels to an off state + strip.Begin(); + strip.Show(); + + // dibs do not default to any color, + // so clear it to black if you aren't going to draw + // into every pixel + image.ClearTo(Black); + + // draw a pattern into the image + uint8_t x = 0; + uint8_t y = 0; + image.SetPixelColor(x++, y, DarkRed); + image.SetPixelColor(x++, y, DarkYellow); + image.SetPixelColor(x++, y, DarkGreen); + image.SetPixelColor(x++, y, DarkCyan); + image.SetPixelColor(x++, y, DarkBlue); + image.SetPixelColor(x++, y, DarkPurple); + + x = 0; + y = 1; + image.SetPixelColor(x++, y, BrightRed); + image.SetPixelColor(x++, y, BrightYellow); + image.SetPixelColor(x++, y, BrightGreen); + image.SetPixelColor(x++, y, BrightCyan); + image.SetPixelColor(x++, y, BrightBlue); + image.SetPixelColor(x++, y, BrightPurple); + + Serial.println(); + Serial.println("Running..."); + + delta = -1; // start by dimming downward +} + +void loop() +{ + // we increment by delta every 30ms + delay(30); + + // update the brightness in shader + // + uint8_t brightness = shader.getBrightness(); + // check if we flip directions + if (brightness == 0) + { + delta = 1; // increment + } + else if (brightness == 255) + { + delta = -1; // decrement + } + // modify and apply + brightness += delta; + shader.setBrightness(brightness); + + Serial.println(brightness); + + + // render the image using the shader and then call Show() + // these two should be called together in order + // + + // need to provide the type of color feature for the strip and + // the type of our custom shader + image.Render>(strip, shader); + strip.Show(); + +} + diff --git a/examples/NeoPixelDibTest/NeoPixelDibTest.ino b/examples/bitmaps/NeoPixelDibTest/NeoPixelDibTest.ino similarity index 97% rename from examples/NeoPixelDibTest/NeoPixelDibTest.ino rename to examples/bitmaps/NeoPixelDibTest/NeoPixelDibTest.ino index 1a13c135..c421b5b1 100644 --- a/examples/NeoPixelDibTest/NeoPixelDibTest.ino +++ b/examples/bitmaps/NeoPixelDibTest/NeoPixelDibTest.ino @@ -34,12 +34,12 @@ const RgbColor White(255); const RgbColor Black(0); // define a custom shader object that provides brightness support -// based upon the NeoShaderBase -class BrightnessShader : public NeoShaderBase +// based upon the NeoBitsBase +class BrightnessShader : public NeoBitsBase { public: BrightnessShader(): - NeoShaderBase(), + NeoBitsBase(), _brightness(255) // default to full bright {} diff --git a/examples/NeoPixelMosaicDump/NeoPixelMosaicDump.ino b/examples/topologies/NeoPixelMosaicDump/NeoPixelMosaicDump.ino similarity index 100% rename from examples/NeoPixelMosaicDump/NeoPixelMosaicDump.ino rename to examples/topologies/NeoPixelMosaicDump/NeoPixelMosaicDump.ino diff --git a/examples/NeoPixelMosaicTest/NeoPixelMosaicTest.ino b/examples/topologies/NeoPixelMosaicTest/NeoPixelMosaicTest.ino similarity index 100% rename from examples/NeoPixelMosaicTest/NeoPixelMosaicTest.ino rename to examples/topologies/NeoPixelMosaicTest/NeoPixelMosaicTest.ino diff --git a/examples/NeoPixelRingTopologyTest/NeoPixelRingTopologyTest.ino b/examples/topologies/NeoPixelRingTopologyTest/NeoPixelRingTopologyTest.ino similarity index 100% rename from examples/NeoPixelRingTopologyTest/NeoPixelRingTopologyTest.ino rename to examples/topologies/NeoPixelRingTopologyTest/NeoPixelRingTopologyTest.ino diff --git a/examples/NeoPixelTilesDump/NeoPixelTilesDump.ino b/examples/topologies/NeoPixelTilesDump/NeoPixelTilesDump.ino similarity index 100% rename from examples/NeoPixelTilesDump/NeoPixelTilesDump.ino rename to examples/topologies/NeoPixelTilesDump/NeoPixelTilesDump.ino diff --git a/examples/NeoPixelTilesTest/NeoPixelTilesTest.ino b/examples/topologies/NeoPixelTilesTest/NeoPixelTilesTest.ino similarity index 100% rename from examples/NeoPixelTilesTest/NeoPixelTilesTest.ino rename to examples/topologies/NeoPixelTilesTest/NeoPixelTilesTest.ino diff --git a/examples/NeoPixelTopologyDump/NeoPixelTopologyDump.ino b/examples/topologies/NeoPixelTopologyDump/NeoPixelTopologyDump.ino similarity index 100% rename from examples/NeoPixelTopologyDump/NeoPixelTopologyDump.ino rename to examples/topologies/NeoPixelTopologyDump/NeoPixelTopologyDump.ino diff --git a/examples/NeoPixelTopologyTest/NeoPixelTopologyTest.ino b/examples/topologies/NeoPixelTopologyTest/NeoPixelTopologyTest.ino similarity index 100% rename from examples/NeoPixelTopologyTest/NeoPixelTopologyTest.ino rename to examples/topologies/NeoPixelTopologyTest/NeoPixelTopologyTest.ino diff --git a/keywords.txt b/keywords.txt index 08c22457..0016e15a 100644 --- a/keywords.txt +++ b/keywords.txt @@ -32,6 +32,8 @@ NeoEsp8266AsyncUart800KbpsMethod KEYWORD1 NeoEsp8266AsyncUart400KbpsMethod KEYWORD1 NeoEsp8266BitBang800KbpsMethod KEYWORD1 NeoEsp8266BitBang400KbpsMethod KEYWORD1 +NeoEsp32BitBang800KbpsMethod KEYWORD1 +NeoEsp32BitBang400KbpsMethod KEYWORD1 DotStarMethod KEYWORD1 DotStarSpiMethod KEYWORD1 NeoPixelAnimator KEYWORD1 diff --git a/library.json b/library.json index 8bb27dbf..7b11f5d9 100644 --- a/library.json +++ b/library.json @@ -8,7 +8,7 @@ "type": "git", "url": "https://github.com/Makuna/NeoPixelBus" }, - "version": "2.2.6", + "version": "2.2.7", "frameworks": "arduino", "platforms": "*" } diff --git a/library.properties b/library.properties index 6cf9c28f..036896a5 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=NeoPixelBus by Makuna -version=2.2.6 +version=2.2.7 author=Michael C. Miller (makuna@live.com) maintainer=Michael C. Miller (makuna@live.com) sentence=A library that makes controlling NeoPixels (WS2811, WS2812 & SK6812) and DotStars (APA102) easy. -paragraph=Supports most Arduino platforms, and especially Esp8266. Support for RGBW pixels. Includes seperate RgbColor, RgbwColor, HslColor, and HsbColor objects. Includes an animator class that helps create asyncronous animations. Supports Matrix layout of pixels. Includes Gamma corretion object. For Esp8266 it has three methods of sending NeoPixel data, DMA, UART, and Bit Bang; and two methods of sending DotStar data, hardware SPI and software SPI. +paragraph=Supports most Arduino platforms, including Esp8266 and Esp32. Support for RGBW pixels. Includes seperate RgbColor, RgbwColor, HslColor, and HsbColor objects. Includes an animator class that helps create asyncronous animations. Supports Matrix layout of pixels. Includes Gamma corretion object. For Esp8266 it has three methods of sending NeoPixel data, DMA, UART, and Bit Bang; and two methods of sending DotStar data, hardware SPI and software SPI. category=Display url=https://github.com/Makuna/NeoPixelBus/wiki architectures=* \ No newline at end of file diff --git a/src/NeoPixelBus.h b/src/NeoPixelBus.h index 4d43a113..e5e0ad67 100644 --- a/src/NeoPixelBus.h +++ b/src/NeoPixelBus.h @@ -60,7 +60,12 @@ License along with NeoPixel. If not, see #include "internal/NeoEsp8266DmaMethod.h" #include "internal/NeoEsp8266UartMethod.h" -#include "internal/NeoEsp8266BitBangMethod.h" +#include "internal/NeoEspBitBangMethod.h" +#include "internal/DotStarGenericMethod.h" + +#elif defined(ARDUINO_ARCH_ESP32) + +#include "internal/NeoEspBitBangMethod.h" #include "internal/DotStarGenericMethod.h" #elif defined(__arm__) // must be before ARDUINO_ARCH_AVR due to Teensy incorrectly having it set diff --git a/src/internal/DotStarSpiMethod.h b/src/internal/DotStarSpiMethod.h index d888eb57..4aabc29c 100644 --- a/src/internal/DotStarSpiMethod.h +++ b/src/internal/DotStarSpiMethod.h @@ -69,7 +69,7 @@ class DotStarSpiMethod void Update() { // due to API inconsistencies need to call different methods on SPI -#if defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) SPI.writeBytes(_sendBuffer, _sizeSendBuffer); #else SPI.transfer(_sendBuffer, _sizeSendBuffer); diff --git a/src/internal/NeoEsp8266BitBangMethod.h b/src/internal/NeoEspBitBangMethod.h similarity index 73% rename from src/internal/NeoEsp8266BitBangMethod.h rename to src/internal/NeoEspBitBangMethod.h index cefc4ee1..96a8a3f0 100644 --- a/src/internal/NeoEsp8266BitBangMethod.h +++ b/src/internal/NeoEspBitBangMethod.h @@ -1,5 +1,5 @@ /*------------------------------------------------------------------------- -NeoPixel library helper functions for Esp8266. +NeoPixel library helper functions for Esp8266 and Esp32 Written by Michael C. Miller. @@ -26,14 +26,20 @@ License along with NeoPixel. If not, see #pragma once -#ifdef ARDUINO_ARCH_ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) -// due to linker overriding the ICACHE_RAM_ATTR for cpp files, these methods are +// ESP32 doesn't use ICACHE_RAM_ATTR +#ifndef ICACHE_RAM_ATTR +#define ICACHE_RAM_ATTR +#endif + +// for esp8266, due to linker overriding the ICACHE_RAM_ATTR for cpp files, these methods are // moved into a C file so the attribute will be applied correctly +// >> this may have been fixed and is no longer a requirement << extern "C" void ICACHE_RAM_ATTR bitbang_send_pixels_800(uint8_t* pixels, uint8_t* end, uint8_t pin); extern "C" void ICACHE_RAM_ATTR bitbang_send_pixels_400(uint8_t* pixels, uint8_t* end, uint8_t pin); -class NeoEsp8266BitBangSpeed800Kbps +class NeoEspBitBangSpeed800Kbps { public: static void send_pixels(uint8_t* pixels, uint8_t* end, uint8_t pin) @@ -42,7 +48,7 @@ class NeoEsp8266BitBangSpeed800Kbps } }; -class NeoEsp8266BitBangSpeed400Kbps +class NeoEspBitBangSpeed400Kbps { public: static void send_pixels(uint8_t* pixels, uint8_t* end, uint8_t pin) @@ -51,10 +57,10 @@ class NeoEsp8266BitBangSpeed400Kbps } }; -template class NeoEsp8266BitBangMethodBase +template class NeoEspBitBangMethodBase { public: - NeoEsp8266BitBangMethodBase(uint8_t pin, uint16_t pixelCount, size_t elementSize) : + NeoEspBitBangMethodBase(uint8_t pin, uint16_t pixelCount, size_t elementSize) : _pin(pin) { pinMode(pin, OUTPUT); @@ -64,7 +70,7 @@ template class NeoEsp8266BitBangMethodBase memset(_pixels, 0, _sizePixels); } - ~NeoEsp8266BitBangMethodBase() + ~NeoEspBitBangMethodBase() { pinMode(_pin, INPUT); @@ -125,7 +131,21 @@ template class NeoEsp8266BitBangMethodBase uint8_t _pin; // output pin number }; -typedef NeoEsp8266BitBangMethodBase NeoEsp8266BitBang800KbpsMethod; -typedef NeoEsp8266BitBangMethodBase NeoEsp8266BitBang400KbpsMethod; + +#if defined(ARDUINO_ARCH_ESP32) + +typedef NeoEspBitBangMethodBase NeoEsp32BitBang800KbpsMethod; +typedef NeoEspBitBangMethodBase NeoEsp32BitBang400KbpsMethod; + +// Bitbang method is the default method for Esp32 +typedef NeoEsp32BitBang800KbpsMethod Neo800KbpsMethod; +typedef NeoEsp32BitBang400KbpsMethod Neo400KbpsMethod; + +#else + +typedef NeoEspBitBangMethodBase NeoEsp8266BitBang800KbpsMethod; +typedef NeoEspBitBangMethodBase NeoEsp8266BitBang400KbpsMethod; + +#endif #endif \ No newline at end of file diff --git a/src/internal/NeoPixelEsp8266.c b/src/internal/NeoPixelEsp.c similarity index 87% rename from src/internal/NeoPixelEsp8266.c rename to src/internal/NeoPixelEsp.c index 07e1e206..2296e362 100644 --- a/src/internal/NeoPixelEsp8266.c +++ b/src/internal/NeoPixelEsp.c @@ -1,5 +1,5 @@ /*------------------------------------------------------------------------- -NeoPixel library helper functions for Esp8266. +NeoPixel library helper functions for Esp8266 and Esp32. Written by Michael C. Miller. @@ -24,10 +24,17 @@ License along with NeoPixel. If not, see . -------------------------------------------------------------------------*/ -#ifdef ARDUINO_ARCH_ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #include +#if defined(ARDUINO_ARCH_ESP8266) #include +#endif + +// ESP32 doesn't use ICACHE_RAM_ATTR +#ifndef ICACHE_RAM_ATTR +#define ICACHE_RAM_ATTR +#endif inline uint32_t _getCycleCount() { @@ -71,7 +78,11 @@ void ICACHE_RAM_ATTR bitbang_send_pixels_800(uint8_t* pixels, uint8_t* end, uint } while ((cyclesStart - cyclesNext) < CYCLES_800); // set high +#if defined(ARDUINO_ARCH_ESP32) + GPIO.out_w1ts = pinRegister; +#else GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); +#endif // wait for the LOW do @@ -80,7 +91,11 @@ void ICACHE_RAM_ATTR bitbang_send_pixels_800(uint8_t* pixels, uint8_t* end, uint } while ((cyclesNext - cyclesStart) < cyclesBit); // set low +#if defined(ARDUINO_ARCH_ESP32) + GPIO.out_w1tc = pinRegister; +#else GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); +#endif } } while (pixels < end); } @@ -111,8 +126,11 @@ void ICACHE_RAM_ATTR bitbang_send_pixels_400(uint8_t* pixels, uint8_t* end, uint cyclesStart = _getCycleCount(); } while ((cyclesStart - cyclesNext) < CYCLES_400); - // set high +#if defined(ARDUINO_ARCH_ESP32) + GPIO.out_w1ts = pinRegister; +#else GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); +#endif // wait for the LOW do @@ -121,7 +139,11 @@ void ICACHE_RAM_ATTR bitbang_send_pixels_400(uint8_t* pixels, uint8_t* end, uint } while ((cyclesNext - cyclesStart) < cyclesBit); // set low +#if defined(ARDUINO_ARCH_ESP32) + GPIO.out_w1tc = pinRegister; +#else GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); +#endif } } while (pixels < end); }