Skip to content

Commit

Permalink
improvements to NeoDib (#159)
Browse files Browse the repository at this point in the history
include dirty support
  • Loading branch information
Makuna authored Jan 26, 2017
1 parent 2ba00f2 commit 85b250d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 17 deletions.
14 changes: 10 additions & 4 deletions examples/NeoPixelDibTest/NeoPixelDibTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
{}

Expand All @@ -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
Expand Down Expand Up @@ -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();

}

8 changes: 6 additions & 2 deletions src/NeoPixelBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
{
Expand All @@ -89,18 +90,21 @@ template<typename T_COLOR_FEATURE, typename T_METHOD> class NeoPixelBus

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)
{
}
Expand Down
75 changes: 64 additions & 11 deletions src/internal/NeoDib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -66,6 +95,7 @@ template<typename T_COLOR_OBJECT> class NeoDib
if (indexPixel < PixelCount())
{
_pixels[indexPixel] = color;
Dirty();
}
};

Expand All @@ -85,24 +115,47 @@ template<typename T_COLOR_OBJECT> class NeoDib
{
_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)
{
countPixels = _countPixels;
}

for (uint16_t indexPixel = 0; indexPixel < countPixels; indexPixel++)
if (IsDirty() || shader.IsDirty())
{
T_COLOR_OBJECT color = shader.Apply(indexPixel, _pixels[indexPixel]);
T_COLOR_FEATURE::applyPixelColor(destBuffer.Pixels, indexPixel, color);
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);
}

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
};

0 comments on commit 85b250d

Please sign in to comment.