diff --git a/examples/NeoPixelDibTest/NeoPixelDibTest.ino b/examples/NeoPixelDibTest/NeoPixelDibTest.ino index 83fbfdc..1a13c13 100644 --- a/examples/NeoPixelDibTest/NeoPixelDibTest.ino +++ b/examples/NeoPixelDibTest/NeoPixelDibTest.ino @@ -34,10 +34,12 @@ const RgbColor White(255); const RgbColor Black(0); // define a custom shader object that provides brightness support -class BrightnessShader +// based upon the NeoShaderBase +class BrightnessShader : public NeoShaderBase { public: BrightnessShader(): + NeoShaderBase(), _brightness(255) // default to full bright {} @@ -57,6 +59,7 @@ public: void setBrightness(uint8_t brightness) { _brightness = brightness; + Dirty(); // must call dirty when a property changes } // provide an accessor to get brightness @@ -141,12 +144,15 @@ void loop() Serial.println(brightness); - // render the image using the shader + + // 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); - - // and just show the strip strip.Show(); + } diff --git a/src/NeoPixelBus.h b/src/NeoPixelBus.h index c886b73..4d43a11 100644 --- a/src/NeoPixelBus.h +++ b/src/NeoPixelBus.h @@ -27,6 +27,9 @@ License along with NeoPixel. If not, see #include +// '_state' flags for internal state +#define NEO_DIRTY 0x80 // a change was made to pixel data that requires a show + #include "internal/NeoHueBlend.h" #include "internal/RgbColor.h" @@ -78,8 +81,6 @@ License along with NeoPixel. If not, see #include "internal/DotStarSpiMethod.h" #endif -// '_state' flags for internal state -#define NEO_DIRTY 0x80 // a change was made to pixel data that requires a show template class NeoPixelBus { @@ -89,18 +90,21 @@ public: NeoPixelBus(uint16_t countPixels, uint8_t pin) : _countPixels(countPixels), + _state(0), _method(pin, countPixels, T_COLOR_FEATURE::PixelSize) { } NeoPixelBus(uint16_t countPixels, uint8_t pinClock, uint8_t pinData) : _countPixels(countPixels), + _state(0), _method(pinClock, pinData, countPixels, T_COLOR_FEATURE::PixelSize) { } NeoPixelBus(uint16_t countPixels) : _countPixels(countPixels), + _state(0), _method(countPixels, T_COLOR_FEATURE::PixelSize) { } diff --git a/src/internal/NeoDib.h b/src/internal/NeoDib.h index ddbd140..ef69ce6 100644 --- a/src/internal/NeoDib.h +++ b/src/internal/NeoDib.h @@ -25,13 +25,42 @@ License along with NeoPixel. If not, see -------------------------------------------------------------------------*/ #pragma once +class NeoShaderBase +{ +public: + NeoShaderBase() : + _state(0) + { + } + + bool IsDirty() const + { + return (_state & NEO_DIRTY); + }; + + void Dirty() + { + _state |= NEO_DIRTY; + }; + + void ResetDirty() + { + _state &= ~NEO_DIRTY; + }; + +protected: + uint8_t _state; // internal state +}; + template class NeoDib { public: NeoDib(uint16_t countPixels) : - _countPixels(countPixels) + _countPixels(countPixels), + _state(0) { _pixels = (T_COLOR_OBJECT*)malloc(PixelsSize()); + ResetDirty(); } ~NeoDib() @@ -66,6 +95,7 @@ public: if (indexPixel < PixelCount()) { _pixels[indexPixel] = color; + Dirty(); } }; @@ -85,24 +115,47 @@ public: { _pixels[pixel] = color; } + Dirty(); }; - template void Render(NeoBufferContext destBuffer, T_SHADER shader) + template void Render(NeoBufferContext destBuffer, T_SHADER& shader) { - uint16_t countPixels = destBuffer.PixelCount(); - if (countPixels > _countPixels) + if (IsDirty() || shader.IsDirty()) { - countPixels = _countPixels; - } + uint16_t countPixels = destBuffer.PixelCount(); + if (countPixels > _countPixels) + { + countPixels = _countPixels; + } - for (uint16_t indexPixel = 0; indexPixel < countPixels; indexPixel++) - { - T_COLOR_OBJECT color = shader.Apply(indexPixel, _pixels[indexPixel]); - T_COLOR_FEATURE::applyPixelColor(destBuffer.Pixels, indexPixel, color); + for (uint16_t indexPixel = 0; indexPixel < countPixels; indexPixel++) + { + T_COLOR_OBJECT color = shader.Apply(indexPixel, _pixels[indexPixel]); + T_COLOR_FEATURE::applyPixelColor(destBuffer.Pixels, indexPixel, color); + } + + shader.ResetDirty(); + ResetDirty(); } } + bool IsDirty() const + { + return (_state & NEO_DIRTY); + }; + + void Dirty() + { + _state |= NEO_DIRTY; + }; + + void ResetDirty() + { + _state &= ~NEO_DIRTY; + }; + private: const uint16_t _countPixels; // Number of RGB LEDs in strip T_COLOR_OBJECT* _pixels; + uint8_t _state; // internal state }; \ No newline at end of file