Skip to content

Commit 0dd063e

Browse files
author
zhoufeiy
committed
Add more comprehensive example and clean up.
1 parent f2cf4df commit 0dd063e

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

blossom_network_interface.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ def __init__(self, server_ip, server_port):
1818

1919
self.url = f"http://{self.server_ip}:{self.server_port}/data"
2020
self.data = {
21-
"function": "do_start_sequence",
21+
"function": "do_sequence",
2222
"kwargs": {
2323
"blossom_id": 0,
2424
"delay_time": 0.5,
25-
"audio_length": 20,
2625
"seq": "reset"
2726
}
2827
}

example.py

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
2+
import threading
23
from time import sleep
4+
from time import time
35

46
import dotenv
57
from blossom_interface import BlossomInterface
@@ -8,7 +10,9 @@
810

911
if __name__ == '__main__':
1012
dotenv.load_dotenv()
11-
bl = BlossomInterface(False, os.getenv("SERVER_IP"), os.getenv("SERVER_PORT"))
13+
# Initialize blossom interface
14+
# Multi-blossom setup will only work with network interface
15+
bl = BlossomInterface(True, os.getenv("SERVER_IP"), os.getenv("SERVER_PORT"))
1216

1317
# Control blossom with bl object
1418
bl.reset()
@@ -27,4 +31,36 @@
2731
bl.do_sequence(idle_sequences[0], 0, 0)
2832
random.shuffle(idle_sequences)
2933
bl.do_sequence(idle_sequences[0], 0, 1)
30-
input()
34+
35+
# You might want to control blossom without use sleep() that blocks main program. In that case, use threading.
36+
# In most cases, sleep() is used for playing several sequences in a row. Because bl.do_sequence() will interrupt
37+
# current sequence.
38+
# You can use threading with sleep to do this.
39+
# Or if your sequence is not dynamic, you can use blossom_sequence_comb.py to combine sequences to be a new
40+
# sequence.
41+
42+
# Example:
43+
class MyBlossomInterface:
44+
def __init__(self, bli):
45+
self.bli = bli
46+
47+
def do_idle_sequences(self, delay_time, blossom_id, total_rounds):
48+
my_idle_sequences = ["breathing/exhale", "breathing/inhale", "fear/fear_startled", "happy/happy_lookingup",
49+
"sad/sad_downcast"]
50+
sleep(delay_time)
51+
for i in range(total_rounds):
52+
random.shuffle(my_idle_sequences)
53+
print(f"Sequence: {my_idle_sequences[0]}, Blossom ID: {0}")
54+
self.bli.do_sequence(my_idle_sequences[0], 0, 0)
55+
random.shuffle(my_idle_sequences)
56+
print(f"Sequence: {my_idle_sequences[0]}, Blossom ID: {1}")
57+
self.bli.do_sequence(my_idle_sequences[0], 0, 1)
58+
sleep(3)
59+
60+
# import threading
61+
my_bl = MyBlossomInterface(bl)
62+
bl_thread_target = my_bl.do_idle_sequences
63+
bl_thread_kwargs = {"delay_time": 0, "blossom_id": 0, "total_rounds": 3}
64+
bl_thread = threading.Thread(target=bl_thread_target, kwargs=bl_thread_kwargs)
65+
bl_thread.start()
66+
# Doing something else

0 commit comments

Comments
 (0)