32
32
33
33
import digitalio
34
34
from neopixel_write import neopixel_write
35
- try :
36
- from pixelbuf import PixelBuf
37
- except :
38
- PixelBuf = None
35
+ from pixelbuf import PixelBuf
39
36
40
37
41
38
__version__ = "0.0.0-auto.0"
42
39
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.git"
43
40
44
41
# Pixel color order constants
45
- RGB = ( 0 , 1 , 2 )
42
+ RGB = PixelBuf . RGB
46
43
"""Red Green Blue"""
47
- GRB = ( 1 , 0 , 2 )
44
+ GRB = PixelBuf . GRB
48
45
"""Green Red Blue"""
49
- RGBW = ( 0 , 1 , 2 , 3 )
46
+ RGBW = PixelBuf . RGB
50
47
"""Red Green Blue White"""
51
- GRBW = ( 1 , 0 , 2 , 3 )
48
+ GRBW = PixelBuf . GRB
52
49
"""Green Red Blue White"""
53
50
54
51
class NeoPixel :
@@ -96,34 +93,18 @@ def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_orde
96
93
self .pin = digitalio .DigitalInOut (pin )
97
94
self .pin .direction = digitalio .Direction .OUTPUT
98
95
self .n = n
99
- if pixel_order is None :
100
- self .order = GRBW
101
- self .bpp = bpp
102
- else :
103
- self .order = pixel_order
104
- self .bpp = len (self .order )
105
- # Set auto_write to False temporarily so brightness setter does _not_
106
- # call show() while in __init__.
107
- self .auto_write = False
108
96
self .auto_write = auto_write
109
- if PixelBuf :
110
- self .buf = PixelBuf (self .n , bytearray (self .n * self .bpp ),
111
- bpp = self .bpp , brightness = brightness ,
112
- rawbuf = bytearray (self .n * self .bpp ))
113
- NeoPixel .__setitem__ = NeoPixel .__setitem_pb__
114
- NeoPixel .__getitem__ = NeoPixel .__getitem_pb__
115
- else :
116
- self .buf = bytearray (self .n * self .bpp )
117
- self .brightness = brightness
97
+ self .bpp = bpp
98
+ self .buf = PixelBuf (self .n , bytearray (self .n * bpp ),
99
+ bpp = self .bpp , brightness = brightness ,
100
+ rawbuf = bytearray (self .n * bpp ),
101
+ byteorder = pixel_order or PixelBuf .BGR )
118
102
119
103
def deinit (self ):
120
104
"""Blank out the NeoPixels and release the pin."""
121
105
for i in range (len (self .buf )):
122
106
self .buf [i ] = 0
123
- if PixelBuf :
124
- neopixel_write (self .pin , self .buf .buf )
125
- else :
126
- neopixel_write (self .pin , self .buf )
107
+ neopixel_write (self .pin , self .buf .buf )
127
108
self .pin .deinit ()
128
109
129
110
def __enter__ (self ):
@@ -135,56 +116,7 @@ def __exit__(self, exception_type, exception_value, traceback):
135
116
def __repr__ (self ):
136
117
return "[" + ", " .join ([str (x ) for x in self ]) + "]"
137
118
138
- def _set_item (self , index , value ):
139
- if index < 0 :
140
- index += len (self )
141
- if index >= self .n or index < 0 :
142
- raise IndexError
143
- offset = index * self .bpp
144
- r = 0
145
- g = 0
146
- b = 0
147
- w = 0
148
- if isinstance (value , int ):
149
- r = value >> 16
150
- g = (value >> 8 ) & 0xff
151
- b = value & 0xff
152
- w = 0
153
- # If all components are the same and we have a white pixel then use it
154
- # instead of the individual components.
155
- if self .bpp == 4 and r == g and g == b :
156
- w = r
157
- r = 0
158
- g = 0
159
- b = 0
160
- elif len (value ) == self .bpp :
161
- if self .bpp == 3 :
162
- r , g , b = value
163
- else :
164
- r , g , b , w = value
165
- self .buf [offset + self .order [0 ]] = r
166
- self .buf [offset + self .order [1 ]] = g
167
- self .buf [offset + self .order [2 ]] = b
168
- if self .bpp == 4 :
169
- self .buf [offset + self .order [3 ]] = w
170
-
171
119
def __setitem__ (self , index , val ):
172
- if isinstance (index , slice ):
173
- start , stop , step = index .indices (len (self .buf ) // self .bpp )
174
- length = stop - start
175
- if step != 0 :
176
- length = math .ceil (length / step )
177
- if len (val ) != length :
178
- raise ValueError ("Slice and input sequence size do not match." )
179
- for val_i , in_i in enumerate (range (start , stop , step )):
180
- self ._set_item (in_i , val [val_i ])
181
- else :
182
- self ._set_item (index , val )
183
-
184
- if self .auto_write :
185
- self .show ()
186
-
187
- def __setitem_pb__ (self , index , val ):
188
120
if isinstance (index , slice ):
189
121
self .buf [index .start :index .stop :index .step ] = val
190
122
else :
@@ -194,21 +126,6 @@ def __setitem_pb__(self, index, val):
194
126
self .show ()
195
127
196
128
def __getitem__ (self , index ):
197
- if isinstance (index , slice ):
198
- out = []
199
- for in_i in range (* index .indices (len (self .buf ) // self .bpp )):
200
- out .append (tuple (self .buf [in_i * self .bpp + self .order [i ]]
201
- for i in range (self .bpp )))
202
- return out
203
- if index < 0 :
204
- index += len (self )
205
- if index >= self .n or index < 0 :
206
- raise IndexError
207
- offset = index * self .bpp
208
- return tuple (self .buf [offset + self .order [i ]]
209
- for i in range (self .bpp ))
210
-
211
- def __getitem_pb__ (self , index ):
212
129
if isinstance (index , slice ):
213
130
return self .buf [index .start :index .stop :index .step ]
214
131
else :
@@ -220,17 +137,12 @@ def __len__(self):
220
137
@property
221
138
def brightness (self ):
222
139
"""Overall brightness of the pixel"""
223
- if PixelBuf :
224
- return self .buf .brightness
225
- return self ._brightness
140
+ return self .buf .brightness
226
141
227
142
@brightness .setter
228
143
def brightness (self , brightness ):
229
144
# pylint: disable=attribute-defined-outside-init
230
- if PixelBuf :
231
- self .buf .brightness = brightness
232
- else :
233
- self ._brightness = min (max (brightness , 0.0 ), 1.0 )
145
+ self .buf .brightness = brightness
234
146
if self .auto_write :
235
147
self .show ()
236
148
@@ -256,9 +168,4 @@ def show(self):
256
168
257
169
The colors may or may not be showing after this function returns because
258
170
it may be done asynchronously."""
259
- if PixelBuf :
260
- neopixel_write (self .pin , self .buf .buf )
261
- elif self .brightness > 0.99 :
262
- neopixel_write (self .pin , self .buf )
263
- else :
264
- neopixel_write (self .pin , bytearray ([int (i * self .brightness ) for i in self .buf ]))
171
+ neopixel_write (self .pin , self .buf .buf )
0 commit comments