Esempio n. 1
0
 def mkline(self, xdata, ydata, style=('black','-'), title=''):
     line = QwtPlotCurve(title)
     if title is '':
         # don't display it in the legend
         # kind of ugly, that the title variable is doing double duty
         line.setItemAttribute(QwtPlotItem.Legend, False)
     pen = self.getPen(*style)
     line.setPen(pen)
     line.setRenderHint(QwtPlotItem.RenderAntialiased)
     line.setData(xdata, ydata)
     return line
Esempio n. 2
0
 def mkline(self, xdata, ydata, style=('black', '-'), title=''):
     line = QwtPlotCurve(title)
     if title is '':
         # don't display it in the legend
         # kind of ugly, that the title variable is doing double duty
         line.setItemAttribute(QwtPlotItem.Legend, False)
     pen = self.getPen(*style)
     line.setPen(pen)
     line.setRenderHint(QwtPlotItem.RenderAntialiased)
     line.setData(xdata, ydata)
     return line
Esempio n. 3
0
 def test(self):
     from math import sin, cos
     from PyQt4.Qwt5 import QwtPlotCurve
     w = self.winManager.newWindow()
     plt = w.plot
     xx = [0.05*x for x in range(100)]
     y1 = [sin(x) for x in xx]
     y2 = [cos(x) for x in xx]
     curve1 = QwtPlotCurve('sin(x)')
     curve1.attach(plt)
     curve1.setData(xx, y1)
     curve2 = QwtPlotCurve('cos(x)')
     curve2.attach(plt)
     curve2.setData(xx, y2)
Esempio n. 4
0
    def attachCurves(self, wdg, profiles):
        for i in range(0, len(profiles)):
            tmp_name = ("%s") % (profiles[i]["layer"].name())

            # As QwtPlotCurve doesn't support nodata, split the data into
            # single lines
            # with breaks wherever data is None.
            # Prepare two lists of coordinates (xx and yy). Make x=None
            # whenever y is None.
            xx = profiles[i]["l"]
            yy = profiles[i]["z"]
            for j in range(len(yy)):
                if yy[j] is None:
                    xx[j] = None

            # Split xx and yy into single lines at None values
            xx = [
                list(g) for k, g in itertools.groupby(xx, lambda x: x is None)
                if not k
            ]
            yy = [
                list(g) for k, g in itertools.groupby(yy, lambda x: x is None)
                if not k
            ]

            Cstyle = profiles[i]["style"]
            # Create & attach one QwtPlotCurve per one single line
            for j in range(len(xx)):
                curve = QwtPlotCurve(tmp_name)
                curve.setData(xx[j], yy[j])
                curve.setPen(Cstyle)
                curve.attach(wdg)

            # scaling this
            try:
                wdg.setAxisScale(2, 0, max(profiles[len(profiles) - 1]["l"]),
                                 0)
                self.resetScale(wdg, profiles)
            except:
                pass
                # self.iface.mainWindow().statusBar().showMessage(
                # "Problem with setting scale of plotting")
        # self.resetScale(wdg, profiles)
        wdg.replot()
class PlotVariable:
    """
    A plot variable corresponds to one curve on the plot.
    It keeps track of the generating expression and of the
    values of the expression over time.
    """
    def __init__(self, label, expression, plot, color=None):
        self.expression = expression
        self.data = ExpandableQwtData()
        self.curve = QwtPlotCurve(label)
        self.curve.setData(self.data)
        self.curve.setPen(QtGui.QPen(get_color(color)))
        self.curve.attach(plot)

    def add_point(self, x, y):
        self.data.add_point(x, y)

    def clear_data(self):
        self.data.clear_data()
class PlotVariable:
    """
    A plot variable corresponds to one curve on the plot.
    It keeps track of the generating expression and of the
    values of the expression over time.
    """
    def __init__(self,label,expression,plot,color = None):
        self.expression = expression
        self.data = ExpandableQwtData()
        self.curve = QwtPlotCurve(label)
        self.curve.setData(self.data)
        self.curve.setPen(QtGui.QPen(get_color(color)))
        self.curve.attach(plot)
        
        
    def add_point(self,x,y):
        self.data.add_point(x,y)

    def clear_data(self):
        self.data.clear_data()
Esempio n. 7
0
    def __init__(self, title, xdata, ydata, style, symbol=None, *args):
        super(BMPlot, self).__init__(*args)
        self.setMinimumSize(200, 200)
        self.setTitle(title)
        self.setAxisTitle(QwtPlot.xBottom, 'x')
        self.setAxisTitle(QwtPlot.yLeft, 'y')
        self.curve_nb = 0
        for idx in range(1, 11):
            self.curve_nb += 1
            curve = QwtPlotCurve()
            curve.setPen(QPen(get_curve_color()))
            curve.setStyle(style)
            curve.setRenderHint(QwtPlotCurve.RenderAntialiased)
            if symbol is not None:
                curve.setSymbol(symbol)
            curve.attach(self)
            curve.setData(xdata, ydata*idx, finite=False)
#        self.setAxisScale(self.yLeft, -1.5, 1.5)
#        self.setAxisScale(self.xBottom, 9.9, 10.)
        self.replot()
Esempio n. 8
0
    def __init__(self, parent=None):
        super(TestPlot, self).__init__(parent)
        self.setWindowTitle("PyQwt" if USE_PYQWT5 else "PythonQwt")
        self.enableAxis(self.xTop, True)
        self.enableAxis(self.yRight, True)
        y = np.linspace(0, 10, 500)
        curve1 = QwtPlotCurve('Test Curve 1')
        curve1.setData(np.sin(y), y)
        curve2 = QwtPlotCurve('Test Curve 2')
        curve2.setData(y**3, y)
        if USE_PYQWT5:
            curve2.setAxis(self.xTop, self.yRight)
        else:
            # PythonQwt
            curve2.setAxes(self.xTop, self.yRight)

        for item, col, xa, ya in ((curve1, Qt.green, self.xBottom, self.yLeft),
                                  (curve2, Qt.red, self.xTop, self.yRight)):
            if not USE_PYQWT5:
                # PythonQwt
                item.setOrientation(Qt.Vertical)
            item.attach(self)
            item.setPen(QPen(col))
            for axis_id in xa, ya:
                palette = self.axisWidget(axis_id).palette()
                palette.setColor(QPalette.WindowText, QColor(col))
                palette.setColor(QPalette.Text, QColor(col))
                self.axisWidget(axis_id).setPalette(palette)
                ticks_font = self.axisFont(axis_id)
                self.setAxisFont(axis_id, ticks_font)

        self.canvas().setFrameStyle(0)  #QFrame.Panel|QFrame.Sunken)
        self.plotLayout().setCanvasMargin(0)
        self.axisWidget(QwtPlot.yLeft).setMargin(0)
        self.axisWidget(QwtPlot.xTop).setMargin(0)
        self.axisWidget(QwtPlot.yRight).setMargin(0)
        self.axisWidget(QwtPlot.xBottom).setMargin(0)

        self.marker = QwtPlotMarker()
        self.marker.setValue(0, 5)
        self.marker.attach(self)
Esempio n. 9
0
    def __init__(self, parent=None):
        super(TestPlot, self).__init__(parent)
        self.setWindowTitle("PyQwt" if USE_PYQWT5 else "PythonQwt")
        self.enableAxis(self.xTop, True)
        self.enableAxis(self.yRight, True)
        y = np.linspace(0, 10, 500)
        curve1 = QwtPlotCurve('Test Curve 1')
        curve1.setData(np.sin(y), y)
        curve2 = QwtPlotCurve('Test Curve 2')
        curve2.setData(y**3, y)
        if USE_PYQWT5:
            curve2.setAxis(self.xTop, self.yRight)
        else:
            # PythonQwt
            curve2.setAxes(self.xTop, self.yRight)

        for item, col, xa, ya in ((curve1, Qt.green, self.xBottom, self.yLeft),
                                  (curve2, Qt.red, self.xTop, self.yRight)):
            if not USE_PYQWT5:
                # PythonQwt
                item.setOrientation(Qt.Vertical)
            item.attach(self)
            item.setPen(QPen(col))
            for axis_id in xa, ya:
                palette = self.axisWidget(axis_id).palette()
                palette.setColor(QPalette.WindowText, QColor(col))
                palette.setColor(QPalette.Text, QColor(col))
                self.axisWidget(axis_id).setPalette(palette)
                ticks_font = self.axisFont(axis_id)
                self.setAxisFont(axis_id, ticks_font)
        
        self.canvas().setFrameStyle(0)#QFrame.Panel|QFrame.Sunken)
        self.plotLayout().setCanvasMargin(0)
        self.axisWidget(QwtPlot.yLeft).setMargin(0)
        self.axisWidget(QwtPlot.xTop).setMargin(0)
        self.axisWidget(QwtPlot.yRight).setMargin(0)
        self.axisWidget(QwtPlot.xBottom).setMargin(0)

        self.marker = QwtPlotMarker()
        self.marker.setValue(0, 5)
        self.marker.attach(self)
Esempio n. 10
0
    def __init__(self, title, xdata, ydata, style, symbol=None, *args):
        super(BMPlot, self).__init__(*args)
        self.setMinimumSize(200, 200)
        self.setTitle(title)
        self.setAxisTitle(QwtPlot.xBottom, 'x')
        self.setAxisTitle(QwtPlot.yLeft, 'y')
        self.curve_nb = 0
        for idx in range(1, 11):
            self.curve_nb += 1
            curve = QwtPlotCurve()
            curve.setPen(QPen(get_curve_color()))
            curve.setStyle(style)
            curve.setRenderHint(QwtPlotCurve.RenderAntialiased)
            if symbol is not None:
                curve.setSymbol(symbol)
            curve.attach(self)
            curve.setData(xdata, ydata * idx, finite=False)


#        self.setAxisScale(self.yLeft, -1.5, 1.5)
#        self.setAxisScale(self.xBottom, 9.9, 10.)
        self.replot()
Esempio n. 11
0
class RTTView(QtGui.QWidget, Ui_RTTView):
    def __init__(self, parent=None):
        super(RTTView, self).__init__(parent)

        self.setupUi(self)

        self.initSetting()

        self.initQwtPlot()

        self.daplink = None

        self.tmrDAP = QtCore.QTimer()
        self.tmrDAP.setInterval(10)
        self.tmrDAP.timeout.connect(self.on_tmrDAP_timeout)
        self.tmrDAP.start()

        self.tmrCntr = 0  # tmrDAP超时一次,tmrCntr加一

    def initSetting(self):
        if not os.path.exists('setting.ini'):
            open('setting.ini', 'w')

        self.conf = ConfigParser.ConfigParser()
        self.conf.read('setting.ini')

        if not self.conf.has_section('globals'):
            self.conf.add_section('globals')
            self.conf.set('globals', 'mappath', '[]')
        for path in eval(self.conf.get('globals', 'mappath')):
            self.cmbMap.insertItem(10, path)

    def initQwtPlot(self):
        self.PlotBuff = ''
        self.PlotData = [0] * 1000

        self.qwtPlot = QwtPlot(self)
        self.vLayout0.insertWidget(0, self.qwtPlot)

        self.PlotCurve = QwtPlotCurve()
        self.PlotCurve.attach(self.qwtPlot)
        self.PlotCurve.setData(range(1, len(self.PlotData) + 1), self.PlotData)

        self.on_cmbMode_currentIndexChanged(u'文本')

    def parseRTTAddr(self):
        with open(self.cmbMap.currentText(), 'r') as f:
            for line in f:
                match = re.match(
                    '\s+_SEGGER_RTT\s+(0x[0-9a-fA-F]{8})\s+Data.+', line)
                if match: return int(match.group(1), 16)
            else:
                raise Exception('Can not found _SEGGER_RTT')

    @QtCore.pyqtSlot()
    def on_btnOpen_clicked(self):
        if self.btnOpen.text() == u'打开连接':
            try:
                self.daplink.open()

                self.dp = coresight.dap.DebugPort(self.daplink)
                self.dp.init()
                self.dp.power_up_debug()

                self.ap = coresight.ap.AHB_AP(self.dp, 0)
                self.ap.init()

                self.RTTAddr = self.parseRTTAddr()
            except Exception as e:
                print e
            else:
                self.btnOpen.setText(u'关闭连接')
                self.lblOpen.setPixmap(QtGui.QPixmap("./Image/inopening.png"))
        else:
            try:
                self.daplink.close()
            except Exception as e:
                print e
            finally:
                self.btnOpen.setText(u'打开连接')
                self.lblOpen.setPixmap(QtGui.QPixmap("./Image/inclosing.png"))

    def aUpEmpty(self):
        LEN = (16 + 4 * 2) + (4 * 6) * 4

        buf = self.ap.readBlockMemoryUnaligned8(self.RTTAddr, LEN)

        arr = struct.unpack('16sLLLLLLLL24xLLLLLL24x',
                            ''.join([chr(x) for x in buf]))

        self.aUp = RingBuffer(arr[3:9])

        print 'WrOff=%d, RdOff=%d' % (self.aUp.WrOff, self.aUp.RdOff)

        self.aDown = RingBuffer(arr[9:15])

        return (self.aUp.RdOff == self.aUp.WrOff)

    def aUpRead(self):
        if self.aUp.RdOff < self.aUp.WrOff:
            len_ = self.aUp.WrOff - self.aUp.RdOff

            arr = self.ap.readBlockMemoryUnaligned8(
                self.aUp.pBuffer + self.aUp.RdOff, len_)

            self.aUp.RdOff += len_

            self.ap.write32(self.RTTAddr + (16 + 4 * 2) + 4 * 4,
                            self.aUp.RdOff)
        else:
            len_ = self.aUp.SizeOfBuffer - self.aUp.RdOff + 1

            arr = self.ap.readBlockMemoryUnaligned8(
                self.aUp.pBuffer + self.aUp.RdOff, len_)

            self.aUp.RdOff = 0  #这样下次再读就会进入执行上个条件

            self.ap.write32(self.RTTAddr + (16 + 4 * 2) + 4 * 4,
                            self.aUp.RdOff)

        return ''.join([chr(x) for x in arr])

    def on_tmrDAP_timeout(self):
        if self.btnOpen.text() == u'关闭连接':
            ss = ''
            try:
                if not self.aUpEmpty():
                    ss = self.aUpRead()
            except Exception as e:
                pass

            if ss:
                if self.mode == u'文本':
                    if len(self.txtMain.toPlainText()) > 50000:
                        self.txtMain.clear()
                    self.txtMain.moveCursor(QtGui.QTextCursor.End)
                    self.txtMain.insertPlainText(ss)

                elif self.mode == u'波形':
                    self.PlotBuff += ss
                    if self.PlotBuff.rfind(',') == -1: return
                    try:
                        d = [
                            int(x) for x in self.
                            PlotBuff[0:self.PlotBuff.rfind(',')].split(',')
                        ]
                        for x in d:
                            self.PlotData.pop(0)
                            self.PlotData.append(x)
                    except:
                        self.PlotBuff = ''
                    else:
                        self.PlotBuff = self.PlotBuff[self.PlotBuff.
                                                      rfind(',') + 1:]

                    self.PlotCurve.setData(range(1,
                                                 len(self.PlotData) + 1),
                                           self.PlotData)
                    self.qwtPlot.replot()

        self.detect_daplink()  # 自动检测 DAPLink 的热插拔

    def detect_daplink(self):
        daplinks = pyDAPAccess.DAPAccess.get_connected_devices()

        if self.daplink and (daplinks == []):  # daplink被拔下
            try:
                self.daplink.close()
            except Exception as e:
                print e
            finally:
                self.daplink = None
                self.linDAP.clear()

                self.btnOpen.setText(u'打开连接')
                self.lblOpen.setPixmap(QtGui.QPixmap("./Image/inclosing.png"))

        if not self.daplink and daplinks != []:
            self.daplink = daplinks[0]

            self.linDAP.clear()
            self.linDAP.setText(self.daplink._product_name)

    @QtCore.pyqtSlot()
    def on_btnMap_clicked(self):
        path = QtGui.QFileDialog.getOpenFileName(
            caption=u'项目.map文件路径',
            filter=u'MDK .map file (*.map)',
            directory=self.cmbMap.currentText())
        if path != '':
            self.cmbMap.insertItem(0, path)
            self.cmbMap.setCurrentIndex(0)

    @QtCore.pyqtSlot(str)
    def on_cmbMode_currentIndexChanged(self, str):
        self.mode = str
        self.txtMain.setVisible(self.mode == u'文本')
        self.qwtPlot.setVisible(self.mode == u'波形')

    @QtCore.pyqtSlot()
    def on_btnClear_clicked(self):
        self.txtMain.clear()

    def closeEvent(self, evt):
        paths = []
        for i in range(min(10, self.cmbMap.count())):
            paths.append(self.cmbMap.itemText(i))
        self.conf.set('globals', 'mappath', repr(paths))
        self.conf.write(open('setting.ini', 'w'))
