@@ -48,6 +48,9 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
4848 800khz), dma, the DMA channel to use (default 10), invert, a boolean
4949 specifying if the signal line should be inverted (default False), and
5050 channel, the PWM channel to use (defaults to 0).
51+
52+ All the methods of a PixelSubStrip are available on PixelStrip
53+ objects.
5154 """
5255
5356 if gamma is None :
@@ -89,6 +92,17 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
8992
9093 self .size = num
9194
95+ # Create a PixelSubStrip and delegate these methods to it
96+ self .main_strip = self .PixelSubStrip (self , 0 , num = num )
97+ self .setPixelColor = self .main_strip .setPixelColor
98+ self .setPixelColorRGB = self .main_strip .setPixelColorRGB
99+ self .setBrightness = self .main_strip .setBrightness
100+ self .getBrightness = self .main_strip .getBrightness
101+ self .getPixels = self .main_strip .getPixels
102+ self .getPixelColor = self .main_strip .getPixelColor
103+ self .getPixelColorRGB = self .main_strip .getPixelColorRGB
104+ self .getPixelColorRGBW = self .main_strip .getPixelColorRGBW
105+
92106 # Substitute for __del__, traps an exit condition and cleans up properly
93107 atexit .register (self ._cleanup )
94108
@@ -126,6 +140,10 @@ def __setitem__(self, pos, value):
126140 def __len__ (self ):
127141 return ws .ws2811_channel_t_count_get (self ._channel )
128142
143+ def numPixels (self ):
144+ """Return the number of pixels in the display."""
145+ return len (self )
146+
129147 def _cleanup (self ):
130148 # Clean up memory used by the library when not needed anymore.
131149 if self ._leds is not None :
@@ -155,46 +173,78 @@ def show(self):
155173 str_resp = ws .ws2811_get_return_t_str (resp )
156174 raise RuntimeError ('ws2811_render failed with code {0} ({1})' .format (resp , str_resp ))
157175
176+
177+ class PixelSubStrip :
178+ """A PixelSubStrip handles a subset of the pixels in a PixelStrip
179+
180+ strip = PixelStrip(...)
181+ strip1 = strip.createPixelSubStrip(0, num=10) # controls first 10 pixels
182+ strip2 = strip.createPixelSubStrip(10, num=10) # controls next 10 pixels
183+
184+ strip2[5] will access the 15th pixel
185+ """
186+ def __init__ (self , strip , first , last = None , num = None ):
187+ self .strip = strip
188+ self .first = first
189+ if last :
190+ self .last = last
191+ self .num = last - first
192+ elif num :
193+ self .last = first + num
194+ self .num = num
195+ else :
196+ raise self .InvalidStrip ("Must specify number or last pixel to "
197+ "create a PixelSubStrip" )
198+
199+ def __len__ (self ):
200+ return self .num
201+
158202 def setPixelColor (self , n , color ):
159203 """Set LED at position n to the provided 24-bit color value (in RGB order).
160204 """
161- self [ n ] = color
205+ self . strip [ self . first + n ] = color
162206
163207 def setPixelColorRGB (self , n , red , green , blue , white = 0 ):
164208 """Set LED at position n to the provided red, green, and blue color.
165209 Each color component should be a value from 0 to 255 (where 0 is the
166210 lowest intensity and 255 is the highest intensity).
167211 """
212+ # Translation to n done in setPixelColor
168213 self .setPixelColor (n , Color (red , green , blue , white ))
169214
170215 def getBrightness (self ):
171- return ws .ws2811_channel_t_brightness_get (self ._channel )
216+ return ws .ws2811_channel_t_brightness_get (self .strip . _channel )
172217
173218 def setBrightness (self , brightness ):
174219 """Scale each LED in the buffer by the provided brightness. A brightness
175220 of 0 is the darkest and 255 is the brightest.
221+
222+ This method affects all pixels in all PixelSubStrips.
176223 """
177- ws .ws2811_channel_t_brightness_set (self ._channel , brightness )
224+ ws .ws2811_channel_t_brightness_set (self .strip . _channel , brightness )
178225
179226 def getPixels (self ):
180227 """Return an object which allows access to the LED display data as if
181228 it were a sequence of 24-bit RGB values.
182229 """
183- return self [: ]
230+ return self . strip [ self . first : self . last ]
184231
185232 def numPixels (self ):
186- """Return the number of pixels in the display ."""
187- return len ( self )
233+ """Return the number of pixels in the strip ."""
234+ return self . num
188235
189236 def getPixelColor (self , n ):
190237 """Get the 24-bit RGB color value for the LED at position n."""
191- return self [ n ]
238+ return self . strip [ self . first + n ]
192239
193240 def getPixelColorRGB (self , n ):
194- return RGBW (self [ n ])
241+ return RGBW (self . strip [ self . first + n ])
195242
196243 def getPixelColorRGBW (self , n ):
197- return RGBW (self [n ])
244+ return RGBW (self .strip [self .first + n ])
245+
246+ def show (self ):
247+ self .strip .show ()
198248
199249# Shim for back-compatibility
200250class Adafruit_NeoPixel (PixelStrip ):
0 commit comments