Skip to content

Commit 1bfda8c

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

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

adafruit_trellism4.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(self, pin, *, width, height, rotation=0):
6262
width, height = height, width
6363
self._width = width
6464
self._height = height
65+
self._auto_write = True
6566

6667
def __setitem__(self, index, value):
6768
if not isinstance(index, tuple) or len(index) != 2:
@@ -82,8 +83,53 @@ def __setitem__(self, index, value):
8283
raise IndexError("Pixel assignment outside available coordinates.")
8384

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

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

130177
@property
131178
def width(self):

0 commit comments

Comments
 (0)