-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsense.py
executable file
·130 lines (106 loc) · 3.4 KB
/
sense.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python
"""
sense.py
"""
import sys
from scientisst import *
from scientisst import __version__
from threading import Timer
from threading import Event
from sense_src.arg_parser import ArgParser
from sense_src.custom_script import get_custom_script, CustomScript
from sense_src.device_picker import DevicePicker
from sense_src.file_writer import *
def run_scheduled_task(duration, stop_event):
def stop(stop_event):
stop_event.set()
timer = Timer(duration, stop, [stop_event])
timer.start()
return timer
def main():
arg_parser = ArgParser()
args = arg_parser.args
if args.version:
sys.stdout.write("sense.py version {}\n".format(__version__))
sys.exit(0)
if args.address:
address = args.address
else:
if args.mode == COM_MODE_BT:
address = DevicePicker().select_device()
if not address:
arg_parser.error("No paired device found")
else:
arg_parser.error("No address provided")
args.channels = sorted(map(int, args.channels.split(",")))
api_mode = API_MODE_DICT[args.api]
scientisst = ScientISST(address, com_mode=args.mode,
log=args.log, api=api_mode)
try:
if args.output:
firmware_version = scientisst.version_and_adc_chars(print=False)
file_writer = FileWriter(
args.output,
address,
args.fs,
args.channels,
args.convert,
__version__,
firmware_version,
args.api
)
if args.stream:
from sense_src.stream_lsl import StreamLSL
lsl = StreamLSL(
args.channels,
args.fs,
address,
)
if args.script:
script = get_custom_script(args.script)
stop_event = Event()
scientisst.start(args.fs, args.channels)
sys.stdout.write("Start acquisition\n")
if args.output:
file_writer.start()
if args.stream:
lsl.start()
if args.script:
script.start()
timer = None
if args.duration > 0:
timer = run_scheduled_task(args.duration, stop_event)
try:
if args.verbose:
header = "\t".join(get_header(
args.channels, args.convert, args.api)) + "\n"
sys.stdout.write(header)
while not stop_event.is_set():
frames = scientisst.read(convert=args.convert)
if args.output:
file_writer.put(frames)
if args.stream:
lsl.put(frames)
if args.script:
script.put(frames)
if args.verbose:
sys.stdout.write("{}\n".format(frames[0]))
except KeyboardInterrupt:
if args.duration and timer:
timer.cancel()
pass
scientisst.stop()
# let the acquisition stop before stoping other threads
time.sleep(0.25)
sys.stdout.write("Stop acquisition\n")
if args.output:
file_writer.stop()
if args.stream:
lsl.stop()
if args.script:
script.stop()
finally:
scientisst.disconnect()
sys.exit(0)
if __name__ == "__main__":
main()