rpi-ws281x / rpi-ws281x-python

Python library wrapping for the rpi-ws281x library
BSD 2-Clause "Simplified" License
319 stars 102 forks source link

Setting pixel colors at once #90

Closed Viicos closed 1 year ago

Viicos commented 1 year ago

As of today, the only possible way (at least to my knowledge) to set multiple pixel colors is by using a for loop:

for i in range(start, stop):
    strip.setPixelColor(i, color)

What setPixelColor is actually doing is:

self._led_data[n] = color

I see that _LED_Data supports slicing:

https://github.com/rpi-ws281x/rpi-ws281x-python/blob/3f7f031b8a87f584fcef72225b7cfb1cff682316/library/rpi_ws281x/rpi_ws281x.py#L47-L51

But it only works this way:

self._led_data[start:end] = [color for _ in range(n)]

I think it would be better to have slicing working the intended way, e.g. self._led_data[start:end] = color, and have a new setPixelsColor method to do so, and the current behaviour could be reproduced with a new method as well, like setPixelsColorMap or something.

@Gadgetoid if you are busy, I'll be happy to make a PR if you think this is a good idea.

Gadgetoid commented 1 year ago

Almost feels like PixelStrip should just subclass _LED_Data which would solve this pretty handily?

Viicos commented 1 year ago

So we could use indexing/slicing on PixelStrip directly?

Gadgetoid commented 1 year ago

Exactly, I think that makes sense?

Viicos commented 1 year ago

Yes, I'll see what I can do, hoping it will be possible without having to change the current API (e.g. setPixelColor and some others might be deprecated then).