from time import sleep from PyQt5.QtCore import QThread from pyqtgraph import AxisItem, InfiniteLine, mkColor, mkPen, setConfigOption from interfaces import * from config import Config class Graph(QThread): def __init__(self, ui, interval, logger, parent=None): super(Graph, self).__init__(parent) 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 = [] self.config = Config().data self.device = self.config["device"] self._configurePlots() try: self.interface = INTERFACES[self.device["type"]](logger, self.device["location"], self.device["channels"]) except FileNotFoundError: self.running = False self.graph.setBackground((240, 240, 240)) def run(self): while True: if self.running: for i, channel in enumerate(self.device["channels"]): self.dataX[i].append(self.dataX[i][-1] + self.interval) self.dataY[i].append(self.interface.read(self.device["channels"][i])) self.plots[i].setData(self.dataX[i], self.dataY[i]) sleep(self.interval) def setInterval(self, interval): self.interval = interval/1000.0 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.logger("[GRAPH]", "Found new device {}".format(self.config["device"]["type"])) for channel in self.device["channels"]: plotItem = self.graph.addPlot() plot = plotItem.plot(pen=mkPen(mkColor(channel["color"]), width=3), name=channel["id"]) self.plots.append(plot) plotItem.setClipToView(True) plotItem.showGrid(True) plotItem.getViewBox().enableAutoRange() plotItem.getViewBox().setAutoPan(x=True) self.dataX.append([0]) self.dataY.append([0]) plotItem.setLabel("left", text=channel["name"], units=channel["unit"]) self.logger("[GRAPH]", "Added channel {}".format(channel["name"]))