Esempio n. 12
0
class ToftofProfileWindow(DlgUtils, QMainWindow):
    def __init__(self, parent):
        QMainWindow.__init__(self, parent)
        DlgUtils.__init__(self, 'Live data')
        self.panel = parent
        layout1 = QVBoxLayout()
        self.plot = QwtPlot(self)
        layout1.addWidget(self.plot)
        self.curve = QwtPlotCurve()
        self.curve.setRenderHint(QwtPlotCurve.RenderAntialiased)
        self.curve.attach(self.plot)
        self.marker = QwtPlotMarker()
        self.marker.attach(self.plot)
        self.markerpen = QPen(Qt.red)
        self.marker.setSymbol(
            QwtSymbol(QwtSymbol.Ellipse, QBrush(), self.markerpen, QSize(7,
                                                                         7)))
        self.zoomer = QwtPlotZoomer(self.plot.canvas())
        self.zoomer.setMousePattern(QwtPlotZoomer.MouseSelect3, Qt.NoButton)
        self.picker = QwtPlotPicker(self.plot.canvas())
        self.picker.setSelectionFlags(QwtPlotPicker.PointSelection
                                      | QwtPlotPicker.ClickSelection)
        self.picker.setMousePattern(QwtPlotPicker.MouseSelect1, Qt.MidButton)
        self.picker.selected.connect(self.pickerSelected)
        layout2 = QHBoxLayout()
        layout2.addWidget(QLabel('Scale:', self))
        self.scale = QComboBox(self)
        self.scale.addItems([
            'Single detectors, sorted by angle',
            'Scattering angle 2theta (deg)', 'Q value (A-1)'
        ])
        self.scale.currentIndexChanged[int].connect(self.scaleChanged)
        layout2.addWidget(self.scale)
        layout2.addStretch()
        self.scaleframe = QFrame(self)
        self.scaleframe.setLayout(layout2)
        self.scaleframe.setVisible(False)
        layout1.addWidget(self.scaleframe)
        mainframe = QFrame(self)
        mainframe.setLayout(layout1)
        self.setCentralWidget(mainframe)
        self.setContentsMargins(6, 6, 6, 6)
        plotfont = scaledFont(self.font(), 0.7)
        self.plot.setAxisFont(QwtPlot.xBottom, plotfont)
        self.plot.setAxisFont(QwtPlot.yLeft, plotfont)
        self.plot.setCanvasBackground(Qt.white)
        self.resize(800, 200)

        self._detinfo = None
        self._anglemap = None
        self._infowindow = None
        self._infolabel = None
        self._xs = self._ys = None
        self._type = None

    def _retrieve_detinfo(self):
        if self._detinfo is None:
            info = self.panel.client.eval(
                'det._detinfo_parsed, '
                'det._anglemap', None)
            if not info:
                return self.showError('Cannot retrieve detector info.')
            self._lambda = self.panel.client.eval('chWL()', None)
            if not self._lambda:
                return self.showError('Cannot retrieve wavelength.')
            self._detinfo, self._anglemap = info
            self._inverse_anglemap = 0
            self._infowindow = QMainWindow(self)
            self._infolabel = QLabel(self._infowindow)
            self._infolabel.setTextFormat(Qt.RichText)
            self._infowindow.setCentralWidget(self._infolabel)
            self._infowindow.setContentsMargins(10, 10, 10, 10)
            self._inv_anglemap = [[
                entry for entry in self._detinfo[1:]
                if entry[12] == self._anglemap[detnr] + 1
            ][0] for detnr in range(len(self._xs))]

    def scaleChanged(self, scale):
        self.update(self._type, self._orig_nbins, self._orig_x, self._orig_y)

    def update(self, proftype, nbins, x, y):
        self._orig_x = x
        self._orig_y = y
        self._orig_nbins = nbins
        x.setsize(8 * nbins)
        y.setsize(8 * nbins)
        xs = struct.unpack('d' * nbins, x)
        ys = struct.unpack('d' * nbins, y)
        if proftype == 0:
            if self.scale.currentIndex() == 0:
                xs = xs
            elif self.scale.currentIndex() == 1:
                self._retrieve_detinfo()
                xs = [self._inv_anglemap[int(xi)][5] for xi in xs]
            else:
                self._retrieve_detinfo()
                if self._lambda is None:
                    self.showError('Could not determine wavelength.')
                    self.scale.setCurrentIndex(1)
                    return
                xs = [
                    4 * pi / self._lambda *
                    sin(radians(self._inv_anglemap[int(xi)][5] / 2.))
                    for xi in xs
                ]
        self._xs = xs
        self._ys = ys
        self.curve.setData(xs, ys)
        self.plot.setAxisAutoScale(QwtPlot.xBottom)
        self.plot.setAxisAutoScale(QwtPlot.yLeft)
        self.marker.setVisible(False)
        self.zoomer.setZoomBase(True)
        self._type = proftype
        if proftype == 0:
            self.setWindowTitle(
                'Single detector view (time-channel integrated)')
            self.scaleframe.setVisible(True)
        elif proftype == 1:
            self.setWindowTitle('Time channel view (detector integrated)')
            self.scaleframe.setVisible(False)
        else:
            self.scaleframe.setVisible(False)

    def pickerSelected(self, point):
        if self._type != 0:
            return
        self._retrieve_detinfo()
        index = self.curve.closestPoint(self.picker.transform(point))[0]
        detentry = self._inv_anglemap[index][:]
        detentry.append(self._xs[index])
        detentry.append(self._ys[index])
        self.marker.setXValue(self._xs[index])
        self.marker.setYValue(self._ys[index])
        self.marker.setVisible(True)
        self.plot.replot()
        self._infowindow.show()
        entrynames = [
            'EntryNr', 'Rack', 'Plate', 'Pos', 'RPos', '2Theta', 'CableNr',
            'CableType', 'CableLen', 'CableEmpty', 'Card', 'Chan', 'Total',
            'DetName', 'BoxNr', 'BoxChan', 'XValue', 'Counts'
        ]
        formats = [
            '%s', '%d', '%d', '%d', '%d', '%.3f', '%d', '%d', '%.2f', '%d',
            '%d', '%d', '%d', '%r', '%d', '%d', '%s', '%d'
        ]
        empties = [1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0]
        self._infolabel.setText('Detector info:<br><table>' + ''.join(
            '<tr><td>%s</td><td></td><td>%s</td></tr>%s' %
            (name, format % value, '<tr></tr>' if empty else '')
            for (name, format, empty,
                 value) in zip(entrynames, formats, empties, detentry)) +
                                '</table>')

    def closeEvent(self, event):
        if self._infowindow:
            self._infowindow.close()
Esempio n. 13
0
app = QtGui.QApplication(sys.argv)

p = QwtPlot()


curve = QwtPlotCurve("Segon i tal")


fn = resDir + '/' + data
f = file(fn)
v = pickle.load(f)


y = v
x = range(len(y))

curve.setData(x, y)

curve.attach(p)

p.replot()

p.show()

sys.exit(app.exec_())




Esempio n. 14
0
    def attachCurves(self, wdg, profiles, model1, library):
        if library == "Qwt5" and has_qwt:
            wdg.plotWdg.clear()
        for i in range(0, model1.rowCount()):
            profileName = model1.item(i, 4).data(Qt.EditRole)
            profileId = model1.item(i, 5).data(Qt.EditRole)
            isVisible = model1.item(i, 0).data(Qt.CheckStateRole)

            xx = profiles[i]["l"]
            yy = profiles[i][profileName]

            if library == "Qwt5" and has_qwt:
                # As QwtPlotCurve doesn't support nodata, split the data into single lines
                # with breaks wherever data is None.
                # Prepare two lists of coordinates (xx and yy). Make x=None whenever y==None.
                for j in range(len(yy)):
                    if yy[j] is None or isnan(yy[j]):
                        xx[j] = None
                        yy[j] = None

                # Split xx and yy into single lines at None values
                xx = [
                    list(g)
                    for k, g in itertools.groupby(xx, lambda x: x is None)
                    if not k
                ]
                yy = [
                    list(g)
                    for k, g in itertools.groupby(yy, lambda x: x is None)
                    if not k
                ]

                # Create & attach one QwtPlotCurve per one single line
                for j in range(len(xx)):
                    curve = QwtPlotCurve(profileId)
                    curve.setData(xx[j], yy[j])

                    curve.setPen(
                        QPen(model1.item(i, 1).data(Qt.BackgroundRole), 3))
                    curve.attach(wdg.plotWdg)
                    curve.setVisible(isVisible)

            elif library == "Matplotlib" and has_mpl:
                lines = wdg.plotWdg.figure.get_axes()[0].get_lines()
                lineIds = [line.get_gid() for line in lines]
                if profileId in lineIds:
                    # Update existing line
                    line = lines[lineIds.index(profileId)]
                    line.set_data([xx, yy])
                else:
                    # Create new line
                    line = wdg.plotWdg.figure.get_axes()[0].plot(
                        xx, yy, gid=profileId)[0]
                line.set_visible(isVisible)
                # X-axis have to be set before wdg is redrawn in changeColor, otherwise and
                # exception is sometimes thrown when time-based axis is used.
                minimumValueX = min(z for z in profiles[i]["l"])
                maximumValueX = max(z for z in profiles[i]["l"])
                wdg.plotWdg.figure.get_axes()[0].set_xlim(
                    [minimumValueX, maximumValueX])
                self.changeColor(wdg, "Matplotlib",
                                 model1.item(i, 1).data(Qt.BackgroundRole),
                                 profileId)
Esempio n. 15
0
 def setData(self, x, y):
     return QwtPlotCurve.setData(self, list(map(float, x)),
                                 list(map(float, y)))
