-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicTerminalGUI.py
157 lines (124 loc) · 6.13 KB
/
BasicTerminalGUI.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
#!/usr/bin/python -d
import lucidCUE_GUI
import sys
from PyQt4 import QtCore, QtGui
from PortDialog import Ui_PortDialog
from ZeoViewer import Ui_ZeoViewer
from ZeoRawData import BaseLink, Parser
from matplotlib.lines import *
#define the port name
port = '/dev/tty.usbserial'
class MyThread(QtCore.QThread):
updated = QtCore.pyqtSignal(str)
def run( self ):
self.updated.emit(str)
class ZeoViewer(QtGui.QMainWindow):
def __init__(self, parent=None):
#super(ZeoViewer, self).__init__(parent)
#self.wave_thread = MyThread(self)
#self.wave_thread.updated.connect(self.updateWaveform)
# create a line edit and a button
#self.wave_thread.start()
#self._button.clicked.connect(self._thread.start)
self.sampleLength = 128
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_ZeoViewer()
self.ui.setupUi(self)
self.wave = [0]*self.sampleLength*2
self.prevBadSignal = False #Used to determine color of previous seconds waveform
self.hyp = []
self.hypToHeight = {'Undefined' : 0,
'Deep' : 1,
'Light' : 2,
'REM' : 3,
'Awake' : 4}
#Colors grabbed from website
self.hypColors = ['#000000', '#00552a', '#99989b', '#29a639', '#d05827']
#Create initial baselines
self.WaveForm = self.ui.WaveGraph.axes.plot(range(self.sampleLength*2), self.wave, color = 'g')[0]
self.FFTbars = self.ui.FreqGraph.axes.bar((0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5), [0]*7, 1)
self.Sleepbars = self.ui.SleepGraph.axes.bar(range(15,300,30), [0]*10, 30, color = 'w')
def updateWaveform(self, waveform):
self.wave = self.wave[self.sampleLength:] + waveform
self.WaveForm.set_ydata(self.wave)
#self.wave_thread.start()
self.ui.WaveGraph.draw()
def updateFFT(self, bins):
for i,bar in enumerate(self.FFTbars):
bar.set_height(bins[i]*100) # Scaled to be essentially percents
self.ui.FreqGraph.draw()
def updateHypnogram(self, stage):
if len(self.hyp) == 10:
self.hyp = self.hyp[1:]
self.hyp.append(self.hypToHeight[stage])
for i,bar in enumerate(self.Sleepbars):
bar.set_height(self.hyp[i])
bar.set_color(self.hypColors[self.hyp[i]])
else:
self.hyp.append(self.hypToHeight[stage])
self.Sleepbars[len(self.hyp)-1].set_height(self.hyp[len(self.hyp)-1])
self.Sleepbars[len(self.hyp)-1].set_color(self.hypColors[self.hyp[len(self.hyp)-1]])
self.ui.SleepGraph.draw()
def updateSliceG(self, slice):
self.ui.TimeLabel.setText(QtGui.QApplication.translate('ZeoViewer',
slice['ZeoTimestamp'],
None, QtGui.QApplication.UnicodeUTF8))
self.ui.VersionLabel.setText(QtGui.QApplication.translate('ZeoViewer',
'Version: ' + str(slice['Version']),
None, QtGui.QApplication.UnicodeUTF8))
if not slice['SQI'] == None:
sqi = str(slice['SQI'])
else:
sqi = '--'
self.ui.SQILabel.setText(QtGui.QApplication.translate('ZeoViewer',
'SQI: ' + sqi + ' (0-30)',
None, QtGui.QApplication.UnicodeUTF8))
if not slice['Impedance'] == None:
imp = str(int(slice['Impedance']))
else:
imp = '--'
self.ui.ImpLabel.setText(QtGui.QApplication.translate('ZeoViewer',
'Impedance: ' + imp,
None, QtGui.QApplication.UnicodeUTF8))
if slice['BadSignal']:
for i,bar in enumerate(self.FFTbars):
bar.set_color('r')
self.WaveForm.set_color('r')
else:
for i,bar in enumerate(self.FFTbars):
bar.set_color('b')
self.WaveForm.set_color('g')
if not slice['Waveform'] == []:
self.updateWaveform(slice['Waveform'])
if len(slice['FrequencyBins'].values()) == 7:
f = slice['FrequencyBins']
bins = [f['2-4'],f['4-8'],f['8-13'],f['11-14'],f['13-18'],f['18-21'],f['30-50']]
self.updateFFT(bins)
if not slice['SleepStage'] == None:
self.updateHypnogram(slice['SleepStage'])
def updateEvent(self, timestamp, version, event):
self.ui.TimeLabel.setText(QtGui.QApplication.translate('ZeoViewer',
timestamp,
None, QtGui.QApplication.UnicodeUTF8))
self.ui.VersionLabel.setText(QtGui.QApplication.translate('ZeoViewer',
'Version: ' + str(version),
None, QtGui.QApplication.UnicodeUTF8))
self.ui.EventLabel.setText(QtGui.QApplication.translate('ZeoViewer',
event,
None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
# Initialize GUI viewer
app = QtGui.QApplication(sys.argv)
viewer = ZeoViewer()
# Initialize Serial Link
link = BaseLink.BaseLink(port)
parser = Parser.Parser()
cue = lucidCUE_GUI.lucidCue()
# Add Callbacks and Start the Link
link.addCallback(parser.update)
parser.addSliceCallback(cue.updateSlice)
parser.addSliceCallback(viewer.updateSliceG)
parser.addEventCallback(viewer.updateEvent)
viewer.show()
link.start()
sys.exit(app.exec_())