Skip to content

Commit c18f162

Browse files
committed
Add createPixelSubStrip to allow access to PixelSubStrips
Split for easier reviewing Signed-off-by: David Greaves <[email protected]>
1 parent b1bf4de commit c18f162

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

library/rpi_ws281x/rpi_ws281x.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,29 @@ def show(self):
173173
str_resp = ws.ws2811_get_return_t_str(resp)
174174
raise RuntimeError('ws2811_render failed with code {0} ({1})'.format(resp, str_resp))
175175

176+
def createPixelSubStrip(self, first, last=None, num=None):
177+
"""Create a PixelSubStrip starting with pixel `first`
178+
Either specify the `num` of pixels or the `last` pixel.
179+
180+
All the methods of a PixelSubStrip are available on PixelStrip
181+
objects.
182+
183+
Note: PixelSubStrips are not prevented from overlappping
184+
"""
185+
if last:
186+
if last > len(self):
187+
raise self.InvalidStrip(f"Too many pixels ({last})."
188+
f"Strip only has {len(self)}.")
189+
return self.PixelSubStrip(self, first, last=last)
190+
if num:
191+
if first + num > len(self):
192+
raise self.InvalidStrip(f"Too many pixels ({first + num})."
193+
f"Strip only has {len(self)}.")
194+
return self.PixelSubStrip(self, first, num=num)
195+
raise self.InvalidStrip("Need num or last to create a PixelSubStrip")
196+
197+
class InvalidStrip(Exception):
198+
pass
176199

177200
class PixelSubStrip:
178201
"""A PixelSubStrip handles a subset of the pixels in a PixelStrip

0 commit comments

Comments
 (0)