Skip to content

Commit

Permalink
Merge pull request Aircoookie#4419 from DedeHai/PWM_brightness_fix
Browse files Browse the repository at this point in the history
BUGFIX PWM brightness discontinuity (fixes Aircoookie#4396)
  • Loading branch information
DedeHai authored Dec 23, 2024
2 parents 9dad436 + 0ad65f4 commit 8873832
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions wled00/bus_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,19 +563,15 @@ void BusPwm::show() {
const unsigned maxBri = (1<<_depth); // possible values: 16384 (14), 8192 (13), 4096 (12), 2048 (11), 1024 (10), 512 (9) and 256 (8)
[[maybe_unused]] const unsigned bitShift = dithering * 4; // if dithering, _depth is 12 bit but LEDC channel is set to 8 bit (using 4 fractional bits)

// use CIE brightness formula (cubic) to fit (or approximate linearity of) human eye perceived brightness
// the formula is based on 12 bit resolution as there is no need for greater precision
// use CIE brightness formula (linear + cubic) to approximate human eye perceived brightness
// see: https://en.wikipedia.org/wiki/Lightness
unsigned pwmBri = (unsigned)_bri * 100; // enlarge to use integer math for linear response
if (pwmBri < 2040) {
// linear response for values [0-20]
pwmBri = ((pwmBri << 12) + 115043) / 230087; //adding '0.5' before division for correct rounding
} else {
// cubic response for values [21-255]
pwmBri += 4080;
float temp = (float)pwmBri / 29580.0f;
temp = temp * temp * temp * (float)maxBri;
pwmBri = (unsigned)temp; // pwmBri is in range [0-maxBri]
unsigned pwmBri = _bri;
if (pwmBri < 21) { // linear response for values [0-20]
pwmBri = (pwmBri * maxBri + 2300 / 2) / 2300 ; // adding '0.5' before division for correct rounding, 2300 gives a good match to CIE curve
} else { // cubic response for values [21-255]
float temp = float(pwmBri + 41) / float(255 + 41); // 41 is to match offset & slope to linear part
temp = temp * temp * temp * (float)maxBri;
pwmBri = (unsigned)temp; // pwmBri is in range [0-maxBri] C
}

[[maybe_unused]] unsigned hPoint = 0; // phase shift (0 - maxBri)
Expand Down

0 comments on commit 8873832

Please sign in to comment.