Skip to content

Commit

Permalink
Merge pull request #24 from caternuson/master
Browse files Browse the repository at this point in the history
added pixel_order
  • Loading branch information
kattni authored Feb 23, 2018
2 parents fe7dcf8 + d426b0c commit f872892
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
13 changes: 13 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ method is called.
pixels[9] = (0, 10, 0)
pixels.show()
This example demonstrates using a single NeoPixel tied to a GPIO pin and with
a ``pixel_order`` to specify the color channel order. Note that ``bpp`` does not
need to be specified as it is computed from the supplied ``pixel_order``.

.. code-block:: python
import board
import neopixel
pixel = neopixel.NeoPixel(board.D0, 1, pixel_order=neopixel.RGBW)
pixel[0] = (30, 0, 20, 10)
Contributing
============

Expand Down
24 changes: 24 additions & 0 deletions examples/pixel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This example shows how to create a single pixel with a specific color channel
# order and blink it.
# Most NeoPixels = neopixel.GRB or neopixel.GRBW
# The 8mm Diffused NeoPixel (PID 1734) = neopixel.RGB
import time
import board
import neopixel

# Configure the setup
PIXEL_PIN = board.D1 # pin that the NeoPixel is connected to
ORDER = neopixel.RGB # pixel color channel order
COLOR = (100, 50, 150) # color to blink
CLEAR = (0, 0, 0) # clear (or second color)
DELAY = 0.25 # blink rate in seconds

# Create the NeoPixel object
pixel = neopixel.NeoPixel(PIXEL_PIN, 1, pixel_order=ORDER)

# Loop forever and blink the color
while True:
pixel[0] = COLOR
time.sleep(DELAY)
pixel[0] = CLEAR
time.sleep(DELAY)
35 changes: 25 additions & 10 deletions neopixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.git"

# Pixel color order constants
RGB = (0, 1, 2)
"""Red Green Blue"""
GRB = (1, 0, 2)
"""Green Red Blue"""
RGBW = (0, 1, 2, 3)
"""Red Green Blue White"""
GRBW = (1, 0, 2, 3)
"""Green Red Blue White"""

class NeoPixel:
"""
A sequence of neopixels.
Expand All @@ -47,6 +57,7 @@ class NeoPixel:
brightness
:param bool auto_write: True if the neopixels should immediately change when set. If False,
`show` must be called explicitly.
:param tuple pixel_order: Set the pixel color channel order. GRBW is set by default.
Example for Circuit Playground Express:
Expand Down Expand Up @@ -76,13 +87,17 @@ class NeoPixel:
pixels[::2] = [RED] * (len(pixels) // 2)
time.sleep(2)
"""
ORDER = (1, 0, 2, 3)
def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True):
def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None):
self.pin = digitalio.DigitalInOut(pin)
self.pin.direction = digitalio.Direction.OUTPUT
self.n = n
self.bpp = bpp
self.buf = bytearray(n * bpp)
if pixel_order is None:
self.order = GRBW
self.bpp = bpp
else:
self.order = pixel_order
self.bpp = len(self.order)
self.buf = bytearray(self.n * self.bpp)
# Set auto_write to False temporarily so brightness setter does _not_
# call show() while in __init__.
self.auto_write = False
Expand Down Expand Up @@ -132,11 +147,11 @@ def _set_item(self, index, value):
r, g, b = value
else:
r, g, b, w = value
self.buf[offset + self.ORDER[0]] = r
self.buf[offset + self.ORDER[1]] = g
self.buf[offset + self.ORDER[2]] = b
self.buf[offset + self.order[0]] = r
self.buf[offset + self.order[1]] = g
self.buf[offset + self.order[2]] = b
if self.bpp == 4:
self.buf[offset + self.ORDER[3]] = w
self.buf[offset + self.order[3]] = w

def __setitem__(self, index, val):
if isinstance(index, slice):
Expand All @@ -158,15 +173,15 @@ def __getitem__(self, index):
if isinstance(index, slice):
out = []
for in_i in range(*index.indices(len(self.buf) // self.bpp)):
out.append(tuple(self.buf[in_i * self.bpp + self.ORDER[i]]
out.append(tuple(self.buf[in_i * self.bpp + self.order[i]]
for i in range(self.bpp)))
return out
if index < 0:
index += len(self)
if index >= self.n or index < 0:
raise IndexError
offset = index * self.bpp
return tuple(self.buf[offset + self.ORDER[i]]
return tuple(self.buf[offset + self.order[i]]
for i in range(self.bpp))

def __len__(self):
Expand Down

0 comments on commit f872892

Please sign in to comment.