Skip to content

Instantly share code, notes, and snippets.

@dmpayton
Created October 10, 2020 01:50
Show Gist options
  • Save dmpayton/a04a99546acff078babaa2240d5f6c9d to your computer and use it in GitHub Desktop.
Save dmpayton/a04a99546acff078babaa2240d5f6c9d to your computer and use it in GitHub Desktop.
rainbow_chaser.py
import board
import time
import neopixel
from random import choice, randint
leds = neopixel.NeoPixel(board.RX, 50, pixel_order=neopixel.RGB)
color = (128, 0, 255)
def permutations(iterable, r=None):
"""permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)"""
pool = tuple(iterable)
n = len(pool)
if r is None:
r = n
indices = list(range(n))
cycles = list(range(n - r + 1, n + 1))[::-1]
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i + 1 :] + indices[i : i + 1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
pattern = []
for c in permutations(color):
pattern.extend([c] + [[max(l - (40 * x), 0) for l in c] for x in range(1, 20)])
pos = 0
while True:
leds[1:] = leds[0:]
leds[0] = pattern[pos % len(pattern)]
pos += 1
if pos == len(pattern):
pos = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment