improvements to NeoDib (#159)

include dirty support
This commit is contained in:
Michael Miller
2017-01-26 11:50:29 -08:00
committed by GitHub
parent 2ba00f2bf0
commit 85b250d37c
3 changed files with 79 additions and 16 deletions

View File

@@ -34,10 +34,12 @@ const RgbColor White(255);
const RgbColor Black(0); const RgbColor Black(0);
// define a custom shader object that provides brightness support // define a custom shader object that provides brightness support
class BrightnessShader // based upon the NeoShaderBase
class BrightnessShader : public NeoShaderBase
{ {
public: public:
BrightnessShader(): BrightnessShader():
NeoShaderBase(),
_brightness(255) // default to full bright _brightness(255) // default to full bright
{} {}
@@ -57,6 +59,7 @@ public:
void setBrightness(uint8_t brightness) void setBrightness(uint8_t brightness)
{ {
_brightness = brightness; _brightness = brightness;
Dirty(); // must call dirty when a property changes
} }
// provide an accessor to get brightness // provide an accessor to get brightness
@@ -141,12 +144,15 @@ void loop()
Serial.println(brightness); 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 // need to provide the type of color feature for the strip and
// the type of our custom shader // the type of our custom shader
image.Render<NeoGrbFeature, BrightnessShader>(strip, shader); image.Render<NeoGrbFeature, BrightnessShader>(strip, shader);
// and just show the strip
strip.Show(); strip.Show();
} }

View File

@@ -27,6 +27,9 @@ License along with NeoPixel. If not, see
#include <Arduino.h> #include <Arduino.h>
// '_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/NeoHueBlend.h"
#include "internal/RgbColor.h" #include "internal/RgbColor.h"
@@ -78,8 +81,6 @@ License along with NeoPixel. If not, see
#include "internal/DotStarSpiMethod.h" #include "internal/DotStarSpiMethod.h"
#endif #endif
// '_state' flags for internal state
#define NEO_DIRTY 0x80 // a change was made to pixel data that requires a show
template<typename T_COLOR_FEATURE, typename T_METHOD> class NeoPixelBus template<typename T_COLOR_FEATURE, typename T_METHOD> class NeoPixelBus
{ {
@@ -89,18 +90,21 @@ public:
NeoPixelBus(uint16_t countPixels, uint8_t pin) : NeoPixelBus(uint16_t countPixels, uint8_t pin) :
_countPixels(countPixels), _countPixels(countPixels),
_state(0),
_method(pin, countPixels, T_COLOR_FEATURE::PixelSize) _method(pin, countPixels, T_COLOR_FEATURE::PixelSize)
{ {
} }
NeoPixelBus(uint16_t countPixels, uint8_t pinClock, uint8_t pinData) : NeoPixelBus(uint16_t countPixels, uint8_t pinClock, uint8_t pinData) :
_countPixels(countPixels), _countPixels(countPixels),
_state(0),
_method(pinClock, pinData, countPixels, T_COLOR_FEATURE::PixelSize) _method(pinClock, pinData, countPixels, T_COLOR_FEATURE::PixelSize)
{ {
} }
NeoPixelBus(uint16_t countPixels) : NeoPixelBus(uint16_t countPixels) :
_countPixels(countPixels), _countPixels(countPixels),
_state(0),
_method(countPixels, T_COLOR_FEATURE::PixelSize) _method(countPixels, T_COLOR_FEATURE::PixelSize)
{ {
} }

View File

@@ -25,13 +25,42 @@ License along with NeoPixel. If not, see
-------------------------------------------------------------------------*/ -------------------------------------------------------------------------*/
#pragma once #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<typename T_COLOR_OBJECT> class NeoDib template<typename T_COLOR_OBJECT> class NeoDib
{ {
public: public:
NeoDib(uint16_t countPixels) : NeoDib(uint16_t countPixels) :
_countPixels(countPixels) _countPixels(countPixels),
_state(0)
{ {
_pixels = (T_COLOR_OBJECT*)malloc(PixelsSize()); _pixels = (T_COLOR_OBJECT*)malloc(PixelsSize());
ResetDirty();
} }
~NeoDib() ~NeoDib()
@@ -66,6 +95,7 @@ public:
if (indexPixel < PixelCount()) if (indexPixel < PixelCount())
{ {
_pixels[indexPixel] = color; _pixels[indexPixel] = color;
Dirty();
} }
}; };
@@ -85,24 +115,47 @@ public:
{ {
_pixels[pixel] = color; _pixels[pixel] = color;
} }
Dirty();
}; };
template <typename T_COLOR_FEATURE, typename T_SHADER> void Render(NeoBufferContext<T_COLOR_FEATURE> destBuffer, T_SHADER shader) template <typename T_COLOR_FEATURE, typename T_SHADER> void Render(NeoBufferContext<T_COLOR_FEATURE> destBuffer, T_SHADER& shader)
{ {
uint16_t countPixels = destBuffer.PixelCount(); if (IsDirty() || shader.IsDirty())
if (countPixels > _countPixels)
{ {
countPixels = _countPixels; uint16_t countPixels = destBuffer.PixelCount();
} if (countPixels > _countPixels)
{
countPixels = _countPixels;
}
for (uint16_t indexPixel = 0; indexPixel < countPixels; indexPixel++) for (uint16_t indexPixel = 0; indexPixel < countPixels; indexPixel++)
{ {
T_COLOR_OBJECT color = shader.Apply(indexPixel, _pixels[indexPixel]); T_COLOR_OBJECT color = shader.Apply(indexPixel, _pixels[indexPixel]);
T_COLOR_FEATURE::applyPixelColor(destBuffer.Pixels, indexPixel, color); 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: private:
const uint16_t _countPixels; // Number of RGB LEDs in strip const uint16_t _countPixels; // Number of RGB LEDs in strip
T_COLOR_OBJECT* _pixels; T_COLOR_OBJECT* _pixels;
uint8_t _state; // internal state
}; };