-
Notifications
You must be signed in to change notification settings - Fork 5
/
libmpdev.py
360 lines (283 loc) · 9.49 KB
/
libmpdev.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# -*- coding: utf-8 -*-
#
# This file is part of PyGaze - the open-source toolbox for eye tracking
#
# PyGaze is a Python module for easily creating gaze contingent experiments
# or other software (as well as non-gaze contingent experiments/software)
# Copyright (C) 2012-2014 Edwin S. Dalmaijer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import os
import copy
import time
from ctypes import windll, c_int, c_double, byref
from threading import Thread, Lock
import numpy
# load DLL
try:
mpdev = windll.LoadLibrary('mpdev.dll')
except:
try:
mpdev = windll.LoadLibrary(os.path.join(os.path.dirname(os.path.abspath(__file__)),'mpdev.dll'))
except:
raise Exception("Error in libmpdev: could not load mpdev.dll")
# error handling
def check_returncode(returncode):
"""
desc:
Checks a BioPac MP150 returncode, and returns it's meaning as a human readable string
arguments:
returncode:
desc: A code returned by one of the functions from the mpdev DLL
type: int
returns:
desc: A string describing the error
type: str
"""
if returncode == 1:
meaning = "MPSUCCESS"
else:
meaning = "UNKNOWN"
return meaning
# class definition
class MP150:
"""
desc:
Class to communicate with BioPax MP150 Squeezies.
"""
def __init__(self, logfile='default', samplerate=200):
"""
desc:
Finds an MP150, and initializes a connection.
keywords:
logfile:
desc:Name of the logfile (optionally with path), which will
be used to create a textfile, e.g.
'default_MP150_data.tsv' (default = 'default')
type:str
samplerate:
desc:The sampling rate in Hertz (default = 200).
type:int
"""
# settings
self._samplerate = samplerate
self._sampletime = 1000.0 / self._samplerate
self._sampletimesec = self._sampletime / 1000.0
self._logfilename = "%s_MP150_data.tsv" % (logfile)
self._newestsample = (0.0, 0.0, 0.0)
self._buffer = []
self._buffch = 0
# connect to the MP150
# (101 is the code for the MP150, 103 for the MP36R)
# (11 is a code for the communication method)
# ('auto' is for automatically connecting to the first responding
# device)
try:
result = mpdev.connectMPDev(c_int(101), c_int(11), b'auto')
except:
result = "failed to call connectMPDev"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in libmpdev: failed to connect to the MP150: %s" % result)
# get starting time
self._starting_time = time.time()
# set sampling rate
try:
result = mpdev.setSampleRate(c_double(self._sampletime))
except:
result = "failed to call setSampleRate"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in libmpdev: failed to set samplerate: %s" % result)
# set Channels to acquire
try:
channels = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
channels = (c_int * len(channels))(*channels)
result = mpdev.setAcqChannels(byref(channels))
except:
result = "failed to call setAcqChannels"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in libmpdev: failed to set channels to acquire: %s" % result)
# # start acquisition daemon (NOT for getMPBuffer and getMostRecentSample!)
# try:
# result = mpdev.startMPAcqDaemon()
# except:
# result = "failed to call startMPAcqDaemon"
# if check_returncode(result) != "MPSUCCESS":
# raise Exception("Error in libmpdev: failed to start acquisition daemon: %s" % result)
# start acquisition
try:
result = mpdev.startAcquisition()
except:
result = "failed to call startAcquisition"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in libmpdev: failed to start acquisition: %s" % result)
# open log file
self._logfile = open(self._logfilename, 'w')
# write header
header = "\t".join(["timestamp","channel_1","channel_2","channel_3"])
self._logfile.write(header + "\n")
# create logging lock
self._loglock = Lock()
# start sample processing Thread
self._recording = False
self._recordtobuff = False
self._connected = True
self._spthread = Thread(target=self._sampleprocesser)
self._spthread.daemon = True
self._spthread.name = "sampleprocesser"
self._spthread.start()
def start_recording(self):
"""
desc:
Starts writing MP150 samples to the log file.
"""
# signal to the sample processing thread that recording is active
self._recording = True
def stop_recording(self):
"""
desc:
Stops writing MP150 samples to the log file.
"""
# signal to the sample processing thread that recording stopped
self._recording = False
# consolidate logged data
self._loglock.acquire(True)
self._logfile.flush() # internal buffer to RAM
os.fsync(self._logfile.fileno()) # RAM file cache to disk
self._loglock.release()
def start_recording_to_buffer(self, channel=0):
"""
desc:
Starts recording to an internal buffer.
keywords:
channel:
desc: The channel from which needs to be recorded.
(default = 0)
type: int
"""
# clear internal buffer
self._buffer = []
self._buffch = channel
# signal sample processing thread that recording to the internal
# buffer is active
self._recordtobuff = True
def stop_recording_to_buffer(self):
"""
desc:
Stops recording samples to an internal buffer.
"""
# signal to the sample processing thread that recording stopped
self._recordtobuff = False
def sample(self):
"""
desc:
Returns the most recent sample provided by the MP150.
returns:
desc:The latest MP150 output values for three channels
(as a list of floats).
type:list
"""
return self._newestsample
def get_buffer(self):
"""
desc:
Returns the internal sample buffer, which is filled up when
start_recording_to_buffer is called. This function is
safest to call only after stop_recording_to_buffer is called
returns:
desc: A NumPy array containing samples from since
start_recording_to_buffer was last called, until
get_buffer or stop_recording_to_buffer was called
type: numpy.array
"""
return numpy.array(self._buffer)
def log(self, msg):
"""
desc:
Writes a message to the log file.
arguments:
msg:
desc:The message that is to be written to the log file.
type:str
"""
# wait for the logging lock to be released, then lock it
self._loglock.acquire(True)
# write log message, including timestamp
self._logfile.write("MSG\t%d\t%s\n" % (self.get_timestamp(), msg))
# release the logging lock
self._loglock.release()
def close(self):
"""
desc:
Closes the connection to the MP150.
"""
# stop recording
if self._recording:
self.stop_recording()
# close log file
self._logfile.close()
# stop sample processing thread
self._connected = False
# close connection
try:
result = mpdev.disconnectMPDev()
except:
result = "failed to call disconnectMPDev"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in libmpdev: failed to close the connection to the MP150: %s" % result)
def get_timestamp(self):
"""
desc:
Returns the time in milliseconds since the connection was opened
returns:
desc:Time (milliseconds) since connection was opened
type:int
"""
return int((time.time()-self._starting_time) * 1000)
def _sampleprocesser(self):
"""
desc:
Processes samples while self._recording is True (INTERNAL USE!)
"""
# run until the connection is closed
while self._connected:
# get new sample
try:
#data = [0.0,0.0,0.0]
#result, data = mpdev.getMPBuffer(1, data) # 1 for one sample
data = [0.0, 0.0, 0.0]
data = (c_double * len(data))(*data)
result = mpdev.getMostRecentSample(byref(data))
data = tuple(data)
except:
result = "failed to call getMPBuffer"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in libmpdev: failed to obtain a sample from the MP150: %s" % result)
# update newest sample
if data != self._newestsample:
self._newestsample = copy.deepcopy(data)
# write sample to file
if self._recording:
# wait for the logging lock to be released, then lock it
self._loglock.acquire(True)
# log data
self._logfile.write("%d\t%.3f\t%.3f\t%.3f\n" % (self.get_timestamp(), self._newestsample[0], self._newestsample[1], self._newestsample[2]))
# release the logging lock
self._loglock.release()
# add sample to buffer
if self._recordtobuff:
self._buffer.append(self._newestsample[self._buffch])
# pause until the next sample is available
# (NOT necessary, as getMostRecentSample blocks until a
# new sample is available!)
#time.sleep(self._sampletimesec)