1
1
# Demo code to generate an alternating color-gradient effect in
2
- # the QT Py LED cuff bracelet
2
+ # the QT Py LED cuff bracelet LEDs.
3
3
import time
4
4
import board
5
- import neopixel_spi as neopixel
6
-
5
+ import neopixel
6
+
7
7
# Total number of LEDs on both strips
8
8
NUM_PIXELS = 14
9
-
10
- # Using the neopixel_spi library because the LED strip signal uses the SPI bus. It
11
- # would work just as well with the standard neopixel library. This particular code
12
- # As written, this code doesnt require the faster speed of SPI, but it may prove useful
13
- # if additional functionality is added, e.g. reading and responding to sensor inputs
14
- spi = board .SPI ()
15
- pixels = neopixel .NeoPixel_SPI (
16
- spi , NUM_PIXELS , pixel_order = neopixel .GRB , auto_write = False , brightness = 0.4
9
+
10
+ pixels = neopixel .NeoPixel (board .MOSI , NUM_PIXELS , pixel_order = neopixel .GRB , auto_write = False , brightness = 0.4
17
11
)
18
-
12
+
19
13
def wheel (pos ):
20
14
# Input a value 0 to 255 to get a color value.
21
15
# The colours are a transition r - g - b - back to r.
@@ -32,32 +26,32 @@ def wheel(pos):
32
26
# Scales a tuple by a fraction of 255
33
27
def scale (tup , frac ):
34
28
return tuple ((x * frac )// 255 for x in tup )
35
-
29
+
36
30
# Sawtooth function with amplitude and period of 255
37
31
def sawtooth (x ):
38
32
return int (2 * (127.5 - abs ((x % 255 ) - 127.5 )))
39
-
40
- #Hue value at the opposite side of the color wheel
33
+
34
+ # Hue value at the opposite side of the color wheel
41
35
def oppositeHue (x ):
42
36
return ((x + 128 ) % 256 )
43
-
44
- hueIndex = 0 #determines hue value (0->255)
45
- brightnessIndex = 0 #input to the sawtooth function for determining brightness (0->255)
46
- brightnessSpeed = 3 #bigger value = faster shifts in brightness n
47
-
37
+
38
+ hueIndex = 0 # determines hue value (0->255)
39
+ brightnessIndex = 0 # input to the sawtooth function for determining brightness (0->255)
40
+ brightnessSpeed = 3 # bigger value = faster shifts in brightness
41
+
48
42
while True :
49
43
bright = sawtooth (brightnessIndex )
50
44
51
- #get RGB color from wheel function and scale it by the brightness
45
+ # get RGB color from wheel function and scale it by the brightness
52
46
mainColor = scale (wheel (hueIndex ),bright )
53
47
oppColor = scale (wheel (oppositeHue (hueIndex )), 255 - bright )
54
-
55
- #hue and brightness alternate along each strip
48
+
49
+ # hue and brightness alternate along each strip
56
50
for i in range (NUM_PIXELS // 2 ):
57
51
pixels [i * 2 ] = mainColor
58
52
pixels [i * 2 + 1 ] = oppColor
59
53
pixels .show ()
60
54
61
- #increment hue and brightness
55
+ # increment hue and brightness
62
56
hueIndex = (hueIndex + 1 ) % 255
63
57
brightnessIndex = (brightnessIndex + brightnessSpeed ) % 255
0 commit comments