|
1 | | -# CircuitPython demo - NeoPixel |
2 | | - |
3 | 1 | import time |
4 | 2 | import board |
5 | 3 | import neopixel |
6 | 4 |
|
7 | 5 |
|
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 |
13 | 9 |
|
14 | | -# The number of pixels in the strip |
15 | | -numpix = 10 |
| 10 | +# The number of NeoPixels |
| 11 | +num_pixels = 10 |
16 | 12 |
|
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 |
19 | 16 |
|
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) |
21 | 19 |
|
22 | | -def format_tuple(r, g, b): |
23 | | - if BPP == 3: |
24 | | - return (r, g, b) |
25 | | - return (r, g, b, 0) |
26 | 20 |
|
27 | 21 | def wheel(pos): |
28 | 22 | # Input a value 0 to 255 to get a color value. |
29 | 23 | # 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: |
35 | 31 | 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 | + |
39 | 42 |
|
40 | 43 | def rainbow_cycle(wait): |
41 | 44 | 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() |
46 | 49 | time.sleep(wait) |
47 | 50 |
|
| 51 | + |
48 | 52 | 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() |
51 | 58 | time.sleep(1) |
52 | 59 |
|
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() |
55 | 65 | time.sleep(1) |
56 | 66 |
|
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() |
59 | 72 | time.sleep(1) |
60 | 73 |
|
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