1 | # -*- coding: utf-8 -*- |
---|
2 | |
---|
3 | # Copyright Puzzlebox Productions, LLC (2010) |
---|
4 | # |
---|
5 | # This code is released under the GNU Pulic License (GPL) version 2 |
---|
6 | # For more information please refer to http://www.gnu.org/copyleft/gpl.html |
---|
7 | |
---|
8 | # Old Class Names: |
---|
9 | # puzzlebox_synapse_interface = QtUI |
---|
10 | |
---|
11 | __changelog__ = """\ |
---|
12 | Last Update: 2010.08.25 |
---|
13 | |
---|
14 | """ |
---|
15 | |
---|
16 | __todo__ = """ |
---|
17 | - update configuration.ini file with settings entered into interface |
---|
18 | |
---|
19 | """ |
---|
20 | |
---|
21 | ### IMPORTS ### |
---|
22 | import os, sys |
---|
23 | import simplejson as json |
---|
24 | |
---|
25 | import random |
---|
26 | from numpy import arange, sin, pi |
---|
27 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
28 | from matplotlib.figure import Figure |
---|
29 | |
---|
30 | if (sys.platform != 'win32'): |
---|
31 | import bluetooth |
---|
32 | DEFAULT_IMAGE_PATH = '/usr/share/puzzlebox_synapse/images' |
---|
33 | else: |
---|
34 | import serial |
---|
35 | DEFAULT_IMAGE_PATH = 'images' |
---|
36 | |
---|
37 | try: |
---|
38 | import PySide as PyQt4 |
---|
39 | from PySide import QtCore, QtGui |
---|
40 | except: |
---|
41 | print "Using PyQt4 module" |
---|
42 | from PyQt4 import QtCore, QtGui |
---|
43 | else: |
---|
44 | print "Using PySide module" |
---|
45 | |
---|
46 | # from puzzlebox_synapse_interface_design import Ui_Form |
---|
47 | from Interface_Design import Ui_Form as Design |
---|
48 | |
---|
49 | import Configuration as configuration |
---|
50 | import Server as synapse_server |
---|
51 | import Client as thinkgear_client |
---|
52 | #import puzzlebox_logger |
---|
53 | |
---|
54 | ### GLOBALS ### |
---|
55 | |
---|
56 | DEBUG = 1 |
---|
57 | |
---|
58 | THINKGEAR_SERVER_HOST = configuration.THINKGEAR_SERVER_HOST |
---|
59 | THINKGEAR_SERVER_PORT = configuration.THINKGEAR_SERVER_PORT |
---|
60 | |
---|
61 | THINKGEAR_EEG_POWER_BAND_ORDER = configuration.THINKGEAR_EEG_POWER_BAND_ORDER |
---|
62 | |
---|
63 | THINKGEAR_EMULATION_MAX_ESENSE_VALUE = \ |
---|
64 | configuration.THINKGEAR_EMULATION_MAX_ESENSE_VALUE |
---|
65 | THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE = \ |
---|
66 | configuration.THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE |
---|
67 | |
---|
68 | PATH_TO_HCITOOL = '/usr/bin/hcitool' |
---|
69 | |
---|
70 | #UPDATE_INTERFACE_VIA_TIMER = True # Alternative is to establish a |
---|
71 | ## ThinkGear Connect client which |
---|
72 | ## updates the interface on demand |
---|
73 | ## as packets are received |
---|
74 | |
---|
75 | UPDATE_INTERFACE_VIA_TIMER = False |
---|
76 | |
---|
77 | UPDATE_INTERFACE_RAW_EEG_VIA_TIMER = False |
---|
78 | |
---|
79 | INTERFACE_UPDATE_FREQUENCY = 1000 # ms |
---|
80 | INTERFACE_RAW_EEG_UPDATE_FREQUENCY = (1 / 512) * 1000 # ms (512 Hz) |
---|
81 | |
---|
82 | ### CLASSES ### |
---|
83 | |
---|
84 | class QtUI(QtGui.QWidget, Design): |
---|
85 | |
---|
86 | def __init__(self, log, server=None, DEBUG=DEBUG, parent = None): |
---|
87 | |
---|
88 | self.log = log |
---|
89 | self.DEBUG = DEBUG |
---|
90 | |
---|
91 | QtGui.QWidget.__init__(self, parent) |
---|
92 | self.setupUi(self) |
---|
93 | |
---|
94 | self.configureSettings() |
---|
95 | self.connectWidgets() |
---|
96 | |
---|
97 | self.name = "Synapse Interface" |
---|
98 | |
---|
99 | self.thinkGearConnectServer = None |
---|
100 | self.thinkgearConnectClient = None |
---|
101 | |
---|
102 | self.maxEEGPower = THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE |
---|
103 | |
---|
104 | self.debug_console_buffer = '' |
---|
105 | |
---|
106 | if UPDATE_INTERFACE_VIA_TIMER or \ |
---|
107 | UPDATE_INTERFACE_RAW_EEG_VIA_TIMER: |
---|
108 | self.updateInterfaceTimer = QtCore.QTimer() |
---|
109 | QtCore.QObject.connect(self.updateInterfaceTimer, \ |
---|
110 | QtCore.SIGNAL("timeout()"), \ |
---|
111 | self.updateInterface) |
---|
112 | |
---|
113 | |
---|
114 | ################################################################## |
---|
115 | |
---|
116 | def configureSettings(self): |
---|
117 | |
---|
118 | # Synapse Interface |
---|
119 | image_path = "puzzlebox.ico" |
---|
120 | if not os.path.exists(image_path): |
---|
121 | image_path = os.path.join(DEFAULT_IMAGE_PATH, image_path) |
---|
122 | |
---|
123 | if os.path.exists(image_path): |
---|
124 | icon = QtGui.QIcon() |
---|
125 | icon.addPixmap(QtGui.QPixmap(image_path), \ |
---|
126 | QtGui.QIcon.Normal, \ |
---|
127 | QtGui.QIcon.Off) |
---|
128 | self.setWindowIcon(icon) |
---|
129 | |
---|
130 | image_path = "puzzlebox_logo.png" |
---|
131 | if not os.path.exists(image_path): |
---|
132 | image_path = os.path.join(DEFAULT_IMAGE_PATH, image_path) |
---|
133 | if os.path.exists(image_path): |
---|
134 | self.labelPuzzleboxIcon.setPixmap(QtGui.QPixmap(image_path)) |
---|
135 | |
---|
136 | |
---|
137 | # ThinkGear Device |
---|
138 | self.searchForThinkGearDevices() |
---|
139 | |
---|
140 | |
---|
141 | # ThinkGear Connect Server |
---|
142 | self.textLabelBluetoothStatus.setText("Status: Disconnected") |
---|
143 | |
---|
144 | # Display Host for ThinkGear Connect Socket Server |
---|
145 | self.lineEditThinkGearHost.setText(THINKGEAR_SERVER_HOST) |
---|
146 | |
---|
147 | # Display Port for ThinkGear Connect Socket Server |
---|
148 | self.lineEditThinkGearPort.setText('%i' % THINKGEAR_SERVER_PORT) |
---|
149 | |
---|
150 | |
---|
151 | # ThinkgGear Progress Bars |
---|
152 | self.progressBarEEGDelta.setMaximum(THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE) |
---|
153 | self.progressBarEEGTheta.setMaximum(THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE) |
---|
154 | self.progressBarEEGLowAlpha.setMaximum(THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE) |
---|
155 | self.progressBarEEGHighAlpha.setMaximum(THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE) |
---|
156 | self.progressBarEEGLowBeta.setMaximum(THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE) |
---|
157 | self.progressBarEEGHighBeta.setMaximum(THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE) |
---|
158 | self.progressBarEEGLowGamma.setMaximum(THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE) |
---|
159 | self.progressBarEEGMidGamma.setMaximum(THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE) |
---|
160 | |
---|
161 | self.progressBarAttention.setMaximum(THINKGEAR_EMULATION_MAX_ESENSE_VALUE) |
---|
162 | self.progressBarMeditation.setMaximum(THINKGEAR_EMULATION_MAX_ESENSE_VALUE) |
---|
163 | |
---|
164 | self.progressBarSignalContactQuality.setMaximum(200) |
---|
165 | |
---|
166 | |
---|
167 | #self.matplot = dynamicMatplotlibCanvas(QtGui.QWidget(self), width=5, height=4, dpi=100) |
---|
168 | |
---|
169 | #print dir(self.tabEEGSignals) |
---|
170 | |
---|
171 | self.matplot = dynamicMatplotlibCanvas(self.tabEEGSignals, width=8, height=4, dpi=100) |
---|
172 | #self.matplotEEG = dynamicMatplotlibCanvas(self.tabEEGSignals, width=6, height=2, dpi=100) |
---|
173 | #l.addWidget(matplot) |
---|
174 | |
---|
175 | |
---|
176 | ################################################################## |
---|
177 | |
---|
178 | def connectWidgets(self): |
---|
179 | |
---|
180 | self.connect(self.pushButtonBluetoothSearch, \ |
---|
181 | QtCore.SIGNAL("clicked()"), \ |
---|
182 | self.searchForThinkGearDevices) |
---|
183 | |
---|
184 | self.connect(self.pushButtonBluetoothConnect, \ |
---|
185 | QtCore.SIGNAL("clicked()"), \ |
---|
186 | self.connectToThinkGearDevice) |
---|
187 | |
---|
188 | self.connect(self.pushButtonThinkGearConnect, \ |
---|
189 | QtCore.SIGNAL("clicked()"), \ |
---|
190 | self.startThinkGearConnectServer) |
---|
191 | |
---|
192 | |
---|
193 | ################################################################## |
---|
194 | |
---|
195 | def connectToThinkGearDevice(self): |
---|
196 | |
---|
197 | device_selection = self.comboBoxDeviceSelect.currentText() |
---|
198 | |
---|
199 | self.disconnect(self.pushButtonBluetoothConnect, \ |
---|
200 | QtCore.SIGNAL("clicked()"), \ |
---|
201 | self.connectToThinkGearDevice) |
---|
202 | |
---|
203 | self.connect(self.pushButtonBluetoothConnect, \ |
---|
204 | QtCore.SIGNAL("clicked()"), \ |
---|
205 | self.disconnectFromThinkGearDevice) |
---|
206 | |
---|
207 | self.textLabelBluetoothStatus.setText("Status: Connected") |
---|
208 | |
---|
209 | self.pushButtonBluetoothSearch.setEnabled(False) |
---|
210 | |
---|
211 | self.pushButtonBluetoothConnect.setText('Disconnect') |
---|
212 | self.pushButtonBluetoothConnect.setChecked(True) |
---|
213 | |
---|
214 | self.comboBoxDeviceSelect.setEnabled(False) |
---|
215 | |
---|
216 | |
---|
217 | ################################################################## |
---|
218 | |
---|
219 | def disconnectFromThinkGearDevice(self): |
---|
220 | |
---|
221 | self.disconnect(self.pushButtonBluetoothConnect, \ |
---|
222 | QtCore.SIGNAL("clicked()"), \ |
---|
223 | self.disconnectFromThinkGearDevice) |
---|
224 | |
---|
225 | self.connect(self.pushButtonBluetoothConnect, \ |
---|
226 | QtCore.SIGNAL("clicked()"), \ |
---|
227 | self.connectToThinkGearDevice) |
---|
228 | |
---|
229 | self.textLabelBluetoothStatus.setText("Status: Disconnected") |
---|
230 | |
---|
231 | self.pushButtonBluetoothSearch.setEnabled(True) |
---|
232 | |
---|
233 | self.pushButtonBluetoothConnect.setText('Connect') |
---|
234 | self.pushButtonBluetoothConnect.setChecked(False) |
---|
235 | |
---|
236 | self.comboBoxDeviceSelect.setEnabled(True) |
---|
237 | |
---|
238 | |
---|
239 | self.progressBarEEGDelta.setValue(0) |
---|
240 | self.progressBarEEGTheta.setValue(0) |
---|
241 | self.progressBarEEGLowAlpha.setValue(0) |
---|
242 | self.progressBarEEGHighAlpha.setValue(0) |
---|
243 | self.progressBarEEGLowBeta.setValue(0) |
---|
244 | self.progressBarEEGHighBeta.setValue(0) |
---|
245 | self.progressBarEEGLowGamma.setValue(0) |
---|
246 | self.progressBarEEGMidGamma.setValue(0) |
---|
247 | |
---|
248 | self.progressBarAttention.setValue(0) |
---|
249 | self.progressBarMeditation.setValue(0) |
---|
250 | |
---|
251 | self.progressBarSignalContactQuality.setValue(0) |
---|
252 | |
---|
253 | self.maxEEGPower = THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE |
---|
254 | |
---|
255 | # In case the user connects to a MindSet, then disconnects |
---|
256 | # and re-connects to a MindSet Emulator, |
---|
257 | # we need to reset the max power values |
---|
258 | self.progressBarEEGDelta.setMaximum(self.maxEEGPower) |
---|
259 | self.progressBarEEGTheta.setMaximum(self.maxEEGPower) |
---|
260 | self.progressBarEEGLowAlpha.setMaximum(self.maxEEGPower) |
---|
261 | self.progressBarEEGHighAlpha.setMaximum(self.maxEEGPower) |
---|
262 | self.progressBarEEGLowBeta.setMaximum(self.maxEEGPower) |
---|
263 | self.progressBarEEGHighBeta.setMaximum(self.maxEEGPower) |
---|
264 | self.progressBarEEGLowGamma.setMaximum(self.maxEEGPower) |
---|
265 | self.progressBarEEGMidGamma.setMaximum(self.maxEEGPower) |
---|
266 | |
---|
267 | |
---|
268 | ################################################################## |
---|
269 | |
---|
270 | def startThinkGearConnectServer(self): |
---|
271 | |
---|
272 | # Ensure EEG device is connected first |
---|
273 | |
---|
274 | if not self.pushButtonBluetoothConnect.isChecked(): |
---|
275 | self.connectToThinkGearDevice() |
---|
276 | |
---|
277 | |
---|
278 | self.pushButtonBluetoothSearch.setEnabled(False) |
---|
279 | self.pushButtonBluetoothConnect.setEnabled(False) |
---|
280 | |
---|
281 | server_interface = str(self.lineEditThinkGearHost.text()) |
---|
282 | server_port = int(self.lineEditThinkGearPort.text()) |
---|
283 | device_address = str(self.comboBoxDeviceSelect.currentText()) |
---|
284 | emulate_headset_data = (device_address == 'MindSet Emulator') |
---|
285 | |
---|
286 | |
---|
287 | self.thinkGearConnectServer = \ |
---|
288 | synapse_server.ThinkgearServer( \ |
---|
289 | self.log, \ |
---|
290 | server_interface=server_interface, \ |
---|
291 | server_port=server_port, \ |
---|
292 | device_address=device_address, \ |
---|
293 | emulate_headset_data=emulate_headset_data, \ |
---|
294 | DEBUG=DEBUG, \ |
---|
295 | parent=self) |
---|
296 | |
---|
297 | self.thinkGearConnectServer.start() |
---|
298 | |
---|
299 | |
---|
300 | if UPDATE_INTERFACE_RAW_EEG_VIA_TIMER: |
---|
301 | self.updateInterfaceTimer.start(INTERFACE_RAW_EEG_UPDATE_FREQUENCY) |
---|
302 | elif UPDATE_INTERFACE_VIA_TIMER: |
---|
303 | self.updateInterfaceTimer.start(INTERFACE_UPDATE_FREQUENCY) |
---|
304 | |
---|
305 | else: |
---|
306 | self.thinkgearConnectClient = \ |
---|
307 | thinkgear_client.QtClient( \ |
---|
308 | self.log, \ |
---|
309 | server_host=server_interface, \ |
---|
310 | server_port=server_port, \ |
---|
311 | DEBUG=0, \ |
---|
312 | parent=self) |
---|
313 | |
---|
314 | self.thinkgearConnectClient.start() |
---|
315 | |
---|
316 | |
---|
317 | self.disconnect(self.pushButtonThinkGearConnect, \ |
---|
318 | QtCore.SIGNAL("clicked()"), \ |
---|
319 | self.startThinkGearConnectServer) |
---|
320 | |
---|
321 | self.connect(self.pushButtonThinkGearConnect, \ |
---|
322 | QtCore.SIGNAL("clicked()"), \ |
---|
323 | self.stopThinkGearConnectServer) |
---|
324 | |
---|
325 | self.lineEditThinkGearHost.setEnabled(False) |
---|
326 | self.lineEditThinkGearPort.setEnabled(False) |
---|
327 | |
---|
328 | self.pushButtonThinkGearConnect.setText('Stop') |
---|
329 | |
---|
330 | |
---|
331 | ################################################################## |
---|
332 | |
---|
333 | def stopThinkGearConnectServer(self): |
---|
334 | |
---|
335 | if UPDATE_INTERFACE_VIA_TIMER: |
---|
336 | self.updateInterfaceTimer.stop() |
---|
337 | else: |
---|
338 | self.thinkgearConnectClient.disconnectFromHost() |
---|
339 | |
---|
340 | self.thinkGearConnectServer.exitThread() |
---|
341 | |
---|
342 | self.disconnect(self.pushButtonThinkGearConnect, \ |
---|
343 | QtCore.SIGNAL("clicked()"), \ |
---|
344 | self.stopThinkGearConnectServer) |
---|
345 | |
---|
346 | self.connect(self.pushButtonThinkGearConnect, \ |
---|
347 | QtCore.SIGNAL("clicked()"), \ |
---|
348 | self.startThinkGearConnectServer) |
---|
349 | |
---|
350 | self.lineEditThinkGearHost.setEnabled(True) |
---|
351 | self.lineEditThinkGearPort.setEnabled(True) |
---|
352 | |
---|
353 | self.pushButtonThinkGearConnect.setText('Start') |
---|
354 | |
---|
355 | #self.pushButtonBluetoothSearch.setEnabled(True) |
---|
356 | self.pushButtonBluetoothConnect.setEnabled(True) |
---|
357 | |
---|
358 | |
---|
359 | ################################################################## |
---|
360 | |
---|
361 | def updateInterface(self): |
---|
362 | |
---|
363 | if not self.thinkGearConnectServer.emulate_headset_data: |
---|
364 | self.processPacketThinkGear( \ |
---|
365 | self.thinkGearConnectServer.protocol.data_packet) |
---|
366 | |
---|
367 | |
---|
368 | ################################################################## |
---|
369 | |
---|
370 | def updateRawEegInterface(self): |
---|
371 | |
---|
372 | pass |
---|
373 | |
---|
374 | |
---|
375 | ################################################################## |
---|
376 | |
---|
377 | def processPacketThinkGear(self, packet): |
---|
378 | |
---|
379 | if self.DEBUG > 2: |
---|
380 | print packet |
---|
381 | |
---|
382 | |
---|
383 | if ('rawEeg' in packet.keys()): |
---|
384 | value = packet['rawEeg'] |
---|
385 | self.matplot.update_figure(value) |
---|
386 | |
---|
387 | |
---|
388 | if ('poorSignalLevel' in packet.keys()): |
---|
389 | value = 200 - packet['poorSignalLevel'] |
---|
390 | self.progressBarSignalContactQuality.setValue(value) |
---|
391 | self.textEditDebugConsole.append("") |
---|
392 | self.textEditDebugConsole.append("poorSignalLevel: %i" % \ |
---|
393 | packet['poorSignalLevel']) |
---|
394 | |
---|
395 | |
---|
396 | if ('eSense' in packet.keys()): |
---|
397 | |
---|
398 | if ('attention' in packet['eSense'].keys()): |
---|
399 | value = packet['eSense']['attention'] |
---|
400 | self.progressBarAttention.setValue(value) |
---|
401 | self.textEditDebugConsole.append("eSense attention: %i" % value) |
---|
402 | |
---|
403 | if ('meditation' in packet['eSense'].keys()): |
---|
404 | value = packet['eSense']['meditation'] |
---|
405 | self.progressBarMeditation.setValue(value) |
---|
406 | self.textEditDebugConsole.append("eSense meditation: %i" % value) |
---|
407 | |
---|
408 | |
---|
409 | if ('eegPower' in packet.keys()): |
---|
410 | |
---|
411 | # If we are not emulating packets we'll set the maximum EEG Power value |
---|
412 | # threshold to the default (or maximum value found within this packet) |
---|
413 | if not self.thinkGearConnectServer.emulate_headset_data: |
---|
414 | self.maxEEGPower = THINKGEAR_EMULATION_MAX_EEG_POWER_VALUE |
---|
415 | |
---|
416 | for value in packet['eegPower'].keys(): |
---|
417 | if packet['eegPower'][value] > self.maxEEGPower: |
---|
418 | self.maxEEGPower = packet['eegPower'][value] |
---|
419 | |
---|
420 | |
---|
421 | if ('delta' in packet['eegPower'].keys()): |
---|
422 | value = packet['eegPower']['delta'] |
---|
423 | self.progressBarEEGDelta.setMaximum(self.maxEEGPower) |
---|
424 | self.progressBarEEGDelta.setValue(value) |
---|
425 | self.textEditDebugConsole.append("delta: %i" % value) |
---|
426 | |
---|
427 | if ('theta' in packet['eegPower'].keys()): |
---|
428 | value = packet['eegPower']['theta'] |
---|
429 | self.progressBarEEGTheta.setMaximum(self.maxEEGPower) |
---|
430 | self.progressBarEEGTheta.setValue(value) |
---|
431 | self.textEditDebugConsole.append("theta: %i" % value) |
---|
432 | |
---|
433 | if ('lowAlpha' in packet['eegPower'].keys()): |
---|
434 | value = packet['eegPower']['lowAlpha'] |
---|
435 | self.progressBarEEGLowAlpha.setMaximum(self.maxEEGPower) |
---|
436 | self.progressBarEEGLowAlpha.setValue(value) |
---|
437 | self.textEditDebugConsole.append("lowAlpha: %i" % value) |
---|
438 | |
---|
439 | if ('highAlpha' in packet['eegPower'].keys()): |
---|
440 | value = packet['eegPower']['highAlpha'] |
---|
441 | self.progressBarEEGHighAlpha.setMaximum(self.maxEEGPower) |
---|
442 | self.progressBarEEGHighAlpha.setValue(value) |
---|
443 | self.textEditDebugConsole.append("highAlpha: %i" % value) |
---|
444 | |
---|
445 | if ('lowBeta' in packet['eegPower'].keys()): |
---|
446 | value = packet['eegPower']['lowBeta'] |
---|
447 | self.progressBarEEGLowBeta.setMaximum(self.maxEEGPower) |
---|
448 | self.progressBarEEGLowBeta.setValue(value) |
---|
449 | self.textEditDebugConsole.append("lowBeta: %i" % value) |
---|
450 | |
---|
451 | if ('highBeta' in packet['eegPower'].keys()): |
---|
452 | value = packet['eegPower']['highBeta'] |
---|
453 | self.progressBarEEGHighBeta.setMaximum(self.maxEEGPower) |
---|
454 | self.progressBarEEGHighBeta.setValue(value) |
---|
455 | self.textEditDebugConsole.append("highBeta: %i" % value) |
---|
456 | |
---|
457 | if ('lowGamma' in packet['eegPower'].keys()): |
---|
458 | value = packet['eegPower']['lowGamma'] |
---|
459 | self.progressBarEEGLowGamma.setMaximum(self.maxEEGPower) |
---|
460 | self.progressBarEEGLowGamma.setValue(value) |
---|
461 | self.textEditDebugConsole.append("lowGamma: %i" % value) |
---|
462 | |
---|
463 | if ('highGamma' in packet['eegPower'].keys()): |
---|
464 | value = packet['eegPower']['highGamma'] |
---|
465 | self.progressBarEEGMidGamma.setMaximum(self.maxEEGPower) |
---|
466 | self.progressBarEEGMidGamma.setValue(value) |
---|
467 | self.textEditDebugConsole.append("highGamma: %i" % value) |
---|
468 | |
---|
469 | |
---|
470 | ################################################################## |
---|
471 | |
---|
472 | def enumerateSerialPorts(self): |
---|
473 | |
---|
474 | """ Uses the Win32 registry to return an |
---|
475 | iterator of serial (COM) ports |
---|
476 | existing on this computer. |
---|
477 | |
---|
478 | from http://eli.thegreenplace.net/2009/07/31/listing-all-serial-ports-on-windows-with-python/ |
---|
479 | """ |
---|
480 | |
---|
481 | path = 'HARDWARE\\DEVICEMAP\\SERIALCOMM' |
---|
482 | try: |
---|
483 | key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path) |
---|
484 | except WindowsError: |
---|
485 | raise IterationError |
---|
486 | |
---|
487 | for i in itertools.count(): |
---|
488 | try: |
---|
489 | val = winreg.EnumValue(key, i) |
---|
490 | yield str(val[1]) |
---|
491 | except EnvironmentError: |
---|
492 | break |
---|
493 | |
---|
494 | |
---|
495 | ################################################################## |
---|
496 | |
---|
497 | def fullPortName(self, portname): |
---|
498 | |
---|
499 | """ Given a port-name (of the form COM7, |
---|
500 | COM12, CNCA0, etc.) returns a full |
---|
501 | name suitable for opening with the |
---|
502 | Serial class. |
---|
503 | """ |
---|
504 | |
---|
505 | m = re.match('^COM(\d+)$', portname) |
---|
506 | if m and int(m.group(1)) < 10: |
---|
507 | return portname |
---|
508 | |
---|
509 | return '\\\\.\\' + portname |
---|
510 | |
---|
511 | |
---|
512 | ################################################################## |
---|
513 | |
---|
514 | def searchForThinkGearDevices(self): |
---|
515 | |
---|
516 | #self.pushButtonBluetoothSearch.setText('Searching') |
---|
517 | |
---|
518 | mindset_devices = [] |
---|
519 | |
---|
520 | if (sys.platform == 'win32'): |
---|
521 | |
---|
522 | for portname in self.enumerateSerialPorts(): |
---|
523 | |
---|
524 | #portname = self.fullPortName(portname) |
---|
525 | mindset_devices.append(portname) |
---|
526 | |
---|
527 | |
---|
528 | else: |
---|
529 | |
---|
530 | # Bluetooth module doesn't compile properly under Windows |
---|
531 | |
---|
532 | bluetooth_devices = [] |
---|
533 | |
---|
534 | #bluetooth_devices = bluetooth.discover_devices( \ |
---|
535 | #duration=5, \ |
---|
536 | #flush_cache=True, \ |
---|
537 | #lookup_names=True) |
---|
538 | |
---|
539 | command = '%s con' % PATH_TO_HCITOOL |
---|
540 | |
---|
541 | output = os.popen(command, 'r') |
---|
542 | |
---|
543 | for line in output.readlines(): |
---|
544 | try: |
---|
545 | address = line.split(' ')[2] |
---|
546 | except: |
---|
547 | pass |
---|
548 | else: |
---|
549 | bluetooth_devices.append(address) |
---|
550 | |
---|
551 | for address in bluetooth_devices: |
---|
552 | device_name = bluetooth.lookup_name(address) |
---|
553 | if device_name == 'MindSet': |
---|
554 | mindset_devices.append(address) |
---|
555 | |
---|
556 | |
---|
557 | if self.DEBUG: |
---|
558 | print "Bluetooth MindSet devices found:", |
---|
559 | print mindset_devices |
---|
560 | |
---|
561 | |
---|
562 | self.comboBoxDeviceSelect.clear() |
---|
563 | |
---|
564 | self.comboBoxDeviceSelect.addItem('MindSet Emulator') |
---|
565 | |
---|
566 | |
---|
567 | for mindset_device in mindset_devices: |
---|
568 | self.comboBoxDeviceSelect.addItem(mindset_device) |
---|
569 | |
---|
570 | |
---|
571 | #self.pushButtonBluetoothSearch.setText('Search') |
---|
572 | |
---|
573 | |
---|
574 | ################################################################## |
---|
575 | |
---|
576 | def closeEvent(self, event): |
---|
577 | |
---|
578 | quit_message = "Are you sure you want to exit the program?" |
---|
579 | |
---|
580 | reply = QtGui.QMessageBox.question( \ |
---|
581 | self, \ |
---|
582 | 'Message', \ |
---|
583 | quit_message, \ |
---|
584 | QtGui.QMessageBox.Yes, \ |
---|
585 | QtGui.QMessageBox.No) |
---|
586 | |
---|
587 | if reply == QtGui.QMessageBox.Yes: |
---|
588 | |
---|
589 | if UPDATE_INTERFACE_VIA_TIMER: |
---|
590 | self.updateInterfaceTimer.stop() |
---|
591 | else: |
---|
592 | if self.thinkgearConnectClient != None: |
---|
593 | self.thinkgearConnectClient.disconnectFromHost() |
---|
594 | |
---|
595 | if self.thinkGearConnectServer != None: |
---|
596 | self.thinkGearConnectServer.exitThread() |
---|
597 | |
---|
598 | event.accept() |
---|
599 | |
---|
600 | else: |
---|
601 | event.ignore() |
---|
602 | |
---|
603 | |
---|
604 | ##################################################################### |
---|
605 | ##################################################################### |
---|
606 | |
---|
607 | class matplotlibCanvas(FigureCanvas): |
---|
608 | |
---|
609 | """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.).""" |
---|
610 | |
---|
611 | def __init__(self, parent=None, width=8, height=4, dpi=100): |
---|
612 | |
---|
613 | fig = Figure(figsize=(width, height), dpi=dpi) |
---|
614 | self.axes = fig.add_subplot(111) |
---|
615 | # We want the axes cleared every time plot() is called |
---|
616 | self.axes.hold(False) |
---|
617 | |
---|
618 | fig.suptitle('Raw EEG Waves', fontsize=12) |
---|
619 | |
---|
620 | FigureCanvas.__init__(self, fig) |
---|
621 | self.setParent(parent) |
---|
622 | |
---|
623 | FigureCanvas.setSizePolicy(self, |
---|
624 | QtGui.QSizePolicy.Expanding, |
---|
625 | QtGui.QSizePolicy.Expanding) |
---|
626 | FigureCanvas.updateGeometry(self) |
---|
627 | |
---|
628 | |
---|
629 | ################################################################## |
---|
630 | |
---|
631 | def compute_initial_figure(self): |
---|
632 | pass |
---|
633 | |
---|
634 | |
---|
635 | ##################################################################### |
---|
636 | ##################################################################### |
---|
637 | |
---|
638 | class dynamicMatplotlibCanvas(matplotlibCanvas): |
---|
639 | |
---|
640 | def __init__(self, *args, **kwargs): |
---|
641 | |
---|
642 | matplotlibCanvas.__init__(self, *args, **kwargs) |
---|
643 | |
---|
644 | self.DEBUG=DEBUG |
---|
645 | |
---|
646 | #timer = QtCore.QTimer(self) |
---|
647 | #QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"), self.update_figure) |
---|
648 | #timer.start(250) |
---|
649 | |
---|
650 | self.values = [] |
---|
651 | |
---|
652 | self.axes.autoscale_view(tight=None, scalex=False, scaley=False) |
---|
653 | |
---|
654 | self.axes.set_xbound(0, 256) |
---|
655 | self.axes.set_ybound(-1024, 1024) |
---|
656 | #self.axes.set_ybound(-2048, 2047) |
---|
657 | |
---|
658 | self.axes.set_autoscale_on(False) |
---|
659 | |
---|
660 | |
---|
661 | ################################################################## |
---|
662 | |
---|
663 | def update_figure(self, value): |
---|
664 | |
---|
665 | self.values.append(value) |
---|
666 | |
---|
667 | if len(self.values) == 256: |
---|
668 | |
---|
669 | self.axes.plot(range(256), self.values, 'b-', scalex=False, scaley=False) |
---|
670 | self.draw() |
---|
671 | |
---|
672 | self.values = [] |
---|
673 | |
---|
674 | |
---|