Skip to content

Commit cf4a1fe

Browse files
committed
Make generics to help with vertical and horizontal lines.
1 parent 5bd6bfe commit cf4a1fe

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

adafruit_led_animation/helper.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,85 @@ def auto_write(self):
165165
def auto_write(self, value):
166166
self._pixels.auto_write = value
167167

168+
@classmethod
169+
def vertical_lines(cls, pixels, width, height, gridmapper):
170+
"""
171+
Generate a PixelMap of horizontal lines on a strip arranged in a grid.
172+
173+
:param pixels: pixel object
174+
:param width: width of grid
175+
:param height: height of grid
176+
:param gridmapper: a function to map x and y coordinates to the grid
177+
see vertical_strip_gridmap and horizontal_strip_gridmap
178+
:return: PixelMap
179+
180+
Example: Vertical lines on a 32x8 grid with the pixel rows oriented vertically,
181+
alternating direction every row.
182+
183+
.. code-block:: python
184+
PixelMap.vertical_lines(pixels, 32, 8, vertical_strip_gridmap(8))
185+
186+
"""
187+
if len(pixels) < width * height:
188+
raise ValueError("number of pixels is less than width x height")
189+
mapping = []
190+
for x in range(width):
191+
mapping.append([gridmapper(x, y) for y in range(height)])
192+
return cls(pixels, mapping, individual_pixels=True)
193+
194+
@classmethod
195+
def horizontal_lines(cls, pixels, width, height, gridmapper):
196+
"""
197+
Generate a PixelMap of horizontal lines on a strip arranged in a grid.
198+
199+
:param pixels: pixel object
200+
:param width: width of grid
201+
:param height: height of grid
202+
:param gridmapper: a function to map x and y coordinates to the grid
203+
see vertical_strip_gridmap and horizontal_strip_gridmap
204+
:return: PixelMap
205+
206+
Example: Horizontal lines on a 16x16 grid with the pixel rows oriented vertically,
207+
alternating direction every row.
208+
209+
.. code-block:: python
210+
PixelMap.horizontal_lines(pixels, 16, 16, vertical_strip_gridmap(16))
211+
"""
212+
if len(pixels) < width * height:
213+
raise ValueError("number of pixels is less than width x height")
214+
mapping = []
215+
for y in range(height):
216+
mapping.append([gridmapper(x, y) for x in range(width)])
217+
return cls(pixels, mapping, individual_pixels=True)
218+
219+
220+
def vertical_strip_gridmap(height, alternating=True):
221+
"""
222+
Returns a function that determines the pixel number for a grid with strips arranged vertically.
223+
:param height: strip height in pixels
224+
:param alternating: strips alternate directions in a zigzag
225+
:return: mapper(x, y)
226+
"""
227+
def mapper(x, y):
228+
if alternating and x % 2:
229+
return x * height + (height - 1 - y)
230+
return x * height + y
231+
return mapper
232+
233+
234+
def horizontal_strip_gridmap(width, alternating=True):
235+
"""
236+
Determines the pixel number for a grid with strips arranged horizontally.
237+
:param width: strip width in pixels
238+
:param alternating: strips alternate directions in a zigzag
239+
:return: mapper(x, y)
240+
"""
241+
def mapper(x, y):
242+
if alternating and y % 2:
243+
return y * width + (width - 1 - x)
244+
return y * width + x
245+
return mapper
246+
168247

169248
class PixelSubset:
170249
"""

0 commit comments

Comments
 (0)