rendered paste body#!/usr/bin/env python# -*- coding: utf-8 -*-import randomimport sysfrom PyQt4 import Qtfrom PyQt4 import QtCore, QtGui, uicimport PyQt4.Qwt5 as Qwtfrom PyQt4.Qwt5.anynumpy import *SAMPLES=1000class MainApplication(QtGui.QMainWindow): def __init__(self, *args): self.processing = False QtGui.QWidget.__init__(self, *args) uic.loadUi("osciloskopas.ui", self) # available modes self.modes = [[0x03, 0.625], [0x02, 1.25], [0x01, 2.5], [0x00, 5], [0x04, 10]] for mode in self.modes: self.modeBox.addItem(u"±" + str(mode[1]) + "V") self.modeSlider.setRange(0, len(self.modes)-1) # default mode is bipolar 5V self.modeBox.setCurrentIndex(3) self.processCheckBox.setTristate(False) self.connect(self.triggerLevel, QtCore.SIGNAL("valueChanged(int)"), self.dial_x_changed) self.connect(self.sampleRateInput, QtCore.SIGNAL("valueChanged(int)"), self.sample_rate_change) self.connect(self.sampleRateBox, QtCore.SIGNAL("currentIndexChanged(int)"), self.sample_rate_change) self.connect(self.processCheckBox, QtCore.SIGNAL("toggled(bool)"), self.process_toggle) self.connect(self.modeBox, QtCore.SIGNAL("currentIndexChanged(int)"), self.mode_change) self.sample_rate_change(value=10000) # set sampling rate of 10 kHz self.arr1 = self.arr2 = [] self.f = open("results.csv", "r") for line in self.f: line = line.strip().split(";") self.arr1.append(word_to_int(line[0])) self.arr2.append(word_to_int(line[1])) self.__init_plot() self.plot.replot() def __init_plot(self): #self.plot.alignScales = self.alignScales #self.plot.timerEvent = self.timerEvent self.plot.setCanvasBackground(Qt.Qt.white) #self.plot.alignScales() # Initialize data self.plot.x = arange(0.0, SAMPLES, 1) self.plot.y = zeros(len(self.plot.x), Float) self.plot.z = zeros(len(self.plot.x), Float) #self.setTitle("A Moving QwtPlot Demonstration") #self.plot.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend); self.plot.curveR = Qwt.QwtPlotCurve("Data Moving Right") self.plot.curveR.attach(self.plot) self.plot.curveL = Qwt.QwtPlotCurve("Data Moving Left") self.plot.curveL.attach(self.plot) #self.plot.curveL.setSymbol(Qwt.QwtSymbol(Qwt.QwtSymbol.Ellipse, #Qt.QBrush(), Qt.QPen(Qt.Qt.yellow), Qt.QSize(7, 7))) self.plot.curveR.setPen(Qt.QPen(Qt.Qt.red)) self.plot.curveL.setPen(Qt.QPen(Qt.Qt.blue)) """ mY = Qwt.QwtPlotMarker() mY.setLabelAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignTop) mY.setLineStyle(Qwt.QwtPlotMarker.HLine) mY.setYValue(0.0) mY.attach(self.plot) """ #self.setAxisTitle(Qwt.QwtPlot.xBottom, "Time (seconds)") #self.setAxisTitle(Qwt.QwtPlot.yLeft, "Values") self.plot.startTimer(300) self.plot.phase = 0.0 def alignScales(self): self.plot.canvas().setFrameStyle(Qt.QFrame.Box | Qt.QFrame.Plain) self.plot.canvas().setLineWidth(1) for i in range(Qwt.QwtPlot.axisCnt): scaleWidget = self.plot.axisWidget(i) if scaleWidget: scaleWidget.setMargin(0) scaleDraw = self.plot.axisScaleDraw(i) if scaleDraw: scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Backbone, False) # alignScales() def timerEvent(self, e): arr1=[] for tmp in range(SAMPLES): arr1.append(5*sin(2*pi/SAMPLES*tmp)) if self.plot.phase > pi - 0.0001: self.plot.phase = 0.0 self.arr1 = self.arr1[:SAMPLES] self.arr2 = self.arr1[-int(SAMPLES/2):]+self.arr1[:int(SAMPLES/2)] # y moves from left to right: # shift y array right and assign new value y[0] self.plot.y = concatenate((self.plot.y[:1], self.plot.y[:-1]), 1) self.plot.y[0] = sin(self.plot.phase) * (-1.0 + 2.0*random.random()) self.plot.y = self.arr1 #print self.arr1 # z moves from right to left: # Shift z array left and assign new value to z[n-1]. self.plot.z = concatenate((self.plot.z[:1], self.plot.z[:-1]), 1) self.plot.z[0] = 0.8 - (2.0 * self.plot.phase/pi) + 0.4*random.random() #self.plot.z = self.arr2 self.plot.curveR.setData(self.plot.x, self.plot.y) self.plot.curveL.setData(self.plot.x, self.plot.z) self.plot.replot() self.plot.phase += pi*0.02 # timerEvent() def process_toggle(self, state): self.processing = state if self.processing: self.statusBar.showMessage("It is working") else: self.statusBar.showMessage("It is NOT working") print self.processing def dial_x_changed(self, x): print("X: %d" % x) def mode_change(self, mode): #TODO: change #self.engine.mode = self.modes[mode][0] print(self.modes[mode][0]) def sample_rate_change(self, tmp=0, value=False): if value: if value<1000: base = 0 elif value<1000**2: base = 1 elif value<1000**3: base = 2 else: raise ValueError first = int(value/(1000**base)) self.sampleRateInput.setValue(first) self.sampleRateSlider.setValue(first) self.sampleRateBox.setCurrentIndex(base) first = self.sampleRateInput.value() base = int(self.sampleRateBox.currentIndex()) result = first * (1000**base) print first, base, resultdef word_to_int(val): WORD_SPAN = 0 -1500#65536/2 return int(int(val)-WORD_SPAN) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) widget = MainApplication() widget.show() app.exec_()