00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 import sys, time, threading, Queue, types
00014 import os.path
00015 from PyQt4 import QtCore, QtGui, Qt
00016
00017
00018 from UI import simpleUI
00019
00020
00021 from ut2XXX import UT2XXX
00022
00023
00024
00025 import graphic, utils
00026
00027
00028
00029 Que_main2thread = Queue.Queue()
00030
00031 Que_thread2main = Queue.Queue()
00032
00033
00034 def DSO_thread():
00035
00036 try:
00037 want_run = True
00038 offline = False
00039
00040
00041 dso = UT2XXX.UNI_T_DSO()
00042
00043 if not dso.is_present:
00044 Que_thread2main.put("ERR_NOT_FOUND")
00045 offline = True
00046 want_run = False
00047 return
00048
00049 msg = ""
00050
00051 loop = 0
00052
00053 while want_run:
00054 loop += 1
00055 try:
00056 msg = Que_main2thread.get_nowait()
00057 except:
00058 pass
00059 else:
00060
00061 if msg == "END_NOW":
00062 want_run = False
00063
00064 elif msg == "REMOTE_ON" and not offline:
00065 dso.enter_far_mode()
00066
00067 elif msg == "REMOTE_OFF" and not offline:
00068 dso.leave_far_mode()
00069
00070 elif msg == "GET_WAVE" and not offline:
00071
00072 dso.get_waveform()
00073 Que_thread2main.put("DATA")
00074 Que_thread2main.put(dso.ch1_data)
00075 Que_thread2main.put(dso.ch2_data)
00076
00077 elif msg == "SAVE_SCREENSHOT" and not offline:
00078 Que_thread2main.put("PIXDATA")
00079 Que_thread2main.put(dso.get_screenshot())
00080
00081 elif msg == "LOAD_WAVE":
00082 m = Que_main2thread.get_nowait()
00083 print m
00084 dso.parse_waveform(m)
00085 Que_thread2main.put("DATA")
00086 Que_thread2main.put(dso.ch1_data)
00087 Que_thread2main.put(dso.ch2_data)
00088
00089 elif msg == "RECONNECT":
00090 dso.init()
00091 if not dso.is_present:
00092 Que_thread2main.put("ERR_NOT_FOUND")
00093 offline = True
00094 else:
00095 offline = False
00096
00097
00098 elif type(msg) == type(1) and not offline:
00099 dso.send_message(msg)
00100 else:
00101 msg = ""
00102
00103
00104
00105 time.sleep(0.001)
00106
00107 except Exception, (s):
00108 Que_thread2main.put("EXCEPTION")
00109 Que_thread2main.put(s)
00110 print "Thread end."
00111 try:
00112 dso.close()
00113 except:
00114 pass
00115
00116
00117 class DSO_main(QtGui.QMainWindow, simpleUI.Ui_MainWindow):
00118 def __init__(self):
00119 QtGui.QMainWindow.__init__(self)
00120
00121 print "DSO remote app is starting ..."
00122 self.setupUi(self)
00123 print "DSO remote app started."
00124
00125 self.dso_thread = threading.Thread(target=DSO_thread)
00126 self.dso_thread.start()
00127
00128 self.scene = graphic.DSO_Scene()
00129
00130 self.gwScreen.setScene(self.scene)
00131 self.gwScreen.update()
00132
00133
00134 self.auto_timer = QtCore.QTimer()
00135
00136 self.connect(self.auto_timer, QtCore.SIGNAL("timeout()"), self.updateScreen)
00137
00138
00139 self.timer = QtCore.QTimer()
00140 self.timer.start(10)
00141 self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.updateState)
00142
00143 self.loadScreenFromDso()
00144
00145 def reconnect(self):
00146
00147
00148
00149
00150
00151
00152 Que_main2thread.put("RECONNECT")
00153
00154
00155 def setTimer(self, force_stop = False):
00156 if not force_stop and self.auto_timer.isActive():
00157 self.auto_timer.stop()
00158 else:
00159 self.auto_timer.start()
00160
00161
00162 def updateScreen(self):
00163 if Que_main2thread.empty():
00164 Que_main2thread.put("GET_WAVE")
00165
00166 def saveScreenshot2png(self, data):
00167
00168 screen = QtGui.QPixmap()
00169 screen.loadFromData(data)
00170
00171 screen = screen.scaledToWidth(640)
00172
00173 self.scene.showPixmap(screen)
00174
00175
00176
00177
00178
00179
00180 def loadLWave(self):
00181 filename = QtGui.QFileDialog.getSaveFileName(self, u"Enter name and path to file", u"./", u"Data (*.dat)", u"???")
00182 if filename:
00183
00184 self.setTimer(True)
00185 Que_main2thread.put("LOAD_WAVE")
00186 Que_main2thread.put(filename)
00187
00188 def updateState(self):
00189
00190 try:
00191 msg = Que_thread2main.get_nowait()
00192 except Exception:
00193 pass
00194 else:
00195
00196 if msg == "DATA":
00197 self.ch1_data = Que_thread2main.get()
00198 self.ch2_data = Que_thread2main.get()
00199 self.scene.updateScreen(self.ch1_data, self.ch2_data)
00200
00201 if msg == "ERR_NOT_FOUND":
00202 QtGui.QMessageBox.critical(self, u"Error", u"UNI-T DSO not found. This error is cricital.\nTurn on DSO and connect it with PC by USB cable. Then run program again.")
00203 self.close()
00204
00205 if msg == "PIXDATA":
00206 print "Recieve pixmap data."
00207 self.saveScreenshot2png(Que_thread2main.get())
00208
00209 if msg == "EXCEPTION":
00210 self.setTimer(True)
00211 excep = Que_thread2main.get()
00212 print "Exception in thread:",excep
00213 try:
00214 QtGui.QMessageBox.critical(self, u"Exception", u"Communication error ocured:\n"+str(excep))
00215 except:
00216 QtGui.QMessageBox.critical(self, u"Exception", u"Comunication error ocured.")
00217
00218
00219
00220
00221 def closeEvent(self, closeEvent):
00222 print "Closing"
00223 Que_main2thread.put("END_NOW")
00224 time.sleep(0.2)
00225 closeEvent.accept()
00226
00227 def loadDataFromDso(self):
00228 self.updateScreen()
00229
00230 def processAction(self, action):
00231 if action.text() == "About":
00232 QtGui.QMessageBox.about(self,"simpleDSO", "Ing. Tomas Kosan, 2010\nSimplified version of qt2062.\nIt allows take screenshot from DSO and save it to harddisk.")
00233 if action.text() == "Exit":
00234 self.close()
00235
00236
00237 def loadScreenFromDso(self):
00238 Que_main2thread.put("SAVE_SCREENSHOT")
00239
00240 def saveProgramScreen(self):
00241 self.image = QtGui.QPixmap(640,480)
00242 self.image.fill(QtCore.Qt.black)
00243 screenshot = QtGui.QPainter(self.image)
00244 self.gwScreen.render(screenshot)
00245 filename = QtGui.QFileDialog.getSaveFileName(self, u"Enter name and path to file", u"./", u"Images (*.png)", u"???")
00246 if filename:
00247 self.image.save(filename,"PNG")
00248
00249 def setAutoUpdate(self, state):
00250 if state:
00251 self.auto_timer.start(self.updateValue.value())
00252 else:
00253 self.auto_timer.stop()
00254
00255
00256 def main(args):
00257 app=QtGui.QApplication(args)
00258 app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app.quit)
00259
00260 mainWindow = DSO_main()
00261 mainWindow.show()
00262 sys.exit(app.exec_())
00263
00264 if __name__ == "__main__":
00265 main(sys.argv)