@@ -48,6 +48,9 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
48
48
800khz), dma, the DMA channel to use (default 10), invert, a boolean
49
49
specifying if the signal line should be inverted (default False), and
50
50
channel, the PWM channel to use (defaults to 0).
51
+
52
+ All the methods of a PixelSubStrip are available on PixelStrip
53
+ objects.
51
54
"""
52
55
53
56
if gamma is None :
@@ -89,6 +92,17 @@ def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
89
92
90
93
self .size = num
91
94
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
+
92
106
# Substitute for __del__, traps an exit condition and cleans up properly
93
107
atexit .register (self ._cleanup )
94
108
@@ -126,6 +140,10 @@ def __setitem__(self, pos, value):
126
140
def __len__ (self ):
127
141
return ws .ws2811_channel_t_count_get (self ._channel )
128
142
143
+ def numPixels (self ):
144
+ """Return the number of pixels in the display."""
145
+ return len (self )
146
+
129
147
def _cleanup (self ):
130
148
# Clean up memory used by the library when not needed anymore.
131
149
if self ._leds is not None :
@@ -155,46 +173,98 @@ def show(self):
155
173
str_resp = ws .ws2811_get_return_t_str (resp )
156
174
raise RuntimeError ('ws2811_render failed with code {0} ({1})' .format (resp , str_resp ))
157
175
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
+
187
+ def __init__ (self , strip , first , last = None , num = None ):
188
+ self .strip = strip
189
+ if first < 0 :
190
+ raise InvalidStrip (f"First pixel is negative ({ first } )." )
191
+ if first > len (strip ):
192
+ raise InvalidStrip (f"First pixel is too big ({ first } )."
193
+ f"Strip only has { len (strip )} ." )
194
+ self .first = first
195
+ if last :
196
+ if last < 0 :
197
+ raise InvalidStrip (f"Last pixel is negative ({ last } )." )
198
+ if last > len (strip ):
199
+ raise InvalidStrip (f"Too many pixels ({ last } )."
200
+ f"Strip only has { len (strip )} ." )
201
+ self .last = last
202
+ self .num = last - first
203
+ elif num :
204
+ if num < 0 :
205
+ raise InvalidStrip (f"number of pixels is negative ({ num } )." )
206
+ if first + num > len (strip ):
207
+ raise InvalidStrip (f"Too many pixels (last would be { first + num } )."
208
+ f"Strip only has { len (strip )} ." )
209
+ self .last = first + num
210
+ self .num = num
211
+ else :
212
+ raise InvalidStrip ("Must specify number or last pixel to "
213
+ "create a PixelSubStrip" )
214
+
215
+ def __len__ (self ):
216
+ return self .num
217
+
158
218
def setPixelColor (self , n , color ):
159
219
"""Set LED at position n to the provided 24-bit color value (in RGB order).
160
220
"""
161
- self [ n ] = color
221
+ self . strip [ self . first + n ] = color
162
222
163
223
def setPixelColorRGB (self , n , red , green , blue , white = 0 ):
164
224
"""Set LED at position n to the provided red, green, and blue color.
165
225
Each color component should be a value from 0 to 255 (where 0 is the
166
226
lowest intensity and 255 is the highest intensity).
167
227
"""
228
+ # Translation to n done in setPixelColor
168
229
self .setPixelColor (n , Color (red , green , blue , white ))
169
230
170
231
def getBrightness (self ):
171
- return ws .ws2811_channel_t_brightness_get (self ._channel )
232
+ return ws .ws2811_channel_t_brightness_get (self .strip . _channel )
172
233
173
234
def setBrightness (self , brightness ):
174
235
"""Scale each LED in the buffer by the provided brightness. A brightness
175
236
of 0 is the darkest and 255 is the brightest.
237
+
238
+ This method affects all pixels in all PixelSubStrips.
176
239
"""
177
- ws .ws2811_channel_t_brightness_set (self ._channel , brightness )
240
+ ws .ws2811_channel_t_brightness_set (self .strip . _channel , brightness )
178
241
179
242
def getPixels (self ):
180
243
"""Return an object which allows access to the LED display data as if
181
244
it were a sequence of 24-bit RGB values.
182
245
"""
183
- return self [: ]
246
+ return self . strip [ self . first : self . last ]
184
247
185
248
def numPixels (self ):
186
- """Return the number of pixels in the display ."""
187
- return len ( self )
249
+ """Return the number of pixels in the strip ."""
250
+ return self . num
188
251
189
252
def getPixelColor (self , n ):
190
253
"""Get the 24-bit RGB color value for the LED at position n."""
191
- return self [ n ]
254
+ return self . strip [ self . first + n ]
192
255
193
256
def getPixelColorRGB (self , n ):
194
- return RGBW (self [ n ])
257
+ return RGBW (self . strip [ self . first + n ])
195
258
196
259
def getPixelColorRGBW (self , n ):
197
- return RGBW (self [n ])
260
+ return RGBW (self .strip [self .first + n ])
261
+
262
+ def show (self ):
263
+ self .strip .show ()
264
+
265
+
266
+ class InvalidStrip (Exception ):
267
+ pass
198
268
199
269
# Shim for back-compatibility
200
270
class Adafruit_NeoPixel (PixelStrip ):
0 commit comments