forked from Makuna/NeoPixelBus
@@ -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<NeoGrbFeature, BrightnessShader>(strip, shader);
|
||||
|
||||
// and just show the strip
|
||||
strip.Show();
|
||||
|
||||
}
|
||||
|
||||
|
@@ -27,6 +27,9 @@ License along with NeoPixel. If not, see
|
||||
|
||||
#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/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<typename T_COLOR_FEATURE, typename T_METHOD> 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)
|
||||
{
|
||||
}
|
||||
|
@@ -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<typename T_COLOR_OBJECT> 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 <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 (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
|
||||
};
|
Reference in New Issue
Block a user