Hello, I am currently finishing a WS2812 NeoPixel library which simplifies the process of controlling the addressable LEDs with a variety of functions but I am facing an issue.
The problem is that when sending the bits to the array of WS2812 LEDs (which each take 24-bits, one-byte for each color) both the 0 code and 1 code are being interpreted as 1 codes.
Image referred - from WS2812 documentation
I have troubleshot the library trying to find any possible issues and have narrowed it down to the timing between codes sent to the W2812. As the transfer time window between the codes is nanoseconds:
I implemented port manipulation instead of digitalWrite()
and used bitwise operators instead of bitRead()
all to lower the amount of cycles needed to perform the same function.
However still all the bits received by the LEDs were 1 codes and so all LEDs were at maximum brightness (255) and so white.
My library works as follows:
neoByte[]
is a byte
array storing all values from (0 to 255) of each LED (red, green and blue) in the WS2812. So with 20 LEDs there would be 60 bytes. Which is changed dependant on the functions used.
To update the LEDs the function NeoPixel::NeoUpdate()
is called
void NeoPixel::NeoUpdate() {
for(uint16_t bytes = 0; bytes <= byteLength; bytes++) {
for(uint8_t bitIndex = 0; bitIndex < 8; bitIndex++) {
//NeoBit(bitRead(neoByte[bytes], bitIndex));
NeoBit(neoByte[bytes] & char(pow(2, bitIndex)));
}
}
}
To send each bit individually of the array by using the NeoBit()
function which uses port manipulation and __asm__ __volatile__ ("nop\n\t")
to delay by cycles
Where byteLength
in our example = 59 (60 - 1)
I believe the problem is the for loops and bitwise operation of NeoUpdate()
already exceeding the 0 code length and so are interpreted as 1 codes.
Any help is greatly appreciated as I have not been able to fix the problem yet and suggestions on how to optimise the for loops
Helpful sources:
A video explaining WS2812 LEDs with C
WS2812 documentation