Skip to content

Commit 8b1124c

Browse files
authored
Merge pull request #33 from kattni/example-update
Cosmetic fixes, updated rainbow_cycle
2 parents 4b9563d + c7b1bc5 commit 8b1124c

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

examples/neopixel_simpletest.py

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,74 @@
1-
# CircuitPython demo - NeoPixel
2-
31
import time
42
import board
53
import neopixel
64

75

8-
# On CircuitPlayground Express -> Board.NEOPIXEL
9-
# Otherwise choose an open pin connected to the Data In of the NeoPixel strip,
10-
# such as board.D1
11-
# pylint: disable=no-member
12-
pixpin = board.NEOPIXEL
6+
# On CircuitPlayground Express, and boards with built in status NeoPixel -> board.NEOPIXEL
7+
# Otherwise choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D1
8+
pixel_pin = board.NEOPIXEL
139

14-
# The number of pixels in the strip
15-
numpix = 10
10+
# The number of NeoPixels
11+
num_pixels = 10
1612

17-
# number of colors in each pixel, =3 for RGB, =4 for RGB plus white
18-
BPP = 3
13+
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
14+
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
15+
ORDER = neopixel.GRB
1916

20-
strip = neopixel.NeoPixel(pixpin, numpix, bpp=BPP, brightness=0.3, auto_write=False)
17+
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=False,
18+
pixel_order=ORDER)
2119

22-
def format_tuple(r, g, b):
23-
if BPP == 3:
24-
return (r, g, b)
25-
return (r, g, b, 0)
2620

2721
def wheel(pos):
2822
# Input a value 0 to 255 to get a color value.
2923
# The colours are a transition r - g - b - back to r.
30-
if (pos < 0) or (pos > 255):
31-
return format_tuple(0, 0, 0)
32-
if pos < 85:
33-
return format_tuple(int(pos * 3), int(255 - (pos*3)), 0)
34-
if pos < 170:
24+
if pos < 0 or pos > 255:
25+
r = g = b = 0
26+
elif pos < 85:
27+
r = int(pos * 3)
28+
g = int(255 - pos*3)
29+
b = 0
30+
elif pos < 170:
3531
pos -= 85
36-
return format_tuple(int(255 - pos*3), 0, int(pos*3))
37-
pos -= 170
38-
return format_tuple(0, int(pos*3), int(255 - pos*3))
32+
r = int(255 - pos*3)
33+
g = 0
34+
b = int(pos*3)
35+
else:
36+
pos -= 170
37+
r = 0
38+
g = int(pos*3)
39+
b = int(255 - pos*3)
40+
return (r, g, b) if ORDER == neopixel.RGB or ORDER == neopixel.GRB else (r, g, b, 0)
41+
3942

4043
def rainbow_cycle(wait):
4144
for j in range(255):
42-
for i in range(strip.n):
43-
idx = int((i * 256 / len(strip)) + j)
44-
strip[i] = wheel(idx & 255)
45-
strip.show()
45+
for i in range(num_pixels):
46+
pixel_index = (i * 256 // num_pixels) + j
47+
pixels[i] = wheel(pixel_index & 255)
48+
pixels.show()
4649
time.sleep(wait)
4750

51+
4852
while True:
49-
strip.fill(format_tuple(255, 0, 0))
50-
strip.show()
53+
# Comment this line out if you have RGBW/GRBW NeoPixels
54+
pixels.fill((255, 0, 0))
55+
# Uncomment this line if you have RGBW/GRBW NeoPixels
56+
# pixels.fill((255, 0, 0, 0))
57+
pixels.show()
5158
time.sleep(1)
5259

53-
strip.fill(format_tuple(0, 255, 0))
54-
strip.show()
60+
# Comment this line out if you have RGBW/GRBW NeoPixels
61+
pixels.fill((0, 255, 0))
62+
# Uncomment this line if you have RGBW/GRBW NeoPixels
63+
# pixels.fill((0, 255, 0, 0))
64+
pixels.show()
5565
time.sleep(1)
5666

57-
strip.fill(format_tuple(0, 0, 255))
58-
strip.show()
67+
# Comment this line out if you have RGBW/GRBW NeoPixels
68+
pixels.fill((0, 0, 255))
69+
# Uncomment this line if you have RGBW/GRBW NeoPixels
70+
# pixels.fill((0, 0, 255, 0))
71+
pixels.show()
5972
time.sleep(1)
6073

61-
rainbow_cycle(0.001) # rainbowcycle with 1ms delay per step
74+
rainbow_cycle(0.001) # rainbow cycle with 1ms delay per step

0 commit comments

Comments
 (0)