|
| 1 | +# Demo code to generate an alternating color-gradient effect in |
| 2 | +# the QT Py LED cuff bracelet LEDs. |
| 3 | +import time |
| 4 | +import board |
| 5 | +import neopixel |
| 6 | + |
| 7 | +# Total number of LEDs on both strips |
| 8 | +NUM_PIXELS = 14 |
| 9 | + |
| 10 | +pixels = neopixel.NeoPixel(board.MOSI, NUM_PIXELS, pixel_order=neopixel.GRB, auto_write=False, brightness = 0.4 |
| 11 | +) |
| 12 | + |
| 13 | +def wheel(pos): |
| 14 | + # Input a value 0 to 255 to get a color value. |
| 15 | + # The colours are a transition r - g - b - back to r. |
| 16 | + if pos < 0 or pos > 255: |
| 17 | + return (0, 0, 0) |
| 18 | + if pos < 85: |
| 19 | + return (255 - pos * 3, pos * 3, 0) |
| 20 | + if pos < 170: |
| 21 | + pos -= 85 |
| 22 | + return (0, 255 - pos * 3, pos * 3) |
| 23 | + pos -= 170 |
| 24 | + return (pos * 3, 0, 255 - pos * 3) |
| 25 | + |
| 26 | +# Scales a tuple by a fraction of 255 |
| 27 | +def scale(tup, frac): |
| 28 | + return tuple((x*frac)//255 for x in tup) |
| 29 | + |
| 30 | +# Sawtooth function with amplitude and period of 255 |
| 31 | +def sawtooth(x): |
| 32 | + return int(2*(127.5 - abs((x % 255) - 127.5))) |
| 33 | + |
| 34 | +# Hue value at the opposite side of the color wheel |
| 35 | +def oppositeHue(x): |
| 36 | + return ((x + 128) % 256) |
| 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 | + |
| 42 | +while True: |
| 43 | + bright = sawtooth(brightnessIndex) |
| 44 | + |
| 45 | + # get RGB color from wheel function and scale it by the brightness |
| 46 | + mainColor = scale(wheel(hueIndex),bright) |
| 47 | + oppColor = scale(wheel(oppositeHue(hueIndex)), 255 - bright) |
| 48 | + |
| 49 | + # hue and brightness alternate along each strip |
| 50 | + for i in range(NUM_PIXELS//2): |
| 51 | + pixels[i*2] = mainColor |
| 52 | + pixels[i*2 + 1] = oppColor |
| 53 | + pixels.show() |
| 54 | + |
| 55 | + # increment hue and brightness |
| 56 | + hueIndex = (hueIndex + 1) % 255 |
| 57 | + brightnessIndex = (brightnessIndex + brightnessSpeed) % 255 |
0 commit comments