Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wyojustin
Created May 7, 2017 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyojustin/ead0b0d81ce744dc6fbe7a312acba8a3 to your computer and use it in GitHub Desktop.
Save wyojustin/ead0b0d81ce744dc6fbe7a312acba8a3 to your computer and use it in GitHub Desktop.
from bibliopixel.animation import BaseMatrixAnim
import bibliopixel.colors as colors
def getRule(val, n=8):
bits = [(val >> i) & 1 for i in range(n)]
def f(i):
return bits[i]
return f
def getVal(l):
v = sum(v * 2 ** i for i, v in enumerate(l))
return v
def applyRule(row, rule):
out = []
val = getVal([row[-1], row[0], row[1]])
out.append(rule(val))
for i in range(1, len(row) - 1):
val = getVal(row[i-1:i+2])
out.append(rule(val))
val = getVal([row[-2], row[-1], row[0]])
out.append(rule(val))
return out
def test():
bits = [0 for i in range(57)]
bits[28] = 1
rule30 = getRule(30, 8)
for i in range(100):
for b in bits:
if b:
print 'X',
else:
print ' ',
print
bits = applyRule(bits, rule30)
class Wolfram(BaseMatrixAnim):
def __init__(self, led, rule=30):
super(Wolfram, self).__init__(led)
self.rule = getRule(rule)
self.w = led.width
self.h = led.height
self.active_row = [0 for i in range(self.w)]
self.active_row[self.w//2] = 1
self.t = 0
def step(self, amt=1):
self.t += amt
## shift down
for y in range(self.h - 1):
for x in range(self.w):
self._led.set(x, y, self._led.get(x, y + 1))
for i in range(self.w):
if self.active_row[i]:
color = colors.hue2rgb_360((self.t + i) % 360)
self._led.set(i, self.h - 1, color)
else:
self._led.set(i, self.h - 1, (0, 0, 0))
self.active_row = applyRule(self.active_row, self.rule)
MANIFEST = [
{
"class": Wolfram,
"controller": "matrix",
"desc": None,
"display": "Wolfram",
"id": "Wolfram",
"params": [
{
"default": 30,
"help": "",
"id": "rule",
"label": "Rule",
"type": "int"
},
],
"type": "animation"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment