4343* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4444"""
4545
46- # The following creates a mock neopixel_write module to allow importing
47- # the CircuitPython NeoPixel module without actually providing neopixel_write.
48- #pylint: disable=wrong-import-position, exec-used
49- import sys
50- from types import ModuleType
51- MOCK_MODULE = ModuleType ('mock_neopixel_write' )
52- exec ('def neopixel_write(): pass' , MOCK_MODULE .__dict__ )
53- sys .modules ['neopixel_write' ] = MOCK_MODULE
54- #pylint: enable=wrong-import-position, exec-used
55-
56- from neopixel import NeoPixel
46+ from adafruit_pypixelbuf import PixelBuf , fill
47+ from adafruit_bus_device .spi_device import SPIDevice
5748
5849__version__ = "0.0.0-auto.0"
5950__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI.git"
6859GRBW = 'GRBW'
6960"""Green Red Blue White"""
7061
71- class NeoPixel_SPI (NeoPixel ):
62+ class NeoPixel_SPI (PixelBuf ):
7263 """
7364 A sequence of neopixels.
7465
@@ -91,46 +82,19 @@ class NeoPixel_SPI(NeoPixel):
9182 pixels = neopixel_spi.NeoPixel_SPI(board.SPI(), 10)
9283 pixels.fill(0xff0000)
9384 """
94- #pylint: disable=invalid-name, super-init-not-called, too-many-instance-attributes
9585
9686 FREQ = 6400000 # 800kHz * 8, actual may be different
9787 TRST = 80e-6 # Reset code low level time
9888
9989 def __init__ (self , spi , n , * , bpp = 3 , brightness = 1.0 , auto_write = True , pixel_order = None ):
100- # We can't call super().__init__() since we don't actually
101- # have a pin to supply it. So setup is done manually.
102- #
103- # neopixel stuff
104- #
105- self .bpp = bpp
106- self .n = n
90+
91+ # configure bpp and pixel_order
10792 if not pixel_order :
10893 pixel_order = GRB if bpp == 3 else GRBW
10994 else :
110- self .bpp = bpp = len (pixel_order )
111- #
112- # pypixelbuf stuff
113- #
114- bpp , byteorder_tuple , has_white , _ = self .parse_byteorder (pixel_order )
115- self ._pixels = n
116- self ._bytes = bpp * n
117- self ._byteorder = byteorder_tuple
118- self ._byteorder_string = pixel_order
119- self ._has_white = has_white
120- self ._bpp = bpp
121- self ._bytearray = bytearray (n * bpp )
122- self ._two_buffers = True
123- self ._rawbytearray = bytearray (n * bpp )
124- self ._offset = 0
125- self ._dotstar_mode = False
126- self ._pixel_step = bpp
127- self .auto_write = False
128- self .brightness = min (1.0 , max (0 , brightness ))
129- self .auto_write = auto_write
130- #
131- # neopixel_spi stuff
132- #
133- from adafruit_bus_device .spi_device import SPIDevice
95+ bpp = len (pixel_order )
96+
97+ # set up SPI related stuff
13498 self ._spi = SPIDevice (spi , baudrate = self .FREQ )
13599 with self ._spi as spibus :
136100 try :
@@ -139,9 +103,16 @@ def __init__(self, spi, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_orde
139103 except AttributeError :
140104 # use nominal
141105 freq = self .FREQ
142- self .RESET = bytes ([0 ]* round (freq * self .TRST / 8 ))
106+ self ._reset = bytes ([0 ]* round (freq * self .TRST / 8 ))
143107 self .spibuf = bytearray (8 * n * bpp )
144108
109+ # everything else taken care of by base class
110+ super ().__init__ (n , bytearray (n * bpp ),
111+ brightness = brightness ,
112+ rawbuf = bytearray (n * bpp ),
113+ byteorder = pixel_order ,
114+ auto_write = auto_write )
115+
145116 def deinit (self ):
146117 """Blank out the NeoPixels."""
147118 self .fill (0 )
@@ -155,7 +126,7 @@ def show(self):
155126 with self ._spi as spi :
156127 # write out special byte sequence surrounded by RESET
157128 # leading RESET needed for cases where MOSI rests HI
158- spi .write (self .RESET + self .spibuf + self .RESET )
129+ spi .write (self ._reset + self .spibuf + self ._reset )
159130
160131 def _transmogrify (self ):
161132 """Turn every BIT of buf into a special BYTE pattern."""
@@ -169,3 +140,7 @@ def _transmogrify(self):
169140 else :
170141 self .spibuf [k ] = 0b11000000 # A NeoPixel 0 bit
171142 k += 1
143+
144+ def fill (self , color ):
145+ """Colors all pixels the given ***color***."""
146+ fill (self , color )
0 commit comments