Skip to content

Commit a21bee7

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

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

adafruit_trellism4.py

+43-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,50 @@ 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+
Shows the new colors on the pixels themselves if they haven't already
89+
been autowritten.
90+
91+
Use when ``auto_write`` is set to ``False``.
92+
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``.
106+
107+
.. code-block:: python
108+
109+
import adafruit_trellism4
110+
111+
trellis = adafruit_trellism4.TrellisM4Express()
112+
113+
trellis.pixels.auto_write = False
114+
115+
for x in range(trellis.pixels.width):
116+
for y in range(trellis.pixels.height):
117+
trellis.pixels[x, y] = (0, 255, 0)
118+
119+
trellis.pixels.show() # must call show() when auto_write == False
120+
121+
trellis.pixels.auto_write = True
122+
"""
123+
return self._neopixel.auto_write
124+
125+
@auto_write.setter
126+
def auto_write(self, val):
127+
self._neopixel.auto_write = val
128+
87129
@property
88130
def brightness(self):
89131
"""
@@ -125,7 +167,6 @@ def fill(self, color):
125167
126168
"""
127169
self._neopixel.fill(color)
128-
self._neopixel.show()
129170

130171
@property
131172
def width(self):

0 commit comments

Comments
 (0)