Skip to content

Commit f872892

Browse files
authored
Merge pull request #24 from caternuson/master
added pixel_order
2 parents fe7dcf8 + d426b0c commit f872892

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
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: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
__version__ = "0.0.0-auto.0"
3737
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.git"
3838

39+
# Pixel color order constants
40+
RGB = (0, 1, 2)
41+
"""Red Green Blue"""
42+
GRB = (1, 0, 2)
43+
"""Green Red Blue"""
44+
RGBW = (0, 1, 2, 3)
45+
"""Red Green Blue White"""
46+
GRBW = (1, 0, 2, 3)
47+
"""Green Red Blue White"""
48+
3949
class NeoPixel:
4050
"""
4151
A sequence of neopixels.
@@ -47,6 +57,7 @@ class NeoPixel:
4757
brightness
4858
:param bool auto_write: True if the neopixels should immediately change when set. If False,
4959
`show` must be called explicitly.
60+
:param tuple pixel_order: Set the pixel color channel order. GRBW is set by default.
5061
5162
Example for Circuit Playground Express:
5263
@@ -76,13 +87,17 @@ class NeoPixel:
7687
pixels[::2] = [RED] * (len(pixels) // 2)
7788
time.sleep(2)
7889
"""
79-
ORDER = (1, 0, 2, 3)
80-
def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True):
90+
def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None):
8191
self.pin = digitalio.DigitalInOut(pin)
8292
self.pin.direction = digitalio.Direction.OUTPUT
8393
self.n = n
84-
self.bpp = bpp
85-
self.buf = bytearray(n * bpp)
94+
if pixel_order is None:
95+
self.order = GRBW
96+
self.bpp = bpp
97+
else:
98+
self.order = pixel_order
99+
self.bpp = len(self.order)
100+
self.buf = bytearray(self.n * self.bpp)
86101
# Set auto_write to False temporarily so brightness setter does _not_
87102
# call show() while in __init__.
88103
self.auto_write = False
@@ -132,11 +147,11 @@ def _set_item(self, index, value):
132147
r, g, b = value
133148
else:
134149
r, g, b, w = value
135-
self.buf[offset + self.ORDER[0]] = r
136-
self.buf[offset + self.ORDER[1]] = g
137-
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
138153
if self.bpp == 4:
139-
self.buf[offset + self.ORDER[3]] = w
154+
self.buf[offset + self.order[3]] = w
140155

141156
def __setitem__(self, index, val):
142157
if isinstance(index, slice):
@@ -158,15 +173,15 @@ def __getitem__(self, index):
158173
if isinstance(index, slice):
159174
out = []
160175
for in_i in range(*index.indices(len(self.buf) // self.bpp)):
161-
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]]
162177
for i in range(self.bpp)))
163178
return out
164179
if index < 0:
165180
index += len(self)
166181
if index >= self.n or index < 0:
167182
raise IndexError
168183
offset = index * self.bpp
169-
return tuple(self.buf[offset + self.ORDER[i]]
184+
return tuple(self.buf[offset + self.order[i]]
170185
for i in range(self.bpp))
171186

172187
def __len__(self):

0 commit comments

Comments
 (0)