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 3bc6f39..71fd633 100644 --- a/neopixel.py +++ b/neopixel.py @@ -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. @@ -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: @@ -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 @@ -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): @@ -158,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: @@ -166,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):