rpi-ws281x/rpi-ws281x-python

Setting pixel colors at once

Viicos opened this issue · 4 comments

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:

if isinstance(pos, slice):
index = 0
for n in xrange(*pos.indices(self.size)):
ws.ws2811_led_set(self.channel, n, value[index])
index += 1

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.

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

So we could use indexing/slicing on PixelStrip directly?

Exactly, I think that makes sense?

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).