Skip to content

Fix slice assignment and ensure int() cast is done #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions library/rpi_ws281x/rpi_ws281x.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,22 @@ def __getitem__(self, pos):

def __setitem__(self, pos, value):
"""Set the 24-bit RGB color value at the provided position or slice of
positions.
positions. If value is a slice it is zip()'ed with pos to set as many
leds as there are values.
"""
# Handle if a slice of positions are passed in by setting the appropriate
# LED data values to the provided value.
# Cast to int() as value may be a numpy non-int value.
if isinstance(pos, slice):
for n in range(*pos.indices(self.size)):
ws.ws2811_led_set(self._channel, n, value)
try:
for n, c in zip(range(*pos.indices(self.size)), value):
ws.ws2811_led_set(self._channel, n, int(c))
except TypeError:
for n in range(*pos.indices(self.size)):
ws.ws2811_led_set(self._channel, n, int(value))
# Else assume the passed in value is a number to the position.
else:
return ws.ws2811_led_set(self._channel, pos, value)
return ws.ws2811_led_set(self._channel, pos, int(value))

def __len__(self):
return ws.ws2811_channel_t_count_get(self._channel)
Expand Down