43
43
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
44
44
"""
45
45
46
+ # pylint: disable=ungrouped-imports
47
+ import sys
46
48
from adafruit_bus_device .spi_device import SPIDevice
47
- from adafruit_pypixelbuf import PixelBuf , fill
49
+
50
+ if sys .implementation .version [0 ] < 5 :
51
+ import adafruit_pypixelbuf as _pixelbuf
52
+ else :
53
+ try :
54
+ import _pixelbuf
55
+ except ImportError :
56
+ import adafruit_pypixelbuf as _pixelbuf
48
57
49
58
__version__ = "0.0.0-auto.0"
50
59
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI.git"
60
69
"""Green Red Blue White"""
61
70
62
71
63
- class NeoPixel_SPI (PixelBuf ):
72
+ class NeoPixel_SPI (_pixelbuf . PixelBuf ):
64
73
"""
65
74
A sequence of neopixels.
66
75
@@ -96,6 +105,9 @@ def __init__(
96
105
pixel_order = GRB if bpp == 3 else GRBW
97
106
else :
98
107
bpp = len (pixel_order )
108
+ if isinstance (pixel_order , tuple ):
109
+ order_list = [RGBW [order ] for order in pixel_order ]
110
+ pixel_order = "" .join (order_list )
99
111
100
112
# set up SPI related stuff
101
113
self ._spi = SPIDevice (spi , baudrate = self .FREQ )
@@ -111,42 +123,42 @@ def __init__(
111
123
112
124
# everything else taken care of by base class
113
125
super ().__init__ (
114
- n ,
115
- bytearray (n * bpp ),
116
- brightness = brightness ,
117
- rawbuf = bytearray (n * bpp ),
118
- byteorder = pixel_order ,
119
- auto_write = auto_write ,
126
+ n , brightness = brightness , byteorder = pixel_order , auto_write = auto_write
120
127
)
121
128
122
129
def deinit (self ):
123
130
"""Blank out the NeoPixels."""
124
131
self .fill (0 )
125
132
self .show ()
126
133
127
- def show (self ):
134
+ def __repr__ (self ):
135
+ return "[" + ", " .join ([str (x ) for x in self ]) + "]"
136
+
137
+ @property
138
+ def n (self ):
139
+ """
140
+ The number of neopixels in the chain (read-only)
141
+ """
142
+ return len (self )
143
+
144
+ def _transmit (self , buffer ):
128
145
"""Shows the new colors on the pixels themselves if they haven't already
129
146
been autowritten."""
130
- self ._transmogrify ()
147
+ self ._transmogrify (buffer )
131
148
# pylint: disable=no-member
132
149
with self ._spi as spi :
133
150
# write out special byte sequence surrounded by RESET
134
151
# leading RESET needed for cases where MOSI rests HI
135
152
spi .write (self ._reset + self .spibuf + self ._reset )
136
153
137
- def _transmogrify (self ):
154
+ def _transmogrify (self , buffer ):
138
155
"""Turn every BIT of buf into a special BYTE pattern."""
139
156
k = 0
140
- for byte in self .buf :
141
- byte = int (byte * self .brightness )
157
+ for byte in buffer :
142
158
# MSB first
143
159
for i in range (7 , - 1 , - 1 ):
144
160
if byte >> i & 0x01 :
145
161
self .spibuf [k ] = 0b11110000 # A NeoPixel 1 bit
146
162
else :
147
163
self .spibuf [k ] = 0b11000000 # A NeoPixel 0 bit
148
164
k += 1
149
-
150
- def fill (self , color ):
151
- """Colors all pixels the given ***color***."""
152
- fill (self , color )
0 commit comments