Skip to content

Commit 03a37a9

Browse files
authored
Create code.py
1 parent e25446a commit 03a37a9

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

QT_Py_Bracelet/code.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Demo code to generate an alternating color-gradient effect in
2+
# the QT Py LED cuff bracelet
3+
import time
4+
import board
5+
import neopixel_spi as neopixel
6+
7+
# Total number of LEDs on both strips
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
17+
)
18+
19+
def wheel(pos):
20+
# Input a value 0 to 255 to get a color value.
21+
# The colours are a transition r - g - b - back to r.
22+
if pos < 0 or pos > 255:
23+
return (0, 0, 0)
24+
if pos < 85:
25+
return (255 - pos * 3, pos * 3, 0)
26+
if pos < 170:
27+
pos -= 85
28+
return (0, 255 - pos * 3, pos * 3)
29+
pos -= 170
30+
return (pos * 3, 0, 255 - pos * 3)
31+
32+
# Scales a tuple by a fraction of 255
33+
def scale(tup, frac):
34+
return tuple((x*frac)//255 for x in tup)
35+
36+
# Sawtooth function with amplitude and period of 255
37+
def sawtooth(x):
38+
return int(2*(127.5 - abs((x % 255) - 127.5)))
39+
40+
#Hue value at the opposite side of the color wheel
41+
def oppositeHue(x):
42+
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+
48+
while True:
49+
bright = sawtooth(brightnessIndex)
50+
51+
#get RGB color from wheel function and scale it by the brightness
52+
mainColor = scale(wheel(hueIndex),bright)
53+
oppColor = scale(wheel(oppositeHue(hueIndex)), 255 - bright)
54+
55+
#hue and brightness alternate along each strip
56+
for i in range(NUM_PIXELS//2):
57+
pixels[i*2] = mainColor
58+
pixels[i*2 + 1] = oppColor
59+
pixels.show()
60+
61+
#increment hue and brightness
62+
hueIndex = (hueIndex + 1) % 255
63+
brightnessIndex = (brightnessIndex + brightnessSpeed) % 255

0 commit comments

Comments
 (0)