43
43
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
44
44
"""
45
45
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
57
48
58
49
__version__ = "0.0.0-auto.0"
59
50
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI.git"
68
59
GRBW = 'GRBW'
69
60
"""Green Red Blue White"""
70
61
71
- class NeoPixel_SPI (NeoPixel ):
62
+ class NeoPixel_SPI (PixelBuf ):
72
63
"""
73
64
A sequence of neopixels.
74
65
@@ -91,46 +82,19 @@ class NeoPixel_SPI(NeoPixel):
91
82
pixels = neopixel_spi.NeoPixel_SPI(board.SPI(), 10)
92
83
pixels.fill(0xff0000)
93
84
"""
94
- #pylint: disable=invalid-name, super-init-not-called, too-many-instance-attributes
95
85
96
86
FREQ = 6400000 # 800kHz * 8, actual may be different
97
87
TRST = 80e-6 # Reset code low level time
98
88
99
89
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
107
92
if not pixel_order :
108
93
pixel_order = GRB if bpp == 3 else GRBW
109
94
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
134
98
self ._spi = SPIDevice (spi , baudrate = self .FREQ )
135
99
with self ._spi as spibus :
136
100
try :
@@ -139,9 +103,16 @@ def __init__(self, spi, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_orde
139
103
except AttributeError :
140
104
# use nominal
141
105
freq = self .FREQ
142
- self .RESET = bytes ([0 ]* round (freq * self .TRST / 8 ))
106
+ self ._reset = bytes ([0 ]* round (freq * self .TRST / 8 ))
143
107
self .spibuf = bytearray (8 * n * bpp )
144
108
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
+
145
116
def deinit (self ):
146
117
"""Blank out the NeoPixels."""
147
118
self .fill (0 )
@@ -155,7 +126,7 @@ def show(self):
155
126
with self ._spi as spi :
156
127
# write out special byte sequence surrounded by RESET
157
128
# 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 )
159
130
160
131
def _transmogrify (self ):
161
132
"""Turn every BIT of buf into a special BYTE pattern."""
@@ -169,3 +140,7 @@ def _transmogrify(self):
169
140
else :
170
141
self .spibuf [k ] = 0b11000000 # A NeoPixel 0 bit
171
142
k += 1
143
+
144
+ def fill (self , color ):
145
+ """Colors all pixels the given ***color***."""
146
+ fill (self , color )
0 commit comments