/ QT_Py_Bracelet / code.py
code.py
 1  # SPDX-FileCopyrightText: 2020 Anne Barela for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  # Demo code to generate an alternating color-gradient effect in
 6  # the QT Py LED cuff bracelet LEDs.
 7  import time
 8  import board
 9  from rainbowio import colorwheel
10  import neopixel
11  
12  # Total number of LEDs on both strips
13  NUM_PIXELS = 14
14  
15  pixels = neopixel.NeoPixel(board.MOSI, NUM_PIXELS, pixel_order=neopixel.GRB, auto_write=False, brightness = 0.4
16  )
17  
18      
19  # Scales a tuple by a fraction of 255
20  def scale(tup, frac):
21      return tuple((x*frac)//255 for x in tup)
22  
23  # Sawtooth function with amplitude and period of 255
24  def sawtooth(x):
25      return int(2*(127.5 - abs((x % 255) - 127.5)))
26  
27  # Hue value at the opposite side of the color colorwheel
28  def oppositeHue(x):
29      return ((x + 128) % 256)
30  
31  hueIndex = 0         # determines hue value (0->255)
32  brightnessIndex = 0  # input to the sawtooth function for determining brightness (0->255)
33  brightnessSpeed = 3  # bigger value = faster shifts in brightness
34  
35  while True:
36      bright = sawtooth(brightnessIndex)
37      
38      # get RGB color from colorwheel function and scale it by the brightness
39      mainColor = scale(colorwheel(hueIndex),bright)
40      oppColor = scale(colorwheel(oppositeHue(hueIndex)), 255 - bright)
41  
42      # hue and brightness alternate along each strip
43      for i in range(NUM_PIXELS//2):
44          pixels[i*2] = mainColor
45          pixels[i*2 + 1] = oppColor
46      pixels.show()
47      
48      # increment hue and brightness
49      hueIndex = (hueIndex + 1) % 255        
50      brightnessIndex = (brightnessIndex + brightnessSpeed) % 255