Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit d8012d4

Browse files
Update osc_module.py
1 parent aa8249a commit d8012d4

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

modules/osc_module.py

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,48 @@
66

77
ANIMATION_DIR = 'modules/animations'
88

9-
def load_animation(animation_name):
10-
"""Load animation data from a JSON file."""
11-
file_path = os.path.join(ANIMATION_DIR, f'{animation_name}.json')
12-
if not os.path.isfile(file_path):
13-
print(f"Animation file '{file_path}' does not exist.")
14-
return None
15-
with open(file_path, 'r') as file:
16-
return json.load(file)
17-
18-
def handle_prediction(left, right):
19-
"""Determine which animation to execute based on prediction values."""
20-
if left > 0.6:
21-
animation_name = 'Llft' # Load 'Llft' animation for left
22-
elif right > 0.6:
23-
animation_name = 'Rlft' # Load 'Rlft' animation for right
24-
else:
25-
animation_name = 'lower' # Load 'lower' animation
26-
27-
# Send the animation action to the Animate class via PubSub
28-
pub.sendMessage('animate', action=animation_name)
29-
30-
def start_osc_server():
31-
"""Initialize and start the OSC server to handle messages."""
32-
def callback(left, right):
33-
"""Handle OSC messages and execute animations based on predictions."""
34-
print("Left prediction : ", round(left, 2))
35-
print("Right prediction : ", round(right, 2))
36-
handle_prediction(left, right)
37-
38-
osc = OSCThreadServer()
39-
osc.listen(address='127.0.0.1', port=9002, default=True)
40-
osc.bind(b'/neuropype', callback)
41-
42-
print("OSC server started.")
43-
while True:
44-
sleep(1)
9+
class StartOSCServer:
10+
def __init__(self, address='127.0.0.1', port=9002):
11+
self.osc = OSCThreadServer()
12+
self.address = address
13+
self.port = port
14+
15+
def load_animation(self, animation_name):
16+
"""Load animation data from a JSON file."""
17+
file_path = os.path.join(ANIMATION_DIR, f'{animation_name}.json')
18+
if not os.path.isfile(file_path):
19+
print(f"Animation file '{file_path}' does not exist.")
20+
return None
21+
with open(file_path, 'r') as file:
22+
return json.load(file)
23+
24+
def handle_prediction(self, left, right):
25+
"""Determine which animation to execute based on prediction values."""
26+
if left > 0.6:
27+
animation_name = 'Llft' # Load 'Llft' animation for left
28+
elif right > 0.6:
29+
animation_name = 'Rlft' # Load 'Rlft' animation for right
30+
else:
31+
animation_name = 'lower' # Load 'lower' animation
32+
33+
# Send the animation action to the Animate class via PubSub
34+
pub.sendMessage('animate', action=animation_name)
35+
36+
def start_server(self):
37+
"""Initialize and start the OSC server to handle messages."""
38+
def callback(left, right):
39+
"""Handle OSC messages and execute animations based on predictions."""
40+
print("Left prediction : ", round(left, 2))
41+
print("Right prediction : ", round(right, 2))
42+
self.handle_prediction(left, right)
43+
44+
self.osc.listen(address=self.address, port=self.port, default=True)
45+
self.osc.bind(b'/neuropype', callback)
46+
47+
print("OSC server started.")
48+
while True:
49+
sleep(1)
4550

4651
if __name__ == "__main__":
47-
start_osc_server()
52+
osc_server = StartOSCServer()
53+
osc_server.start_server()

0 commit comments

Comments
 (0)