Skip to content

Commit

Permalink
Remove analog flicker (#678)
Browse files Browse the repository at this point in the history
* remove analog LED flicker

run SetRgbwPwm from main loop and with GetPixelColor(0) to get all effects using fade_out() working.

* correct unintended bitwise AND to logical AND

* Update analogLastShow

* new Arduino Core WaveForm library included

* new Arduino Core only for 8266

* correct formating + define for MQTT_KEEP_ALIVE

* fix for ESP32

* reduce scope of variable "done"

* call analogWrite only if Color or Bri did change

* Remove duplicate wifi sleep code

Co-authored-by: Aircoookie <[email protected]>
  • Loading branch information
Def3nder and Aircoookie authored Feb 22, 2020
1 parent 447594b commit e621fde
Show file tree
Hide file tree
Showing 10 changed files with 464 additions and 37 deletions.
9 changes: 8 additions & 1 deletion wled00/FX.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ class WS2812FX {
resetSegments(),
setPixelColor(uint16_t n, uint32_t c),
setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0),
show(void);
show(void),
setRgbwPwm(void);

bool
reverseMode = false,
Expand Down Expand Up @@ -627,6 +628,12 @@ class WS2812FX {
uint32_t _lastPaletteChange = 0;
uint32_t _lastShow = 0;

#ifdef WLED_USE_ANALOG_LEDS
uint32_t _analogLastShow = 0;
uint32_t _analogLastColor = 0;
uint8_t _analogLastBri = 0;
#endif

uint8_t _segment_index = 0;
uint8_t _segment_index_palette_last = 99;
segment _segments[MAX_NUM_SEGMENTS] = { // SRAM footprint: 24 bytes per element
Expand Down
34 changes: 34 additions & 0 deletions wled00/FX_fcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,40 @@ bool WS2812FX::segmentsAreIdentical(Segment* a, Segment* b)
return true;
}

#ifdef WLED_USE_ANALOG_LEDS
void WS2812FX::setRgbwPwm(void) {
uint32_t nowUp = millis(); // Be aware, millis() rolls over every 49 days
if (nowUp - _analogLastShow < MIN_SHOW_DELAY) return;

_analogLastShow = nowUp;

RgbwColor color = bus->GetPixelColorRgbw(0);
byte b = getBrightness();
if (color == _analogLastColor && b == _analogLastBri) return;

// check color values for Warm / Cold white mix (for RGBW) // EsplanexaDevice.cpp
#ifdef WLED_USE_5CH_LEDS
if (color.R == 255 && color.G == 255 && color.B == 255 && color.W == 255) {
bus->SetRgbwPwm(0, 0, 0, 0, color.W * b / 255);
} else if (color.R == 127 && color.G == 127 && color.B == 127 && color.W == 255) {
bus->SetRgbwPwm(0, 0, 0, color.W * b / 512, color.W * b / 255);
} else if (color.R == 0 && color.G == 0 && color.B == 0 && color.W == 255) {
bus->SetRgbwPwm(0, 0, 0, color.W * b / 255, 0);
} else if (color.R == 130 && color.G == 90 && color.B == 0 && color.W == 255) {
bus->SetRgbwPwm(0, 0, 0, color.W * b / 255, color.W * b / 512);
} else if (color.R == 255 && color.G == 153 && color.B == 0 && color.W == 255) {
bus->SetRgbwPwm(0, 0, 0, color.W * b / 255, 0);
} else { // not only white colors
bus->SetRgbwPwm(color.R * b / 255, color.G * b / 255, color.B * b / 255, color.W * b / 255);
}
#else
bus->SetRgbwPwm(color.R * b / 255, color.G * b / 255, color.B * b / 255, color.W * b / 255);
#endif
}
#else
void WS2812FX::setRgbwPwm() {}
#endif

//gamma 2.4 lookup table used for color correction
const byte gammaT[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand Down
39 changes: 7 additions & 32 deletions wled00/NpbWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#define NpbWrapper_h

//PIN CONFIGURATION
#ifndef LEDPIN
#define LEDPIN 2 //strip pin. Any for ESP32, gpio2 or 3 is recommended for ESP8266 (gpio2/3 are labeled D4/RX on NodeMCU and Wemos)
#endif
//#define USE_APA102 // Uncomment for using APA102 LEDs.
//#define USE_WS2801 // Uncomment for using WS2801 LEDs (make sure you have NeoPixelBus v2.5.6 or newer)
//#define USE_LPD8806 // Uncomment for using LPD8806
Expand Down Expand Up @@ -164,7 +166,7 @@ class NeoPixelWrapper
#endif
}
#else // ESP8266
//init PWM pins - PINs 5,12,13,15 are used with Magic Home LED Controller
//init PWM pins
pinMode(RPIN, OUTPUT);
pinMode(GPIN, OUTPUT);
pinMode(BPIN, OUTPUT);
Expand All @@ -185,9 +187,9 @@ class NeoPixelWrapper
void SetRgbwPwm(uint8_t r, uint8_t g, uint8_t b, uint8_t w, uint8_t w2=0)
{
#ifdef ARDUINO_ARCH_ESP32
ledcWrite(0, r); //RPIN
ledcWrite(1, g); //GPIN
ledcWrite(2, b); //BPIN
ledcWrite(0, r);
ledcWrite(1, g);
ledcWrite(2, b);
switch (_type) {
case NeoPixelType_Grb: break;
#ifdef WLED_USE_5CH_LEDS
Expand All @@ -196,7 +198,7 @@ class NeoPixelWrapper
case NeoPixelType_Grbw: ledcWrite(3, w); break;
#endif
}
#else
#else // ESP8266
analogWrite(RPIN, r);
analogWrite(GPIN, g);
analogWrite(BPIN, b);
Expand Down Expand Up @@ -227,11 +229,6 @@ class NeoPixelWrapper
switch (_type) {
case NeoPixelType_Grb: {
_pGrb->SetPixelColor(indexPixel, RgbColor(color.R,color.G,color.B));
#ifdef WLED_USE_ANALOG_LEDS
if (indexPixel != 0) return; //set analog LEDs from first pixel
byte b = _pGrb->GetBrightness();
SetRgbwPwm(color.R * b / 255, color.G * b / 255, color.B * b / 255, 0);
#endif
}
break;
case NeoPixelType_Grbw: {
Expand All @@ -240,28 +237,6 @@ class NeoPixelWrapper
#else
_pGrbw->SetPixelColor(indexPixel, color);
#endif
#ifdef WLED_USE_ANALOG_LEDS
if (indexPixel != 0) return; //set analog LEDs from first pixel
byte b = _pGrbw->GetBrightness();
// check color values for Warm / Cold white mix (for RGBW) // EsplanexaDevice.cpp
#ifdef WLED_USE_5CH_LEDS
if (color.R == 255 & color.G == 255 && color.B == 255 && color.W == 255) {
SetRgbwPwm(0, 0, 0, 0, color.W * b / 255);
} else if (color.R == 127 & color.G == 127 && color.B == 127 && color.W == 255) {
SetRgbwPwm(0, 0, 0, color.W * b / 512, color.W * b / 255);
} else if (color.R == 0 & color.G == 0 && color.B == 0 && color.W == 255) {
SetRgbwPwm(0, 0, 0, color.W * b / 255, 0);
} else if (color.R == 130 & color.G == 90 && color.B == 0 && color.W == 255) {
SetRgbwPwm(0, 0, 0, color.W * b / 255, color.W * b / 512);
} else if (color.R == 255 & color.G == 153 && color.B == 0 && color.W == 255) {
SetRgbwPwm(0, 0, 0, color.W * b / 255, 0);
} else { // not only white colors
SetRgbwPwm(color.R * b / 255, color.G * b / 255, color.B * b / 255, color.W * b / 255);
}
#else
SetRgbwPwm(color.R * b / 255, color.G * b / 255, color.B * b / 255, color.W * b / 255);
#endif
#endif
}
break;
}
Expand Down
Loading

0 comments on commit e621fde

Please sign in to comment.