@@ -102,6 +102,7 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
102
102
self .getPixelColor = self .main_strip .getPixelColor
103
103
self .getPixelColorRGB = self .main_strip .getPixelColorRGB
104
104
self .getPixelColorRGBW = self .main_strip .getPixelColorRGBW
105
+ self .off = self .main_strip .off
105
106
106
107
# Substitute for __del__, traps an exit condition and cleans up properly
107
108
atexit .register (self ._cleanup )
@@ -226,18 +227,54 @@ def __init__(self, strip, first, last=None, num=None):
226
227
def __len__ (self ):
227
228
return self .num
228
229
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
+
229
264
def setPixelColor (self , n , color ):
230
265
"""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.
231
268
"""
232
- self . strip [ self . first + n ] = color
269
+ self [ n ] = color
233
270
234
271
def setPixelColorRGB (self , n , red , green , blue , white = 0 ):
235
272
"""Set LED at position n to the provided red, green, and blue color.
236
273
Each color component should be a value from 0 to 255 (where 0 is the
237
274
lowest intensity and 255 is the highest intensity).
238
275
"""
239
276
# Translation to n done in setPixelColor
240
- self . setPixelColor ( n , Color (red , green , blue , white ) )
277
+ self [ n ] = Color (red , green , blue , white )
241
278
242
279
def getBrightness (self ):
243
280
return ws .ws2811_channel_t_brightness_get (self .strip ._channel )
@@ -254,25 +291,28 @@ def getPixels(self):
254
291
"""Return an object which allows access to the LED display data as if
255
292
it were a sequence of 24-bit RGB values.
256
293
"""
257
- return self . strip [ self . first : self . last ]
294
+ return self [: ]
258
295
259
296
def numPixels (self ):
260
297
"""Return the number of pixels in the strip."""
261
298
return self .num
262
299
263
300
def getPixelColor (self , n ):
264
301
"""Get the 24-bit RGB color value for the LED at position n."""
265
- return self . strip [ self . first + n ]
302
+ return self [ n ]
266
303
267
304
def getPixelColorRGB (self , n ):
268
- return RGBW (self . strip [ self . first + n ])
305
+ return RGBW (self [ n ])
269
306
270
307
def getPixelColorRGBW (self , n ):
271
- return RGBW (self . strip [ self . first + n ])
308
+ return RGBW (self [ n ])
272
309
273
310
def show (self ):
274
311
self .strip .show ()
275
312
313
+ def off (self ):
314
+ self [:] = 0
315
+ self .strip .show ()
276
316
277
317
class InvalidStrip (Exception ):
278
318
pass
0 commit comments