import asyncio import socket import copy import logging from artnet import buildPacket from effects import * import state AVAILABLE_EFFECTS = [rainbow_whole, rainbow_whole_offset_stat, rainbow_whole_offset_dyn, two_colors_red_blue, two_colors_red_red, two_colors_cyan_blue, two_colors_cyan_orange ] current_effect = rainbow_whole framecount = 0 def function_str(func): return str(func).split(" ")[1] def get_options(): return { "effects": { "available": [ function_str(e) for e in AVAILABLE_EFFECTS ] } } def set_immidiate(framecount, channels, update): for inx, (current, target) in enumerate(zip(channels["current"], channels["target"])): update(framecount, inx, target) for i in range (0,3): current[i] = target[i] # TODO make this an acceptor and then mutate async def handle_state_change(old, new): global current_effect global framecount if new["current_effect"] != function_str(current_effect): new_effect = [ x for x in AVAILABLE_EFFECTS if function_str(x) == new["current_effect"] ][0] current_effect = new_effect if new_effect != None else current_effect framecount = 0 async def main(target_ips): global current_effect global framecount channels = { "target": [], "current": [] } for x in range(0,4): channels["target"].append( [0,0,0] ) channels["current"].append( [0,0,0] ) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) debug = False current_state = await state.get_data() state.register_callback(handle_state_change) await state.mutate({ "current_effect": function_str(current_effect) }) await state.inc_initialized() while True: old_channels = copy.deepcopy(channels["current"]) set_immidiate(framecount, channels, current_effect) if old_channels != channels["current"]: if debug: print("frame:{} channels:{}".format(framecount, channels)) channels_transmit = channels["current"] for ip in target_ips: sock.sendto(buildPacket(1, 255, channels_transmit), (ip, 6454)) await asyncio.sleep(0.02) framecount+=1 sock.close()