4343* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4444"""
4545
46+ # pylint: disable=ungrouped-imports
47+ import sys
4648from 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
4857
4958__version__ = "0.0.0-auto.0"
5059__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI.git"
6069"""Green Red Blue White"""
6170
6271
63- class NeoPixel_SPI (PixelBuf ):
72+ class NeoPixel_SPI (_pixelbuf . PixelBuf ):
6473 """
6574 A sequence of neopixels.
6675
@@ -95,7 +104,10 @@ def __init__(
95104 if not pixel_order :
96105 pixel_order = GRB if bpp == 3 else GRBW
97106 else :
98- bpp = len (pixel_order )
107+ if isinstance (pixel_order , tuple ):
108+ order_list = [RGBW [order ] for order in pixel_order ]
109+ pixel_order = "" .join (order_list )
110+ bpp = len (pixel_order )
99111
100112 # set up SPI related stuff
101113 self ._spi = SPIDevice (spi , baudrate = self .FREQ )
@@ -111,42 +123,42 @@ def __init__(
111123
112124 # everything else taken care of by base class
113125 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
120127 )
121128
122129 def deinit (self ):
123130 """Blank out the NeoPixels."""
124131 self .fill (0 )
125132 self .show ()
126133
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 ):
128145 """Shows the new colors on the pixels themselves if they haven't already
129146 been autowritten."""
130- self ._transmogrify ()
147+ self ._transmogrify (buffer )
131148 # pylint: disable=no-member
132149 with self ._spi as spi :
133150 # write out special byte sequence surrounded by RESET
134151 # leading RESET needed for cases where MOSI rests HI
135152 spi .write (self ._reset + self .spibuf + self ._reset )
136153
137- def _transmogrify (self ):
154+ def _transmogrify (self , buffer ):
138155 """Turn every BIT of buf into a special BYTE pattern."""
139156 k = 0
140- for byte in self .buf :
141- byte = int (byte * self .brightness )
157+ for byte in buffer :
142158 # MSB first
143159 for i in range (7 , - 1 , - 1 ):
144160 if byte >> i & 0x01 :
145161 self .spibuf [k ] = 0b11110000 # A NeoPixel 1 bit
146162 else :
147163 self .spibuf [k ] = 0b11000000 # A NeoPixel 0 bit
148164 k += 1
149-
150- def fill (self , color ):
151- """Colors all pixels the given ***color***."""
152- fill (self , color )
0 commit comments