forked from OrionAerospaceYT/SideKick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphing.py
194 lines (162 loc) · 8.28 KB
/
Graphing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from Ui.GraphingUi import Ui_MainWindow as graphing
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui
from PyQt5 import QtWidgets as qtw
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import __main__
class Graphing(qtw.QMainWindow):
def __init__(self, parent = None):
self.debug = False
self.com_port = 0
self.i = 0
self.blink = False
super(Graphing, self).__init__(parent=parent)
# Define the gui.
self.ui = graphing()
self.ui.setupUi(self)
for name in __main__.supported_devices:
self.ui.device.addItem(name)
cursor = self.ui.terminal.textCursor()
cursor.clearSelection()
self.ui.terminal.setTextCursor(cursor)
# Set pyqtPlot to the top widget.
self.gui_top_graph = pg.PlotWidget()
self.ui.gui_top_graph = qtw.QVBoxLayout()
self.ui.top_widget.setLayout(self.ui.gui_top_graph)
self.ui.gui_top_graph.addWidget(self.gui_top_graph)
self.top_legend = self.gui_top_graph.addLegend()
self.top_plots = []
# Sets the style for pyqtPlot top widget.
self.gui_top_graph.setBackground('#32323C')
self.top_legend.setLabelTextColor("#FFFFFF")
self.gui_top_graph.getAxis('left').setPen(pg.mkPen(color='#FFFFFF'))
self.gui_top_graph.getAxis('bottom').setPen(pg.mkPen(color='#FFFFFF'))
self.gui_top_graph.getAxis("left").setTextPen((255,255,255))
self.gui_top_graph.getAxis("bottom").setTextPen((255,255,255))
# Set pyqtPlot lib to the bottom widget.
self.gui_bottom_graph = pg.PlotWidget()
self.gui_bottom_graph.getAxis("left").setTextPen((255,255,255))
self.gui_bottom_graph.getAxis("bottom").setTextPen((255,255,255))
self.gui_bottom_graph.getAxis('left').setPen(pg.mkPen(color='#FFFFFF'))
self.gui_bottom_graph.getAxis('bottom').setPen(pg.mkPen(color='#FFFFFF'))
self.ui.gui_bottom_graph = qtw.QVBoxLayout()
self.ui.bottom_widget.setLayout(self.ui.gui_bottom_graph)
self.ui.gui_bottom_graph.addWidget(self.gui_bottom_graph)
self.gui_bottom_graph.setBackground('#32323C')
self.bottom_legend = self.gui_bottom_graph.addLegend()
self.bottom_legend.setLabelTextColor("#FFFFFF")
self.bottom_plots = []
# Connect buttons to their function in event handler.
#self.ui.projects.clicked.connect(__main__.eventHandler.launchProjects)
self.ui.render.setDisabled(True)
self.ui.help.clicked.connect(__main__.eventHandler.help)
self.ui.new_project.clicked.connect(__main__.eventHandler.new_project)
self.ui.upload.clicked.connect(__main__.eventHandler.upload)
self.ui.com_ports.activated[str].connect(__main__.eventHandler.update_com)
self.ui.disconnect.clicked.connect(__main__.eventHandler.disconnect_device)
self.ui.send.clicked.connect(__main__.eventHandler.send_serial_input_to_device)
self.ui.record.clicked.connect(__main__.eventHandler.record)
self.ui.lib_manager.clicked.connect(__main__.eventHandler.launchLibrary)
self.ui.update.clicked.connect(__main__.eventHandler.update_warning)
# Adds placeholder text
self.ui.project_name.setPlaceholderText("Enter projct name here.")
# Timings for repeating tasks such as getting data or updating graphs.
timer = qtc.QTimer(self)
timer.setInterval(15)
timer.timeout.connect(self.update)
timer.start()
# One function that calls all other update functions
def update(self):
self.update_data()
self.update_top_plot()
self.update_bottom_plot()
self.update_projects()
self.update_com()
self.blink_record()
if __main__.data.errors:
__main__.eventHandler.display_error()
__main__.data.errors = False
# Updates the com ports drop down to all avaliable com ports
def update_com(self):
for serial_port in __main__.data.serial_ports():
if serial_port not in [self.ui.com_ports.itemText(i) for i in range(self.ui.com_ports.count())]:
self.ui.com_ports.addItem(serial_port)
for serial_port in [self.ui.com_ports.itemText(i) for i in range(self.ui.com_ports.count())]:
if serial_port not in __main__.data.serial_ports() and serial_port != "Select COM":
target = self.ui.com_ports.findText(serial_port)
self.ui.com_ports.removeItem(target)
# Updates the current projects window so it shows all projects in the project folder.
def update_projects(self):
for project in __main__.fileManager.get_all_projects():
if project not in [self.ui.project_paths.itemText(i) for i in range(self.ui.project_paths.count())]:
self.ui.project_paths.addItem(project)
for project in [self.ui.project_paths.itemText(i) for i in range(self.ui.project_paths.count())]:
if project not in __main__.fileManager.get_all_projects():
target = self.ui.project_paths.findText(project)
self.ui.project_paths.removeItem(target)
# Calls the main function to get all serial data
def update_data(self):
__main__.data.get_data()
height_of_terminal = self.ui.centralwidget.height()
__main__.data.number_of_lines_displayed_on_terminal = int((height_of_terminal - 328) / 28)
if not self.debug:
self.ui.terminal.setHtml(__main__.data.html_terminal_text)
def add_top_plots(self):
for i in range(0, len(self.top_plots)):
self.gui_top_graph.removeItem(self.top_plots[i])
self.top_plots = []
__main__.data.x_data = []
__main__.data.top_plots_raw_data = []
__main__.data.top_plots_data = []
if len(__main__.data.labels[0]) > 0:
for i in range(0, __main__.data.num_of_top_plots):
pen = pg.mkPen("#FFFFFF", width=1)
self.top_plots.append(self.gui_top_graph.plot([0],[0],name=__main__.data.labels[0][i], pen=pen))
def add_bottom_plots(self):
for i in range(0, len(self.bottom_plots)):
self.gui_bottom_graph.removeItem(self.bottom_plots[i])
self.bottom_plots = []
__main__.data.x_data = []
__main__.data.bottom_plots_raw_data = []
__main__.data.bottom_plots_data = []
if len(__main__.data.labels[1]) > 0:
for i in range(0, __main__.data.num_of_bottom_plots):
pen = pg.mkPen("#FFFFFF", width=1)
self.bottom_plots.append(self.gui_bottom_graph.plot([0],[0],name=__main__.data.labels[1][i], pen=pen))
def update_top_plot(self):
if len(self.top_plots) != __main__.data.num_of_top_plots:
self.add_top_plots()
# Updates the Y axis data.
try:
for i in range(0, len(self.top_plots)):
pen = pg.mkPen(color=__main__.colour_order[i%len(__main__.colour_order)])
self.top_plots[i].setData(__main__.data.top_plots_data[i], pen=pen)
except:
pass
def update_bottom_plot(self):
if len(self.bottom_plots) != __main__.data.num_of_bottom_plots:
self.add_bottom_plots()
# Updates the Y axis data.
try:
for i in range(0, len(self.bottom_plots)):
pen = pg.mkPen(color=__main__.colour_order[i%len(__main__.colour_order)])
self.bottom_plots[i].setData(__main__.data.bottom_plots_data[i], pen=pen)
except:
pass
def blink_record(self):
self.i += 1
if self.i % 75 == 0 and __main__.data.save_data:
self.blink = not self.blink
if self.blink:
self.ui.record.setStyleSheet("""image: url(Ui/Record.png);
image-position: left;
padding-left: 10px;
width: 10px""")
else:
self.ui.record.setStyleSheet("""""")
elif self.i % 75 == 0 and not __main__.data.save_data:
self.ui.record.setStyleSheet("""image: url(Ui/Record.png);
image-position: left;
padding-left: 10px;
width: 10px""")