Esempio n. 16
0
class ImageController(QFrame):
    """An ImageController is a widget for controlling the display of one image.
    It can emit the following signals from the image:
    raise                     raise button was clicked
    center                  center-on-image option was selected
    unload                  unload option was selected
    slice                     image slice has changed, need to redraw (emitted by SkyImage automatically)
    repaint                 image display range or colormap has changed, need to redraw (emitted by SkyImage automatically)
    """

    def __init__(self, image, parent, imgman, name=None, save=False):
        QFrame.__init__(self, parent)
        self.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
        # init state
        self.image = image
        self._imgman = imgman
        self._currier = PersistentCurrier()
        self._control_dialog = None
        # create widgets
        self._lo = lo = QHBoxLayout(self)
        lo.setContentsMargins(0, 0, 0, 0)
        lo.setSpacing(2)
        # raise button
        self._wraise = QToolButton(self)
        lo.addWidget(self._wraise)
        self._wraise.setIcon(pixmaps.raise_up.icon())
        self._wraise.setAutoRaise(True)
        self._can_raise = False
        QObject.connect(self._wraise, SIGNAL("clicked()"), self._raiseButtonPressed)
        self._wraise.setToolTip("""<P>Click here to raise this image above other images. Hold the button down briefly to
      show a menu of image operations.</P>""")
        # center label
        self._wcenter = QLabel(self)
        self._wcenter.setPixmap(pixmaps.center_image.pm())
        self._wcenter.setToolTip(
            "<P>The plot is currently centered on (the reference pixel %d,%d) of this image.</P>" % self.image.referencePixel())
        lo.addWidget(self._wcenter)
        # name/filename label
        self.name = image.name
        self._wlabel = QLabel(self.name, self)
        self._number = 0
        self.setName(self.name)
        self._wlabel.setToolTip("%s %s" % (image.filename, "\u00D7".join(map(str, image.data().shape))))
        lo.addWidget(self._wlabel, 1)
        # if 'save' is specified, create a "save" button
        if save:
            self._wsave = QToolButton(self)
            lo.addWidget(self._wsave)
            self._wsave.setText("save")
            self._wsave.setAutoRaise(True)
            self._save_dir = save if isinstance(save, str) else "."
            QObject.connect(self._wsave, SIGNAL("clicked()"), self._saveImage)
            self._wsave.setToolTip("""<P>Click here to write this image to a FITS file.</P>""")
        # render control
        dprint(2, "creating RenderControl")
        self._rc = RenderControl(image, self)
        dprint(2, "done")
        # selectors for extra axes
        self._wslicers = []
        curslice = self._rc.currentSlice();  # this may be loaded from config, so not necessarily 0
        for iextra, axisname, labels in self._rc.slicedAxes():
            if axisname.upper() not in ["STOKES", "COMPLEX"]:
                lbl = QLabel("%s:" % axisname, self)
                lo.addWidget(lbl)
            else:
                lbl = None
            slicer = QComboBox(self)
            self._wslicers.append(slicer)
            lo.addWidget(slicer)
            slicer.addItems(labels)
            slicer.setToolTip("""<P>Selects current slice along the %s axis.</P>""" % axisname)
            slicer.setCurrentIndex(curslice[iextra])
            QObject.connect(slicer, SIGNAL("activated(int)"), self._currier.curry(self._rc.changeSlice, iextra))
        # min/max display ranges
        lo.addSpacing(5)
        self._wrangelbl = QLabel(self)
        lo.addWidget(self._wrangelbl)
        self._minmaxvalidator = FloatValidator(self)
        self._wmin = QLineEdit(self)
        self._wmax = QLineEdit(self)
        width = self._wmin.fontMetrics().width("1.234567e-05")
        for w in self._wmin, self._wmax:
            lo.addWidget(w, 0)
            w.setValidator(self._minmaxvalidator)
            w.setMaximumWidth(width)
            w.setMinimumWidth(width)
            QObject.connect(w, SIGNAL("editingFinished()"), self._changeDisplayRange)
        # full-range button
        self._wfullrange = QToolButton(self)
        lo.addWidget(self._wfullrange, 0)
        self._wfullrange.setIcon(pixmaps.zoom_range.icon())
        self._wfullrange.setAutoRaise(True)
        QObject.connect(self._wfullrange, SIGNAL("clicked()"), self.renderControl().resetSubsetDisplayRange)
        rangemenu = QMenu(self)
        rangemenu.addAction(pixmaps.full_range.icon(), "Full subset", self.renderControl().resetSubsetDisplayRange)
        for percent in (99.99, 99.9, 99.5, 99, 98, 95):
            rangemenu.addAction("%g%%" % percent, self._currier.curry(self._changeDisplayRangeToPercent, percent))
        self._wfullrange.setPopupMode(QToolButton.DelayedPopup)
        self._wfullrange.setMenu(rangemenu)
        # update widgets from current display range
        self._updateDisplayRange(*self._rc.displayRange())
        # lock button
        self._wlock = QToolButton(self)
        self._wlock.setIcon(pixmaps.unlocked.icon())
        self._wlock.setAutoRaise(True)
        self._wlock.setToolTip("""<P>Click to lock or unlock the intensity range. When the intensity range is locked across multiple images, any changes in the intensity
          range of one are propagated to the others. Hold the button down briefly for additional options.</P>""")
        lo.addWidget(self._wlock)
        QObject.connect(self._wlock, SIGNAL("clicked()"), self._toggleDisplayRangeLock)
        QObject.connect(self.renderControl(), SIGNAL("displayRangeLocked"), self._setDisplayRangeLock)
        QObject.connect(self.renderControl(), SIGNAL("dataSubsetChanged"), self._dataSubsetChanged)
        lockmenu = QMenu(self)
        lockmenu.addAction(pixmaps.locked.icon(), "Lock all to this",
                           self._currier.curry(imgman.lockAllDisplayRanges, self.renderControl()))
        lockmenu.addAction(pixmaps.unlocked.icon(), "Unlock all", imgman.unlockAllDisplayRanges)
        self._wlock.setPopupMode(QToolButton.DelayedPopup)
        self._wlock.setMenu(lockmenu)
        self._setDisplayRangeLock(self.renderControl().isDisplayRangeLocked())
        # dialog button
        self._wshowdialog = QToolButton(self)
        lo.addWidget(self._wshowdialog)
        self._wshowdialog.setIcon(pixmaps.colours.icon())
        self._wshowdialog.setAutoRaise(True)
        self._wshowdialog.setToolTip("""<P>Click for colourmap and intensity policy options.</P>""")
        QObject.connect(self._wshowdialog, SIGNAL("clicked()"), self.showRenderControls)
        tooltip = """<P>You can change the currently displayed intensity range by entering low and high limits here.</P>
    <TABLE>
      <TR><TD><NOBR>Image min:</NOBR></TD><TD>%g</TD><TD>max:</TD><TD>%g</TD></TR>
      </TABLE>""" % self.image.imageMinMax()
        for w in self._wmin, self._wmax, self._wrangelbl:
            w.setToolTip(tooltip)

        # create image operations menu
        self._menu = QMenu(self.name, self)
        self._qa_raise = self._menu.addAction(pixmaps.raise_up.icon(), "Raise image",
                                              self._currier.curry(self.image.emit, SIGNAL("raise")))
        self._qa_center = self._menu.addAction(pixmaps.center_image.icon(), "Center plot on image",
                                               self._currier.curry(self.image.emit, SIGNAL("center")))
        self._qa_show_rc = self._menu.addAction(pixmaps.colours.icon(), "Colours && Intensities...",
                                                self.showRenderControls)
        if save:
            self._qa_save = self._menu.addAction("Save image...", self._saveImage)
        self._menu.addAction("Export image to PNG file...", self._exportImageToPNG)
        self._export_png_dialog = None
        self._menu.addAction("Unload image", self._currier.curry(self.image.emit, SIGNAL("unload")))
        self._wraise.setMenu(self._menu)
        self._wraise.setPopupMode(QToolButton.DelayedPopup)

        # connect updates from renderControl and image
        self.image.connect(SIGNAL("slice"), self._updateImageSlice)
        QObject.connect(self._rc, SIGNAL("displayRangeChanged"), self._updateDisplayRange)

        # default plot depth of image markers
        self._z_markers = None
        # and the markers themselves
        self._image_border = QwtPlotCurve()
        self._image_label = QwtPlotMarker()

        # subset markers
        self._subset_pen = QPen(QColor("Light Blue"))
        self._subset_border = QwtPlotCurve()
        self._subset_border.setPen(self._subset_pen)
        self._subset_border.setVisible(False)
        self._subset_label = QwtPlotMarker()
        text = QwtText("subset")
        text.setColor(self._subset_pen.color())
        self._subset_label.setLabel(text)
        self._subset_label.setLabelAlignment(Qt.AlignRight | Qt.AlignBottom)
        self._subset_label.setVisible(False)
        self._setting_lmrect = False

        self._all_markers = [self._image_border, self._image_label, self._subset_border, self._subset_label]

    def close(self):
        if self._control_dialog:
            self._control_dialog.close()
            self._control_dialog = None

    def __del__(self):
        self.close()

    def __eq__(self, other):
        return self is other

    def renderControl(self):
        return self._rc

    def getMenu(self):
        return self._menu

    def getFilename(self):
        return self.image.filename

    def setName(self, name):
        self.name = name
        self._wlabel.setText("%s: %s" % (chr(ord('a') + self._number), self.name))

    def setNumber(self, num):
        self._number = num
        self._menu.menuAction().setText("%s: %s" % (chr(ord('a') + self._number), self.name))
        self._qa_raise.setShortcut(QKeySequence("Alt+" + chr(ord('A') + num)))
        self.setName(self.name)

    def getNumber(self):
        return self._number

    def setPlotProjection(self, proj):
        self.image.setPlotProjection(proj)
        sameproj = proj == self.image.projection
        self._wcenter.setVisible(sameproj)
        self._qa_center.setVisible(not sameproj)
        if self._image_border:
            (l0, l1), (m0, m1) = self.image.getExtents()
            path = numpy.array([l0, l0, l1, l1, l0]), numpy.array([m0, m1, m1, m0, m0])
            self._image_border.setData(*path)
            if self._image_label:
                self._image_label.setValue(path[0][2], path[1][2])

    def addPlotBorder(self, border_pen, label, label_color=None, bg_brush=None):
        # make plot items for image frame
        # make curve for image borders
        (l0, l1), (m0, m1) = self.image.getExtents()
        self._border_pen = QPen(border_pen)
        self._image_border.show()
        self._image_border.setData([l0, l0, l1, l1, l0], [m0, m1, m1, m0, m0])
        self._image_border.setPen(self._border_pen)
        self._image_border.setZ(self.image.z() + 1 if self._z_markers is None else self._z_markers)
        if label:
            self._image_label.show()
            self._image_label_text = text = QwtText(" %s " % label)
            text.setColor(label_color)
            text.setBackgroundBrush(bg_brush)
            self._image_label.setValue(l1, m1)
            self._image_label.setLabel(text)
            self._image_label.setLabelAlignment(Qt.AlignRight | Qt.AlignVCenter)
            self._image_label.setZ(self.image.z() + 2 if self._z_markers is None else self._z_markers)

    def setPlotBorderStyle(self, border_color=None, label_color=None):
        if border_color:
            self._border_pen.setColor(border_color)
            self._image_border.setPen(self._border_pen)
        if label_color:
            self._image_label_text.setColor(label_color)
            self._image_label.setLabel(self._image_label_text)

    def showPlotBorder(self, show=True):
        self._image_border.setVisible(show)
        self._image_label.setVisible(show)

    def attachToPlot(self, plot, z_markers=None):
        for item in [self.image] + self._all_markers:
            if item.plot() != plot:
                item.attach(plot)

    def setImageVisible(self, visible):
        self.image.setVisible(visible)

    def showRenderControls(self):
        if not self._control_dialog:
            dprint(1, "creating control dialog")
            self._control_dialog = ImageControlDialog(self, self._rc, self._imgman)
            dprint(1, "done")
        if not self._control_dialog.isVisible():
            dprint(1, "showing control dialog")
            self._control_dialog.show()
        else:
            self._control_dialog.hide()

    def _changeDisplayRangeToPercent(self, percent):
        if not self._control_dialog:
            self._control_dialog = ImageControlDialog(self, self._rc, self._imgman)
        self._control_dialog._changeDisplayRangeToPercent(percent)

    def _updateDisplayRange(self, dmin, dmax):
        """Updates display range widgets."""
        self._wmin.setText("%.4g" % dmin)
        self._wmax.setText("%.4g" % dmax)
        self._updateFullRangeIcon()

    def _changeDisplayRange(self):
        """Gets display range from widgets and updates the image with it."""
        try:
            newrange = float(str(self._wmin.text())), float(str(self._wmax.text()))
        except ValueError:
            return
        self._rc.setDisplayRange(*newrange)

    def _dataSubsetChanged(self, subset, minmax, desc, subset_type):
        """Called when the data subset changes (or is reset)"""
        # hide the subset indicator -- unless we're invoked while we're actually setting the subset itself
        if not self._setting_lmrect:
            self._subset = None
            self._subset_border.setVisible(False)
            self._subset_label.setVisible(False)

    def setLMRectSubset(self, rect):
        self._subset = rect
        l0, m0, l1, m1 = rect.getCoords()
        self._subset_border.setData([l0, l0, l1, l1, l0], [m0, m1, m1, m0, m0])
        self._subset_border.setVisible(True)
        self._subset_label.setValue(max(l0, l1), max(m0, m1))
        self._subset_label.setVisible(True)
        self._setting_lmrect = True
        self.renderControl().setLMRectSubset(rect)
        self._setting_lmrect = False

    def currentSlice(self):
        return self._rc.currentSlice()

    def _updateImageSlice(self, slice):
        dprint(2, slice)
        for i, (iextra, name, labels) in enumerate(self._rc.slicedAxes()):
            slicer = self._wslicers[i]
            if slicer.currentIndex() != slice[iextra]:
                dprint(3, "setting widget", i, "to", slice[iextra])
                slicer.setCurrentIndex(slice[iextra])

    def setMarkersZ(self, z):
        self._z_markers = z
        for i, elem in enumerate(self._all_markers):
            elem.setZ(z + i)

    def setZ(self, z, top=False, depthlabel=None, can_raise=True):
        self.image.setZ(z)
        if self._z_markers is None:
            for i, elem in enumerate(self._all_markers):
                elem.setZ(z + i + i)
        # set the depth label, if any
        label = "%s: %s" % (chr(ord('a') + self._number), self.name)
        # label = "%s %s"%(depthlabel,self.name) if depthlabel else self.name
        if top:
            label = "%s: <B>%s</B>" % (chr(ord('a') + self._number), self.name)
        self._wlabel.setText(label)
        # set hotkey
        self._qa_show_rc.setShortcut(Qt.Key_F9 if top else QKeySequence())
        # set raise control
        self._can_raise = can_raise
        self._qa_raise.setVisible(can_raise)
        self._wlock.setVisible(can_raise)
        if can_raise:
            self._wraise.setToolTip(
                "<P>Click here to raise this image to the top. Click on the down-arrow to access the image menu.</P>")
        else:
            self._wraise.setToolTip("<P>Click to access the image menu.</P>")

    def _raiseButtonPressed(self):
        if self._can_raise:
            self.image.emit(SIGNAL("raise"))
        else:
            self._wraise.showMenu()

    def _saveImage(self):
        filename = QFileDialog.getSaveFileName(self, "Save FITS file", self._save_dir,
                                               "FITS files(*.fits *.FITS *fts *FTS)")
        filename = str(filename)
        if not filename:
            return
        busy = BusyIndicator()
        self._imgman.showMessage("""Writing FITS image %s""" % filename, 3000)
        QApplication.flush()
        try:
            self.image.save(filename)
        except Exception as exc:
            busy = None
            traceback.print_exc()
            self._imgman.showErrorMessage("""Error writing FITS image %s: %s""" % (filename, str(sys.exc_info()[1])))
            return None
        self.renderControl().startSavingConfig(filename)
        self.setName(self.image.name)
        self._qa_save.setVisible(False)
        self._wsave.hide()
        busy = None

    def _exportImageToPNG(self, filename=None):
        if not filename:
            if not self._export_png_dialog:
                dialog = self._export_png_dialog = QFileDialog(self, "Export image to PNG", ".", "*.png")
                dialog.setDefaultSuffix("png")
                dialog.setFileMode(QFileDialog.AnyFile)
                dialog.setAcceptMode(QFileDialog.AcceptSave)
                dialog.setModal(True)
                QObject.connect(dialog, SIGNAL("filesSelected(const QStringList &)"), self._exportImageToPNG)
            return self._export_png_dialog.exec_() == QDialog.Accepted
        busy = BusyIndicator()
        if isinstance(filename, QStringList):
            filename = filename[0]
        filename = str(filename)
        # make QPixmap
        nx, ny = self.image.imageDims()
        (l0, l1), (m0, m1) = self.image.getExtents()
        pixmap = QPixmap(nx, ny)
        painter = QPainter(pixmap)
        # use QwtPlot implementation of draw canvas, since we want to avoid caching
        xmap = QwtScaleMap()
        xmap.setPaintInterval(0, nx)
        xmap.setScaleInterval(l1, l0)
        ymap = QwtScaleMap()
        ymap.setPaintInterval(ny, 0)
        ymap.setScaleInterval(m0, m1)
        self.image.draw(painter, xmap, ymap, pixmap.rect())
        painter.end()
        # save to file
        try:
            pixmap.save(filename, "PNG")
        except Exception as exc:
            self.emit(SIGNAL("showErrorMessage"), "Error writing %s: %s" % (filename, str(exc)))
            return
        self.emit(SIGNAL("showMessage"), "Exported image to file %s" % filename)

    def _toggleDisplayRangeLock(self):
        self.renderControl().lockDisplayRange(not self.renderControl().isDisplayRangeLocked())

    def _setDisplayRangeLock(self, locked):
        self._wlock.setIcon(pixmaps.locked.icon() if locked else pixmaps.unlocked.icon())

    def _updateFullRangeIcon(self):
        if self._rc.isSubsetDisplayRange():
            self._wfullrange.setIcon(pixmaps.zoom_range.icon())
            self._wfullrange.setToolTip(
                """<P>The current intensity range is the full range. Hold this button down briefly for additional options.</P>""")
        else:
            self._wfullrange.setIcon(pixmaps.full_range.icon())
            self._wfullrange.setToolTip(
                """<P>Click to reset to a full intensity range. Hold the button down briefly for additional options.</P>""")
