Graph.py 2.17 KB
Newer Older
1
2
from time import sleep
from PyQt5.QtCore import QThread
3
4
from pyqtgraph import AxisItem, mkColor, mkPen
from interfaces import *
5
from config import Config
6
7

class Graph(QThread):
8
    def __init__(self, graph, 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
15
16
17
18
19
20
        self.dataX = []
        self.dataY = []
        self.graph = graph
        self.plots = []
        self.axis = []

        self.config = Config().data
        self._configurePlots()
21
        device = self.config["device"]
22
23
24
25
        try:
            self.interface = INTERFACES[device["type"]](logger, device["location"], device["channels"])
        except FileNotFoundError:
            self.running = False
26
27
28

    def run(self):
        while self.running:
29
            data = self.interface.readAll()
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
30
            #print(len(self.dataX[0]))
31
32
            for i, channel in enumerate(self.plots):
                self.dataX[i].append(self.dataX[i][-1] + self.interval)
33
                self.dataY[i].append(data[i])
34
                channel.setData(self.dataX[i], self.dataY[i])
35
36
37
38
39
40
            sleep(self.interval)

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

    def stop(self):
41
42
43
        self.running = False        

    def _configurePlots(self):
44
45
        first = True
        self.graph.getPlotItem().addLegend()
46
47
48
49
50
        self.logger("[GRAPH]", "Found new device {}".format(self.config["device"]["type"]))
        for channel in self.config["device"]["channels"]:
            self.logger("[GRAPH]", "Added channel {}".format(channel["name"]))
            self.dataX.append([0])
            self.dataY.append([0])
51
52
53
54
55
56
57
58
59
            plot = self.graph.plot(pen=mkPen(mkColor(channel["color"]), width=2), name=channel["name"])
            self.plots.append(plot)
            if first:
                self.graph.getPlotItem().setLabel("left", text=channel["name"], units=channel["unit"])
                first = False
            else:
                self.graph.getPlotItem().setLabel("right", text=channel["name"], units=channel["unit"])
        
        self.graph.getPlotItem().setLabel("bottom", text="Tempo", units="s")