@@ -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,78 @@ 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
+ 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
+
158
202
def setPixelColor (self , n , color ):
159
203
"""Set LED at position n to the provided 24-bit color value (in RGB order).
160
204
"""
161
- self [ n ] = color
205
+ self . strip [ self . first + n ] = color
162
206
163
207
def setPixelColorRGB (self , n , red , green , blue , white = 0 ):
164
208
"""Set LED at position n to the provided red, green, and blue color.
165
209
Each color component should be a value from 0 to 255 (where 0 is the
166
210
lowest intensity and 255 is the highest intensity).
167
211
"""
212
+ # Translation to n done in setPixelColor
168
213
self .setPixelColor (n , Color (red , green , blue , white ))
169
214
170
215
def getBrightness (self ):
171
- return ws .ws2811_channel_t_brightness_get (self ._channel )
216
+ return ws .ws2811_channel_t_brightness_get (self .strip . _channel )
172
217
173
218
def setBrightness (self , brightness ):
174
219
"""Scale each LED in the buffer by the provided brightness. A brightness
175
220
of 0 is the darkest and 255 is the brightest.
221
+
222
+ This method affects all pixels in all PixelSubStrips.
176
223
"""
177
- ws .ws2811_channel_t_brightness_set (self ._channel , brightness )
224
+ ws .ws2811_channel_t_brightness_set (self .strip . _channel , brightness )
178
225
179
226
def getPixels (self ):
180
227
"""Return an object which allows access to the LED display data as if
181
228
it were a sequence of 24-bit RGB values.
182
229
"""
183
- return self [: ]
230
+ return self . strip [ self . first : self . last ]
184
231
185
232
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
188
235
189
236
def getPixelColor (self , n ):
190
237
"""Get the 24-bit RGB color value for the LED at position n."""
191
- return self [ n ]
238
+ return self . strip [ self . first + n ]
192
239
193
240
def getPixelColorRGB (self , n ):
194
- return RGBW (self [ n ])
241
+ return RGBW (self . strip [ self . first + n ])
195
242
196
243
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 ()
198
248
199
249
# Shim for back-compatibility
200
250
class Adafruit_NeoPixel (PixelStrip ):
0 commit comments