Skip to content

Commit eb6e167

Browse files
committed
Make PixelSubStrip a subclass (whitespace only)
Signed-off-by: David Greaves <[email protected]>
1 parent 097a34f commit eb6e167

File tree

1 file changed

+83
-83
lines changed

1 file changed

+83
-83
lines changed

library/rpi_ws281x/rpi_ws281x.py

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -174,93 +174,93 @@ def show(self):
174174
raise RuntimeError('ws2811_render failed with code {0} ({1})'.format(resp, str_resp))
175175

176176

177-
class PixelSubStrip:
178-
"""A PixelSubStrip handles a subset of the pixels in a PixelStrip
177+
class PixelSubStrip:
178+
"""A PixelSubStrip handles a subset of the pixels in a PixelStrip
179179
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
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
183183
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-
218-
def setPixelColor(self, n, color):
219-
"""Set LED at position n to the provided 24-bit color value (in RGB order).
220-
"""
221-
self.strip[self.first + n] = color
222-
223-
def setPixelColorRGB(self, n, red, green, blue, white=0):
224-
"""Set LED at position n to the provided red, green, and blue color.
225-
Each color component should be a value from 0 to 255 (where 0 is the
226-
lowest intensity and 255 is the highest intensity).
227-
"""
228-
# Translation to n done in setPixelColor
229-
self.setPixelColor(n, Color(red, green, blue, white))
230-
231-
def getBrightness(self):
232-
return ws.ws2811_channel_t_brightness_get(self.strip._channel)
233-
234-
def setBrightness(self, brightness):
235-
"""Scale each LED in the buffer by the provided brightness. A brightness
236-
of 0 is the darkest and 255 is the brightest.
237-
238-
This method affects all pixels in all PixelSubStrips.
239-
"""
240-
ws.ws2811_channel_t_brightness_set(self.strip._channel, brightness)
241-
242-
def getPixels(self):
243-
"""Return an object which allows access to the LED display data as if
244-
it were a sequence of 24-bit RGB values.
184+
strip2[5] will access the 15th pixel
245185
"""
246-
return self.strip[self.first:self.last]
247-
248-
def numPixels(self):
249-
"""Return the number of pixels in the strip."""
250-
return self.num
251186

252-
def getPixelColor(self, n):
253-
"""Get the 24-bit RGB color value for the LED at position n."""
254-
return self.strip[self.first + n]
255-
256-
def getPixelColorRGB(self, n):
257-
return RGBW(self.strip[self.first + n])
258-
259-
def getPixelColorRGBW(self, n):
260-
return RGBW(self.strip[self.first + n])
261-
262-
def show(self):
263-
self.strip.show()
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+
218+
def setPixelColor(self, n, color):
219+
"""Set LED at position n to the provided 24-bit color value (in RGB order).
220+
"""
221+
self.strip[self.first + n] = color
222+
223+
def setPixelColorRGB(self, n, red, green, blue, white=0):
224+
"""Set LED at position n to the provided red, green, and blue color.
225+
Each color component should be a value from 0 to 255 (where 0 is the
226+
lowest intensity and 255 is the highest intensity).
227+
"""
228+
# Translation to n done in setPixelColor
229+
self.setPixelColor(n, Color(red, green, blue, white))
230+
231+
def getBrightness(self):
232+
return ws.ws2811_channel_t_brightness_get(self.strip._channel)
233+
234+
def setBrightness(self, brightness):
235+
"""Scale each LED in the buffer by the provided brightness. A brightness
236+
of 0 is the darkest and 255 is the brightest.
237+
238+
This method affects all pixels in all PixelSubStrips.
239+
"""
240+
ws.ws2811_channel_t_brightness_set(self.strip._channel, brightness)
241+
242+
def getPixels(self):
243+
"""Return an object which allows access to the LED display data as if
244+
it were a sequence of 24-bit RGB values.
245+
"""
246+
return self.strip[self.first:self.last]
247+
248+
def numPixels(self):
249+
"""Return the number of pixels in the strip."""
250+
return self.num
251+
252+
def getPixelColor(self, n):
253+
"""Get the 24-bit RGB color value for the LED at position n."""
254+
return self.strip[self.first + n]
255+
256+
def getPixelColorRGB(self, n):
257+
return RGBW(self.strip[self.first + n])
258+
259+
def getPixelColorRGBW(self, n):
260+
return RGBW(self.strip[self.first + n])
261+
262+
def show(self):
263+
self.strip.show()
264264

265265

266266
class InvalidStrip(Exception):

0 commit comments

Comments
 (0)