From f14582a1cf0403eea53aee6956648c663f91f9e5 Mon Sep 17 00:00:00 2001 From: Jane Petrovna Date: Sun, 8 Nov 2020 23:15:31 -0500 Subject: [PATCH] network stuff? --- leds.py | 12 +++++++++++ logger.py | 4 +++- pattern.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/leds.py b/leds.py index b100cca..a45cb9d 100644 --- a/leds.py +++ b/leds.py @@ -13,6 +13,7 @@ import light_manager import pattern import logger import threading +import socket this = sys.modules[__name__] @@ -56,9 +57,20 @@ def displayon(lcd): def socket_loop(): injected = False + s = socket.create_server(('0.0.0.0', 29999)) while True: if not injected: 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(): diff --git a/logger.py b/logger.py index f2a6a9f..9f311df 100644 --- a/logger.py +++ b/logger.py @@ -1,8 +1,10 @@ debug_statements = False + def debug(msg): if debug_statements: print(msg) + def info(msg): - print(msg) \ No newline at end of file + print(msg) diff --git a/pattern.py b/pattern.py index 44727e0..44099c8 100644 --- a/pattern.py +++ b/pattern.py @@ -1,16 +1,77 @@ import sys +import json 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): return (((index + tick) * 5) % 255, (tick * 42) % 255, (tick * 50) % 255) 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): this.encoded = str print(this.encoded) + this.pattern = json.loads(this.encoded)