11# 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.
33import time
44import board
5- import neopixel_spi as neopixel
6-
5+ import neopixel
6+
77# Total number of LEDs on both strips
88NUM_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
1711)
18-
12+
1913def wheel (pos ):
2014 # Input a value 0 to 255 to get a color value.
2115 # The colours are a transition r - g - b - back to r.
@@ -32,32 +26,32 @@ def wheel(pos):
3226# Scales a tuple by a fraction of 255
3327def scale (tup , frac ):
3428 return tuple ((x * frac )// 255 for x in tup )
35-
29+
3630# Sawtooth function with amplitude and period of 255
3731def sawtooth (x ):
3832 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
4135def oppositeHue (x ):
4236 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+
4842while True :
4943 bright = sawtooth (brightnessIndex )
5044
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
5246 mainColor = scale (wheel (hueIndex ),bright )
5347 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
5650 for i in range (NUM_PIXELS // 2 ):
5751 pixels [i * 2 ] = mainColor
5852 pixels [i * 2 + 1 ] = oppColor
5953 pixels .show ()
6054
61- #increment hue and brightness
55+ # increment hue and brightness
6256 hueIndex = (hueIndex + 1 ) % 255
6357 brightnessIndex = (brightnessIndex + brightnessSpeed ) % 255
0 commit comments