Esempio n. 17
0
 def setData(self, x, y):
     QwtPlotCurve.setData(self, MaskedData(x, y))
Esempio n. 18
0
class ExampleApp(QtGui.QMainWindow, gui.Ui_MainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.label_current_state.installEventFilter(self)
        self.label_sensor.installEventFilter(self)
        self.label_sensor_1.installEventFilter(self)
        self.label_sensor_2.installEventFilter(self)
        self.label_sensor_3.installEventFilter(self)
        self.label_sensor_4.installEventFilter(self)

        self.label_humidity.installEventFilter(self)
        self.label_humidity_1.installEventFilter(self)
        self.label_humidity_2.installEventFilter(self)
        self.label_humidity_3.installEventFilter(self)
        self.label_humidity_4.installEventFilter(self)

        self.label_temperature.installEventFilter(self)
        self.label_temperature_1.installEventFilter(self)
        self.label_temperature_2.installEventFilter(self)
        self.label_temperature_3.installEventFilter(self)
        self.label_temperature_4.installEventFilter(self)
        """self.doubleSpinBox_humidity.installEventFilter(self)
        self.doubleSpinBox_humidity1.installEventFilter(self)
        self.doubleSpinBox_humidity2.installEventFilter(self)
        self.doubleSpinBox_humidity3.installEventFilter(self)
        self.doubleSpinBox_humidity4.installEventFilter(self)
        
        self.doubleSpinBox_temperature.installEventFilter(self)
        self.doubleSpinBox_temperature1.installEventFilter(self)
        self.doubleSpinBox_temperature2.installEventFilter(self)
        self.doubleSpinBox_temperature3.installEventFilter(self)
        self.doubleSpinBox_temperature4.installEventFilter(self)"""

        self.stateOfCurvers = [True, True, True, True, True]

        self.sensors = [
            "p11/raspberry/sht75.01", "p11/raspberry/sht75.02",
            "p11/raspberry/sht75.03", "p11/raspberry/sht75.04",
            "p11/raspberry/sht75.05"
        ]

        cwd = os.getcwd()
        self.filePath = cwd + "/data/"
        if not os.path.exists(self.filePath):
            os.makedirs(self.filePath)
        #print self.cwd
        self.filename = 'hum_data%s.csv' % time.strftime('%Y%m%d-%H%M%S')
        self.file = open(self.filePath + self.filename, 'wb')
        self.writer = csv.writer(self.file,
                                 delimiter=';',
                                 quotechar="",
                                 quoting=csv.QUOTE_NONE,
                                 escapechar=';')
        self.writer.writerow([
            '#Time',
            '#Temperature_1',
            'Humidity_1',
            'Temperature_2',
            'Humidity_2',
            'Temperature_3',
            'Humidity_3',
            'Temperature_4',
            'Humidity_4',
            'Temperature_5',
            'Humidity_5',
        ])

        self.label_sensor.setStyleSheet(
            "background-color: rgb(153,0,0); color: rgb(255,255,255);")
        self.label_sensor_1.setStyleSheet(
            "background-color: rgb(153,153,0); color: rgb(255,255,255);")
        self.label_sensor_2.setStyleSheet(
            "background-color: rgb(0,0,0); color: rgb(255,255,255);")
        self.label_sensor_3.setStyleSheet(
            "background-color: rgb(0,0,153); color: rgb(255,255,255);")
        self.label_sensor_4.setStyleSheet(
            "background-color: rgb(0,153,0); color: rgb(255,255,255);")

        #self.humidity = []
        #self.temperature = []
        self.humidity1 = "---"
        self.humidity2 = "---"
        self.humidity3 = "---"
        self.humidity4 = "---"
        self.humidity5 = "---"
        self.temperature1 = "---"
        self.temperature2 = "---"
        self.temperature3 = "---"
        self.temperature4 = "---"
        self.temperature5 = "---"

        self.doubleSpinBox_temperature.setValue(0.00)
        self.doubleSpinBox_humidity.setValue(0.00)
        self.doubleSpinBox_temperature1.setValue(0.00)
        self.doubleSpinBox_humidity1.setValue(0.00)
        self.doubleSpinBox_temperature2.setValue(0.00)
        self.doubleSpinBox_humidity2.setValue(0.00)
        self.doubleSpinBox_temperature3.setValue(0.00)
        self.doubleSpinBox_humidity3.setValue(0.00)
        self.doubleSpinBox_temperature4.setValue(0.00)
        self.doubleSpinBox_humidity4.setValue(0.00)

        self.containers = []
        for i in range(len(self.sensors)):
            #self.containers.append(deque(maxlen = 2250))
            self.containers.append(deque(maxlen=111))
        #self.times = deque(maxlen = 2250)
        self.times = deque(maxlen=111)

        self.qwtPlot.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend)
        self.curve1 = QwtPlotCurve("Sensor1")
        #self.curve1.setPen(Qt.QPen(Qt.Qt.red, 3, Qt.Qt.SolidLine))
        self.curve1.setPen(Qt.QPen(QColor(153, 0, 0), 3, Qt.Qt.SolidLine))
        self.curve2 = QwtPlotCurve("Sensor2")
        #self.curve2.setPen(Qt.QPen(Qt.Qt.yellow, 3, Qt.Qt.SolidLine))
        self.curve2.setPen(Qt.QPen(QColor(153, 153, 0), 3, Qt.Qt.SolidLine))
        self.curve3 = QwtPlotCurve("Sensor3")
        #self.curve3.setPen(Qt.QPen(Qt.Qt.black, 3, Qt.Qt.SolidLine))
        self.curve3.setPen(Qt.QPen(QColor(0, 0, 0), 3, Qt.Qt.SolidLine))
        self.curve4 = QwtPlotCurve("Sensor4")
        #self.curve4.setPen(Qt.QPen(Qt.Qt.blue, 3, Qt.Qt.SolidLine))
        self.curve4.setPen(Qt.QPen(QColor(0, 0, 153), 3, Qt.Qt.SolidLine))
        #self.curve5 = QwtPlotCurve("Sensor5")
        #self.curve5.setPen(Qt.QPen(Qt.Qt.green, 3, Qt.Qt.SolidLine))
        #self.curve5.setPen(Qt.QPen(QColor(0,153,0), 3, Qt.Qt.SolidLine))
        self.curve1.attach(self.qwtPlot)
        self.curve2.attach(self.qwtPlot)
        self.curve3.attach(self.qwtPlot)
        self.curve4.attach(self.qwtPlot)
        #self.curve5.attach(self.qwtPlot)

        #print self.qwtPlot.axisAutoScale(0)
        #print self.qwtPlot.axisAutoScale(1)
        #print self.qwtPlot.axisAutoScale(2)
        #print self.qwtPlot.axisAutoScale(3)
        #self.qwtPlot.setAxisAutoScale(0)
        #self.qwtPlot.setAxisScale(2, 0, 0.3)

        self.data_thread1 = read_rasp_Thread(self.sensors[0])
        self.connect(self.data_thread1, SIGNAL("newData"), self.updateData1)
        self.connect(self.data_thread1, SIGNAL("connectionFailed()"),
                     self.connectionFailed1)
        self.data_thread2 = read_rasp_Thread(self.sensors[1])
        self.connect(self.data_thread2, SIGNAL("newData"), self.updateData2)
        self.connect(self.data_thread2, SIGNAL("connectionFailed()"),
                     self.connectionFailed2)
        self.data_thread3 = read_rasp_Thread(self.sensors[2])
        self.connect(self.data_thread3, SIGNAL("newData"), self.updateData3)
        self.connect(self.data_thread3, SIGNAL("connectionFailed()"),
                     self.connectionFailed3)
        self.data_thread4 = read_rasp_Thread(self.sensors[3])
        self.connect(self.data_thread4, SIGNAL("newData"), self.updateData4)
        self.connect(self.data_thread4, SIGNAL("connectionFailed()"),
                     self.connectionFailed4)
        self.data_thread5 = read_rasp_Thread(self.sensors[4])
        self.connect(self.data_thread5, SIGNAL("newData"), self.updateData5)
        self.connect(self.data_thread5, SIGNAL("connectionFailed()"),
                     self.connectionFailed5)
        self.data_thread1.start()
        self.data_thread2.start()
        self.data_thread3.start()
        self.data_thread4.start()
        self.data_thread5.start()

        self.connections = [True, True, True, True, True]
        self.signals = [False, False, False, False, False]

        self.start = time.time()
        self.TimeToWrite = time.time()

    def connectionFailed1(self):
        self.connections[0] = False
        self.updateData1("-----", "-----", True)

    def connectionFailed2(self):
        self.connections[1] = False
        self.updateData2("-----", "-----", True)

    def connectionFailed3(self):
        self.connections[2] = False
        self.updateData3("-----", "-----", True)

    def connectionFailed4(self):
        self.connections[3] = False
        self.updateData4("-----", "-----", True)

    def connectionFailed5(self):
        self.connections[4] = False
        self.updateData5("-----", "-----", True)

    def updateData1(self, humidity, temperature, failed=False):
        self.signals[0] = True
        if not failed:
            self.doubleSpinBox_humidity.setStyleSheet(
                "background-color: rgb(255,255,255);")
            self.doubleSpinBox_temperature.setStyleSheet(
                "background-color: rgb(255,255,255);")
            self.doubleSpinBox_humidity.setValue(humidity)
            self.doubleSpinBox_temperature.setValue(temperature)
            self.humidity1 = float("%.2f" % humidity)
            self.temperature1 = float("%.2f" % temperature)
        else:
            self.doubleSpinBox_humidity.setStyleSheet(
                "background-color: rgb(255,0,0);")
            self.doubleSpinBox_temperature.setStyleSheet(
                "background-color: rgb(255,0,0);")
            self.doubleSpinBox_humidity.setValue(0.00)
            self.doubleSpinBox_temperature.setValue(0.00)
            self.humidity1 = "---"
            self.temperature1 = "---"

    def updateData2(self, humidity, temperature, failed=False):
        self.signals[1] = True
        if not failed:
            self.doubleSpinBox_humidity1.setStyleSheet(
                "background-color: rgb(255,255,255);")
            self.doubleSpinBox_temperature1.setStyleSheet(
                "background-color: rgb(255,255,255);")
            self.doubleSpinBox_humidity1.setValue(humidity)
            self.doubleSpinBox_temperature1.setValue(temperature)
            self.humidity2 = float("%.2f" % humidity)
            self.temperature2 = float("%.2f" % temperature)
        else:
            self.doubleSpinBox_humidity1.setStyleSheet(
                "background-color: rgb(255,0,0);")
            self.doubleSpinBox_temperature1.setStyleSheet(
                "background-color: rgb(255,0,0);")
            self.doubleSpinBox_humidity1.setValue(0.00)
            self.doubleSpinBox_temperature1.setValue(0.00)
            self.humidity2 = "---"
            self.temperature2 = "---"

    def updateData3(self, humidity, temperature, failed=False):
        self.signals[2] = True
        if not failed:
            self.doubleSpinBox_humidity2.setStyleSheet(
                "background-color: rgb(255,255,255);")
            self.doubleSpinBox_temperature2.setStyleSheet(
                "background-color: rgb(255,255,255);")
            self.doubleSpinBox_humidity2.setValue(humidity)
            self.doubleSpinBox_temperature2.setValue(temperature)
            self.humidity3 = float("%.2f" % humidity)
            self.temperature3 = float("%.2f" % temperature)
        else:
            self.doubleSpinBox_humidity2.setStyleSheet(
                "background-color: rgb(255,0,0);")
            self.doubleSpinBox_temperature2.setStyleSheet(
                "background-color: rgb(255,0,0);")
            self.doubleSpinBox_humidity2.setValue(0.00)
            self.doubleSpinBox_temperature2.setValue(0.00)
            self.humidity3 = "---"
            self.temperature3 = "---"

    def updateData4(self, humidity, temperature, failed=False):
        self.signals[3] = True
        if not failed:
            self.doubleSpinBox_humidity3.setStyleSheet(
                "background-color: rgb(255,255,255);")
            self.doubleSpinBox_temperature3.setStyleSheet(
                "background-color: rgb(255,255,255);")
            self.doubleSpinBox_humidity3.setValue(humidity)
            self.doubleSpinBox_temperature3.setValue(temperature)
            self.humidity4 = float("%.2f" % humidity)
            self.temperature4 = float("%.2f" % temperature)
        else:
            self.doubleSpinBox_humidity3.setStyleSheet(
                "background-color: rgb(255,0,0);")
            self.doubleSpinBox_temperature3.setStyleSheet(
                "background-color: rgb(255,0,0);")
            self.doubleSpinBox_humidity3.setValue(0.00)
            self.doubleSpinBox_temperature3.setValue(0.00)
            self.humidity4 = "---"
            self.temperature4 = "---"

    def updateData5(self, humidity, temperature, failed=False):
        self.signals[4] = True
        if not failed:
            self.doubleSpinBox_humidity4.setStyleSheet(
                "background-color: rgb(255,255,255);")
            self.doubleSpinBox_temperature4.setStyleSheet(
                "background-color: rgb(255,255,255);")
            self.doubleSpinBox_humidity4.setValue(humidity)
            self.doubleSpinBox_temperature4.setValue(temperature)
            self.humidity5 = float("%.2f" % humidity)
            self.temperature5 = float("%.2f" % temperature)
        else:
            self.doubleSpinBox_humidity4.setStyleSheet(
                "background-color: rgb(255,0,0);")
            self.doubleSpinBox_temperature4.setStyleSheet(
                "background-color: rgb(255,0,0);")
            self.doubleSpinBox_humidity4.setValue(0.00)
            self.doubleSpinBox_temperature4.setValue(0.00)
            self.humidity5 = "---"
            self.temperature5 = "---"
        if self.signals[0] and self.signals[1] and self.signals[
                2] and self.signals[3] and self.signals[4]:
            for i in range(len(self.signals)):
                self.signals[i] = False
            if self.humidity1 == "---":
                self.containers[0].append(0)
            else:
                self.containers[0].append(self.humidity1)
            if self.humidity2 == "---":
                self.containers[1].append(0)
            else:
                self.containers[1].append(self.humidity2)
            if self.humidity3 == "---":
                self.containers[2].append(0)
            else:
                self.containers[2].append(self.humidity3)
            if self.humidity4 == "---":
                self.containers[3].append(0)
            else:
                self.containers[3].append(self.humidity4)
            if self.humidity5 == "---":
                self.containers[4].append(0)
            else:
                self.containers[4].append(self.humidity5)
            elapsedTime = (time.time() - self.start) / 60.
            self.times.append(elapsedTime)
            self.xdata = np.array(self.times)
            self.ydata1 = np.array(self.containers[0])
            self.ydata2 = np.array(self.containers[1])
            self.ydata3 = np.array(self.containers[2])
            self.ydata4 = np.array(self.containers[3])
            self.ydata5 = np.array(self.containers[4])
            self.plotData()
            a = ""
            for i in range(len(self.connections)):
                if not self.connections[i]:
                    a += str(i + 1) + " "
            if a == "":
                self.label_current_state.setStyleSheet("color: rgb(50,150,0);")
                self.label_current_state.setText("Current state: ON")
            else:
                self.label_current_state.setStyleSheet("color: rgb(255,0,0);")
                self.label_current_state.setText(
                    "Connection to sensor N %s failed!!!\nTry properly make the connection"
                    % a[0:-1])
            for i in range(len(self.connections)):
                self.connections[i] = True
            row = []
            timeToWrite = '%s' % time.strftime('%Y%m%d-%H%M%S')
            row.append(timeToWrite)
            if self.temperature1 == "---":
                row.append(self.temperature1)
            else:
                row.append("%.2f" % self.temperature1)
            if self.humidity1 == "---":
                row.append(self.humidity1)
            else:
                row.append("%.2f" % self.humidity1)
            if self.temperature2 == "---":
                row.append(self.temperature2)
            else:
                row.append("%.2f" % self.temperature2)
            if self.humidity2 == "---":
                row.append(self.humidity2)
            else:
                row.append("%.2f" % self.humidity2)
            if self.temperature3 == "---":
                row.append(self.temperature3)
            else:
                row.append("%.2f" % self.temperature3)
            if self.humidity3 == "---":
                row.append(self.humidity3)
            else:
                row.append("%.2f" % self.humidity3)
            if self.temperature4 == "---":
                row.append(self.temperature4)
            else:
                row.append("%.2f" % self.temperature4)
            if self.humidity4 == "---":
                row.append(self.humidity4)
            else:
                row.append("%.2f" % self.humidity4)
            if self.temperature5 == "---":
                row.append(self.temperature5)
            else:
                row.append("%.2f" % self.temperature5)
            if self.humidity5 == "---":
                row.append(self.humidity5)
            else:
                row.append("%.2f" % self.humidity5)
            self.writer.writerow(row)
            if ((time.time() - self.TimeToWrite) / 60.) > 5:
                self.file.close()
                self.file = open(self.filePath + self.filename, 'a')
                self.writer = csv.writer(self.file,
                                         delimiter=';',
                                         quotechar="",
                                         quoting=csv.QUOTE_NONE,
                                         escapechar=';')
                self.TimeToWrite = time.time()

    def plotData(self):
        #print time.time()
        self.curve1.setData(self.xdata, self.ydata1)
        self.curve2.setData(self.xdata, self.ydata2)
        self.curve3.setData(self.xdata, self.ydata3)
        self.curve4.setData(self.xdata, self.ydata4)
        #self.curve5.setData(self.xdata, self.ydata5)
        self.qwtPlot.replot()

    def eventFilter(self, obj, event):
        if (event.type() == Qt.QEvent.MouseButtonRelease) and (
                obj == self.label_current_state):
            self.blackScreen = black_screen_Thread()
            self.blackScreen.start()
        if (event.type() == Qt.QEvent.MouseButtonRelease) and (
                obj == self.label_sensor or obj == self.label_humidity
                or obj == self.label_temperature):
            if not self.stateOfCurvers[0]:
                self.curve1.attach(self.qwtPlot)
                self.curve2.attach(self.qwtPlot)
                self.curve3.attach(self.qwtPlot)
                self.curve4.attach(self.qwtPlot)
                #self.curve5.attach(self.qwtPlot)
                self.qwtPlot.replot()
                self.stateOfCurvers = [True, True, True, True, True]
            else:
                self.curve1.attach(self.qwtPlot)
                self.curve2.detach()
                self.curve3.detach()
                self.curve4.detach()
                #self.curve5.detach()
                self.qwtPlot.replot()
                self.stateOfCurvers = [False, True, True, True, True]
        if (event.type() == Qt.QEvent.MouseButtonRelease) and (
                obj == self.label_sensor_1 or obj == self.label_humidity_1
                or obj == self.label_temperature_1):
            if not self.stateOfCurvers[1]:
                self.curve2.detach()
                self.curve1.attach(self.qwtPlot)
                self.curve2.attach(self.qwtPlot)
                self.curve3.attach(self.qwtPlot)
                self.curve4.attach(self.qwtPlot)
                #self.curve5.attach(self.qwtPlot)
                self.qwtPlot.replot()
                self.stateOfCurvers = [True, True, True, True, True]
            else:
                self.curve2.attach(self.qwtPlot)
                self.curve1.detach()
                self.curve3.detach()
                self.curve4.detach()
                #self.curve5.detach()
                self.qwtPlot.replot()
                self.stateOfCurvers = [True, False, True, True, True]
        if (event.type() == Qt.QEvent.MouseButtonRelease) and (
                obj == self.label_sensor_2 or obj == self.label_humidity_2
                or obj == self.label_temperature_2):
            if not self.stateOfCurvers[2]:
                self.curve3.detach()
                self.curve1.attach(self.qwtPlot)
                self.curve2.attach(self.qwtPlot)
                self.curve3.attach(self.qwtPlot)
                self.curve4.attach(self.qwtPlot)
                #self.curve5.attach(self.qwtPlot)
                self.qwtPlot.replot()
                self.stateOfCurvers = [True, True, True, True, True]
            else:
                self.curve3.attach(self.qwtPlot)
                self.curve1.detach()
                self.curve2.detach()
                self.curve4.detach()
                #self.curve5.detach()
                self.qwtPlot.replot()
                self.stateOfCurvers = [True, True, False, True, True]
        if (event.type() == Qt.QEvent.MouseButtonRelease) and (
                obj == self.label_sensor_3 or obj == self.label_humidity_3
                or obj == self.label_temperature_3):
            if not self.stateOfCurvers[3]:
                self.curve4.detach()
                self.curve1.attach(self.qwtPlot)
                self.curve2.attach(self.qwtPlot)
                self.curve3.attach(self.qwtPlot)
                self.curve4.attach(self.qwtPlot)
                #self.curve5.attach(self.qwtPlot)
                self.qwtPlot.replot()
                self.stateOfCurvers = [True, True, True, True, True]
            else:
                self.curve4.attach(self.qwtPlot)
                self.curve1.detach()
                self.curve2.detach()
                self.curve3.detach()
                #self.curve5.detach()
                self.qwtPlot.replot()
                self.stateOfCurvers = [True, True, True, False, True]
        if (event.type() == Qt.QEvent.MouseButtonRelease) and (
                obj == self.label_sensor_4 or obj == self.label_humidity_4
                or obj == self.label_temperature_4):
            if not self.stateOfCurvers[4]:
                #self.curve5.detach()
                self.curve1.attach(self.qwtPlot)
                self.curve2.attach(self.qwtPlot)
                self.curve3.attach(self.qwtPlot)
                self.curve4.attach(self.qwtPlot)
                #self.curve5.attach(self.qwtPlot)
                self.qwtPlot.replot()
                self.stateOfCurvers = [True, True, True, True, True]
            else:
                #self.curve5.attach(self.qwtPlot)
                self.curve1.detach()
                self.curve2.detach()
                self.curve3.detach()
                self.curve4.detach()
                self.qwtPlot.replot()
                self.stateOfCurvers = [True, True, True, True, False]
        return QtGui.QMainWindow.eventFilter(self, obj, event)

    def stopMeasure(self):
        if self.data_thread1 is not None:
            self.data_thread1.stop()
        if self.data_thread2 is not None:
            self.data_thread2.stop()
        if self.data_thread3 is not None:
            self.data_thread3.stop()
        if self.data_thread4 is not None:
            self.data_thread4.stop()
        if self.data_thread5 is not None:
            self.data_thread5.stop()
        self.file.close()

    def __del__(self):
        self.stopMeasure()
