#!/usr/bin/env python3

import sacn
import time

ipaddr = "192.168.68.130"


sender = None
data = None

start = time.time()

def init():
    global sender
    global data
    sender = sacn.sACNsender(universeDiscovery=False)
    sender.start()  # start the sending thread
    sender.activate_output(2)  # start sending out data
    sender[2].destination = ipaddr
    sender.manual_flush = True

    # initialize global pixel data list
    data = list()
    for x in range(170):
        data.append((1,2,3)) # some random data

def fastsendall(datain):
    # send all LED data to all controllers
    # data must have all LED data in it as [(R,G,B,)] tuples in an array, 1 tuple per pixel
    global sender
    sender[2].dmx_data = list(sum(datain[0:170] , ())) # flatten the subsection of the data array
    
    sender.flush()

def close():
    global sender
    time.sleep(0.5)
    sender.stop()

def send_fps(fps=140):
    global start
    while time.time() - start < 1/fps:
        time.sleep(0.00001)
    print("FPS:", 1 / (time.time() - start))
    start = time.time()
    global data
    fastsendall(data)

    
if __name__ == "__main__":
    init()
    while True:
        send_fps()

    time.sleep(1)
    close()
    #sys.exit(0)