Skip to content

Commit

Permalink
Added optional power off time out and gamma correction
Browse files Browse the repository at this point in the history
Benik3 committed Oct 30, 2022
1 parent 2cd032b commit 3f350d7
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions Adalight_ESP/Adalight_ESP.ino
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
//Adalight on ESP8266 with NeoPixelBus library and Uart method for non blocking render of WS2812 and similar.
//You can define smoothing of the frames with variable SMOOTH

#include <NeoPixelBus.h>
#include <chrono>

#define PixelCount 110 // number of leds
#define PixelPin 2 // must be GPIO2 (D4) for ESP8266 Uart Method. It's here just as note.
#define SMOOTH 0 // number of interpolations for smoothing, one interpolation takes around (PixelCount * 30us) + 700us, 0 = no smoothing
#define PWROFTIMEOUT 1 // Number of seconds after which LEDs turn "off" (black) if there are no new data on serial line
#define GAMMA 1 // Use gamma corection from g22 array. The already prepared correction LUT is measured by color calibration device

#define PixelCount 110 //number of leds
#define PixelPin 4 //must be D4 (GPIO2) for ESP8266 Uart Method
#define SERIAL_RX_BUFF ((PixelCount * 3) + 6) * 3 //Set higher UART buffer to prevent overflow, this will set buffer to 3x size of the frame
#define SMOOTH 0 //number of interpolations for smoothing, one interpolation takes around (PixelCount * 30us) + 700us, 0 = no smoothing

#if SMOOTH
RgbColor startBuff[PixelCount], currentBuff[PixelCount], endBuff[PixelCount];
float progress;
#endif

//gamma 2.2 calibration curve
#if GAMMA
const uint8_t g22[] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32, 32, 33, 34, 34, 35, 36, 37, 37, 38, 39, 40, 40, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 50, 51, 52, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 89, 90, 91, 92, 93, 95, 96, 97, 98, 99, 101, 102, 103, 105, 106, 107, 109, 110, 111, 113, 114, 116, 117, 119, 120, 122, 123, 125, 126, 128, 129, 131, 132, 134, 135, 137, 139, 140, 142, 143, 145, 147, 148, 150, 152, 153, 155, 157, 159, 160, 162, 164, 165, 167, 169, 170, 172, 174, 176, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 249, 251, 253, 255};
#endif

// Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
const uint8_t prefix[] = {'A', 'd', 'a'};
uint8_t hi, lo, chk;

NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart1Ws2812xMethod> strip(PixelCount, PixelPin);
NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart1Ws2812xMethod> strip(PixelCount);

void setup()
{
@@ -50,8 +55,19 @@ void setup()
void loop()
{
// wait for the first byte of Magic Word
for (int i = 0; i < sizeof prefix; ++i) {
while (!Serial.available()) ;;
for (uint8_t i = 0; i < sizeof prefix; ++i) {
auto beginTime = std::chrono::high_resolution_clock::now();
while (!Serial.available())
{
#if PWROFTIMEOUT
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - beginTime);
if (elapsed.count() > PWROFTIMEOUT)
{
strip.ClearTo(RgbColor(0, 0, 0));
strip.Show();
}
#endif
}
// Check next byte in Magic Word
if (prefix[i] != Serial.read()) {
i = 0;
@@ -90,9 +106,11 @@ void loop()
RgbColor color = RgbColor::LinearBlend(startBuff[i], endBuff[i], progress);
currentBuff[i] = color;

#if GAMMA
color.R = g22[color.R];
color.G = g22[color.G];
color.B = g22[color.B];
#endif //GAMMA
strip.SetPixelColor(i, color);
}
strip.Show();
@@ -108,19 +126,28 @@ void loop()
if (progress > 1.0) progress = 1.0;
}
} while (Serial.available() < PixelCount);
#else
#else //!SMOOTH
for (uint8_t i = 0; i < PixelCount; i++) {
RgbColor color;
#if GAMMA
while (!Serial.available());
color.R = g22[Serial.read()];
while (!Serial.available());
color.G = g22[Serial.read()];
while (!Serial.available());
color.B = g22[Serial.read()];
#else
while (!Serial.available());
color.R = Serial.read();
while (!Serial.available());
color.G = Serial.read();
while (!Serial.available());
color.B = Serial.read();
#endif //GAMMA
strip.SetPixelColor(i, color);
}
strip.Show();
#endif
#endif //SMOOTH
}
else
{

0 comments on commit 3f350d7

Please sign in to comment.