from datetime import datetime from pathlib import Path from PyQt5.QtCore import QTimer from pyqtgraph import AxisItem, InfiniteLine, mkColor, mkPen, setConfigOption from interfaces import * from config import Config from util.Logger import Logger import pyqtgraph as pg class Graph(): def __init__(self, ui, interval, parent=None): self.running = True self.logger = Logger() self.interval = interval#/1000.0 self.dataX = [] self.dataY = [] self.ui = ui self.graph = ui.mainGraph self.plots = [] self.axis = [] #try: self.config = Config().data #except FileNotFoundError: # logger("[FATAL]", "Arquivo de configuração não encontrado!") self.device = self.config["device"] self._configurePlots() self.lastupdated = 0 pg.setConfigOptions(useOpenGL=True) #try: self.interface = INTERFACES[self.device["type"]](self.device["location"], self.device["channels"]) #except: # self.running = False # logger("[FATAL]", "Erro ao abrir a interface!") print(ui.samplingBox) ui.min_amostra.valueChanged.connect(self.updateScale) ui.min_pressao.valueChanged.connect(self.updateScale) ui.max_amostra.valueChanged.connect(self.updateScale) ui.max_pressao.valueChanged.connect(self.updateScale) self.graph.setBackground((240, 240, 240)) def updateGraphs(self): for i, channel in enumerate(self.device["channels"]): self.dataX[i].append(self.dataX[i][-1] + self.interval/1000.0) self.dataY[i].append(self.interface.read(self.device["channels"][i])) self.plots[i].setData(self.dataX[i], self.dataY[i]) self.combinedPlot.setData(self.dataY[1], self.dataY[0]) dados = "{}, {}, {}".format(self.dataY[0][-1], self.dataY[1][-1], self.dataX[0][-1]) self.logger.writeLog("acquisicao", dados, isCSV=True) self.ui.forceLabel.setText("{:.0f} bar".format(self.dataY[1][-1])) self.ui.calibratorLabel.setText("{:.2f} mV".format(self.dataY[0][-1] * 1000)) pg.QtGui.QApplication.processEvents() def updateScale(self): minX = self.ui.min_pressao.value() minY = self.ui.min_amostra.value() / 1000.0 maxX = self.ui.max_pressao.value() maxY = self.ui.max_amostra.value() / 1000.0 self.combinedPlot.getViewBox().setRange(xRange=(minX, maxX), yRange=(minY, maxY)) def setInterval(self, interval): self.interval = interval def restart(self): self.running = True def stop(self): self.running = False def addMarker(self, label): marker = InfiniteLine(angle=90, pos=self.dataX[0][-1], label=label) self.graph.getPlotItem().addItem(marker) def _configurePlots(self): self.debugLayout = self.graph.addLayout() header = "" for i, channel in enumerate(self.device["channels"]): plotItem = self.debugLayout.addPlot(row=i, col=1) plotItem.setMenuEnabled(False) # Previne acesso ao menu do pyqtgraph pelos usuários plot = plotItem.plot(pen=mkPen(mkColor(channel["color"]), width=2), name=channel["id"]) self.plots.append(plot) plotItem.setClipToView(True) plotItem.showGrid(True) self.dataX.append([0]) self.dataY.append([0]) plotItem.setLabel("left", text=channel["name"], units=channel["unit"]) header += "{},".format(channel["name"]) self.ui.min_amostra.setSuffix(self.device["channels"][0]["unit"]) self.ui.min_pressao.setSuffix(self.device["channels"][1]["unit"]) self.ui.max_amostra.setSuffix(self.device["channels"][0]["unit"]) self.ui.max_pressao.setSuffix(self.device["channels"][1]["unit"]) header += "timestamp" self.logger.writeLog("acquisicao", header, isCSV=True) # Faz com que ambos eixos X se movam juntos sempre # TODO: escrever de forma mais limpa self.plots[0].getViewBox().linkView(self.plots[0].getViewBox().XAxis, self.plots[1].getViewBox()) self.combinedPlot = self.debugLayout.addPlot(row=0, col=2, rowspan=2) self.combinedPlot.setClipToView(True) self.combinedPlot.showGrid(True) self.combinedPlot.getViewBox().enableAutoRange(plotItem.getViewBox().YAxis) self.combinedPlot.setLabel("left", text=self.device["channels"][0]["name"], units=self.device["channels"][0]["unit"]) self.combinedPlot.setLabel("bottom", text=self.device["channels"][1]["name"], units=self.device["channels"][1]["unit"]) self.combinedPlot.getViewBox().setMouseEnabled(False, False) # Previne uso do mouse para controle do zoom self.combinedPlot.setMenuEnabled(False) # Previne acesso ao menu do pyqtgraph pelos usuários qGraphicsGridLayout = self.debugLayout.layout qGraphicsGridLayout.setColumnStretchFactor(1, 1) qGraphicsGridLayout.setColumnStretchFactor(2, 3) self.combinedPlot = self.combinedPlot.plot(pen=mkPen(mkColor("#FF0000"), width=2))