Files
moodconsole/processor.py

75 lines
2.4 KiB
Python

import asyncio
import socket
import copy
import logging
from artnet import buildPacket
from effects import *
import state
AVAILABLE_EFFECTS = [ black, 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 serialize_internal_state():
global current_effect
return {
"current_effect": function_str(current_effect)
}
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
await state.mutate(serialize_internal_state())
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 = state.get_data()
state.register_on_input_callback(handle_state_change)
await state.mutate(serialize_internal_state())
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()