Esempio n. 19
0
class IVSweepDaqWidget(IVSweepsDaqUi.Ui_Form, QWidget):
    def __init__(self, parent=None):
        super(IVSweepDaqWidget, self).__init__(parent)
        self.setupUi(self)

        self.aoDeviceCombo.currentIndexChanged.connect(
            self.updateDaqChannelsAo)
        self.aiDeviceCombo.currentIndexChanged.connect(
            self.updateDaqChannelsAi)
        self.populateDaqCombos()
        self.restoreSettings()
        self.msmThread = None
        self.startPb.clicked.connect(self.startPbClicked)
        self.hkSub = HousekeepingSubscriber(self)
        self.hkSub.adrTemperatureReceived.connect(self.temperatureSb.setValue)
        self.hkSub.start()
        self.rawPlot.setAxisTitle(QwtPlot.yLeft, 'Vmeas')
        self.rawPlot.setAxisTitle(QwtPlot.xBottom, 'Vdrive')
        self.rawCurve = QwtPlotCurve('')
        self.rawCurve.attach(self.rawPlot)
        self.criticalCurve1 = QwtPlotCurve('+')
        self.criticalCurve1.setSymbol(
            Qwt.QwtSymbol(Qwt.QwtSymbol.Cross, Qt.QBrush(), Qt.QPen(Qt.Qt.red),
                          Qt.QSize(5, 5)))
        self.criticalCurve1.attach(self.criticalPlot)
        self.criticalCurve2 = QwtPlotCurve('-')
        self.criticalCurve2.setSymbol(
            Qwt.QwtSymbol(Qwt.QwtSymbol.Cross, Qt.QBrush(),
                          Qt.QPen(Qt.Qt.blue), Qt.QSize(5, 5)))
        self.criticalCurve2.attach(self.criticalPlot)
        self.criticalPlot.setAxisTitle(QwtPlot.yLeft, 'Vcrit')
        self.clearData()
        self.clearCriticalData()
        self.clearPb.clicked.connect(self.clearData)
        self.coilSweepCb.toggled.connect(self.toggleCoilSweep)
        self.clearCriticalPb.clicked.connect(self.clearCriticalData)
        self.samplesPerPointSb.valueChanged.connect(
            lambda value: self.discardSamplesSb.setMaximum(value - 1))
        self.coilEnableCb.toggled.connect(self.toggleCoil)
        self.coilVoltageSb.valueChanged.connect(self.updateCoilVoltage)
        self.toggleCoil(self.coilEnableCb.isChecked())
        self.coilDriverCombo.currentIndexChanged.connect(
            self.coilDriverChanged)
        self.Vcoil = np.nan

    def coilDriverChanged(self):
        text = self.coilDriverCombo.currentText()
        self.coilVisaLabel.setText('%s VISA' % text)
        self.coilVisaCombo.clear()
        if text == 'SR830':
            suffix = ' V'
            maxDrive = 10
            self.coilVisaCombo.addItem('GPIB0::12')
            self.auxOutChannelSb.setEnabled(True)

        elif text == 'Keithley 6430':
            suffix = ' mA'
            maxDrive = 50
            self.coilVisaCombo.addItem('GPIB0::24')
            self.auxOutChannelSb.setEnabled(False)

        controls = [
            self.coilVoltageSb, self.coilSweepMinSb, self.coilSweepMaxSb
        ]
        for control in controls:
            control.setSuffix(suffix)
            control.setMinimum(-maxDrive)
            control.setMaximum(+maxDrive)

    def toggleCoil(self, enabled):
        self.coilDriverCombo.setEnabled(not enabled)
        if enabled:
            driver = self.coilDriverCombo.currentText()
            if driver == 'SR830':
                self.sr830 = SR830(str(self.coilVisaCombo.currentText()))
                self.coilAo = VoltageSourceSR830(self.sr830,
                                                 self.auxOutChannelSb.value())
            elif driver == 'Keithley 6430':
                self.coilAo = CurrentSourceKeithley(str(
                    self.coilVisaCombo.currentText()),
                                                    currentRange=10E-3)
            self.Vcoil = self.coilAo.dcDrive()
            self.coilVoltageSb.setValue(self.Vcoil)
            self.auxOutChannelSb.setEnabled(False)
            self.coilVisaCombo.setEnabled(False)
        else:
            self.coilAo = None
            self.auxOutChannelSb.setEnabled(True)
            self.coilVisaCombo.setEnabled(True)

    def updateCoilVoltage(self, V):
        if self.coilAo is not None:
            self.coilAo.setDcDrive(V)
            self.Vcoil = self.coilAo.dcDrive()

    def updateDaqChannelsAi(self):
        dev = str(self.aiDeviceCombo.currentText())
        self.aiChannelCombo.clear()
        if len(dev):
            device = daq.Device(dev)
            aiChannels = device.findAiChannels()
            for channel in aiChannels:
                self.aiChannelCombo.addItem(channel)

    def updateDaqChannelsAo(self):
        dev = str(self.aoDeviceCombo.currentText())
        self.aoChannelCombo.clear()
        if len(dev):
            device = daq.Device(dev)
            aoChannels = device.findAoChannels()
            for channel in aoChannels:
                self.aoChannelCombo.addItem(channel)

    def populateDaqCombos(self):
        system = daq.System()
        devices = system.findDevices()
        for devName in devices:
            dev = daq.Device(devName)
            if len(dev.findAiChannels()):
                self.aiDeviceCombo.addItem(devName)
            if len(dev.findAoChannels()):
                self.aoDeviceCombo.addItem(devName)

    def clearData(self):
        self.Vdrive = []
        self.Vmeas = []
        self.rawCurve.setData(self.Vdrive, self.Vmeas)
        self.rawPlot.replot()

    def clearCriticalData(self):
        self.Vcrit1 = []
        self.Vcrit2 = []
        self.Tcrit1 = []
        self.Tcrit2 = []
        self.VcoilCrit1 = []
        self.VcoilCrit2 = []
        self.updateCriticalPlot()

    def updateRawData(self, t, Vsource, Vmeas, Vo, T):
        string = "%.3f\t%.6f\t%.6f\t%.6f\t%.6f\t%.3f\n" % (t, Vsource, Vmeas,
                                                           Vo, T, self.Vcoil)
        self.outputFile.write(string)
        self.Vdrive.append(Vsource)
        self.Vmeas.append(Vmeas - Vo)
        maxLength = 10000
        if len(self.Vdrive) > int(
                maxLength * 1.1):  # Automatically expire old data
            self.Vdrive = self.Vdrive[-maxLength:]
            self.Vmeas = self.Vmeas[-maxLength:]
        self.rawCurve.setData(self.Vdrive, self.Vmeas)
        self.rawPlot.replot()

    def toggleCoilSweep(self, checked):
        if checked:
            self.coilVoltages = np.linspace(self.coilSweepMinSb.value(),
                                            self.coilSweepMaxSb.value(),
                                            self.coilSweepStepsSb.value())
            self.coilVoltages = np.append(self.coilVoltages,
                                          self.coilVoltages[::-1])
            self.currentCoilStep = 0
            self.stepCoil()
        else:
            self.coilVoltages = []
            self.currentCoilStep = 0

    def stepCoil(self):
        if self.coilAo is not None:
            self.coilVoltageSb.setValue(
                self.coilVoltages[self.currentCoilStep])
        self.currentCoilStep += 1
        if self.currentCoilStep >= len(self.coilVoltages):  # Start over
            self.currentCoilStep = 0

    def collectSweep(self, T, Vc):
        if Vc >= 0:
            self.Tcrit1.append(T)
            self.Vcrit1.append(Vc)
            self.VcoilCrit1.append(self.Vcoil)
        else:
            self.Tcrit2.append(T)
            self.Vcrit2.append(-Vc)
            self.VcoilCrit2.append(self.Vcoil)
        self.updateCriticalPlot()

        if self.coilSweepCb.isChecked():  # Move on to next coil voltage
            if self.bipolarCb.isChecked():
                if not len(self.Tcrit2) == len(self.Tcrit1):
                    print "Still need to do negative"
                    return
            self.stepCoil()

    def updateCriticalPlot(self):
        xAxis = self.plotAxisCombo.currentText()
        if xAxis == 'T':
            x1 = self.Tcrit1
            x2 = self.Tcrit2
            self.criticalPlot.setAxisTitle(QwtPlot.xBottom, 'T')
        elif xAxis == 'Coil V':
            x1 = self.VcoilCrit1
            x2 = self.VcoilCrit2
            self.criticalPlot.setAxisTitle(QwtPlot.xBottom, 'Coil V')
        self.criticalCurve1.setData(x1, self.Vcrit1)
        self.criticalCurve2.setData(x2, self.Vcrit2)
        self.criticalPlot.replot()

    def startPbClicked(self):
        timeString = time.strftime('%Y%m%d-%H%M%S')
        fileName = self.sampleLineEdit.text() + '_%s_IV.dat' % timeString
        self.outputFile = open(fileName, 'a+')
        self.outputFile.write('#Program=IVSweepsDaq.py\n')
        self.outputFile.write('#Date=%s\n' % timeString)
        self.outputFile.write('#Sample=%s\n' % self.sampleLineEdit.text())
        self.outputFile.write('#Source=%s\n' % self.sourceCombo.currentText())
        self.outputFile.write('#Pre-amp gain=%.5g\n' %
                              self.preampGainSb.value())
        self.outputFile.write('#Drive impedance=%.6g\n' %
                              self.dcDriveImpedanceSb.value())
        self.outputFile.write('#Inter-sweep delay=%d\n' %
                              self.interSweepDelaySb.value())
        self.outputFile.write('#Samples per point=%d\n' %
                              self.samplesPerPointSb.value())
        self.outputFile.write('#Discard samples=%d\n' %
                              self.discardSamplesSb.value())
        self.outputFile.write('#Threshold=%.5g\n' %
                              self.thresholdVoltageSb.value())
        self.outputFile.write('#Bipolar=%d\n' %
                              int(self.bipolarCb.isChecked()))
        if self.coilAo is not None:
            self.outputFile.write('#Coil enabled=1\n')
            self.outputFile.write('#Coil driver=%s\n' % self.coilAo.name())
            self.outputFile.write('#Coil drive=%.3g\n' % self.coilAo.dcDrive())
        else:
            self.outputFile.write('#Coil enabled=0\n')
        if self.coilSweepCb.isChecked():
            self.outputFile.write('#Coil sweep=1\n')
            self.outputFile.write('#Coil min=%.3f\n' %
                                  self.coilSweepMinSb.value())
            self.outputFile.write('#Coil max=%.3f\n' %
                                  self.coilSweepMaxSb.value())
            self.outputFile.write('#Coil steps=%d\n' %
                                  self.coilSweepStepsSb.value())
        else:
            self.outputFile.write('#Coil sweep=0\n')

        self.ao = VoltageSourceDaq(str(self.aoDeviceCombo.currentText()),
                                   str(self.aoChannelCombo.currentText()))
        self.ai = VoltmeterDaq(str(self.aiDeviceCombo.currentText()),
                               str(self.aiChannelCombo.currentText()),
                               -10,
                               10,
                               samples=self.samplesPerPointSb.value(),
                               drop=self.discardSamplesSb.value())
        self.msmThread = IVSweepMeasurement(self.ao, self.ai, self)
        self.msmThread.setFileName(fileName)
        self.msmThread.setThreshold(self.thresholdVoltageSb.value())
        self.msmThread.readingAvailable.connect(self.updateRawData)
        self.msmThread.setMinimumVoltage(self.startVSb.value())
        self.msmThread.setMaximumVoltage(self.stopVSb.value())
        self.msmThread.setSteps(self.stepsSb.value())
        self.msmThread.setInterSweepDelay(self.interSweepDelaySb.value())
        self.msmThread.sweepComplete.connect(self.collectSweep)
        self.msmThread.enableBipolar(self.bipolarCb.isChecked())
        self.hkSub.adrTemperatureReceived.connect(
            self.msmThread.updateTemperature)
        self.msmThread.finished.connect(self.finished)
        self.stopPb.clicked.connect(self.msmThread.stop)
        self.thresholdVoltageSb.valueChanged.connect(
            self.msmThread.setThreshold)
        self.outputFile.write(
            '#' + '\t'.join(['time', 'Vdrive', 'Vmeas', 'Vo', 'T', 'Vcoil']) +
            '\n')
        self.enableWidgets(False)
        self.msmThread.start()
        print "Thread started"

    def finished(self):
        self.ai.clear()
        self.ao.clear()
        self.outputFile.close()
        self.enableWidgets(True)
        self.msmThread.deleteLater()


