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
13
+ # The number of colors in each pixel: 3 for RGB, 4 for RGBW ( RGB plus white)
18
14
BPP = 3
19
15
20
- strip = neopixel .NeoPixel (pixpin , numpix , bpp = BPP , brightness = 0.3 , auto_write = False )
16
+ pixels = neopixel .NeoPixel (pixel_pin , num_pixels , bpp = BPP , brightness = 0.3 , auto_write = False )
17
+
21
18
22
19
def format_tuple (r , g , b ):
23
20
if BPP == 3 :
24
- return (r , g , b )
25
- return (r , g , b , 0 )
21
+ return r , g , b
22
+ return r , g , b , 0
23
+
26
24
27
25
def wheel (pos ):
28
26
# Input a value 0 to 255 to get a color value.
29
27
# The colours are a transition r - g - b - back to r.
30
- if ( pos < 0 ) or ( pos > 255 ) :
28
+ if pos < 0 or pos > 255 :
31
29
return format_tuple (0 , 0 , 0 )
32
30
if pos < 85 :
33
31
return format_tuple (int (pos * 3 ), int (255 - (pos * 3 )), 0 )
@@ -37,25 +35,27 @@ def wheel(pos):
37
35
pos -= 170
38
36
return format_tuple (0 , int (pos * 3 ), int (255 - pos * 3 ))
39
37
38
+
40
39
def rainbow_cycle (wait ):
41
40
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 ()
41
+ for i in range (num_pixels ):
42
+ pixel_index = ( i * 256 // num_pixels ) + j
43
+ pixels [i ] = wheel (pixel_index & 255 )
44
+ pixels .show ()
46
45
time .sleep (wait )
47
46
47
+
48
48
while True :
49
- strip .fill (format_tuple (255 , 0 , 0 ))
50
- strip .show ()
49
+ pixels .fill (format_tuple (255 , 0 , 0 ))
50
+ pixels .show ()
51
51
time .sleep (1 )
52
52
53
- strip .fill (format_tuple (0 , 255 , 0 ))
54
- strip .show ()
53
+ pixels .fill (format_tuple (0 , 255 , 0 ))
54
+ pixels .show ()
55
55
time .sleep (1 )
56
56
57
- strip .fill (format_tuple (0 , 0 , 255 ))
58
- strip .show ()
57
+ pixels .fill (format_tuple (0 , 0 , 255 ))
58
+ pixels .show ()
59
59
time .sleep (1 )
60
60
61
- rainbow_cycle (0.001 ) # rainbowcycle with 1ms delay per step
61
+ rainbow_cycle (0.001 ) # rainbow cycle with 1ms delay per step
0 commit comments