Hundemeier / sacn

A simple ANSI E1.31 (aka sACN) module for python.
MIT License
48 stars 21 forks source link

Is it possible to change the universe on which we listen even when the code is in progress #44

Open corentindrd opened 1 year ago

corentindrd commented 1 year ago
import time
import tkinter
import rtmidi
import customtkinter
import sacn
import threading
receiver = sacn.sACNreceiver()
receiver.start()

def sacntomidi():
    global channel
    global joinok
    global uni
    while True:
        if activesacn.get() == 1 and joinok == 1 and entryuniverse.get() != "":
            channel = int(entrychannel.get()) - 1
            uni = int(entryuniverse.get())
            receiver.join_multicast(uni)
            joinok = 0
        elif activesacn.get() == 0 and joinok == 0:
            channel = int(entrychannel.get()) - 1
            uni = int(entryuniverse.get())
            receiver.leave_multicast(uni)
            joinok = 1
            pass
        else:
            time.sleep(0.01)
            pass

root = customtkinter.CTk()
customtkinter.set_appearance_mode("System")
root.title("Controle Reaper SACN")

activesacn = customtkinter.IntVar(value=0)
joinok = 1
uni = 1

midiout = rtmidi.MidiOut()
midiout.open_virtual_port("SACN REAPER")

textuniverse = customtkinter.CTkLabel(root, text="Univers", font=("Avenir-Black", 10))
textuniverse.place(relx=0.5, rely=0.2, anchor=tkinter.CENTER)

textchannel = customtkinter.CTkLabel(root, text="Adresse", font=("Avenir-Black", 10))
textchannel.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)

entryuniverse = customtkinter.CTkEntry(root)
entryuniverse.place(relx=0.5, rely=0.3, anchor=tkinter.CENTER)

entrychannel = customtkinter.CTkEntry(root)
entrychannel.place(relx=0.5, rely=0.6, anchor=tkinter.CENTER)

switchartnet = customtkinter.CTkSwitch(master=root, text="", fg_color="white", variable=activesacn, onvalue="1",
                                       offvalue="0")
switchartnet.place(relx=0.65, rely=0.8, anchor=tkinter.CENTER)

@receiver.listen_on('universe', universe=uni)  # listens on universe 1
def callback(packet):  # packet type: sacn.DataPacket
    if packet.dmxData:  # Vérifiez que la liste n'est pas vide
        print(uni)
        value = packet.dmxData[channel]  # Accédez à la première valeur de la liste
        print(value)
        midiout.send_message([0x90, 60, int(value / 255 * 127)])

th1 = threading.Thread(target=sacntomidi, daemon=True)
th1.start()

root.mainloop()
receiver.stop()
Hundemeier commented 1 year ago

I don't know if I understood the question correctly, but the only way I can think of to change the universe of a listener is to unregister the listener and then re-registering the listener. You should be able to use the remove_listener or remove_listener_from_universe functions even from within your callback and also should be able to register inside the callback. However, I'm not quite sure how the Python code for that should look like. Maybe something like receiver.listen_on('universe', callback)() works?