From faa40b3d267946b1ae08d5ff495472030f9a039b Mon Sep 17 00:00:00 2001 From: caternuson Date: Wed, 21 Feb 2018 11:53:48 -0800 Subject: [PATCH 1/4] added pixel_order --- neopixel.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/neopixel.py b/neopixel.py index 3bc6f39..1b00f04 100644 --- a/neopixel.py +++ b/neopixel.py @@ -36,6 +36,20 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.git" +# Pixel color order constants +RGB = 0 +GRB = 1 +RGBW = 2 +GRBW = 3 + +# Pixel color order +PIXEL_ORDER = { + RGB: (0, 1, 2), + GRB: (1, 0, 2), + RGBW: (0, 1, 2, 3), + GRBW: (1, 0, 2, 3), +} + class NeoPixel: """ A sequence of neopixels. @@ -76,12 +90,18 @@ 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): + #ORDER = (1, 0, 2, 3) + 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.bpp = bpp + if pixel_order is None: + self.ORDER = PIXEL_ORDER[GRBW] + self.bpp = bpp + else: + self.ORDER = PIXEL_ORDER[pixel_order] + self.bpp = len(self.ORDER) self.buf = bytearray(n * bpp) # Set auto_write to False temporarily so brightness setter does _not_ # call show() while in __init__. From b90fd40f9e1663881bf50925b3d11e28f238211d Mon Sep 17 00:00:00 2001 From: caternuson Date: Wed, 21 Feb 2018 12:26:14 -0800 Subject: [PATCH 2/4] removed dict --- neopixel.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/neopixel.py b/neopixel.py index 1b00f04..d95d1a5 100644 --- a/neopixel.py +++ b/neopixel.py @@ -37,18 +37,10 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.git" # Pixel color order constants -RGB = 0 -GRB = 1 -RGBW = 2 -GRBW = 3 - -# Pixel color order -PIXEL_ORDER = { - RGB: (0, 1, 2), - GRB: (1, 0, 2), - RGBW: (0, 1, 2, 3), - GRBW: (1, 0, 2, 3), -} +RGB = (0, 1, 2) +GRB = (1, 0, 2) +RGBW = (0, 1, 2, 3) +GRBW = (1, 0, 2, 3) class NeoPixel: """ @@ -97,10 +89,10 @@ def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_orde self.n = n #self.bpp = bpp if pixel_order is None: - self.ORDER = PIXEL_ORDER[GRBW] + self.ORDER = GRBW self.bpp = bpp else: - self.ORDER = PIXEL_ORDER[pixel_order] + self.ORDER = pixel_order self.bpp = len(self.ORDER) self.buf = bytearray(n * bpp) # Set auto_write to False temporarily so brightness setter does _not_ From b73aae36dbde3a0c5e1d66dce64391a869fdef73 Mon Sep 17 00:00:00 2001 From: caternuson Date: Wed, 21 Feb 2018 16:17:17 -0800 Subject: [PATCH 3/4] fixed buf bug --- neopixel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neopixel.py b/neopixel.py index d95d1a5..7bf5cd5 100644 --- a/neopixel.py +++ b/neopixel.py @@ -94,7 +94,7 @@ def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_orde else: self.ORDER = pixel_order self.bpp = len(self.ORDER) - self.buf = bytearray(n * bpp) + 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 From d426b0ca359cce706506fa709c1b107562787e4e Mon Sep 17 00:00:00 2001 From: caternuson Date: Thu, 22 Feb 2018 16:21:42 -0800 Subject: [PATCH 4/4] clean up, doc, and example --- README.rst | 13 +++++++++++++ examples/pixel.py | 24 ++++++++++++++++++++++++ neopixel.py | 25 ++++++++++++++----------- 3 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 examples/pixel.py diff --git a/README.rst b/README.rst index 8ea05fe..d0a1060 100644 --- a/README.rst +++ b/README.rst @@ -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 ============ diff --git a/examples/pixel.py b/examples/pixel.py new file mode 100644 index 0000000..f62813d --- /dev/null +++ b/examples/pixel.py @@ -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) diff --git a/neopixel.py b/neopixel.py index 7bf5cd5..71fd633 100644 --- a/neopixel.py +++ b/neopixel.py @@ -38,9 +38,13 @@ # 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: """ @@ -53,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: @@ -82,18 +87,16 @@ 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, pixel_order=None): self.pin = digitalio.DigitalInOut(pin) self.pin.direction = digitalio.Direction.OUTPUT self.n = n - #self.bpp = bpp if pixel_order is None: - self.ORDER = GRBW + self.order = GRBW self.bpp = bpp else: - self.ORDER = pixel_order - self.bpp = len(self.ORDER) + 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__. @@ -144,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): @@ -170,7 +173,7 @@ 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: @@ -178,7 +181,7 @@ def __getitem__(self, index): 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):