Skip to content

Commit d426b0c

Browse files
author
caternuson
committed
clean up, doc, and example
1 parent b73aae3 commit d426b0c

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

README.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ method is called.
6969
pixels[9] = (0, 10, 0)
7070
pixels.show()
7171
72+
This example demonstrates using a single NeoPixel tied to a GPIO pin and with
73+
a ``pixel_order`` to specify the color channel order. Note that ``bpp`` does not
74+
need to be specified as it is computed from the supplied ``pixel_order``.
75+
76+
.. code-block:: python
77+
78+
import board
79+
import neopixel
80+
81+
pixel = neopixel.NeoPixel(board.D0, 1, pixel_order=neopixel.RGBW)
82+
pixel[0] = (30, 0, 20, 10)
83+
84+
7285
Contributing
7386
============
7487

examples/pixel.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This example shows how to create a single pixel with a specific color channel
2+
# order and blink it.
3+
# Most NeoPixels = neopixel.GRB or neopixel.GRBW
4+
# The 8mm Diffused NeoPixel (PID 1734) = neopixel.RGB
5+
import time
6+
import board
7+
import neopixel
8+
9+
# Configure the setup
10+
PIXEL_PIN = board.D1 # pin that the NeoPixel is connected to
11+
ORDER = neopixel.RGB # pixel color channel order
12+
COLOR = (100, 50, 150) # color to blink
13+
CLEAR = (0, 0, 0) # clear (or second color)
14+
DELAY = 0.25 # blink rate in seconds
15+
16+
# Create the NeoPixel object
17+
pixel = neopixel.NeoPixel(PIXEL_PIN, 1, pixel_order=ORDER)
18+
19+
# Loop forever and blink the color
20+
while True:
21+
pixel[0] = COLOR
22+
time.sleep(DELAY)
23+
pixel[0] = CLEAR
24+
time.sleep(DELAY)

neopixel.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@
3838

3939
# Pixel color order constants
4040
RGB = (0, 1, 2)
41+
"""Red Green Blue"""
4142
GRB = (1, 0, 2)
43+
"""Green Red Blue"""
4244
RGBW = (0, 1, 2, 3)
45+
"""Red Green Blue White"""
4346
GRBW = (1, 0, 2, 3)
47+
"""Green Red Blue White"""
4448

4549
class NeoPixel:
4650
"""
@@ -53,6 +57,7 @@ class NeoPixel:
5357
brightness
5458
:param bool auto_write: True if the neopixels should immediately change when set. If False,
5559
`show` must be called explicitly.
60+
:param tuple pixel_order: Set the pixel color channel order. GRBW is set by default.
5661
5762
Example for Circuit Playground Express:
5863
@@ -82,18 +87,16 @@ class NeoPixel:
8287
pixels[::2] = [RED] * (len(pixels) // 2)
8388
time.sleep(2)
8489
"""
85-
#ORDER = (1, 0, 2, 3)
8690
def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None):
8791
self.pin = digitalio.DigitalInOut(pin)
8892
self.pin.direction = digitalio.Direction.OUTPUT
8993
self.n = n
90-
#self.bpp = bpp
9194
if pixel_order is None:
92-
self.ORDER = GRBW
95+
self.order = GRBW
9396
self.bpp = bpp
9497
else:
95-
self.ORDER = pixel_order
96-
self.bpp = len(self.ORDER)
98+
self.order = pixel_order
99+
self.bpp = len(self.order)
97100
self.buf = bytearray(self.n * self.bpp)
98101
# Set auto_write to False temporarily so brightness setter does _not_
99102
# call show() while in __init__.
@@ -144,11 +147,11 @@ def _set_item(self, index, value):
144147
r, g, b = value
145148
else:
146149
r, g, b, w = value
147-
self.buf[offset + self.ORDER[0]] = r
148-
self.buf[offset + self.ORDER[1]] = g
149-
self.buf[offset + self.ORDER[2]] = b
150+
self.buf[offset + self.order[0]] = r
151+
self.buf[offset + self.order[1]] = g
152+
self.buf[offset + self.order[2]] = b
150153
if self.bpp == 4:
151-
self.buf[offset + self.ORDER[3]] = w
154+
self.buf[offset + self.order[3]] = w
152155

153156
def __setitem__(self, index, val):
154157
if isinstance(index, slice):
@@ -170,15 +173,15 @@ def __getitem__(self, index):
170173
if isinstance(index, slice):
171174
out = []
172175
for in_i in range(*index.indices(len(self.buf) // self.bpp)):
173-
out.append(tuple(self.buf[in_i * self.bpp + self.ORDER[i]]
176+
out.append(tuple(self.buf[in_i * self.bpp + self.order[i]]
174177
for i in range(self.bpp)))
175178
return out
176179
if index < 0:
177180
index += len(self)
178181
if index >= self.n or index < 0:
179182
raise IndexError
180183
offset = index * self.bpp
181-
return tuple(self.buf[offset + self.ORDER[i]]
184+
return tuple(self.buf[offset + self.order[i]]
182185
for i in range(self.bpp))
183186

184187
def __len__(self):

0 commit comments

Comments
 (0)