#        self.updateStatus('Completed')

    def enableWidgets(self, enable=True):
        self.sampleLineEdit.setEnabled(enable)
        self.startPb.setEnabled(enable)
        self.dcDriveImpedanceSb.setEnabled(enable)
        self.aiChannelCombo.setEnabled(enable)
        self.aiDeviceCombo.setEnabled(enable)
        self.aoChannelCombo.setEnabled(enable)
        self.aoDeviceCombo.setEnabled(enable)
        self.startVSb.setEnabled(enable)
        self.stopVSb.setEnabled(enable)
        self.stepsSb.setEnabled(enable)
        self.interSweepDelaySb.setEnabled(enable)
        self.bipolarCb.setEnabled(enable)

        self.stopPb.setEnabled(not enable)

    def closeEvent(self, e):
        if self.msmThread:
            self.msmThread.stop()
        if self.hkSub:
            self.hkSub.stop()
        self.saveSettings()
        super(IVSweepDaqWidget, self).closeEvent(e)

    def saveSettings(self):
        s = QSettings()
        s.setValue('sampleId', self.sampleLineEdit.text())
        s.setValue('dcDriveImpedance', self.dcDriveImpedanceSb.value())
        s.setValue('bipolar', self.bipolarCb.isChecked())
        s.setValue('startV', self.startVSb.value())
        s.setValue('stopV', self.stopVSb.value())
        s.setValue('steps', self.stepsSb.value())
        s.setValue('interSweepDelay', self.interSweepDelaySb.value())
        s.setValue('thresholdVoltage', self.thresholdVoltageSb.value())
        s.setValue('samplesPerPoint', self.samplesPerPointSb.value())
        s.setValue('discardSamples', self.discardSamplesSb.value())

        s.setValue('preampGain', self.preampGainSb.value())
        s.setValue('geometry', self.saveGeometry())
        s.setValue('AI_Device', self.aiDeviceCombo.currentText())
        s.setValue('AO_Device', self.aoDeviceCombo.currentText())
        s.setValue('AI_Channel', self.aiChannelCombo.currentText())
        s.setValue('AO_Channel', self.aoChannelCombo.currentText())
        s.setValue('SourceType', self.sourceCombo.currentText())

        s.setValue('coilVisa', self.coilVisaCombo.currentText())
        s.setValue('CoilAuxOut', self.auxOutChannelSb.value())
        s.setValue('CoilVoltage', self.coilVoltageSb.value())

    def restoreSettings(self):
        s = QSettings()
        self.sampleLineEdit.setText(s.value('sampleId', '', type=QString))
        self.dcDriveImpedanceSb.setValue(
            s.value('dcDriveImpedance', 10E3, type=float))
        self.bipolarCb.setChecked(s.value('bipolar', False, type=bool))
        self.startVSb.setValue(s.value('startV', 0, type=float))
        self.stopVSb.setValue(s.value('stopV', 3, type=float))
        self.stepsSb.setValue(s.value('steps', 10, type=int))
        self.preampGainSb.setValue(s.value('preampGain', 1., type=float))
        self.interSweepDelaySb.setValue(s.value('interSweepDelay', 0,
                                                type=int))
        self.thresholdVoltageSb.setValue(
            s.value('thresholdVoltage', 0.010, type=float))
        self.samplesPerPointSb.setValue(s.value('samplesPerPoint', 1,
                                                type=int))
        self.discardSamplesSb.setValue(s.value('discardSamples', 0, type=int))
        self.aiDeviceCombo.setCurrentIndex(
            self.aiDeviceCombo.findText(s.value('AI_Device', '', type=str)))
        self.aoDeviceCombo.setCurrentIndex(
            self.aoDeviceCombo.findText(s.value('AO_Device', '', type=str)))
        self.aiChannelCombo.setCurrentIndex(
            self.aiChannelCombo.findText(s.value('AI_Channel', '', type=str)))
        self.aoChannelCombo.setCurrentIndex(
            self.aoChannelCombo.findText(s.value('AO_Channel', '', type=str)))
        self.sourceCombo.setCurrentIndex(
            self.sourceCombo.findText(s.value('SourceType', '', type=str)))
        self.coilVisaCombo.setCurrentIndex(
            self.coilVisaCombo.findText(s.value('coilVisa', '', type=str)))
        self.auxOutChannelSb.setValue(s.value('CoilAuxOut', 1, type=int))
        self.coilVoltageSb.setValue(s.value('CoilVoltage', 0.0, type=float))
        geometry = s.value('geometry', QByteArray(), type=QByteArray)
        self.restoreGeometry(geometry)
