From f12afe02126f25000e03ccba57cfa10b0b2b59e6 Mon Sep 17 00:00:00 2001 From: David Greaves Date: Sun, 1 Dec 2024 11:05:46 +0000 Subject: [PATCH 1/2] Ensure that the value passed to ws2811_led_set is an int The caller can't cast this when value is a slice This matters when a Numpy array is being used. Signed-off-by: David Greaves --- library/rpi_ws281x/rpi_ws281x.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/rpi_ws281x/rpi_ws281x.py b/library/rpi_ws281x/rpi_ws281x.py index f9d6a8a..6fbd55e 100644 --- a/library/rpi_ws281x/rpi_ws281x.py +++ b/library/rpi_ws281x/rpi_ws281x.py @@ -110,12 +110,13 @@ def __setitem__(self, pos, value): """ # 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) + 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) From 6f144bb71278d7d90e58f2d39bfdfe637c7b198a Mon Sep 17 00:00:00 2001 From: David Greaves Date: Sun, 1 Dec 2024 11:39:13 +0000 Subject: [PATCH 2/2] Restore support for assigning a list to strip[:] This functionality was lost in commit: f80276b323452f141829c79942f1227d53c3521f Signed-off-by: David Greaves --- library/rpi_ws281x/rpi_ws281x.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/library/rpi_ws281x/rpi_ws281x.py b/library/rpi_ws281x/rpi_ws281x.py index 6fbd55e..bffa6a1 100644 --- a/library/rpi_ws281x/rpi_ws281x.py +++ b/library/rpi_ws281x/rpi_ws281x.py @@ -106,14 +106,19 @@ 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, int(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, int(value))