Update code.py
Change from using neopixel_spi to neopixel
This commit is contained in:
parent
03a37a9b4d
commit
c98362fe7b
1 changed files with 18 additions and 24 deletions
|
|
@ -1,21 +1,15 @@
|
|||
# Demo code to generate an alternating color-gradient effect in
|
||||
# the QT Py LED cuff bracelet
|
||||
# the QT Py LED cuff bracelet LEDs.
|
||||
import time
|
||||
import board
|
||||
import neopixel_spi as neopixel
|
||||
|
||||
import neopixel
|
||||
|
||||
# Total number of LEDs on both strips
|
||||
NUM_PIXELS = 14
|
||||
|
||||
# Using the neopixel_spi library because the LED strip signal uses the SPI bus. It
|
||||
# would work just as well with the standard neopixel library. This particular code
|
||||
# As written, this code doesnt require the faster speed of SPI, but it may prove useful
|
||||
# if additional functionality is added, e.g. reading and responding to sensor inputs
|
||||
spi = board.SPI()
|
||||
pixels = neopixel.NeoPixel_SPI(
|
||||
spi, NUM_PIXELS, pixel_order=neopixel.GRB, auto_write=False, brightness = 0.4
|
||||
|
||||
pixels = neopixel.NeoPixel(board.MOSI, NUM_PIXELS, pixel_order=neopixel.GRB, auto_write=False, brightness = 0.4
|
||||
)
|
||||
|
||||
|
||||
def wheel(pos):
|
||||
# Input a value 0 to 255 to get a color value.
|
||||
# The colours are a transition r - g - b - back to r.
|
||||
|
|
@ -32,32 +26,32 @@ def wheel(pos):
|
|||
# Scales a tuple by a fraction of 255
|
||||
def scale(tup, frac):
|
||||
return tuple((x*frac)//255 for x in tup)
|
||||
|
||||
|
||||
# Sawtooth function with amplitude and period of 255
|
||||
def sawtooth(x):
|
||||
return int(2*(127.5 - abs((x % 255) - 127.5)))
|
||||
|
||||
#Hue value at the opposite side of the color wheel
|
||||
|
||||
# Hue value at the opposite side of the color wheel
|
||||
def oppositeHue(x):
|
||||
return ((x + 128) % 256)
|
||||
|
||||
hueIndex = 0 #determines hue value (0->255)
|
||||
brightnessIndex = 0 #input to the sawtooth function for determining brightness (0->255)
|
||||
brightnessSpeed = 3 #bigger value = faster shifts in brightness n
|
||||
|
||||
|
||||
hueIndex = 0 # determines hue value (0->255)
|
||||
brightnessIndex = 0 # input to the sawtooth function for determining brightness (0->255)
|
||||
brightnessSpeed = 3 # bigger value = faster shifts in brightness
|
||||
|
||||
while True:
|
||||
bright = sawtooth(brightnessIndex)
|
||||
|
||||
#get RGB color from wheel function and scale it by the brightness
|
||||
# get RGB color from wheel function and scale it by the brightness
|
||||
mainColor = scale(wheel(hueIndex),bright)
|
||||
oppColor = scale(wheel(oppositeHue(hueIndex)), 255 - bright)
|
||||
|
||||
#hue and brightness alternate along each strip
|
||||
|
||||
# hue and brightness alternate along each strip
|
||||
for i in range(NUM_PIXELS//2):
|
||||
pixels[i*2] = mainColor
|
||||
pixels[i*2 + 1] = oppColor
|
||||
pixels.show()
|
||||
|
||||
#increment hue and brightness
|
||||
# increment hue and brightness
|
||||
hueIndex = (hueIndex + 1) % 255
|
||||
brightnessIndex = (brightnessIndex + brightnessSpeed) % 255
|
||||
|
|
|
|||
Loading…
Reference in a new issue