WS2812 brightness control?

How can I control the brightness of neopixels?

Adafruit library has setBrightness, how does that work?

Maintain the ratio of values between R, G, and B, but reduce the magnitude of the values.

Source code is here : Adafruit_NeoPixel/Adafruit_NeoPixel.cpp at e2d1a72c241c3c681edebc50773766f4b4077f49 · adafruit/Adafruit_NeoPixel · GitHub

Line 2555

public partial class Adafruit_NeoPixel
{
	public void setBrightness(byte b)
	{
	  byte newBrightness = b + 1;
	  if (newBrightness != brightness)
	  { 
		byte c; 
		byte * ptr = pixels;
		byte oldBrightness = brightness - 1;
		ushort scale;
		if (oldBrightness == 0)
		{
			scale = 0;
		}
		else if (b == 255)
		{
			scale = 65535 / oldBrightness;
		}
		else
		{
			scale = (((ushort)newBrightness << 8) - 1) / oldBrightness;
		}
		for (ushort i = 0; i < numBytes; i++)
		{
		  c = ptr;
		  *ptr++= (c * scale) >> 8;
		}
		brightness = newBrightness;
	  }
	}
}

So, ive converted it like so, but i dont think this applies to TinyCLR, not sure how to use it.

This was in our plan, but because hardware doesn’t really support this, so we gave it up. You can scale as above.

What if i drive the power supply (5V) with a mosfet and use PWM? Would that work?

No that would not work

That would not work as the WS2812 has some shift registers that need to be permanently powered.

The code snippet above has me pretty confused. Might make more sense in context, but the use of oldBrightness and newBrightness looks wrong even in isolation.

The Adafruit code uses larger values that are then scaled to the size of a byte and a direct translation of that code may not be compatible with the way TinyCLR is representing the pixel-string data. I was referencing it as an example of color vector scaling.

What you need to port is the concept of scaling the color vector. The R, G, and B values form a vector in three dimensional color space. You want to scale the magnitude of that vector without altering the direction. You don’t need a power solution - you need a math solution.

2 Likes