Skip to content

Commit 5e1f3dc

Browse files
committed
Add slice handling and an off() method
Signed-off-by: David Greaves <[email protected]>
1 parent 2956dda commit 5e1f3dc

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

library/rpi_ws281x/rpi_ws281x.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
102102
self.getPixelColor = self.main_strip.getPixelColor
103103
self.getPixelColorRGB = self.main_strip.getPixelColorRGB
104104
self.getPixelColorRGBW = self.main_strip.getPixelColorRGBW
105+
self.off = self.main_strip.off
105106

106107
# Substitute for __del__, traps an exit condition and cleans up properly
107108
atexit.register(self._cleanup)
@@ -226,18 +227,54 @@ def __init__(self, strip, first, last=None, num=None):
226227
def __len__(self):
227228
return self.num
228229

230+
def _adjust_pos(self, pos):
231+
# create an adjusted pos, either a slice or index
232+
if isinstance(pos, slice):
233+
if pos.start and pos.start < 0:
234+
apos_start = self.first + self.num + pos.start
235+
else:
236+
apos_start = (0 if pos.start is None else pos.start) + self.first
237+
238+
if pos.stop and pos.stop < 0:
239+
apos_stop = pos.stop + self.last
240+
else:
241+
apos_stop = (self.num if pos.stop is None else pos.stop) + self.first
242+
apos = slice(apos_start,
243+
apos_stop,
244+
pos.step)
245+
return apos
246+
if pos < 0:
247+
return self.num + pos + self.first
248+
return pos + self.first
249+
250+
251+
def __getitem__(self, pos):
252+
"""Return the 24-bit RGB color value at the provided position or slice
253+
of positions.
254+
"""
255+
return self.strip[self._adjust_pos(pos)]
256+
257+
def __setitem__(self, pos, value):
258+
"""Set the 24-bit RGB color value at the provided position or slice of
259+
positions. If value is a slice it is zip()'ed with pos to set as many
260+
leds as there are values.
261+
"""
262+
self.strip[self._adjust_pos(pos)] = value
263+
229264
def setPixelColor(self, n, color):
230265
"""Set LED at position n to the provided 24-bit color value (in RGB order).
266+
If n is a slice then color can be a value which is repeated for all leds
267+
or a slice of values which are applied to the leds.
231268
"""
232-
self.strip[self.first + n] = color
269+
self[n] = color
233270

234271
def setPixelColorRGB(self, n, red, green, blue, white=0):
235272
"""Set LED at position n to the provided red, green, and blue color.
236273
Each color component should be a value from 0 to 255 (where 0 is the
237274
lowest intensity and 255 is the highest intensity).
238275
"""
239276
# Translation to n done in setPixelColor
240-
self.setPixelColor(n, Color(red, green, blue, white))
277+
self[n] = Color(red, green, blue, white)
241278

242279
def getBrightness(self):
243280
return ws.ws2811_channel_t_brightness_get(self.strip._channel)
@@ -254,25 +291,28 @@ def getPixels(self):
254291
"""Return an object which allows access to the LED display data as if
255292
it were a sequence of 24-bit RGB values.
256293
"""
257-
return self.strip[self.first:self.last]
294+
return self[:]
258295

259296
def numPixels(self):
260297
"""Return the number of pixels in the strip."""
261298
return self.num
262299

263300
def getPixelColor(self, n):
264301
"""Get the 24-bit RGB color value for the LED at position n."""
265-
return self.strip[self.first + n]
302+
return self[n]
266303

267304
def getPixelColorRGB(self, n):
268-
return RGBW(self.strip[self.first + n])
305+
return RGBW(self[n])
269306

270307
def getPixelColorRGBW(self, n):
271-
return RGBW(self.strip[self.first + n])
308+
return RGBW(self[n])
272309

273310
def show(self):
274311
self.strip.show()
275312

313+
def off(self):
314+
self[:] = 0
315+
self.strip.show()
276316

277317
class InvalidStrip(Exception):
278318
pass

0 commit comments

Comments
 (0)