/
plotduino.py
executable file
·275 lines (208 loc) · 8.44 KB
/
plotduino.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
#!/usr/bin/env python
"""
Copyright 2014 Francesco Bossu
ciacco.posta@gmail.com
Plotduino 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.
Plotduino 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 sys
from PyQt4 import QtGui, QtCore
#import my libraries
import mtp, data
class MainWin(QtGui.QMainWindow):
def __init__( self, parent=None ):
QtGui.QMainWindow.__init__( self, parent )
self.setWindowTitle( 'Plotduino' )
# the data acquisition
self.data_source = data.data()
self.data_source.selection = 0
# time interval for the plot update
self.timestep = 200
# timer for updating the plot
self.timer = QtCore.QTimer(self)
#create the layout and connect the signals to the buttons
self.set_layout()
self.connectSignals()
def set_layout( self ):
""" definistion of the window layout """
self.main_frame = QtGui.QWidget()
#self.main_frame.setFixedHeight(500)
self.plot = mtp.mtp( self.main_frame )
#actual layout
#******************
# layout left:
#-------------
vboxleft = QtGui.QVBoxLayout()
# the matplotlib canvas
vboxleft.addWidget(self.plot.canvas)
# the play and stop button
hbox = QtGui.QHBoxLayout()
self.play_button = QtGui.QPushButton("&Play")
hbox.addWidget(self.play_button)
self.stop_button = QtGui.QPushButton("&Stop")
hbox.addWidget(self.stop_button)
vboxleft.addLayout(hbox)
#layout right:
#------------
# prepare the buttons, the horizontal lines and the spacing
# Serial port configuration
self.select_serial_box = QtGui.QComboBox()
self.select_serial_box.addItems( self.data_source.si.getListOfSerialPorts() )
self.select_speed_box = QtGui.QComboBox()
self.select_speed_box.addItems( self.data_source.si.bitrates )
self.select_speed_box.setCurrentIndex( 1 )
# Configuration of the output plot
self.plotname_box = QtGui.QComboBox()
self.plotname_box.addItems( self.plot.plotnames )
# select the time interval to update the plot
labelspinbox = QtGui.QLabel("Update (ms):")
self.spinbox_timestep = QtGui.QSpinBox()
self.spinbox_timestep.setRange(100,2000) #from 0.1 to 5 seconds
self.spinbox_timestep.setSingleStep(50)
self.spinbox_timestep.setValue( self.timestep)
labelspinbox.setBuddy(self.spinbox_timestep )
# show last N data points
self.showlastsCheckBox = QtGui.QCheckBox("Show last:")
self.spinbox_showlast= QtGui.QSpinBox()
self.spinbox_showlast.setRange(10,1000) #from 0.1 to 5 seconds
self.spinbox_showlast.setValue( 50)
# Save plot button
self.save_button = QtGui.QPushButton("S&ave plot")
# Reset data button
self.resetdata_button = QtGui.QPushButton("&Reset data")
# Close button
self.close_button = QtGui.QPushButton("&Close")
# in order to fix the width of the right layout
# one needs to put the boxlayout in a widget
vboxrightWidget = QtGui.QWidget()
vboxright = QtGui.QVBoxLayout(vboxrightWidget)
# inserting the widgets in the layout
# serial configuration
label = QtGui.QLabel("Serial configuration")
vboxright.addWidget(label)
hbox = QtGui.QHBoxLayout()
label = QtGui.QLabel("Port:")
hbox.addWidget( label )
hbox.addWidget( self.select_serial_box)
vboxright.addLayout( hbox )
hbox = QtGui.QHBoxLayout()
label = QtGui.QLabel("Bitrate:")
hbox.addWidget( label )
hbox.addWidget( self.select_speed_box)
vboxright.addLayout( hbox )
# horizontal line
line = QtGui.QFrame(self)
line.setFrameShape(QtGui.QFrame.HLine)
line.setFrameShadow(QtGui.QFrame.Sunken)
vboxright.addWidget(line)
# plot type
label = QtGui.QLabel("Plot type")
vboxright.addWidget(label)
vboxright.addWidget(self.plotname_box)
# plot interval
hbox_spinbox = QtGui.QHBoxLayout()
hbox_spinbox.addWidget(labelspinbox)
hbox_spinbox.addWidget(self.spinbox_timestep)
vboxright.addLayout(hbox_spinbox)
# horizontal line
line = QtGui.QFrame(self)
line.setFrameShape(QtGui.QFrame.HLine)
line.setFrameShadow(QtGui.QFrame.Sunken)
vboxright.addWidget(line)
# Plot confifuration
hboxshowlast = QtGui.QHBoxLayout()
hboxshowlast.addWidget( self.showlastsCheckBox )
hboxshowlast.addWidget( self.spinbox_showlast )
vboxright.addLayout( hboxshowlast )
# vertical space
vboxright.addItem(QtGui.QSpacerItem(20,40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding))
# buttons
vboxright.addWidget(self.save_button)
vboxright.addWidget(self.resetdata_button)
vboxright.addWidget(self.close_button)
#fix the width of the right layout through its enclosing widget
vboxright.setContentsMargins(0,0,0,0)
vboxrightWidget.setFixedWidth(180)
# Global horizontal layout: takes the two vertical box layouts
#-------------------------------------------------------------
hboxmain = QtGui.QHBoxLayout()
hboxmain.addLayout(vboxleft)
hboxmain.addWidget(vboxrightWidget)
# setting the global horizontal box as the main_frame layout
#-----------------------------------------------------------
self.main_frame.setLayout( hboxmain )
self.setCentralWidget( self.main_frame )
def connectSignals(self):
""" """
self.connect(self.play_button, QtCore.SIGNAL('clicked()'), self.play)
self.connect(self.stop_button, QtCore.SIGNAL('clicked()'), self.stop)
self.connect(self.close_button, QtCore.SIGNAL('clicked()'), self.close)
self.connect(self.save_button, QtCore.SIGNAL('clicked()'), self.plot.saveplot)
self.connect(self.resetdata_button, QtCore.SIGNAL('clicked()'), self.plot.reset)
self.connect(self.showlastsCheckBox, QtCore.SIGNAL('clicked()'), self.showlasts)
self.connect(self.timer, QtCore.SIGNAL('timeout()'), self.draw)
def start_serial( self ):
self.data_source.setPortName(self.select_serial_box.currentText())
self.data_source.bitrate = int(self.select_speed_box.currentText())
def play(self):
""" This function is activated with the Play button. """
# set the type of plot
self.plot.plotkind = self.plotname_box.currentText()
#connect the mtp interface to the data source
self.plot.importdata( self.data_source )
# start the serial communication only the first time
if self.select_serial_box.isEnabled():
self.start_serial()
# get and set the time interval for updating the plot
self.timestep = self.spinbox_timestep.value()
#start the timer
self.timer.start(self.timestep)
#disable the various buttons
self.play_button.setDown(True)
self.play_button.setEnabled(False)
self.stop_button.setEnabled(True)
self.select_serial_box.setEnabled(False)
self.select_speed_box.setEnabled(False)
self.plotname_box.setEnabled(False)
self.spinbox_timestep.setEnabled(False)
def stop( self ):
""" This function stop the acquisition and the update of the plot"""
self.data_source.si.daqStop()
self.timer.stop()
#re-enable the play button
self.play_button.setEnabled(True)
self.stop_button.setEnabled(False)
self.spinbox_timestep.setEnabled(True)
def draw(self):
self.plot.updateplot()
def showlasts( self ):
if self.showlastsCheckBox.isChecked():
self.spinbox_showlast.setEnabled( False )
self.plot.showlastN = self.spinbox_showlast.value()
self.plot.showlasts = True
else:
self.spinbox_showlast.setEnabled( True )
self.plot.showlasts = False
def __del__(self):
del self.data_source
del self.plot
#------------------------------------------------------------------
def main():
#start Qt the Qt application
app = QtGui.QApplication(sys.argv)
#create and draw the main window
w = MainWin()
w.show()
#exit when the window is closed
sys.exit(app.exec_())
if __name__ == '__main__':
main()