Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
// Main.cpp
//
// This file is the main entry point for the NDSCPP LED Matrix Server application.
// It creates a Canvas, adds a GreenFillEffect to it, and then enters a loop where it
// renders the effect to the canvas, compresses the data, and sends it to the LED
// matrix via a SocketChannel. The program will continue to run until it receives
// a SIGINT signal (Ctrl-C).

// Main.cpp
//
// This file is the main entry point for the NDSCPP LED Matrix Server application.
Expand Down Expand Up @@ -123,6 +115,7 @@ int main(int argc, char *argv[])
signal(SIGINT, HandleSignal);
signal(SIGTERM, HandleSignal);


ptrController->Connect();
ptrController->Start(true); // Consider if effect managers want to run

Expand Down
50 changes: 17 additions & 33 deletions pixeltypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ constexpr uint8_t lerp8by8(uint8_t a, uint8_t b, fract8 frac)

/// Forward declaration of hsv2rgb_rainbow here,
/// to avoid circular dependencies.
inline void hsv2rgb_rainbow(const CHSV & hsv, CRGB & rgb, bool fast);
inline void hsv2rgb_rainbow(const CHSV & hsv, CRGB & rgb, bool fast = true);

typedef enum
{
Expand Down Expand Up @@ -421,10 +421,9 @@ struct CRGB

constexpr CRGB blendWith(const CRGB &other, double blendFactor) const
{
return CRGB(
static_cast<uint8_t>(r * (1 - blendFactor) + other.r * blendFactor),
static_cast<uint8_t>(g * (1 - blendFactor) + other.g * blendFactor),
static_cast<uint8_t>(b * (1 - blendFactor) + other.b * blendFactor));
if (blendFactor <= 0.0) return *this;
if (blendFactor >= 1.0) return other;
return lerp8(other, static_cast<uint8_t>(blendFactor * 255.0));
}

/// Allow assignment from red, green, and blue
Expand Down Expand Up @@ -477,34 +476,19 @@ struct CRGB

static CRGB HSV2RGB(double h, double s = 1.0, double v = 1.0)
{
h = fmod(h, 360.0); // Ensure h is in the range [0, 360)
if (h < 0)
h += 360.0;
// Normalize h to [0, 1) range for hue conversion
double h_norm = h / 360.0;
h_norm -= floor(h_norm);
if (h_norm < 0) h_norm += 1.0;

uint8_t hi = static_cast<uint8_t>(h / 60.0) % 6;
double f = (h / 60.0) - hi;
uint8_t p = static_cast<uint8_t>(v * (1.0 - s) * 255);
uint8_t q = static_cast<uint8_t>(v * (1.0 - f * s) * 255);
uint8_t t = static_cast<uint8_t>(v * (1.0 - (1.0 - f) * s) * 255);
uint8_t vByte = static_cast<uint8_t>(v * 255);
CHSV hsv(
static_cast<uint8_t>(h_norm * 255.0),
static_cast<uint8_t>(s * 255.0),
static_cast<uint8_t>(v * 255.0));

switch (hi)
{
case 0:
return CRGB(vByte, t, p);
case 1:
return CRGB(q, vByte, p);
case 2:
return CRGB(p, vByte, t);
case 3:
return CRGB(p, q, vByte);
case 4:
return CRGB(t, p, vByte);
case 5:
return CRGB(vByte, p, q);
default:
return CRGB(0, 0, 0); // Should never reach here
}
CRGB rgb;
hsv2rgb_rainbow(hsv, rgb, true);
return rgb;
}

/// Add one CRGB to another, saturating at 0xFF for each channel
Expand Down Expand Up @@ -1267,7 +1251,7 @@ constexpr array<uint8_t, 256> HSV_SINE_TABLE =
// The "fast" flag causes the code to use a minimal (and thus faster)
// set of transitions. I think Ken Fishkin originally came up with it.

inline void hsv2rgb_rainbow(const CHSV & hsv, CRGB & rgb, bool fast = false)
inline void hsv2rgb_rainbow(const CHSV & hsv, CRGB & rgb, bool fast)
{
if (fast)
{
Expand Down Expand Up @@ -1299,7 +1283,7 @@ inline void hsv2rgb_rainbow(const CHSV & hsv, CRGB & rgb, bool fast = false)
}
else
{
// Original smooth floating-point conversion
// Smooth floating-point conversion
float h = hsv.h * (6.0f / 256.0f);
float s = hsv.s * (1.0f / 255.0f);
float v = hsv.v * (1.0f / 255.0f);
Expand Down
53 changes: 17 additions & 36 deletions utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,49 +51,30 @@ class Utilities

static vector<uint8_t> ConvertPixelsToByteArray(const vector<CRGB> &pixels, bool reversed, bool redGreenSwap)
{
vector<uint8_t> byteArray(pixels.size() * 3); // Allocate space upfront

// This code makes all kinds of assumptions that CRGB is three RGB bytes, so let's assert that fact
static_assert(sizeof(CRGB) == 3);

size_t index = 0;
vector<uint8_t> byteArray(pixels.size() * sizeof(CRGB));

if (reversed)
if (!reversed && !redGreenSwap)
{
for (auto it = pixels.rbegin(); it != pixels.rend(); ++it)
{
if (redGreenSwap)
{
byteArray[index++] = it->g;
byteArray[index++] = it->r;
byteArray[index++] = it->b;
}
else
{
byteArray[index++] = it->r;
byteArray[index++] = it->g;
byteArray[index++] = it->b;
}
}
memcpy(byteArray.data(), pixels.data(), pixels.size() * sizeof(CRGB));
return byteArray;
}
else

size_t index = 0;
auto writePixel = [&](const CRGB &pixel)
{
byteArray[index++] = redGreenSwap ? pixel.g : pixel.r;
byteArray[index++] = redGreenSwap ? pixel.r : pixel.g;
byteArray[index++] = pixel.b;
};

if (reversed)
for (auto it = pixels.rbegin(); it != pixels.rend(); ++it)
writePixel(*it);
else
for (const auto &pixel : pixels)
{
if (redGreenSwap)
{
byteArray[index++] = pixel.g;
byteArray[index++] = pixel.r;
byteArray[index++] = pixel.b;
}
else
{
byteArray[index++] = pixel.r;
byteArray[index++] = pixel.g;
byteArray[index++] = pixel.b;
}
}
}
writePixel(pixel);

return byteArray;
}
Expand Down