Esempio n. 20
0
class RTTView(QtGui.QWidget):
    def __init__(self, parent=None):
        super(RTTView, self).__init__(parent)
        
        uic.loadUi('RTTView.ui', self)

        self.initSetting()

        self.initQwtPlot()

        self.rcvbuff = b''
        
        self.tmrRTT = QtCore.QTimer()
        self.tmrRTT.setInterval(10)
        self.tmrRTT.timeout.connect(self.on_tmrRTT_timeout)
        self.tmrRTT.start()
    
    def initSetting(self):
        if not os.path.exists('setting.ini'):
            open('setting.ini', 'w')
        
        self.conf = ConfigParser.ConfigParser()
        self.conf.read('setting.ini')
        
        if not self.conf.has_section('J-Link'):
            self.conf.add_section('J-Link')
            self.conf.set('J-Link', 'dllpath', '')
            self.conf.add_section('Memory')
            self.conf.set('Memory', 'StartAddr', '0x20000000')

        self.linDLL.setText(self.conf.get('J-Link', 'dllpath').decode('gbk'))

    def initQwtPlot(self):
        self.PlotData = [0]*1000
        
        self.qwtPlot = QwtPlot(self)
        self.qwtPlot.setVisible(False)
        self.vLayout.insertWidget(0, self.qwtPlot)
        
        self.PlotCurve = QwtPlotCurve()
        self.PlotCurve.attach(self.qwtPlot)
        self.PlotCurve.setData(range(1, len(self.PlotData)+1), self.PlotData)
    
    @QtCore.pyqtSlot()
    def on_btnOpen_clicked(self):
        if self.btnOpen.text() == u'打开连接':
            try:
                self.jlink = ctypes.cdll.LoadLibrary(self.linDLL.text())

                err_buf = (ctypes.c_char * 64)()
                self.jlink.JLINKARM_ExecCommand('Device = Cortex-M0', err_buf, 64)

                self.jlink.JLINKARM_TIF_Select(1)
                self.jlink.JLINKARM_SetSpeed(8000)
                
                buff = ctypes.create_string_buffer(1024)
                Addr = int(self.conf.get('Memory', 'StartAddr'), 16)
                for i in range(256):
                    self.jlink.JLINKARM_ReadMem(Addr + 1024*i, 1024, buff)
                    index = buff.raw.find('SEGGER RTT')
                    if index != -1:
                        self.RTTAddr = Addr + 1024*i + index

                        buff = ctypes.create_string_buffer(ctypes.sizeof(SEGGER_RTT_CB))
                        self.jlink.JLINKARM_ReadMem(self.RTTAddr, ctypes.sizeof(SEGGER_RTT_CB), buff)

                        rtt_cb = SEGGER_RTT_CB.from_buffer(buff)
                        self.aUpAddr = self.RTTAddr + 16 + 4 + 4
                        self.aDownAddr = self.aUpAddr + ctypes.sizeof(RingBuffer) * rtt_cb.MaxNumUpBuffers

                        self.txtMain.append('\n_SEGGER_RTT @ 0x%08X with %d aUp and %d aDown\n' %(self.RTTAddr, rtt_cb.MaxNumUpBuffers, rtt_cb.MaxNumDownBuffers))
                        break
                else:
                    raise Exception('Can not find _SEGGER_RTT')
            except Exception as e:
                self.txtMain.append('\n%s\n' %str(e))
            else:
                self.linDLL.setEnabled(False)
                self.btnDLL.setEnabled(False)
                self.btnOpen.setText(u'关闭连接')
        else:
            self.linDLL.setEnabled(True)
            self.btnDLL.setEnabled(True)
            self.btnOpen.setText(u'打开连接')
    
    def aUpRead(self):
        buf = ctypes.create_string_buffer(ctypes.sizeof(RingBuffer))
        self.jlink.JLINKARM_ReadMem(self.aUpAddr, ctypes.sizeof(RingBuffer), buf)

        aUp = RingBuffer.from_buffer(buf)
        
        if aUp.RdOff == aUp.WrOff:
            str = ctypes.create_string_buffer(0)

        elif aUp.RdOff < aUp.WrOff:
            cnt = aUp.WrOff - aUp.RdOff
            str = ctypes.create_string_buffer(cnt)
            self.jlink.JLINKARM_ReadMem(ctypes.cast(aUp.pBuffer, ctypes.c_void_p).value + aUp.RdOff, cnt, str)
            
            aUp.RdOff += cnt
            
            self.jlink.JLINKARM_WriteU32(self.aUpAddr + 4*4, aUp.RdOff)

        else:
            cnt = aUp.SizeOfBuffer - aUp.RdOff
            str = ctypes.create_string_buffer(cnt)
            self.jlink.JLINKARM_ReadMem(ctypes.cast(aUp.pBuffer, ctypes.c_void_p).value + aUp.RdOff, cnt, str)
            
            aUp.RdOff = 0  #这样下次再读就会进入执行上个条件
            
            self.jlink.JLINKARM_WriteU32(self.aUpAddr + 4*4, aUp.RdOff)
        
        return str.raw
    
    def on_tmrRTT_timeout(self):
        if self.btnOpen.text() == u'关闭连接':
            try:
                self.rcvbuff += self.aUpRead()

                if self.txtMain.isVisible():
                    if self.chkHEXShow.isChecked():
                        text = ''.join('%02X ' %ord(c) for c in self.rcvbuff)

                    else:
                        text = self.rcvbuff

                    if len(self.txtMain.toPlainText()) > 25000: self.txtMain.clear()
                    self.txtMain.moveCursor(QtGui.QTextCursor.End)
                    self.txtMain.insertPlainText(text)

                    self.rcvbuff = b''

                else:
                    if self.rcvbuff.rfind(',') == -1: return
                    
                    d = [int(x) for x in self.rcvbuff[0:self.rcvbuff.rfind(',')].split(',')]
                    for x in d:
                        self.PlotData.pop(0)
                        self.PlotData.append(x)
                    self.PlotCurve.setData(range(1, len(self.PlotData)+1), self.PlotData)
                    self.qwtPlot.replot()

                    self.rcvbuff = self.rcvbuff[self.rcvbuff.rfind(',')+1:]

            except Exception as e:
                self.rcvbuff = b''
                self.txtMain.append('\n%s\n' %str(e))
    
    def aDownWrite(self, bytes):
        buf = ctypes.create_string_buffer(ctypes.sizeof(RingBuffer))
        self.jlink.JLINKARM_ReadMem(self.aDownAddr, ctypes.sizeof(RingBuffer), buf)

        aDown = RingBuffer.from_buffer(buf)
        
        if aDown.WrOff >= aDown.RdOff:
            if aDown.RdOff != 0: cnt = min(aDown.SizeOfBuffer - aDown.WrOff, len(bytes))
            else:                cnt = min(aDown.SizeOfBuffer - 1 - aDown.WrOff, len(bytes))    # 写入操作不能使得 aDown.WrOff == aDown.RdOff,以区分满和空
            str = ctypes.create_string_buffer(bytes[:cnt])
            self.jlink.JLINKARM_WriteMem(ctypes.cast(aDown.pBuffer, ctypes.c_void_p).value + aDown.WrOff, cnt, str)
            
            aDown.WrOff += cnt
            if aDown.WrOff == aDown.SizeOfBuffer: aDown.WrOff = 0

            bytes = bytes[cnt:]

        if bytes and aDown.RdOff != 0 and aDown.RdOff != 1:     # != 0 确保 aDown.WrOff 折返回 0,!= 1 确保有空间可写入
            cnt = min(aDown.RdOff - 1 - aDown.WrOff, len(bytes))    # - 1 确保写入操作不导致WrOff与RdOff指向同一位置
            str = ctypes.create_string_buffer(bytes[:cnt])
            self.jlink.JLINKARM_WriteMem(ctypes.cast(aDown.pBuffer, ctypes.c_void_p).value + aDown.WrOff, cnt, str)

            aDown.WrOff += cnt

        self.jlink.JLINKARM_WriteU32(self.aDownAddr + 4*3, aDown.WrOff)

    @QtCore.pyqtSlot()
    def on_btnSend_clicked(self):
        if self.btnOpen.text() == u'关闭连接':
            text = self.txtSend.toPlainText()

            try:
                if self.chkHEXSend.isChecked():
                    bytes = ''.join([chr(int(x, 16)) for x in text.split()])

                else:
                    bytes = text

                self.aDownWrite(bytes)

            except Exception as e:
                self.txtMain.append('\n%s\n' %str(e))

    @QtCore.pyqtSlot()
    def on_btnDLL_clicked(self):
        path = QtGui.QFileDialog.getOpenFileName(caption=u'JLinkARM.dll路径', filter=u'动态链接库文件 (*.dll)', directory=self.linDLL.text())
        if path != '':
            self.linDLL.setText(path)

    @QtCore.pyqtSlot(int)
    def on_chkWavShow_stateChanged(self, state):
        self.qwtPlot.setVisible(state == QtCore.Qt.Checked)
        self.txtMain.setVisible(state == QtCore.Qt.Unchecked)

    @QtCore.pyqtSlot()
    def on_btnClear_clicked(self):
        self.txtMain.clear()
    
    def closeEvent(self, evt):
        self.conf.set('J-Link', 'dllpath', self.linDLL.text().encode('gbk'))
        self.conf.write(open('setting.ini', 'w'))
