From fdd65a2321614b63b0baa0ea920cc66bfc1af028 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 17 Oct 2018 13:34:51 -0400 Subject: [PATCH 1/3] Cosmetic fixes, updated rainbow_cycle --- examples/neopixel_simpletest.py | 50 ++++++++++++++++----------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/examples/neopixel_simpletest.py b/examples/neopixel_simpletest.py index 2448ec1..12a9005 100644 --- a/examples/neopixel_simpletest.py +++ b/examples/neopixel_simpletest.py @@ -1,33 +1,31 @@ -# CircuitPython demo - NeoPixel - import time import board import neopixel -# On CircuitPlayground Express -> Board.NEOPIXEL -# Otherwise choose an open pin connected to the Data In of the NeoPixel strip, -# such as board.D1 -# pylint: disable=no-member -pixpin = board.NEOPIXEL +# On CircuitPlayground Express, and boards with built in status NeoPixel -> board.NEOPIXEL +# Otherwise choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D1 +pixel_pin = board.NEOPIXEL -# The number of pixels in the strip -numpix = 10 +# The number of NeoPixels +num_pixels = 10 -# number of colors in each pixel, =3 for RGB, =4 for RGB plus white +# The number of colors in each pixel: 3 for RGB, 4 for RGBW (RGB plus white) BPP = 3 -strip = neopixel.NeoPixel(pixpin, numpix, bpp=BPP, brightness=0.3, auto_write=False) +pixels = neopixel.NeoPixel(pixel_pin, num_pixels, bpp=BPP, brightness=0.3, auto_write=False) + def format_tuple(r, g, b): if BPP == 3: - return (r, g, b) - return (r, g, b, 0) + return r, g, b + return r, g, b, 0 + def wheel(pos): # Input a value 0 to 255 to get a color value. # The colours are a transition r - g - b - back to r. - if (pos < 0) or (pos > 255): + if pos < 0 or pos > 255: return format_tuple(0, 0, 0) if pos < 85: return format_tuple(int(pos * 3), int(255 - (pos*3)), 0) @@ -37,25 +35,27 @@ def wheel(pos): pos -= 170 return format_tuple(0, int(pos*3), int(255 - pos*3)) + def rainbow_cycle(wait): for j in range(255): - for i in range(strip.n): - idx = int((i * 256 / len(strip)) + j) - strip[i] = wheel(idx & 255) - strip.show() + for i in range(num_pixels): + pixel_index = (i * 256 // num_pixels) + j + pixels[i] = wheel(pixel_index & 255) + pixels.show() time.sleep(wait) + while True: - strip.fill(format_tuple(255, 0, 0)) - strip.show() + pixels.fill(format_tuple(255, 0, 0)) + pixels.show() time.sleep(1) - strip.fill(format_tuple(0, 255, 0)) - strip.show() + pixels.fill(format_tuple(0, 255, 0)) + pixels.show() time.sleep(1) - strip.fill(format_tuple(0, 0, 255)) - strip.show() + pixels.fill(format_tuple(0, 0, 255)) + pixels.show() time.sleep(1) - rainbow_cycle(0.001) # rainbowcycle with 1ms delay per step + rainbow_cycle(0.001) # rainbow cycle with 1ms delay per step From 2e7ab0911884fbac142467786566a1cf417c5110 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 17 Oct 2018 14:06:50 -0400 Subject: [PATCH 2/3] Updated to use pixel_order --- examples/neopixel_simpletest.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/neopixel_simpletest.py b/examples/neopixel_simpletest.py index 12a9005..f619994 100644 --- a/examples/neopixel_simpletest.py +++ b/examples/neopixel_simpletest.py @@ -10,14 +10,16 @@ # The number of NeoPixels num_pixels = 10 -# The number of colors in each pixel: 3 for RGB, 4 for RGBW (RGB plus white) -BPP = 3 +# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed! +# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW. +ORDER = neopixel.GRB -pixels = neopixel.NeoPixel(pixel_pin, num_pixels, bpp=BPP, brightness=0.3, auto_write=False) +pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False, + pixel_order=ORDER) def format_tuple(r, g, b): - if BPP == 3: + if ORDER == neopixel.RGB or ORDER == neopixel.GRB: return r, g, b return r, g, b, 0 From c7b1bc5f18e018ddd04cffaa3fe11bb0d722da47 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 17 Oct 2018 15:37:14 -0400 Subject: [PATCH 3/3] Removed tuple function, updated wheel --- examples/neopixel_simpletest.py | 45 ++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/examples/neopixel_simpletest.py b/examples/neopixel_simpletest.py index f619994..f137faa 100644 --- a/examples/neopixel_simpletest.py +++ b/examples/neopixel_simpletest.py @@ -14,28 +14,30 @@ # For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW. ORDER = neopixel.GRB -pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False, +pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER) -def format_tuple(r, g, b): - if ORDER == neopixel.RGB or ORDER == neopixel.GRB: - return r, g, b - return r, g, b, 0 - - def wheel(pos): # Input a value 0 to 255 to get a color value. # The colours are a transition r - g - b - back to r. if pos < 0 or pos > 255: - return format_tuple(0, 0, 0) - if pos < 85: - return format_tuple(int(pos * 3), int(255 - (pos*3)), 0) - if pos < 170: + r = g = b = 0 + elif pos < 85: + r = int(pos * 3) + g = int(255 - pos*3) + b = 0 + elif pos < 170: pos -= 85 - return format_tuple(int(255 - pos*3), 0, int(pos*3)) - pos -= 170 - return format_tuple(0, int(pos*3), int(255 - pos*3)) + r = int(255 - pos*3) + g = 0 + b = int(pos*3) + else: + pos -= 170 + r = 0 + g = int(pos*3) + b = int(255 - pos*3) + return (r, g, b) if ORDER == neopixel.RGB or ORDER == neopixel.GRB else (r, g, b, 0) def rainbow_cycle(wait): @@ -48,15 +50,24 @@ def rainbow_cycle(wait): while True: - pixels.fill(format_tuple(255, 0, 0)) + # Comment this line out if you have RGBW/GRBW NeoPixels + pixels.fill((255, 0, 0)) + # Uncomment this line if you have RGBW/GRBW NeoPixels + # pixels.fill((255, 0, 0, 0)) pixels.show() time.sleep(1) - pixels.fill(format_tuple(0, 255, 0)) + # Comment this line out if you have RGBW/GRBW NeoPixels + pixels.fill((0, 255, 0)) + # Uncomment this line if you have RGBW/GRBW NeoPixels + # pixels.fill((0, 255, 0, 0)) pixels.show() time.sleep(1) - pixels.fill(format_tuple(0, 0, 255)) + # Comment this line out if you have RGBW/GRBW NeoPixels + pixels.fill((0, 0, 255)) + # Uncomment this line if you have RGBW/GRBW NeoPixels + # pixels.fill((0, 0, 255, 0)) pixels.show() time.sleep(1)