-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo_A_GUI_full.py
546 lines (455 loc) · 18.4 KB
/
demo_A_GUI_full.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Demonstration of multithreaded real-time plotting and logging of live Arduino
data using PyQt/PySide and PyQtGraph.
"""
__author__ = "Dennis van Gils"
__authoremail__ = "vangils.dennis@gmail.com"
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
__date__ = "11-06-2024"
__version__ = "9.0"
# pylint: disable=missing-function-docstring, unnecessary-lambda
import os
import sys
import time
import datetime
import qtpy
from qtpy import QtCore, QtGui, QtWidgets as QtWid
from qtpy.QtCore import Slot # type: ignore
import psutil
import pyqtgraph as pg
from dvg_debug_functions import tprint
from dvg_pyqtgraph_threadsafe import HistoryChartCurve, PlotManager
from dvg_pyqt_filelogger import FileLogger
import dvg_pyqt_controls as controls
from WaveGeneratorArduino import WaveGeneratorArduino, FakeWaveGeneratorArduino
from WaveGeneratorArduino_qdev import WaveGeneratorArduino_qdev
# Constants
DAQ_INTERVAL_MS = 10
"""[ms] Update interval for the data acquisition (DAQ)"""
CHART_INTERVAL_MS = 20
"""[ms] Update interval for the chart"""
CHART_HISTORY_TIME = 10
"""[s] History length of the chart"""
# Global flags
TRY_USING_OPENGL = True
USE_LARGER_TEXT = False
"""For demonstration on a beamer"""
USE_PC_TIME = True
"""Use Arduino time or PC time?"""
SIMULATE_ARDUINO = False
"""Simulate an Arduino in software?"""
if sys.argv[-1] == "simulate":
SIMULATE_ARDUINO = True
DEBUG = False
"""Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
"""
print(
f"{qtpy.API_NAME:9s} "
f"{qtpy.QT_VERSION}" # pyright: ignore[reportPrivateImportUsage]
)
print(f"PyQtGraph {pg.__version__}")
if TRY_USING_OPENGL:
try:
import OpenGL.GL as gl # pylint: disable=unused-import
from OpenGL.version import __version__ as gl_version
except Exception: # pylint: disable=broad-except
print("PyOpenGL not found")
print("To install: `conda install pyopengl` or `pip install pyopengl`")
else:
print(f"PyOpenGL {gl_version}")
pg.setConfigOptions(useOpenGL=True)
pg.setConfigOptions(antialias=True)
pg.setConfigOptions(enableExperimental=True)
else:
print("PyOpenGL disabled")
# Global pyqtgraph configuration
# pg.setConfigOptions(leftButtonPan=False)
pg.setConfigOption("foreground", "#EEE")
# ------------------------------------------------------------------------------
# MainWindow
# ------------------------------------------------------------------------------
class MainWindow(QtWid.QWidget):
def __init__(
self,
qdev: WaveGeneratorArduino_qdev,
qlog: FileLogger,
parent=None,
**kwargs,
):
super().__init__(parent, **kwargs)
self.qdev = qdev
self.qdev.signal_DAQ_updated.connect(self.update_GUI)
self.qlog = qlog
# Run/pause mechanism on updating the GUI and graphs
self.allow_GUI_update_of_readings = True
self.setWindowTitle("Arduino & PyQt multithread demo")
if USE_LARGER_TEXT:
self.setGeometry(40, 60, 1024, 768)
else:
self.setGeometry(40, 60, 960, 660)
self.setStyleSheet(controls.SS_TEXTBOX_READ_ONLY + controls.SS_GROUP)
# -------------------------
# Chart refresh timer
# -------------------------
self.timer_chart = QtCore.QTimer()
self.timer_chart.setTimerType(QtCore.Qt.TimerType.PreciseTimer)
self.timer_chart.timeout.connect(self.update_chart)
# -------------------------
# Top frame
# -------------------------
# Left box
self.qlbl_update_counter = QtWid.QLabel("0")
self.qlbl_DAQ_rate = QtWid.QLabel("DAQ: nan Hz")
self.qlbl_DAQ_rate.setStyleSheet("QLabel {min-width: 7em}")
self.qlbl_recording_time = QtWid.QLabel()
vbox_left = QtWid.QVBoxLayout()
vbox_left.addWidget(self.qlbl_update_counter, stretch=0)
vbox_left.addStretch(1)
vbox_left.addWidget(self.qlbl_recording_time, stretch=0)
vbox_left.addWidget(self.qlbl_DAQ_rate, stretch=0)
# Middle box
self.qlbl_title = QtWid.QLabel("Arduino & PyQt multithread demo")
self.qlbl_title.setFont(
QtGui.QFont(
"Palatino",
20 if USE_LARGER_TEXT else 14,
weight=QtGui.QFont.Weight.Bold,
)
)
self.qlbl_title.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.qlbl_cur_date_time = QtWid.QLabel("00-00-0000 00:00:00")
self.qlbl_cur_date_time.setAlignment(
QtCore.Qt.AlignmentFlag.AlignCenter
)
self.qpbt_record = controls.create_Toggle_button(
"Click to start recording to file", minimumHeight=40
)
self.qpbt_record.setMinimumWidth(400)
self.qpbt_record.clicked.connect(lambda state: qlog.record(state))
vbox_middle = QtWid.QVBoxLayout()
vbox_middle.addWidget(self.qlbl_title)
vbox_middle.addWidget(self.qlbl_cur_date_time)
vbox_middle.addWidget(self.qpbt_record)
# Right box
p = {
"alignment": QtCore.Qt.AlignmentFlag.AlignRight
| QtCore.Qt.AlignmentFlag.AlignVCenter,
"parent": self,
}
self.qpbt_exit = QtWid.QPushButton("Exit")
self.qpbt_exit.clicked.connect(self.close)
self.qpbt_exit.setMinimumHeight(30)
self.qlbl_GitHub = QtWid.QLabel(
f'<a href="{__url__}">GitHub source</a>', **p
)
self.qlbl_GitHub.setTextFormat(QtCore.Qt.TextFormat.RichText)
self.qlbl_GitHub.setTextInteractionFlags(
QtCore.Qt.TextInteractionFlag.TextBrowserInteraction
)
self.qlbl_GitHub.setOpenExternalLinks(True)
vbox_right = QtWid.QVBoxLayout()
vbox_right.setSpacing(4)
vbox_right.addWidget(self.qpbt_exit, stretch=0)
vbox_right.addStretch(1)
vbox_right.addWidget(QtWid.QLabel(__author__, **p))
vbox_right.addWidget(self.qlbl_GitHub)
vbox_right.addWidget(QtWid.QLabel(f"v{__version__}", **p))
# Round up top frame
hbox_top = QtWid.QHBoxLayout()
hbox_top.addLayout(vbox_left, stretch=0)
hbox_top.addStretch(1)
hbox_top.addLayout(vbox_middle, stretch=0)
hbox_top.addStretch(1)
hbox_top.addLayout(vbox_right, stretch=0)
# -------------------------
# Bottom frame
# -------------------------
# GraphicsLayoutWidget
self.gw = pg.GraphicsLayoutWidget()
self.plot = self.gw.addPlot()
p = {
"color": "#EEE",
"font-size": "20pt" if USE_LARGER_TEXT else "10pt",
}
self.plot.setClipToView(True)
self.plot.showGrid(x=1, y=1)
self.plot.setLabel("bottom", text="history (sec)", **p)
self.plot.setLabel("left", text="amplitude", **p)
self.plot.setRange(
xRange=[-1.04 * CHART_HISTORY_TIME, CHART_HISTORY_TIME * 0.04],
yRange=[-1.1, 1.1],
disableAutoRange=True,
)
if USE_LARGER_TEXT:
font = QtGui.QFont()
font.setPixelSize(26)
self.plot.getAxis("bottom").setTickFont(font)
self.plot.getAxis("bottom").setStyle(tickTextOffset=20)
self.plot.getAxis("bottom").setHeight(90)
self.plot.getAxis("left").setTickFont(font)
self.plot.getAxis("left").setStyle(tickTextOffset=20)
self.plot.getAxis("left").setWidth(120)
self.history_chart_curve = HistoryChartCurve(
capacity=round(CHART_HISTORY_TIME * 1e3 / DAQ_INTERVAL_MS),
linked_curve=self.plot.plot(
pen=pg.mkPen(color=[255, 255, 0], width=3)
),
)
# 'Readings'
p = {"readOnly": True, "maximumWidth": 112 if USE_LARGER_TEXT else 63}
self.qlin_reading_t = QtWid.QLineEdit(**p)
self.qlin_reading_1 = QtWid.QLineEdit(**p)
self.qpbt_running = controls.create_Toggle_button(
"Running", checked=True
)
self.qpbt_running.clicked.connect(
lambda state: self.process_qpbt_running(state)
)
# fmt: off
grid = QtWid.QGridLayout()
grid.addWidget(self.qpbt_running , 0, 0, 1, 2)
grid.addWidget(QtWid.QLabel("time"), 1, 0)
grid.addWidget(self.qlin_reading_t , 1, 1)
grid.addWidget(QtWid.QLabel("#01") , 2, 0)
grid.addWidget(self.qlin_reading_1 , 2, 1)
grid.setAlignment(QtCore.Qt.AlignmentFlag.AlignTop)
# fmt: on
qgrp_readings = QtWid.QGroupBox("Readings")
qgrp_readings.setLayout(grid)
# 'Wave type'
self.qpbt_wave_sine = QtWid.QPushButton("Sine")
self.qpbt_wave_sine.clicked.connect(
self.qdev.request_set_waveform_to_sine
)
self.qpbt_wave_square = QtWid.QPushButton("Square")
self.qpbt_wave_square.clicked.connect(
self.qdev.request_set_waveform_to_square
)
self.qpbt_wave_sawtooth = QtWid.QPushButton("Sawtooth")
self.qpbt_wave_sawtooth.clicked.connect(
self.qdev.request_set_waveform_to_sawtooth
)
# fmt: off
grid = QtWid.QGridLayout()
grid.addWidget(self.qpbt_wave_sine , 0, 0)
grid.addWidget(self.qpbt_wave_square , 1, 0)
grid.addWidget(self.qpbt_wave_sawtooth, 2, 0)
grid.setAlignment(QtCore.Qt.AlignmentFlag.AlignTop)
# fmt: on
qgrp_wave_type = QtWid.QGroupBox("Wave type")
qgrp_wave_type.setLayout(grid)
# -------------------------
# PlotManager
# -------------------------
self.plot_manager = PlotManager(parent=self)
self.plot_manager.add_autorange_buttons(linked_plots=self.plot)
self.plot_manager.add_preset_buttons(
linked_plots=self.plot,
linked_curves=[self.history_chart_curve],
presets=[
{
"button_label": "0.100",
"x_axis_label": "history (msec)",
"x_axis_divisor": 1e-3,
"x_axis_range": (-101, 0),
},
{
"button_label": "0:05",
"x_axis_label": "history (sec)",
"x_axis_divisor": 1,
"x_axis_range": (-5.05, 0),
},
{
"button_label": "0:10",
"x_axis_label": "history (sec)",
"x_axis_divisor": 1,
"x_axis_range": (-10.1, 0),
},
],
)
self.plot_manager.add_clear_button(
linked_curves=[self.history_chart_curve]
)
self.plot_manager.perform_preset(1)
qgrp_chart = QtWid.QGroupBox("Chart")
qgrp_chart.setLayout(self.plot_manager.grid)
vbox = QtWid.QVBoxLayout()
vbox.addWidget(qgrp_readings)
vbox.addWidget(qgrp_wave_type)
vbox.addWidget(qgrp_chart)
vbox.addStretch()
# Round up bottom frame
hbox_bot = QtWid.QHBoxLayout()
hbox_bot.addWidget(self.gw, 1)
hbox_bot.addLayout(vbox, 0)
# -------------------------
# Round up full window
# -------------------------
vbox = QtWid.QVBoxLayout(self)
vbox.addLayout(hbox_top, stretch=0)
vbox.addSpacerItem(QtWid.QSpacerItem(0, 10))
vbox.addLayout(hbox_bot, stretch=1)
# --------------------------------------------------------------------------
# Handle controls
# --------------------------------------------------------------------------
@Slot(bool)
def process_qpbt_running(self, state: bool):
self.qpbt_running.setText("Running" if state else "Paused")
self.allow_GUI_update_of_readings = state
@Slot()
def update_GUI(self):
state = self.qdev.dev.state # Shorthand
self.qlbl_cur_date_time.setText(
f"{datetime.datetime.now().strftime('%d-%m-%Y %H:%M:%S')}"
)
self.qlbl_update_counter.setText(f"{self.qdev.update_counter_DAQ}")
self.qlbl_DAQ_rate.setText(
f"DAQ: {self.qdev.obtained_DAQ_rate_Hz:.1f} Hz"
)
self.qlbl_recording_time.setText(
f"REC: {self.qlog.pretty_elapsed()}"
if self.qlog.is_recording()
else ""
)
if not self.allow_GUI_update_of_readings:
return
self.qlin_reading_t.setText(f"{state.time:.3f}")
self.qlin_reading_1.setText(f"{state.reading_1:.4f}")
@Slot()
def update_chart(self):
if not self.allow_GUI_update_of_readings:
return
if DEBUG:
tprint("update_curve")
self.history_chart_curve.update()
# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":
# Set priority of this process to maximum in the operating system
print(f"PID: {os.getpid()}\n")
try:
proc = psutil.Process(os.getpid())
if os.name == "nt":
proc.nice(psutil.REALTIME_PRIORITY_CLASS) # Windows
else:
proc.nice(-20) # Other
except Exception: # pylint: disable=broad-except
print("Warning: Could not set process to maximum priority.\n")
# --------------------------------------------------------------------------
# Connect to Arduino
# --------------------------------------------------------------------------
if SIMULATE_ARDUINO:
ard = FakeWaveGeneratorArduino()
else:
ard = WaveGeneratorArduino()
ard.serial_settings["baudrate"] = 115200
ard.auto_connect()
if not ard.is_alive:
print("\nCheck connection and try resetting the Arduino.")
print("Exiting...\n")
sys.exit(0)
# --------------------------------------------------------------------------
# Create application
# --------------------------------------------------------------------------
main_thread = QtCore.QThread.currentThread()
if isinstance(main_thread, QtCore.QThread):
main_thread.setObjectName("MAIN") # For DEBUG info
app = QtWid.QApplication(sys.argv)
if USE_LARGER_TEXT:
app.setFont(QtGui.QFont(QtWid.QApplication.font().family(), 16))
# --------------------------------------------------------------------------
# Set up multithreaded communication with the Arduino
# --------------------------------------------------------------------------
def DAQ_function() -> bool:
"""Perform a single data acquisition and append this data to the chart
and log.
Returns: True if successful, False otherwise.
"""
# Query the Arduino for new readings, parse them and update the
# corresponding variables of its `state` member.
if not ard.perform_DAQ():
return False
# Use Arduino time or PC time?
now = time.perf_counter() if USE_PC_TIME else ard.state.time
if ard_qdev.update_counter_DAQ == 1:
ard.state.time_0 = now
ard.state.time = 0
else:
ard.state.time = now - ard.state.time_0
# Add readings to chart history
window.history_chart_curve.appendData(
ard.state.time, ard.state.reading_1
)
# Create and add readings to the log
log.update()
return True
ard_qdev = WaveGeneratorArduino_qdev(
dev=ard,
DAQ_function=DAQ_function,
DAQ_interval_ms=DAQ_INTERVAL_MS,
debug=DEBUG,
)
# --------------------------------------------------------------------------
# File logger
# --------------------------------------------------------------------------
def write_header_to_log():
log.write("elapsed [s]\treading_1\n")
def write_data_to_log():
if USE_PC_TIME:
timestamp = log.elapsed() # Starts at 0 s every recording
else:
timestamp = ard.state.time
log.write(f"{timestamp:.3f}\t{ard.state.reading_1:.4f}\n")
log = FileLogger(
write_header_function=write_header_to_log,
write_data_function=write_data_to_log,
)
log.signal_recording_started.connect(
lambda filepath: window.qpbt_record.setText(
f"Recording to file: {filepath}"
)
)
log.signal_recording_stopped.connect(
lambda: window.qpbt_record.setText("Click to start recording to file")
)
# --------------------------------------------------------------------------
# Program termination routines
# --------------------------------------------------------------------------
def stop_running():
app.processEvents()
log.close()
ard_qdev.quit()
ard.close()
print("Stopping timers: ", end="")
window.timer_chart.stop()
print("done.")
def about_to_quit():
print("\nAbout to quit")
stop_running()
@Slot()
def notify_connection_lost():
stop_running()
window.qlbl_title.setText("! ! ! LOST CONNECTION ! ! !")
str_msg = (
f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
"Lost connection to Arduino."
)
print(f"\nCRITICAL ERROR @ {str_msg}")
QtWid.QMessageBox.warning(
window,
"CRITICAL ERROR",
str_msg,
QtWid.QMessageBox.StandardButton.Ok,
)
# --------------------------------------------------------------------------
# Start the main GUI event loop
# --------------------------------------------------------------------------
window = MainWindow(qdev=ard_qdev, qlog=log)
window.timer_chart.start(CHART_INTERVAL_MS)
window.show()
ard_qdev.signal_connection_lost.connect(notify_connection_lost)
ard_qdev.start(DAQ_priority=QtCore.QThread.Priority.TimeCriticalPriority)
app.aboutToQuit.connect(about_to_quit)
sys.exit(app.exec())