add conditionals
This commit is contained in:
parent
3116435878
commit
d445996bdb
2 changed files with 50 additions and 22 deletions
1
leds.py
1
leds.py
|
@ -71,6 +71,7 @@ def socket_loop():
|
||||||
pattern_data = sock.recv(length)
|
pattern_data = sock.recv(length)
|
||||||
logger.debug(pattern_data)
|
logger.debug(pattern_data)
|
||||||
pattern.parse(pattern_data.decode())
|
pattern.parse(pattern_data.decode())
|
||||||
|
this.color_state = 7
|
||||||
|
|
||||||
|
|
||||||
def loop():
|
def loop():
|
||||||
|
|
71
pattern.py
71
pattern.py
|
@ -9,6 +9,11 @@ this.encoded = None
|
||||||
this.pattern = None
|
this.pattern = None
|
||||||
this.values = {
|
this.values = {
|
||||||
"stack": 0,
|
"stack": 0,
|
||||||
|
"stack2": 0,
|
||||||
|
"stack3": 0,
|
||||||
|
"stack4": 0,
|
||||||
|
"stack5": 0,
|
||||||
|
"stack6": 0,
|
||||||
"r": 0,
|
"r": 0,
|
||||||
"g": 0,
|
"g": 0,
|
||||||
"b": 0,
|
"b": 0,
|
||||||
|
@ -17,52 +22,70 @@ this.values = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def constant(target, arg):
|
def constant(target, arg, index):
|
||||||
return arg
|
return (arg, index)
|
||||||
|
|
||||||
|
|
||||||
def add(target, arg):
|
def add(target, arg, index):
|
||||||
return target + arg
|
return (target + arg, index)
|
||||||
|
|
||||||
|
|
||||||
def sub(target, arg):
|
def sub(target, arg, index):
|
||||||
return target - arg
|
return (target - arg, index)
|
||||||
|
|
||||||
|
|
||||||
def mult(target, arg):
|
def mult(target, arg, index):
|
||||||
return target * arg
|
return (target * arg, index)
|
||||||
|
|
||||||
|
|
||||||
def div(target, arg):
|
def div(target, arg, index):
|
||||||
return target / arg
|
return (target / arg, index)
|
||||||
|
|
||||||
|
|
||||||
def mod(target, arg):
|
def mod(target, arg, index):
|
||||||
if arg <= 0:
|
if arg <= 0:
|
||||||
return target
|
return target
|
||||||
return target % arg
|
return (target % arg, index)
|
||||||
|
|
||||||
|
|
||||||
def rand(target, arg):
|
def rand(target, arg):
|
||||||
return random.randrange(0, 255)
|
return random.randrange(0, 255)
|
||||||
|
|
||||||
|
|
||||||
def _apply(target, arg, func):
|
def jnz(target, arg, index):
|
||||||
|
if target != 0:
|
||||||
|
return (target, arg)
|
||||||
|
else:
|
||||||
|
return (target, index)
|
||||||
|
|
||||||
|
|
||||||
|
def jez(target, arg, index):
|
||||||
|
if target == 0:
|
||||||
|
return (target, arg)
|
||||||
|
else:
|
||||||
|
return (target, index)
|
||||||
|
|
||||||
|
|
||||||
|
def _apply(index, target, arg, func):
|
||||||
if type(arg) is int:
|
if type(arg) is int:
|
||||||
return func(target, arg)
|
return func(target, arg, index)
|
||||||
elif type(arg) is str and arg in this.values:
|
elif type(arg) is str and arg in this.values:
|
||||||
return func(target, this.values[arg])
|
return func(target, this.values[arg], index)
|
||||||
|
|
||||||
|
|
||||||
def apply(targets, args, func):
|
def apply(index, targets, args, func):
|
||||||
for target in range(len(targets)):
|
for target in range(len(targets)):
|
||||||
if this.values[targets[target]['channel']] != None:
|
if this.values[targets[target]['channel']] != None:
|
||||||
if target < len(args):
|
if target < len(args):
|
||||||
this.values[targets[target]['channel']] = _apply(
|
val, jump = _apply(
|
||||||
this.values[targets[target]['channel']], args[target], func)
|
index, this.values[targets[target]['channel']], args[target], func)
|
||||||
|
if val != this.values[targets[target]['channel']]:
|
||||||
|
this.values[targets[target]['channel']] = val
|
||||||
else:
|
else:
|
||||||
this.values[targets[target]['channel']] = _apply(
|
val, jump = _apply(
|
||||||
this.values[targets[target]['channel']], 0, func)
|
index, this.values[targets[target]['channel']], 0, func)
|
||||||
|
if val != this.values[targets[target]['channel']]:
|
||||||
|
this.values[targets[target]['channel']] = val
|
||||||
logger.debug("{} : {}".format(
|
logger.debug("{} : {}".format(
|
||||||
targets[target]['channel'],
|
targets[target]['channel'],
|
||||||
this.values[targets[target]['channel']]
|
this.values[targets[target]['channel']]
|
||||||
|
@ -76,7 +99,9 @@ this.instructions = {
|
||||||
"MULTIPLY": mult,
|
"MULTIPLY": mult,
|
||||||
"DIVIDE": div,
|
"DIVIDE": div,
|
||||||
"MODULO": mod,
|
"MODULO": mod,
|
||||||
"RANDOM": rand
|
"RANDOM": rand,
|
||||||
|
"JNZ": jnz,
|
||||||
|
"JEZ": jez
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -96,7 +121,9 @@ def pat(index, tick, previous_values):
|
||||||
if 'args' in this.pattern[i]['instruction']:
|
if 'args' in this.pattern[i]['instruction']:
|
||||||
args = this.pattern[i]['instruction']['args']
|
args = this.pattern[i]['instruction']['args']
|
||||||
if this.instructions[name] != None:
|
if this.instructions[name] != None:
|
||||||
apply(targets, args, this.instructions[name])
|
jump = apply(index, targets, args, this.instructions[name])
|
||||||
|
if jump != i and jump < len(this.pattern) - 1:
|
||||||
|
i = jump - 1
|
||||||
r = this.values["r"]
|
r = this.values["r"]
|
||||||
g = this.values["g"]
|
g = this.values["g"]
|
||||||
b = this.values["b"]
|
b = this.values["b"]
|
||||||
|
|
Loading…
Reference in a new issue