65 lines
1.4 KiB
Python
Raw Normal View History

2022-03-13 17:09:05 +01:00
from osc4py3.as_eventloop import *
from osc4py3 import oscbuildparse
2022-03-20 16:06:57 +01:00
from threading import Lock
mutex = Lock()
2022-04-04 10:48:02 +02:00
temp_mutex = Lock()
2022-03-13 17:09:05 +01:00
2022-03-20 10:45:12 +01:00
def start_osc():
osc_startup()
def terminate_osc():
osc_terminate()
def temperature(temp, name):
print(f'{temp} - {name}')
def update():
2022-03-20 16:06:57 +01:00
global mutex
with mutex:
osc_process()
2022-03-20 10:45:12 +01:00
2022-03-13 17:09:05 +01:00
class OscBroadcaster:
def __init__(self, name: str, host: str, port: str, command_channel: str):
self.name = name
self.host = host
self.port = port
self.cmd = command_channel
2022-03-20 10:45:12 +01:00
osc_udp_client(self.host, self.port, self.name)
2022-03-13 17:09:05 +01:00
def utterance(self, utterance: str, channel: str):
msg = oscbuildparse.OSCMessage(channel, None, [utterance])
osc_send(msg, self.name)
2022-03-20 16:06:57 +01:00
update()
2022-03-13 17:09:05 +01:00
def command(self, command: str):
msg = oscbuildparse.OSCMessage(self.cmd, None, [command])
osc_send(msg, self.name)
2022-03-20 16:06:57 +01:00
update()
2022-03-13 17:09:05 +01:00
2022-04-04 09:12:49 +02:00
def temperature(self, temp: float, channel: str):
2022-04-04 10:48:02 +02:00
global temp_mutex
with temp_mutex:
msg = oscbuildparse.OSCMessage(channel, None, [temp])
osc_send(msg, self.name)
osc_process()
2022-04-04 09:12:49 +02:00
2022-03-20 10:45:12 +01:00
class OscReceiver:
2022-03-13 17:09:05 +01:00
2022-03-20 10:45:12 +01:00
def __init__(self, name: str, host: str, port: str, callback_fn=None):
self.name = name
self.host = host
self.port = port
2022-04-09 18:01:15 +02:00
# osc_udp_server(self.host, self.port, self.name)
osc_udp_server("0.0.0.0", self.port, self.name)
2022-03-20 10:45:12 +01:00
if callback_fn:
osc_method('/', callback_fn)
else:
osc_method('/', temperature)
2022-03-13 17:09:05 +01:00