Esempio n. 21
0
class IVSweepWidget(IVSweepsUi.Ui_Form, QWidget):
    def __init__(self, parent=None):
        super(IVSweepWidget, self).__init__(parent)
        self.setupUi(self)
        self.restoreSettings()
        self.msmThread = None
        self.startPb.clicked.connect(self.startPbClicked)
        self.adrTemp = TemperatureSubscriber(self)
        self.adrTemp.adrTemperature.connect(self.temperatureSb.setValue)
        self.adrTemp.start()
        self.plot.setAxisTitle(QwtPlot.yLeft, 'Vmeas')
        self.plot.setAxisTitle(QwtPlot.xBottom, 'Vdrive')
        self.curve = QwtPlotCurve('')
        self.curve.attach(self.plot)
        self.clearData()
        self.clearPb.clicked.connect(self.clearData)

    def clearData(self):
        self.Vdrive = []
        self.Vmeas = []
        self.curve.setData(self.Vdrive, self.Vmeas)
        self.plot.replot()

    def updateData(self, t, Vsource, Vmeas, T):
        self.Vdrive.append(Vsource)
        self.Vmeas.append(Vmeas)
        self.curve.setData(self.Vdrive, self.Vmeas)
        self.plot.replot()

    def startPbClicked(self):
        self.dmm = Agilent34401A(str(self.dmmCombo.currentText()))
        if not '34401A' in self.dmm.visaId():
            print 'Agilent DMM not found.'
            return
        self.sr830 = SR830(str(self.sr830Combo.currentText()))
        if not 'SR830' in self.sr830.visaId():
            print 'SR830 not found.'
            return
        self.enableWidgets(False)
        fileName = self.sampleLineEdit.text() + '_IV.dat'
        self.msmThread = IVSweepMeasurement(self.sr830, self.dmm, self)
        self.msmThread.setFileName(fileName)
        self.msmThread.readingAvailable.connect(self.updateData)
        Vs1 = np.linspace(self.startVSb.value(), self.stopVSb.value(),
                          self.stepsSb.value())
        Vs2 = np.linspace(self.stopVSb.value(), self.stop2VSb.value(),
                          self.steps2Sb.value())
        Vs = np.append(Vs1, Vs2[1:])
        Vs = np.append(Vs, Vs[-2::-1])
        print Vs
        self.msmThread.setVoltages(Vs)
        self.adrTemp.adrTemperature.connect(self.msmThread.updateTemperature)
        self.msmThread.finished.connect(self.finished)
        self.stopPb.clicked.connect(self.msmThread.stop)
        self.msmThread.start()
        print "Thread started"

    def finished(self):
        self.enableWidgets(True)


#        self.updateStatus('Completed')

    def enableWidgets(self, enable=True):
        self.sampleLineEdit.setEnabled(enable)
        self.startPb.setEnabled(enable)
        self.stopPb.setEnabled(~enable)
        self.dcDriveImpedanceSb.setEnabled(enable)

    def closeEvent(self, e):
        if self.msmThread:
            self.msmThread.stop()
        if self.adrTemp:
            self.adrTemp.stop()
        self.saveSettings()
        super(IVSweepWidget, self).closeEvent(e)

    def saveSettings(self):
        s = QSettings()
        s.setValue('sampleId', self.sampleLineEdit.text())
        s.setValue('dcDriveImpedance', self.dcDriveImpedanceSb.value())
        s.setValue('startV', self.startVSb.value())
        s.setValue('stopV', self.stopVSb.value())
        s.setValue('step', self.stepsSb.value())
        s.setValue('stop2V', self.stop2VSb.value())
        s.setValue('step2', self.steps2Sb.value())

        s.setValue('preampGain', self.preampGainSb.value())
        s.setValue('geometry', self.saveGeometry())
        s.setValue('sr830Visa', self.sr830Combo.currentText())
        s.setValue('dmmVisa', self.dmmCombo.currentText())

    def restoreSettings(self):
        s = QSettings()
        self.sampleLineEdit.setText(s.value('sampleId', '', type=QString))
        self.dcDriveImpedanceSb.setValue(
            s.value('dcDriveImpedance', 10E3, type=float))
        self.startVSb.setValue(s.value('startV', 0, type=float))
        self.stopVSb.setValue(s.value('stopV', 3, type=float))
        self.stepsSb.setValue(s.value('steps', 10, type=int))
        self.stop2VSb.setValue(s.value('stop2V', 6, type=float))
        self.steps2Sb.setValue(s.value('steps2', 10, type=int))
        self.preampGainSb.setValue(s.value('preamGain', 1., type=float))
        i = self.sr830Combo.findText(s.value('sr830Visa', '', type=str))
        self.sr830Combo.setCurrentIndex(i)
        i = self.dmmCombo.findText(s.value('dmmVisa', '', type=str))
        self.dmmCombo.setCurrentIndex(i)

        geometry = s.value('geometry', QByteArray(), type=QByteArray)
        self.restoreGeometry(geometry)
Esempio n. 22
0
 def setData(self, x, y):
     return QwtPlotCurve.setData(self, list(map(float, x)), list(map(float, y)))
Esempio n. 23
0
    sys.exit(-1)

from PyQt4 import QtCore, QtGui
from PyQt4.Qwt5 import QwtPlot, QwtPlotCurve
import pickle

data = sys.argv[1]

app = QtGui.QApplication(sys.argv)

p = QwtPlot()

curve = QwtPlotCurve("Segon i tal")

fn = resDir + '/' + data
f = file(fn)
v = pickle.load(f)

y = v
x = range(len(y))

curve.setData(x, y)

curve.attach(p)

p.replot()

p.show()

sys.exit(app.exec_())
Esempio n. 24
0
class RTTView(QtGui.QWidget):
    def __init__(self, parent=None):
        super(RTTView, self).__init__(parent)

        uic.loadUi('RTTView.ui', self)

        self.initSetting()

        self.initQwtPlot()

        self.rcvbuff = b''

        self.daplink = None

        self.tmrRTT = QtCore.QTimer()
        self.tmrRTT.setInterval(10)
        self.tmrRTT.timeout.connect(self.on_tmrRTT_timeout)
        self.tmrRTT.start()

        self.tmrRTT_Cnt = 0

    def initSetting(self):
        if not os.path.exists('setting.ini'):
            open('setting.ini', 'w')

        self.conf = ConfigParser.ConfigParser()
        self.conf.read('setting.ini')

        if not self.conf.has_section('Memory'):
            self.conf.add_section('Memory')
            self.conf.set('Memory', 'StartAddr', '0x20000000')

    def initQwtPlot(self):
        self.PlotData = [0] * 1000

        self.qwtPlot = QwtPlot(self)
        self.qwtPlot.setVisible(False)
        self.vLayout.insertWidget(0, self.qwtPlot)

        self.PlotCurve = QwtPlotCurve()
        self.PlotCurve.attach(self.qwtPlot)
        self.PlotCurve.setData(range(1, len(self.PlotData) + 1), self.PlotData)

    @QtCore.pyqtSlot()
    def on_btnOpen_clicked(self):
        if self.btnOpen.text() == u'打开连接':
            try:
                self.daplink = self.daplinks[self.cmbDAP.currentText()]
                self.daplink.open()

                dp = coresight.dap.DebugPort(self.daplink, None)
                dp.init()
                dp.power_up_debug()

                ap = coresight.ap.AHB_AP(dp, 0)
                ap.init()

                self.dap = coresight.cortex_m.CortexM(None, ap)

                Addr = int(self.conf.get('Memory', 'StartAddr'), 16)
                for i in range(256):
                    buff = self.dap.read_memory_block8(Addr + 1024 * i, 1024)
                    buff = ''.join([chr(x) for x in buff])
                    index = buff.find('SEGGER RTT')
                    if index != -1:
                        self.RTTAddr = Addr + 1024 * i + index

                        buff = self.dap.read_memory_block8(
                            self.RTTAddr, ctypes.sizeof(SEGGER_RTT_CB))

                        rtt_cb = SEGGER_RTT_CB.from_buffer(bytearray(buff))
                        self.aUpAddr = self.RTTAddr + 16 + 4 + 4
                        self.aDownAddr = self.aUpAddr + ctypes.sizeof(
                            RingBuffer) * rtt_cb.MaxNumUpBuffers

                        self.txtMain.append(
                            '\n_SEGGER_RTT @ 0x%08X with %d aUp and %d aDown\n'
                            % (self.RTTAddr, rtt_cb.MaxNumUpBuffers,
                               rtt_cb.MaxNumDownBuffers))
                        break
                else:
                    raise Exception('Can not find _SEGGER_RTT')
            except Exception as e:
                self.txtMain.append('\n%s\n' % str(e))
            else:
                self.cmbDAP.setEnabled(False)
                self.btnOpen.setText(u'关闭连接')
        else:
            self.daplink.close()
            self.cmbDAP.setEnabled(True)
            self.btnOpen.setText(u'打开连接')

    def aUpRead(self):
        buf = self.dap.read_memory_block8(self.aUpAddr,
                                          ctypes.sizeof(RingBuffer))
        aUp = RingBuffer.from_buffer(bytearray(buf))

        if aUp.RdOff == aUp.WrOff:
            buf = []

        elif aUp.RdOff < aUp.WrOff:
            cnt = aUp.WrOff - aUp.RdOff
            buf = self.dap.read_memory_block8(
                ctypes.cast(aUp.pBuffer, ctypes.c_void_p).value + aUp.RdOff,
                cnt)

            aUp.RdOff += cnt

            self.dap.write32(self.aUpAddr + 4 * 4, aUp.RdOff)

        else:
            cnt = aUp.SizeOfBuffer - aUp.RdOff
            buf = self.dap.read_memory_block8(
                ctypes.cast(aUp.pBuffer, ctypes.c_void_p).value + aUp.RdOff,
                cnt)

            aUp.RdOff = 0  #这样下次再读就会进入执行上个条件

            self.dap.write32(self.aUpAddr + 4 * 4, aUp.RdOff)

        return ''.join([chr(x) for x in buf])

    def on_tmrRTT_timeout(self):
        if self.btnOpen.text() == u'关闭连接':
            try:
                self.rcvbuff += self.aUpRead()

                if self.txtMain.isVisible():
                    if self.chkHEXShow.isChecked():
                        text = ''.join('%02X ' % ord(c) for c in self.rcvbuff)
                    else:
                        text = self.rcvbuff

                    if len(self.txtMain.toPlainText()) > 25000:
                        self.txtMain.clear()
                    self.txtMain.moveCursor(QtGui.QTextCursor.End)
                    self.txtMain.insertPlainText(text)

                    self.rcvbuff = b''

                else:
                    if self.rcvbuff.rfind(',') == -1: return

                    d = [
                        int(x) for x in
                        self.rcvbuff[0:self.rcvbuff.rfind(',')].split(',')
                    ]
                    for x in d:
                        self.PlotData.pop(0)
                        self.PlotData.append(x)
                    self.PlotCurve.setData(range(1,
                                                 len(self.PlotData) + 1),
                                           self.PlotData)
                    self.qwtPlot.replot()

                    self.rcvbuff = self.rcvbuff[self.rcvbuff.rfind(',') + 1:]

            except Exception as e:
                self.rcvbuff = b''
                self.txtMain.append('\n%s\n' % str(e))

        else:
            self.tmrRTT_Cnt += 1
            if self.tmrRTT_Cnt % 20 == 0:
                self.detect_daplink()  # 自动检测 DAPLink 的热插拔

    def aDownWrite(self, bytes):
        buf = self.dap.read_memory_block8(self.aDownAddr,
                                          ctypes.sizeof(RingBuffer))
        aDown = RingBuffer.from_buffer(bytearray(buf))

        if aDown.WrOff >= aDown.RdOff:
            if aDown.RdOff != 0:
                cnt = min(aDown.SizeOfBuffer - aDown.WrOff, len(bytes))
            else:
                cnt = min(
                    aDown.SizeOfBuffer - 1 - aDown.WrOff,
                    len(bytes))  # 写入操作不能使得 aDown.WrOff == aDown.RdOff,以区分满和空
            self.dap.write_memory_block8(
                ctypes.cast(aDown.pBuffer, ctypes.c_void_p).value +
                aDown.WrOff, [ord(x) for x in bytes[:cnt]])

            aDown.WrOff += cnt
            if aDown.WrOff == aDown.SizeOfBuffer: aDown.WrOff = 0

            bytes = bytes[cnt:]

        if bytes and aDown.RdOff != 0 and aDown.RdOff != 1:  # != 0 确保 aDown.WrOff 折返回 0,!= 1 确保有空间可写入
            cnt = min(aDown.RdOff - 1 - aDown.WrOff,
                      len(bytes))  # - 1 确保写入操作不导致WrOff与RdOff指向同一位置
            self.dap.write_memory_block8(
                ctypes.cast(aDown.pBuffer, ctypes.c_void_p).value +
                aDown.WrOff, [ord(x) for x in bytes[:cnt]])

            aDown.WrOff += cnt

        self.dap.write32(self.aDownAddr + 4 * 3, aDown.WrOff)

    @QtCore.pyqtSlot()
    def on_btnSend_clicked(self):
        if self.btnOpen.text() == u'关闭连接':
            text = self.txtSend.toPlainText()

            try:
                if self.chkHEXSend.isChecked():
                    bytes = ''.join([chr(int(x, 16)) for x in text.split()])

                else:
                    bytes = text

                self.aDownWrite(bytes)

            except Exception as e:
                self.txtMain.append('\n%s\n' % str(e))

    def detect_daplink(self):
        daplinks = aggregator.DebugProbeAggregator.get_all_connected_probes()

        if len(daplinks) != self.cmbDAP.count():
            self.cmbDAP.clear()
            for daplink in daplinks:
                self.cmbDAP.addItem(daplink.product_name)

            self.daplinks = collections.OrderedDict([
                (daplink.product_name, daplink) for daplink in daplinks
            ])

            if self.daplink and self.daplink.product_name in self.daplinks:
                self.cmbDAP.setCurrentIndex(self.daplinks.keys().index(
                    self.daplink.product_name))
            else:  # daplink被拔掉
                self.btnOpen.setText(u'打开连接')

    @QtCore.pyqtSlot(int)
    def on_chkWavShow_stateChanged(self, state):
        self.qwtPlot.setVisible(state == QtCore.Qt.Checked)
        self.txtMain.setVisible(state == QtCore.Qt.Unchecked)

    @QtCore.pyqtSlot()
    def on_btnClear_clicked(self):
        self.txtMain.clear()

    def closeEvent(self, evt):
        self.conf.write(open('setting.ini', 'w'))