@@ -62,6 +62,7 @@ def __init__(self, pin, *, width, height, rotation=0):
62
62
width , height = height , width
63
63
self ._width = width
64
64
self ._height = height
65
+ self ._auto_write = True
65
66
66
67
def __setitem__ (self , index , value ):
67
68
if not isinstance (index , tuple ) or len (index ) != 2 :
@@ -82,8 +83,53 @@ def __setitem__(self, index, value):
82
83
raise IndexError ("Pixel assignment outside available coordinates." )
83
84
84
85
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
+ """
85
99
self ._neopixel .show ()
86
100
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
+
87
133
@property
88
134
def brightness (self ):
89
135
"""
@@ -125,7 +171,8 @@ def fill(self, color):
125
171
126
172
"""
127
173
self ._neopixel .fill (color )
128
- self ._neopixel .show ()
174
+ if self ._auto_write :
175
+ self ._neopixel .show ()
129
176
130
177
@property
131
178
def width (self ):
0 commit comments