Graph.py 2.38 KB
Newer Older
1 2
from time import sleep
from PyQt5.QtCore import QThread
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
3
from pyqtgraph import AxisItem, InfiniteLine, mkColor, mkPen, setConfigOption
4
from interfaces import *
5
from config import Config
6 7

class Graph(QThread):
8
    def __init__(self, ui, interval, logger, parent=None):
9 10
        super(Graph, self).__init__(parent)
        self.running = True
11
        self.logger = logger
12
        self.interval = interval/1000.0
13 14
        self.dataX = []
        self.dataY = []
15 16
        self.ui = ui
        self.graph = ui.mainGraph
17 18 19
        self.plots = []
        self.axis = []
        self.config = Config().data
20
        self.device = self.config["device"]
21
        self._configurePlots()
22
        try:
23
            self.interface = INTERFACES[self.device["type"]](logger, self.device["location"], self.device["channels"])
24 25
        except FileNotFoundError:
            self.running = False
26

27
        self.graph.setBackground((240, 240, 240))
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
28

29
    def run(self):
30 31 32 33 34 35 36
        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)
37 38 39 40

    def setInterval(self, interval):
        self.interval = interval/1000.0

41 42 43
    def restart(self):
        self.running = True

44
    def stop(self):
45 46 47 48 49
        self.running = False

    def addMarker(self, label):
        marker = InfiniteLine(angle=90, pos=self.dataX[0][-1], label=label)
        self.graph.getPlotItem().addItem(marker)
50 51 52

    def _configurePlots(self):
        self.logger("[GRAPH]", "Found new device {}".format(self.config["device"]["type"]))
53 54 55 56 57 58 59 60 61 62

        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)
63 64
            self.dataX.append([0])
            self.dataY.append([0])
65 66
            plotItem.setLabel("left", text=channel["name"], units=channel["unit"])
            self.logger("[GRAPH]", "Added channel {}".format(channel["name"]))