forked from thearn/pyemotiv
/
GUI_.py
119 lines (85 loc) · 2.75 KB
/
GUI_.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
from pyemotiv import Epoc
import numpy as np
from PyQt4 import QtCore, QtGui, Qt
from PyQt4.QtGui import QApplication, QMainWindow
import PyQt4.Qwt5 as Qwt
import sys
import guiplot
import time
class EEGQt(QtCore.QObject):
def __init__(self, mainwindow):
super(EEGQt, self).__init__()
class QtEEG(QtCore.QObject):
finished_adq = QtCore.pyqtSignal()
#need to add the acquisition module in here
def __init__(self,mainwindow):
super(QtEEG, self).__init__()
def initUI(self):
# Emotiv Initialization and data acquisition
epoc= Epoc()
# collecting data from the headset and placing into two different arrays
data= epoc.aquire([9])
#data2= epoc.aquire([14])
datarray = np.concatenate((datarray, data), axis = 1)
#datarray2= np.concatenate((datarray2, data2), axis=1)
#self.clab = ['F3', 'F4', 'P7', 'FC6', 'F7', 'F8', 'T7', 'P8', 'FC5', 'AF4','T8', 'O2', 'O1', 'AF3']
self.timek = 512
self.x = np.arange(0,self.timek,1)
self.y = [[0] for i in xrange(len(self.datarray[0]))]
self.offset = [2500.0*i for i in xrange(len(self.datarray[0]))]
self.cancel = False
def addTimeSeries(self,datarray):
self.plt = []
for i in xrange(len(datarray[0])):
self.plt.append(Qwt.QwtPlotCurve(datarray[0][i]))
self.plt[i].attach(self.mainwindow.eegPlot)
self.plt[i].setPen(Qt.QPen(Qt.Qt.red))
def plotEEG(self):
#import gevent
#from gevent import monkey
#self.headset = emotiv.Emotiv()
#monkey.patch_all(True)
print('Plotting!!!!')
#gevent.spawn(self.headset.setup)
while True:
self.mutex_stop = Qt.QMutex()
self.mutex_stop.lock()
if self.cancel:
break
self.mutex_stop.unlock()
self.packet = self.headset.dequeue()
if self.packet is not None:
for i in xrange(len(self.datarray[0])):
#p = self.packet.sensors[self.datarray[0][i]]
self.y[i].append(p['value'])
if len(self.y[i]) > self.timek:
self.y[i].pop(0)
self.plt[i].setData(self.x,self.y[i]-np.mean(self.y[i])+self.offset[i])
self.mainwindow.eegPlot.replot()
gevent.sleep(0.1)
self.cancel = False
self.headset.close()
self.finished_adq.emit()
def stop(self):
print('Cancel!')
self.mutex_stop.lock()
self.cancel = True
self.mutex_stop.unlock()
def __del__(self):
self.headset.close()
def main():
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()
emotivObj = QtEEG(ui)
adqThread = QtCore.QThread()
emotivObj.moveToThread(adqThread)
emotivObj.finished_adq.connect(adqThread.quit)
adqThread.started.connect(emotivObj.plotEEG)
ui.startEEG.clicked.connect(adqThread.start)
ui.stopEEG.clicked.connect(emotivObj.stop)
sys.exit(app.exec_())
if __name__ == '__main__':
main()