Skip to content

Commit 92578d4

Browse files
committed
Adding auto_write and show()
1 parent 73eeaec commit 92578d4

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

adafruit_trellism4.py

+44-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
class _NeoPixelArray:
5555
"""Creates a NeoPixel array for use in the ``TrellisM4Express`` class."""
5656
def __init__(self, pin, *, width, height, rotation=0):
57-
self._neopixel = neopixel.NeoPixel(pin, width * height, auto_write=False)
57+
self._neopixel = neopixel.NeoPixel(pin, width * height, auto_write=True)
5858
if rotation % 90 != 0:
5959
raise ValueError("Only 90 degree rotations supported")
6060
self._rotation = rotation % 360
@@ -82,8 +82,51 @@ def __setitem__(self, index, value):
8282
raise IndexError("Pixel assignment outside available coordinates.")
8383

8484
self._neopixel[offset] = value
85+
86+
def show(self):
87+
"""
88+
Pass through to underlying NeoPixel object.
89+
For use when auto_write == False
90+
91+
Shows the new colors on the pixels themselves if they haven't already
92+
been autowritten.
93+
The colors may or may not be showing after this function returns because
94+
it may be done asynchronously.
95+
"""
8596
self._neopixel.show()
8697

98+
@property
99+
def auto_write(self):
100+
"""
101+
True if the neopixels should immediately change when set. If False,
102+
`show` must be called explicitly.
103+
104+
This example disables auto_write, sets every pixel, calls show(), then
105+
re-enables auto-write. trellis.pixels.fill() should really be used here
106+
since all pixels are the same, but this is just for the sake of example.
107+
108+
.. code-block:: python
109+
110+
import adafruit_trellism4
111+
112+
trellis = adafruit_trellism4.TrellisM4Express()
113+
114+
trellis.pixels.auto_write = False
115+
116+
for x in range(trellis.pixels.width):
117+
for y in range(trellis.pixels.height):
118+
trellis.pixels[x, y] = (0, 255, 0)
119+
120+
trellis.pixels.show() # must call show() when auto_write == False
121+
122+
trellis.pixels.auto_write = True
123+
"""
124+
return self._neopixel.auto_write
125+
126+
@auto_write.setter
127+
def auto_write(self, val):
128+
self._neopixel.auto_write = val
129+
87130
@property
88131
def brightness(self):
89132
"""
@@ -125,7 +168,6 @@ def fill(self, color):
125168
126169
"""
127170
self._neopixel.fill(color)
128-
self._neopixel.show()
129171

130172
@property
131173
def width(self):

0 commit comments

Comments
 (0)