From 164c5a072f5f2458169707d850ed828d4c3fd1d0 Mon Sep 17 00:00:00 2001 From: David Greaves Date: Sat, 10 Sep 2022 19:26:36 +0100 Subject: [PATCH] Add off() and let SetColorXX() handle slices of colors too. Signed-off-by: David Greaves --- library/rpi_ws281x/rpi_ws281x.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/library/rpi_ws281x/rpi_ws281x.py b/library/rpi_ws281x/rpi_ws281x.py index efaa09e..dfd3d6a 100644 --- a/library/rpi_ws281x/rpi_ws281x.py +++ b/library/rpi_ws281x/rpi_ws281x.py @@ -102,6 +102,7 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False, self.getPixelColor = self.main_strip.getPixelColor self.getPixelColorRGB = self.main_strip.getPixelColorRGB self.getPixelColorRGBW = self.main_strip.getPixelColorRGBW + self.off = self.main_strip.off # Substitute for __del__, traps an exit condition and cleans up properly atexit.register(self._cleanup) @@ -224,8 +225,14 @@ def __len__(self): def setPixelColor(self, n, color): """Set LED at position n to the provided 24-bit color value (in RGB order). + If n is a slice then color can be a value which is repeated for all leds + or a slice of values which are applied to the leds. """ - self.strip[self.first + n] = color + if isinstance(n, slice): + n = slice(n.start + self.first, n.stop + self.first, n.step) + else: + n = self.first + n + self.strip[n] = color def setPixelColorRGB(self, n, red, green, blue, white=0): """Set LED at position n to the provided red, green, and blue color. @@ -269,6 +276,10 @@ def getPixelColorRGBW(self, n): def show(self): self.strip.show() + def off(self): + self.strip[self.first:self.last] = 0 + self.strip.show() + # Shim for back-compatibility class Adafruit_NeoPixel(PixelStrip): pass