forked from francocruces/MioConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mio_connect.py
89 lines (72 loc) · 2.36 KB
/
mio_connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from src.myodriver import MyoDriver
from src.config import Config
import serial
import getopt
import sys
def main(argv):
config = Config()
# Get options and arguments
try:
opts, args = getopt.getopt(argv, 'hsn:a:p:v', ['help', 'shutdown', 'nmyo', 'address', 'port', 'verbose'])
except getopt.GetoptError:
sys.exit(2)
turnoff = False
for opt, arg in opts:
if opt in ('-h', '--help'):
print_usage()
sys.exit()
elif opt in ('-s', '--shutdown'):
turnoff = True
elif opt in ("-n", "--nmyo"):
config.MYO_AMOUNT = int(arg)
elif opt in ("-a", "--address"):
config.OSC_ADDRESS = arg
elif opt in ("-p", "--port"):
config.OSC_PORT = arg
elif opt in ("-v", "--verbose"):
config.VERBOSE = True
# Run
myo_driver = None
try:
# Init
myo_driver = MyoDriver(config)
# Connect
myo_driver.run()
if turnoff:
# Turn off
myo_driver.deep_sleep_all()
return
if Config.GET_MYO_INFO:
# Get info
myo_driver.get_info()
print("Ready for data.")
print()
# Receive and handle data
while True:
myo_driver.receive()
except KeyboardInterrupt:
print("Interrupted.")
except serial.serialutil.SerialException:
print("ERROR: Couldn't open port. Please close MyoConnect and any program using this serial port.")
finally:
print("Disconnecting...")
if myo_driver is not None:
if Config.DEEP_SLEEP_AT_KEYBOARD_INTERRUPT:
myo_driver.deep_sleep_all()
else:
myo_driver.disconnect_all()
print("Disconnected")
def print_usage():
message = """usage: python mio_connect.py [-h | --help] [-s | --shutdown] [-n | --nmyo <amount>] [-a | --address \
<address>] [-p | --port <port_number>] [-v | --verbose]
Options and arguments:
-h | --help: display this message
-s | --shutdown: turn off (deep_sleep) the expected amount of myos
-n | --nmyo <amount>: set the amount of devices to expect
-a | --address <address>: set OSC address
-p | --port <port_number>: set OSC port
-v | --verbose: get verbose output
"""
print(message)
if __name__ == "__main__":
main(sys.argv[1:])