32
32
33
33
import digitalio
34
34
from neopixel_write import neopixel_write
35
- from pixelbuf import PixelBuf , RGB , GRB , RGBW , GRBW
35
+ try :
36
+ import _pixelbuf
37
+ except ImportError :
38
+ import pypixelbuf as _pixelbuf
36
39
37
40
38
41
__version__ = "0.0.0-auto.0"
39
42
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.git"
40
43
41
44
42
- class NeoPixel :
45
+ # Pixel color order constants
46
+ RGB = 'rgb'
47
+ """Red Green Blue"""
48
+ GRB = 'grb'
49
+ """Green Red Blue"""
50
+ RGBW = 'rgbw'
51
+ """Red Green Blue White"""
52
+ GRBW = 'grbw'
53
+ """Green Red Blue White"""
54
+
55
+
56
+ class NeoPixel (_pixelbuf .PixelBuf ):
57
+
58
+ bpp = None
59
+ n = 0
43
60
"""
44
61
A sequence of neopixels.
45
62
@@ -50,7 +67,7 @@ class NeoPixel:
50
67
brightness
51
68
:param bool auto_write: True if the neopixels should immediately change when set. If False,
52
69
`show` must be called explicitly.
53
- :param tuple pixel_order : Set the pixel color channel order. GRBW is set by default.
70
+ :param str : Set the pixel color channel order. GRBW is set by default.
54
71
55
72
Example for Circuit Playground Express:
56
73
@@ -83,20 +100,30 @@ class NeoPixel:
83
100
def __init__ (self , pin , n , * , bpp = 3 , brightness = 1.0 , auto_write = True , pixel_order = None ):
84
101
self .pin = digitalio .DigitalInOut (pin )
85
102
self .pin .direction = digitalio .Direction .OUTPUT
86
- self .n = n
87
- self .auto_write = auto_write
88
103
self .bpp = bpp
89
- # TODO switch to correct byteorder if bpp specified but not pixel_order
90
- self .buf = PixelBuf (self .n , bytearray (self .n * bpp ),
91
- bpp = self .bpp , brightness = brightness ,
92
- rawbuf = bytearray (self .n * bpp ),
93
- byteorder = pixel_order or GRB )
104
+ self .n = n
105
+ if not pixel_order :
106
+ pixel_order = 'grb' if bpp == 3 else 'grbw'
107
+ else :
108
+ self .bpp = bpp = len (pixel_order )
109
+ # Backwards compatibility with tuples
110
+ if isinstance (pixel_order , tuple ):
111
+ order_chars = 'rgbw'
112
+ order = []
113
+ for char_no , order in enumerate (pixel_order ):
114
+ order [pixel_order ] = order_chars [char_no ]
115
+ pixel_order = '' .join (order )
116
+
117
+ super ().__init__ (n , bytearray (self .n * bpp ),
118
+ brightness = brightness ,
119
+ rawbuf = bytearray (self .n * bpp ),
120
+ byteorder = pixel_order ,
121
+ auto_write = auto_write )
94
122
95
123
def deinit (self ):
96
124
"""Blank out the NeoPixels and release the pin."""
97
- for i in range (len (self .buf )):
98
- self .buf [i ] = 0
99
- neopixel_write (self .pin , self .buf .buf )
125
+ self .fill (0 )
126
+ self .show ()
100
127
self .pin .deinit ()
101
128
102
129
def __enter__ (self ):
@@ -108,36 +135,6 @@ def __exit__(self, exception_type, exception_value, traceback):
108
135
def __repr__ (self ):
109
136
return "[" + ", " .join ([str (x ) for x in self ]) + "]"
110
137
111
- def __setitem__ (self , index , val ):
112
- if isinstance (index , slice ):
113
- self .buf [index .start :index .stop :index .step ] = val
114
- else :
115
- self .buf [index ] = val
116
-
117
- if self .auto_write :
118
- self .show ()
119
-
120
- def __getitem__ (self , index ):
121
- if isinstance (index , slice ):
122
- return self .buf [index .start :index .stop :index .step ]
123
- else :
124
- return self .buf [index ]
125
-
126
- def __len__ (self ):
127
- return len (self .buf ) // self .bpp
128
-
129
- @property
130
- def brightness (self ):
131
- """Overall brightness of the pixel"""
132
- return self .buf .brightness
133
-
134
- @brightness .setter
135
- def brightness (self , brightness ):
136
- # pylint: disable=attribute-defined-outside-init
137
- self .buf .brightness = brightness
138
- if self .auto_write :
139
- self .show ()
140
-
141
138
def fill (self , color ):
142
139
"""Colors all pixels the given ***color***."""
143
140
auto_write = self .auto_write
@@ -160,4 +157,4 @@ def show(self):
160
157
161
158
The colors may or may not be showing after this function returns because
162
159
it may be done asynchronously."""
163
- neopixel_write (self .pin , self .buf . buf )
160
+ neopixel_write (self .pin , self .buf )
0 commit comments