leds/pattern.py

80 lines
1.7 KiB
Python

import sys
import json
this = sys.modules[__name__]
this.encoded = None
this.pattern = None
this.values = {
"stack": 0,
"r": 0,
"g": 0,
"b": 0
}
def constant(target, arg):
return arg
def add(target, arg):
res = target + arg
if res > 255:
res = 255
elif res < 0:
res = 0
return res
def sub(target, arg):
res = target - arg
if res > 255:
res = 255
elif res < 0:
res = 0
return res
def apply(targets, args, func):
for target in range(len(targets)):
if this.values[targets[target]['channel']] != None:
this.values[targets[target]['channel']] = func(
this.values[targets[target]['channel']], args[target])
this.instructions = {
"CONSTANT": constant,
"ADD": add,
"SUB": sub
}
def default(index, tick):
return (((index + tick) * 5) % 255, (tick * 42) % 255, (tick * 50) % 255)
def pat(index, tick, previous_values):
if this.pattern != None:
for i in range(len(this.pattern)):
name = this.pattern[i]['instruction']['name']
targets = this.pattern[i]['instruction']['targets']
args = this.pattern[i]['instruction']['args']
if this.instructions[name] != None:
apply(targets, args, this.instructions[name])
r = this.values["r"]
g = this.values["g"]
b = this.values["b"]
this.values["r"] = 0
this.values["g"] = 0
this.values["b"] = 0
this.values["stack"] = 0
return (r, g, b)
else:
return default(index, tick)
def parse(str):
this.encoded = str
print(this.encoded)
this.pattern = json.loads(this.encoded)