-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
310 lines (265 loc) · 10.4 KB
/
main.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import os
import sys
import ctypes
from PySide6 import QtCore, QtWidgets, QtGui, QtUiTools
from PySide6.QtCore import QTimer
from scripts.settings import Settings
from scripts.logger import log_values, log_action
from scripts.devicesLink import list_all_devices
from datetime import datetime
import scripts.realTimePlotting as realTimePlotting
import ctypes
import numpy as np
from picosdk.ps2000a import ps2000a as ps
from picosdk.functions import adc2mV, assert_pico_ok
import time
from scripts.deviceLibs.picoDevices import open_pico, get_value
import threading
import datetime
import time
import threading
import sys
import numpy as np
def loadUiWidget(uifilename, parent=None):
"""
Loads a UI file and returns the corresponding widget.
Parameters:
- uifilename (str): The path to the UI file.
- parent (QWidget): The parent widget (default: None).
Returns:
- QWidget: The loaded widget.
"""
loader = QtUiTools.QUiLoader()
uifile = QtCore.QFile(uifilename)
uifile.open(QtCore.QFile.ReadOnly)
ui = loader.load(uifile, parent)
uifile.close()
return ui
def load_settings(MainWindow):
"""
Loads the settings for the application.
Parameters:
- MainWindow: The main window of the application.
Returns:
None
"""
# Log path
label_logPath = MainWindow.findChild(
QtWidgets.QLabel, "label_logPath")
if not settings.does_setting_exist('logPath'):
settings.write_to_settings_file('logPath', './logs/')
label_logPath.setText(
os.getcwd()+settings.read_from_settings_file('logPath'))
# Log Frequency
spinBox_logFrequency = MainWindow.findChild(
QtWidgets.QSpinBox, "spinBox_logFrequency")
if not settings.does_setting_exist('logFrequency'):
settings.write_to_settings_file(
'logFrequency', spinBox_logFrequency.value())
else:
spinBox_logFrequency.setValue(
int(settings.read_from_settings_file('logFrequency')))
# File Size Limit
spinBox_fileSizeLimit = MainWindow.findChild(
QtWidgets.QSpinBox, "spinBox_fileSizeLimit")
if not settings.does_setting_exist('fileSizeLimit'):
settings.write_to_settings_file(
'fileSizeLimit', spinBox_fileSizeLimit.value())
else:
spinBox_fileSizeLimit.setValue(
int(settings.read_from_settings_file('fileSizeLimit')))
# Log On/Off
pushButton_LogOnOff = MainWindow.findChild(
QtWidgets.QPushButton, "pushButton_LogOnOff")
if not settings.does_setting_exist('logOnOff'):
settings.write_to_settings_file(
'logOnOff', pushButton_LogOnOff.isChecked())
else:
pushButton_LogOnOff.setChecked(
settings.read_from_settings_file('logOnOff') == 'True')
pushButton_LogOnOff.setText(settings.read_from_settings_file('logOnOff'))
def bind_settings(MainWindow):
"""
Binds the settings of the MainWindow to the corresponding UI elements.
Args:
MainWindow: The main window object.
Returns:
None
"""
# Log Frequency
spinBox_logFrequency = MainWindow.findChild(
QtWidgets.QSpinBox, "spinBox_logFrequency")
spinBox_logFrequency.valueChanged.connect(
lambda value: settings.write_to_settings_file('logFrequency', value))
# File Size Limit
spinBox_fileSizeLimit = MainWindow.findChild(
QtWidgets.QSpinBox, "spinBox_fileSizeLimit")
spinBox_fileSizeLimit.valueChanged.connect(
lambda value: settings.write_to_settings_file('fileSizeLimit', value))
# Log On/Off
pushButton_LogOnOff = MainWindow.findChild(
QtWidgets.QPushButton, "pushButton_LogOnOff")
pushButton_LogOnOff.clicked.connect(
lambda: settings.write_to_settings_file('logOnOff', pushButton_LogOnOff.isChecked()))
pushButton_LogOnOff.clicked.connect(
lambda: pushButton_LogOnOff.setText(settings.read_from_settings_file('logOnOff')))
def bind_values_to_log(MainWindow):
timer = QTimer(MainWindow)
spinBox_logFrequency = MainWindow.findChild(
QtWidgets.QSpinBox, "spinBox_logFrequency")
logFrequencyInMS = spinBox_logFrequency.value() * 1000 # Convert to milliseconds
timer.setInterval(logFrequencyInMS)
spinBox_fileSizeLimit = MainWindow.findChild(
QtWidgets.QSpinBox, "spinBox_fileSizeLimit")
fileSizeLimit = spinBox_fileSizeLimit.value()
def log_values_periodically():
spinBox_RayonCanneC1 = MainWindow.findChild(
QtWidgets.QSpinBox, "spinBox_RayonCanneC1")
spinBox_RayonCanneC2 = MainWindow.findChild(
QtWidgets.QSpinBox, "spinBox_RayonCanneC2")
values_to_log = [
datetime.datetime.now().strftime("%H:%M:%S"),
spinBox_RayonCanneC1.value(),
spinBox_RayonCanneC2.value()
]
log_values(values_to_log, fileSizeLimit)
timer.timeout.connect(log_values_periodically)
timer.start()
def bind_actions_to_log(MainWindow):
"""
Binds actions to log.
Parameters:
- MainWindow: The main window object.
Returns:
None
"""
log_action("Application started")
pushButton_LogOnOff = MainWindow.findChild(
QtWidgets.QPushButton, "pushButton_LogOnOff")
pushButton_LogOnOff.clicked.connect(
lambda: log_action("Logging is turned on" if pushButton_LogOnOff.isChecked() else "Logging is turned off"))
pushButton_Diaphragme = MainWindow.findChild(
QtWidgets.QPushButton, "pushButton_Diaphragme")
pushButton_Diaphragme.clicked.connect(
lambda: log_action("Diaphragme is turned on" if pushButton_Diaphragme.isChecked() else "Diaphragme is turned off"))
pushButton_Diaphragme.clicked.connect(
lambda: pushButton_Diaphragme.setText("On" if pushButton_Diaphragme.isChecked() else "Off"))
pushButton_Refresh = MainWindow.findChild(
QtWidgets.QPushButton, "pushButton_Refresh")
pushButton_Refresh.clicked.connect(
lambda: log_action("Ports are refreshed"))
radioButton_C1S1 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C1S1")
radioButton_C1S1.clicked.connect(
lambda: log_action("Cible 1 Stripper 1 is selected"))
radioButton_C1S2 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C1S2")
radioButton_C1S2.clicked.connect(
lambda: log_action("Cible 1 Stripper 2 is selected"))
radioButton_C1S3 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C1S3")
radioButton_C1S3.clicked.connect(
lambda: log_action("Cible 1 Stripper 3 is selected"))
radioButton_C2S1 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C2S1")
radioButton_C2S1.clicked.connect(
lambda: log_action("Cible 2 Stripper 1 is selected"))
radioButton_C2S2 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C2S2")
radioButton_C2S2.clicked.connect(
lambda: log_action("Cible 2 Stripper 2 is selected"))
radioButton_C2S3 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C2S3")
radioButton_C2S3.clicked.connect(
lambda: log_action("Cible 2 Stripper 3 is selected"))
def refresh_ports():
"""
Refreshes the list of available ports in the GUI.
This function clears the existing list of ports in the GUI, retrieves the updated list of available ports,
and populates the GUI list with the new ports. If no ports are detected, a message indicating that no ports
were found is displayed in the GUI.
Args:
None
Returns:
None
"""
listWidget_PortList = main_window.findChild(
QtWidgets.QListView, "listWidget_PortList")
listWidget_PortList.clear()
item = QtWidgets.QListWidgetItem("Refreshing...")
listWidget_PortList.addItem(item)
ports = list_all_devices()
if not ports:
listWidget_PortList.clear()
item = QtWidgets.QListWidgetItem("No device detected")
listWidget_PortList.addItem(item)
else:
listWidget_PortList.clear()
for port in ports:
item = QtWidgets.QListWidgetItem(port)
listWidget_PortList.addItem(item)
def update_data(plot):
"""
Updates the data for the given plot.
Args:
plot: The plot object to update.
Returns:
None
"""
settings = Settings()
while plot.running:
plot.add_datas([get_value('PS2000A_CHANNEL_A')])
freq = float(settings.read_from_settings_file('logFrequency'))
time.sleep(freq)
def plotting(parent, title, num_of_lines, legend_labels):
"""
Create a real-time plotting widget and start a separate thread to update the data.
Args:
parent: The parent widget.
title: The title of the plot.
num_of_lines: The number of lines to be plotted.
legend_labels: The labels for the legend.
Returns:
None
"""
plot = realTimePlotting.RealTimePlot(parent, title, num_of_lines, legend_labels)
data_thread = threading.Thread(target=update_data, args=(plot,))
data_thread.start()
layout = QtWidgets.QVBoxLayout()
parent.setLayout(layout)
layout.addWidget(plot)
if __name__ == "__main__":
if os.name == 'nt':
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("aima.minicyc")
app = QtWidgets.QApplication([])
main_window = loadUiWidget("GUI.ui")
main_window.setWindowTitle("MiniCyc")
app_icon = QtGui.QIcon("assets/icon.ico")
app.setWindowIcon(app_icon)
main_window.showFullScreen()
main_window.showMaximized()
tabWidget = main_window.findChild(QtWidgets.QTabWidget, "tabWidget")
tabWidget.setCurrentIndex(tabWidget.count() - 1)
# Settings
settings = Settings()
load_settings(main_window)
bind_settings(main_window)
# Logger
bind_actions_to_log(main_window)
bind_values_to_log(main_window)
# Devices
refresh_ports()
pushButton_Refresh = main_window.findChild(
QtWidgets.QPushButton, "pushButton_Refresh")
pushButton_Refresh.clicked.connect(refresh_ports)
# realTimePlot
try:
open_pico()
listWidget_vacuum = main_window.findChild(QtWidgets.QWidget, "listWidget_vacuum")
plotting(listWidget_vacuum, "Pompe à vide", 1, ["Channel A"])
listWidget_magnet = main_window.findChild(QtWidgets.QWidget, "listWidget_magnet")
plotting(listWidget_magnet, "Aimant", 1, ["Channel B"])
except Exception as e:
print("No pico device detected : "+str(e))
sys.exit(app.exec())
# © AIMA DEVELOPPEMENT 2024