Neopixel library too slow?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
hdsjulian
Posts: 10
Joined: Mon Dec 03, 2018 8:29 pm

Neopixel library too slow?

Post by hdsjulian » Wed Dec 19, 2018 11:19 am

I recently tried to build some POV things with an ESP32, micropython and a WS2812 strip.
In the end I found out that WS2812 itself might be to slow, but something else baffled me.
Even if I don't attach any hardware (and WS2812 doesn't give "feedback" to the microcontroller anyway), setting a LED takes approximately 720µs. So in order to set 12 LEDs it'll take me 8.6 µs with zero code around it.
I made a quick example here:
https://pastebin.com/8ss5CEaS
the code prints the average time it takes for the while loop to run, every 10 loops. Apart from that it just sets the LEDs to an arbitrary color.
Any idea why this is so slow? And is there a way to speed this all up? The alternative for me is to restart the whole project and write it in C++ or Arduino, which would kinda suck.
(Again, i know that there's also the hardware restrictions with PWM being slow and everything, that is something i can basically address by using APA102 instead of Neopixel, but as long as the underlying library seems to be the problem i'm a little lost)

OutoftheBOTS_
Posts: 502
Joined: Mon Nov 20, 2017 10:18 am

Re: Neopixel library too slow?

Post by OutoftheBOTS_ » Wed Dec 19, 2018 9:00 pm

WS2812 is a 1 wire protocol so the speed that it is written to is set no matter what language you use to write to it with.

Python will taken a certain time to look up the function your calling to do this writing to the 1 wire. If you make this fuction local istead of global this means the function with stored on the heap instead of the stack and will look up much faster. See this presentation by Damien Goerge https://www.youtube.com/watch?v=hHec4qL00x0&t=1480s

I have used APA102 a bit myself on RPi because they have a clock line and on RPi I used SPI to send the data and found that I could go as fast as 64Mhz before the APA102 didn't respond correctly.

Not sure that you will get much faster with the APA102 because I think your bottle neck is in calling the function not the speed that the function is executed at.

Aukat
Posts: 1
Joined: Wed Dec 26, 2018 12:58 pm

Re: Neopixel library too slow?

Post by Aukat » Wed Dec 26, 2018 1:23 pm

I'm a novice programmer, but I did a little investigation:

I have a 8x16 display of 128 RGB LED units, which amounts to 384 bytes in the neo.buf array. I am using a Wemos D1 mini esp8266.
The build in neo pixel method "fill" will iterate over the entire bytearray, calculating the offset and location of each byte. For me, this takes 99 ms. As you mentioned, updating /writing to the pixels is fast: This takes 8 ms.

If I instead manually fill the bytearray in one go using neo.buf = bytearray(384), it only takes 0.6 ms. That's a factor of 165 faster than the build in function.

I am tinkering a bit with micro python this Christmas, and being able to clear my LEDs is definitely a good start, but I still need a fast way to make custom adjustments. I'

Post Reply