functional

This commit is contained in:
Michael Miller
2024-04-09 11:05:45 -07:00
parent 6711f5d823
commit 8698a9eb96
4 changed files with 42 additions and 69 deletions

View File

@@ -41,8 +41,8 @@ License along with NeoPixel. If not, see
template<typename T_COLOR_FEATURE, template<typename T_COLOR_FEATURE,
typename T_METHOD, typename T_METHOD,
typename T_EXPOSED_COLOR_OBJECT, // = T_COLOR_FEATURE::ColorObject, typename T_EXPOSED_COLOR_OBJECT = typename T_COLOR_FEATURE::ColorObject,
typename T_GAMMA = NeoGammaEquationMethod> typename T_GAMMA = NeoGammaTableMethod>
class NeoPixelBusLg class NeoPixelBusLg
{ {
public: public:

View File

@@ -34,12 +34,13 @@ struct Rgbw64Color; // forward declared
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
struct Rgb48Color : RgbColorBase struct Rgb48Color : RgbColorBase
{ {
typedef uint16_t ElementType;
typedef NeoRgbCurrentSettings SettingsObject; typedef NeoRgbCurrentSettings SettingsObject;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Construct a Rgb48Color using R, G, B values (0-65535) // Construct a Rgb48Color using R, G, B values (0-65535)
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Rgb48Color(uint16_t r, uint16_t g, uint16_t b) : Rgb48Color(ElementType r, ElementType g, ElementType b) :
R(r), G(g), B(b) R(r), G(g), B(b)
{ {
}; };
@@ -49,7 +50,7 @@ struct Rgb48Color : RgbColorBase
// This works well for creating gray tone colors // This works well for creating gray tone colors
// (0) = black, (65535) = white, (32768) = gray // (0) = black, (65535) = white, (32768) = gray
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Rgb48Color(uint16_t brightness) : Rgb48Color(ElementType brightness) :
R(brightness), G(brightness), B(brightness) R(brightness), G(brightness), B(brightness)
{ {
}; };
@@ -60,9 +61,9 @@ struct Rgb48Color : RgbColorBase
Rgb48Color(const RgbColor& color) Rgb48Color(const RgbColor& color)
{ {
// x16 = map(x8, 0, 255, 0, 65535); // refactors to just * 257 // x16 = map(x8, 0, 255, 0, 65535); // refactors to just * 257
R = (uint16_t)color.R * 257; // 257 = MAXUINT16/MAXUINT8 = 65535/255 R = (ElementType)color.R * 257; // 257 = MAXUINT16/MAXUINT8 = 65535/255
G = (uint16_t)color.G * 257; G = (ElementType)color.G * 257;
B = (uint16_t)color.B * 257; B = (ElementType)color.B * 257;
}; };
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@@ -125,7 +126,7 @@ struct Rgb48Color : RgbColorBase
// negative - this is less than other // negative - this is less than other
// positive - this is greater than other // positive - this is greater than other
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
int32_t CompareTo(const Rgb48Color& other, uint16_t epsilon = 256) int32_t CompareTo(const Rgb48Color& other, ElementType epsilon = 256)
{ {
return _Compare<Rgb48Color, int32_t>(*this, other, epsilon); return _Compare<Rgb48Color, int32_t>(*this, other, epsilon);
} }
@@ -138,7 +139,7 @@ struct Rgb48Color : RgbColorBase
// negative - left is less than right // negative - left is less than right
// positive - left is greater than right // positive - left is greater than right
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
static int32_t Compare(const Rgb48Color& left, const Rgb48Color& right, uint16_t epsilon = 256) static int32_t Compare(const Rgb48Color& left, const Rgb48Color& right, ElementType epsilon = 256)
{ {
return _Compare<Rgb48Color, int32_t>(left, right, epsilon); return _Compare<Rgb48Color, int32_t>(left, right, epsilon);
} }
@@ -148,7 +149,7 @@ struct Rgb48Color : RgbColorBase
// access elements in order by index rather than R,G,B // access elements in order by index rather than R,G,B
// see static Count for the number of elements // see static Count for the number of elements
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
uint16_t operator[](size_t idx) const ElementType operator[](size_t idx) const
{ {
switch (idx) switch (idx)
{ {
@@ -166,7 +167,7 @@ struct Rgb48Color : RgbColorBase
// access elements in order by index rather than R,G,B // access elements in order by index rather than R,G,B
// see static Count for the number of elements // see static Count for the number of elements
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
uint16_t& operator[](size_t idx) ElementType& operator[](size_t idx)
{ {
switch (idx) switch (idx)
{ {
@@ -183,7 +184,7 @@ struct Rgb48Color : RgbColorBase
// CalculateBrightness will calculate the overall brightness // CalculateBrightness will calculate the overall brightness
// NOTE: This is a simple linear brightness // NOTE: This is a simple linear brightness
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
uint16_t CalculateBrightness() const; ElementType CalculateBrightness() const;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Dim will return a new color that is blended to black with the given ratio // Dim will return a new color that is blended to black with the given ratio
@@ -191,7 +192,7 @@ struct Rgb48Color : RgbColorBase
// //
// NOTE: This is a simple linear blend // NOTE: This is a simple linear blend
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Rgb48Color Dim(uint16_t ratio) const; Rgb48Color Dim(ElementType ratio) const;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Dim will return a new color that is blended to black with the given ratio // Dim will return a new color that is blended to black with the given ratio
@@ -201,7 +202,7 @@ struct Rgb48Color : RgbColorBase
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Rgb48Color Dim(uint8_t ratio) const Rgb48Color Dim(uint8_t ratio) const
{ {
uint16_t expanded = ratio << 8; ElementType expanded = ratio << 8;
return Dim(expanded); return Dim(expanded);
} }
@@ -211,7 +212,7 @@ struct Rgb48Color : RgbColorBase
// //
// NOTE: This is a simple linear blend // NOTE: This is a simple linear blend
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Rgb48Color Brighten(uint16_t ratio) const; Rgb48Color Brighten(ElementType ratio) const;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Brighten will return a new color that is blended to white with the given ratio // Brighten will return a new color that is blended to white with the given ratio
@@ -221,7 +222,7 @@ struct Rgb48Color : RgbColorBase
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Rgb48Color Brighten(uint8_t ratio) const Rgb48Color Brighten(uint8_t ratio) const
{ {
uint16_t expanded = ratio << 8; ElementType expanded = ratio << 8;
return Brighten(expanded); return Brighten(expanded);
} }
@@ -230,14 +231,14 @@ struct Rgb48Color : RgbColorBase
// NOTE: This is a simple linear change // NOTE: This is a simple linear change
// delta - (0-65535) the amount to dim the color // delta - (0-65535) the amount to dim the color
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void Darken(uint16_t delta); void Darken(ElementType delta);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Lighten will adjust the color by the given delta toward white // Lighten will adjust the color by the given delta toward white
// NOTE: This is a simple linear change // NOTE: This is a simple linear change
// delta - (0-65535) the amount to lighten the color // delta - (0-65535) the amount to lighten the color
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void Lighten(uint16_t delta); void Lighten(ElementType delta);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// LinearBlend between two colors by the amount defined by progress variable // LinearBlend between two colors by the amount defined by progress variable
@@ -283,20 +284,21 @@ struct Rgb48Color : RgbColorBase
// Red, Green, Blue color members (0-65535) where // Red, Green, Blue color members (0-65535) where
// (0,0,0) is black and (65535,65535,65535) is white // (0,0,0) is black and (65535,65535,65535) is white
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
uint16_t R; ElementType R;
uint16_t G; ElementType G;
uint16_t B; ElementType B;
const static uint16_t Max = 65535; const static ElementType Max = 65535;
const static size_t Count = 3; // three elements in [] const static size_t Count = 3; // three elements in []
const static size_t Size = Count * sizeof(ElementType);
private: private:
inline static uint16_t _elementDim(uint16_t value, uint16_t ratio) inline static ElementType _elementDim(ElementType value, ElementType ratio)
{ {
return (static_cast<uint32_t>(value) * (static_cast<uint32_t>(ratio) + 1)) >> 16; return (static_cast<uint32_t>(value) * (static_cast<uint32_t>(ratio) + 1)) >> 16;
} }
inline static uint16_t _elementBrighten(uint16_t value, uint16_t ratio) inline static ElementType _elementBrighten(ElementType value, ElementType ratio)
{ {
uint32_t element = ((static_cast<uint32_t>(value) + 1) << 16) / (static_cast<uint32_t>(ratio) + 1); uint32_t element = ((static_cast<uint32_t>(value) + 1) << 16) / (static_cast<uint32_t>(ratio) + 1);

View File

@@ -31,47 +31,20 @@ class Neo3WordFeature :
public NeoWordElements<6, Rgb48Color, uint16_t> public NeoWordElements<6, Rgb48Color, uint16_t>
{ {
public: public:
static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) static void applyPixelColor(uint8_t* pixel, size_t pixelSize, ColorObject color)
{ {
uint8_t* p = getPixelAddress(pPixels, indexPixel); if (PixelSize <= pixelSize)
{
uint8_t* p = pixel;
// due to endianness the byte order must be copied to output // due to endianness the byte order must be copied to output
*p++ = color[V_IC_1] >> 8; *p++ = color[V_IC_1] >> 8;
*p++ = color[V_IC_1] & 0xff; *p++ = color[V_IC_1] & 0xff;
*p++ = color[V_IC_2] >> 8; *p++ = color[V_IC_2] >> 8;
*p++ = color[V_IC_2] & 0xff; *p++ = color[V_IC_2] & 0xff;
*p++ = color[V_IC_3] >> 8; *p++ = color[V_IC_3] >> 8;
*p = color[V_IC_3] & 0xff; *p = color[V_IC_3] & 0xff;
} }
static ColorObject retrievePixelColor(const uint8_t* pPixels, uint16_t indexPixel)
{
ColorObject color;
const uint8_t* p = getPixelAddress(pPixels, indexPixel);
// due to endianness the byte order must be copied to output
color[V_IC_1] = (static_cast<uint16_t>(*p++) << 8);
color[V_IC_1] |= *p++;
color[V_IC_2] = (static_cast<uint16_t>(*p++) << 8);
color[V_IC_2] |= *p++;
color[V_IC_3] = (static_cast<uint16_t>(*p++) << 8);
color[V_IC_3] |= *p;
return color;
}
static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel)
{
ColorObject color;
const uint16_t* p = reinterpret_cast<const uint16_t*>(getPixelAddress(reinterpret_cast<const uint8_t*>(pPixels), indexPixel));
// PROGMEM unit of storage expected to be the same size as color element
// so no endianness issues to worry about
color[V_IC_1] = pgm_read_word(p++);
color[V_IC_2] = pgm_read_word(p++);
color[V_IC_3] = pgm_read_word(p);
return color;
} }
}; };

View File

@@ -219,12 +219,12 @@ public:
const size_t sendDataSize = T_COLOR_FEATURE::SettingsSize >= T_COLOR_FEATURE::PixelSize ? T_COLOR_FEATURE::SettingsSize : T_COLOR_FEATURE::PixelSize; const size_t sendDataSize = T_COLOR_FEATURE::SettingsSize >= T_COLOR_FEATURE::PixelSize ? T_COLOR_FEATURE::SettingsSize : T_COLOR_FEATURE::PixelSize;
uint8_t sendData[sendDataSize]; uint8_t sendData[sendDataSize];
noInterrupts(); // Need 100% focus on instruction timing
// if there are settings at the front // if there are settings at the front
if (T_COLOR_FEATURE::applyFrontSettings(sendData, sendDataSize, featureSettings)) if (T_COLOR_FEATURE::applyFrontSettings(sendData, sendDataSize, featureSettings))
{ {
noInterrupts(); // Need 100% focus on instruction timing
T_SPEED::send_data(sendData, T_COLOR_FEATURE::SettingsSize, _port, _pinMask); T_SPEED::send_data(sendData, T_COLOR_FEATURE::SettingsSize, _port, _pinMask);
interrupts();
} }
// send primary color data // send primary color data
@@ -236,9 +236,7 @@ public:
typename T_COLOR_FEATURE::ColorObject color = shader.Apply(*pixel); typename T_COLOR_FEATURE::ColorObject color = shader.Apply(*pixel);
T_COLOR_FEATURE::applyPixelColor(sendData, T_COLOR_FEATURE::PixelSize, color); T_COLOR_FEATURE::applyPixelColor(sendData, T_COLOR_FEATURE::PixelSize, color);
noInterrupts(); // Need 100% focus on instruction timing
T_SPEED::send_data(sendData, T_COLOR_FEATURE::PixelSize, _port, _pinMask); T_SPEED::send_data(sendData, T_COLOR_FEATURE::PixelSize, _port, _pinMask);
interrupts();
pixel++; pixel++;
} }
@@ -246,11 +244,11 @@ public:
// if there are settings at the back // if there are settings at the back
if (T_COLOR_FEATURE::applyBackSettings(sendData, sendDataSize, featureSettings)) if (T_COLOR_FEATURE::applyBackSettings(sendData, sendDataSize, featureSettings))
{ {
noInterrupts(); // Need 100% focus on instruction timing
T_SPEED::send_data(sendData, T_COLOR_FEATURE::SettingsSize, _port, _pinMask); T_SPEED::send_data(sendData, T_COLOR_FEATURE::SettingsSize, _port, _pinMask);
interrupts();
} }
interrupts();
// save EOD time for latch on next call // save EOD time for latch on next call
_endTime = micros(); _endTime = micros();
} }