network stuff?

This commit is contained in:
jane 2020-11-08 23:15:31 -05:00
parent ee040dad58
commit f14582a1cf
3 changed files with 77 additions and 2 deletions

12
leds.py
View file

@ -13,6 +13,7 @@ import light_manager
import pattern import pattern
import logger import logger
import threading import threading
import socket
this = sys.modules[__name__] this = sys.modules[__name__]
@ -56,9 +57,20 @@ def displayon(lcd):
def socket_loop(): def socket_loop():
injected = False injected = False
s = socket.create_server(('0.0.0.0', 29999))
while True: while True:
if not injected: if not injected:
this.lights.set_pattern(pattern.pat) this.lights.set_pattern(pattern.pat)
injected = True
sock, addr = s.accept()
with sock:
length_data = sock.recv(4)
print(length_data)
length = int(length_data.decode())
print(length)
pattern_data = sock.recv(length)
print(pattern_data)
pattern.parse(pattern_data.decode())
def loop(): def loop():

View file

@ -1,8 +1,10 @@
debug_statements = False debug_statements = False
def debug(msg): def debug(msg):
if debug_statements: if debug_statements:
print(msg) print(msg)
def info(msg): def info(msg):
print(msg) print(msg)

View file

@ -1,16 +1,77 @@
import sys import sys
import json
this = sys.modules[__name__] this = sys.modules[__name__]
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): def default(index, tick):
return (((index + tick) * 5) % 255, (tick * 42) % 255, (tick * 50) % 255) return (((index + tick) * 5) % 255, (tick * 42) % 255, (tick * 50) % 255)
def pat(index, tick, previous_values): def pat(index, tick, previous_values):
return default(index, tick) 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].instructions.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): def parse(str):
this.encoded = str this.encoded = str
print(this.encoded) print(this.encoded)
this.pattern = json.loads(this.encoded)