Skip to content

Commit 5f96644

Browse files
committed
continued work on making NeoPixel a subclass of _pixelbuf.PixelBuf
1 parent 4d12673 commit 5f96644

File tree

1 file changed

+41
-44
lines changed

1 file changed

+41
-44
lines changed

neopixel.py

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,31 @@
3232

3333
import digitalio
3434
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
3639

3740

3841
__version__ = "0.0.0-auto.0"
3942
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.git"
4043

4144

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
4360
"""
4461
A sequence of neopixels.
4562
@@ -50,7 +67,7 @@ class NeoPixel:
5067
brightness
5168
:param bool auto_write: True if the neopixels should immediately change when set. If False,
5269
`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.
5471
5572
Example for Circuit Playground Express:
5673
@@ -83,20 +100,30 @@ class NeoPixel:
83100
def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None):
84101
self.pin = digitalio.DigitalInOut(pin)
85102
self.pin.direction = digitalio.Direction.OUTPUT
86-
self.n = n
87-
self.auto_write = auto_write
88103
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)
94122

95123
def deinit(self):
96124
"""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()
100127
self.pin.deinit()
101128

102129
def __enter__(self):
@@ -108,36 +135,6 @@ def __exit__(self, exception_type, exception_value, traceback):
108135
def __repr__(self):
109136
return "[" + ", ".join([str(x) for x in self]) + "]"
110137

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-
141138
def fill(self, color):
142139
"""Colors all pixels the given ***color***."""
143140
auto_write = self.auto_write
@@ -160,4 +157,4 @@ def show(self):
160157
161158
The colors may or may not be showing after this function returns because
162159
it may be done asynchronously."""
163-
neopixel_write(self.pin, self.buf.buf)
160+
neopixel_write(self.pin, self.buf)

0 commit comments

Comments
 (0)