Skip to content

Commit 9e5ad7b

Browse files
Mimic demo A closer in code structure
1 parent a22c488 commit 9e5ad7b

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

demo_C_singlethread_for_comparison.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
"""Demonstration of singlethreaded real-time plotting and logging of live Arduino
4-
data using PyQt/PySide and PyQtGraph.
3+
"""Demonstration of singlethreaded real-time plotting and logging of live
4+
Arduino data using PyQt/PySide and PyQtGraph.
55
66
NOTE: This demonstrates the bad case of what happens when both the acquisition
77
and the plotting happen on the same thread. You should observe a drop in the
@@ -15,6 +15,7 @@
1515
__date__ = "11-06-2024"
1616
__version__ = "9.0"
1717
# pylint: disable=missing-function-docstring, unnecessary-lambda
18+
# pylint: disable=global-statement
1819

1920
import os
2021
import sys
@@ -36,13 +37,16 @@
3637

3738
from WaveGeneratorArduino import WaveGeneratorArduino, FakeWaveGeneratorArduino
3839

39-
# fmt: off
4040
# Constants
41-
DAQ_INTERVAL_MS = 10 # 10 [ms]
42-
CHART_INTERVAL_MS = 20 # 20 [ms]
43-
CHART_HISTORY_TIME = 10 # 10 [s]
41+
DAQ_INTERVAL_MS = 10
42+
"""[ms] Update interval for the data acquisition (DAQ)"""
43+
CHART_INTERVAL_MS = 20
44+
"""[ms] Update interval for the chart"""
45+
CHART_HISTORY_TIME = 10
46+
"""[s] History length of the chart"""
4447

4548
# Global flags
49+
# fmt: off
4650
TRY_USING_OPENGL = True
4751
USE_PC_TIME = True # Use Arduino time or PC time?
4852
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
@@ -118,6 +122,9 @@ def __init__(
118122
self.dev = dev
119123
self.qlog = qlog
120124

125+
# Run/pause mechanism on updating the GUI and graphs
126+
self.allow_GUI_update_of_readings = True
127+
121128
self.setWindowTitle("Arduino & PyQt singlethread demo")
122129
self.setGeometry(40, 60, 960, 660)
123130
self.setStyleSheet(controls.SS_TEXTBOX_READ_ONLY + controls.SS_GROUP)
@@ -235,9 +242,7 @@ def __init__(
235242
"Running", checked=True
236243
)
237244
self.qpbt_running.clicked.connect(
238-
lambda state: self.qpbt_running.setText(
239-
"Running" if state else "Run"
240-
)
245+
lambda state: self.process_qpbt_running(state)
241246
)
242247

243248
# fmt: off
@@ -336,6 +341,11 @@ def __init__(
336341
# Handle controls
337342
# --------------------------------------------------------------------------
338343

344+
@Slot(bool)
345+
def process_qpbt_running(self, state: bool):
346+
self.qpbt_running.setText("Running" if state else "Paused")
347+
self.allow_GUI_update_of_readings = state
348+
339349
@Slot()
340350
def update_GUI(self):
341351
str_cur_date, str_cur_time = current_date_time_strings()
@@ -349,11 +359,17 @@ def update_GUI(self):
349359
if self.qlog.is_recording()
350360
else ""
351361
)
362+
363+
if not self.allow_GUI_update_of_readings:
364+
return
365+
352366
self.qlin_reading_t.setText(f"{state.time:.3f}")
353367
self.qlin_reading_1.setText(f"{state.reading_1:.4f}")
354368

355369
@Slot()
356370
def update_chart(self):
371+
if not self.allow_GUI_update_of_readings:
372+
return
357373
if DEBUG:
358374
tprint("update_curve")
359375

@@ -432,13 +448,12 @@ def write_data_to_log():
432448
)
433449

434450
# --------------------------------------------------------------------------
435-
# Single-threaded DAQ function
451+
# Singlethreaded DAQ function
436452
# --------------------------------------------------------------------------
437453

438454
@Slot()
439455
def DAQ_function():
440-
global update_counter_DAQ, QET_rate, rate_accumulator
441-
global obtained_DAQ_rate_Hz
456+
global update_counter_DAQ, rate_accumulator, obtained_DAQ_rate_Hz
442457

443458
state = ard.state # Shorthand
444459
update_counter_DAQ += 1
@@ -489,10 +504,10 @@ def DAQ_function():
489504
else:
490505
state.time = now - state.time_0
491506

492-
# Add readings to chart histories
507+
# Add readings to chart history
493508
window.history_chart_curve.appendData(state.time, state.reading_1)
494509

495-
# Logging to file
510+
# Add readings to the log
496511
log.update()
497512

498513
# We update the GUI right now because this is a singlethread demo
@@ -522,13 +537,9 @@ def about_to_quit():
522537
# Start the main GUI event loop
523538
# --------------------------------------------------------------------------
524539

525-
app.aboutToQuit.connect(about_to_quit)
526-
527540
window = MainWindow(dev=ard, qlog=log)
528-
window.qpbt_running.clicked.connect(
529-
lambda state: timer_state.start() if state else timer_state.stop()
530-
)
531541
window.timer_chart.start(CHART_INTERVAL_MS)
532542
window.show()
533543

544+
app.aboutToQuit.connect(about_to_quit)
534545
sys.exit(app.exec())

demo_E_no_GUI.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from WaveGeneratorArduino import WaveGeneratorArduino, FakeWaveGeneratorArduino
3232

3333
# Constants
34-
DAQ_INTERVAL_MS = 10 # 10 [ms]
34+
DAQ_INTERVAL_MS = 10
35+
"""[ms] Update interval for the data acquisition (DAQ)"""
3536

3637
# Global flags
3738
USE_PC_TIME = True # Use Arduino time or PC time?
@@ -50,8 +51,9 @@
5051

5152

5253
class WaveGeneratorArduino_qdev(QDeviceIO):
53-
"""Manages multithreaded communication and periodical data acquisition for
54-
a wave generator Arduino, referred to as the 'device'."""
54+
"""Manages multithreaded communication and periodical data acquisition
55+
for an Arduino that is programmed as a wave generator, referred to as the
56+
'device'."""
5557

5658
def __init__(
5759
self,
@@ -75,13 +77,13 @@ def __init__(
7577
)
7678
self.create_worker_jobs(debug=debug)
7779

78-
def set_waveform_to_sine(self):
80+
def request_set_waveform_to_sine(self):
7981
self.send(self.dev.set_waveform_to_sine)
8082

81-
def set_waveform_to_square(self):
83+
def request_set_waveform_to_square(self):
8284
self.send(self.dev.set_waveform_to_square)
8385

84-
def set_waveform_to_sawtooth(self):
86+
def request_set_waveform_to_sawtooth(self):
8587
self.send(self.dev.set_waveform_to_sawtooth)
8688

8789
# --------------------------------------------------------------------------
@@ -238,5 +240,4 @@ def about_to_quit():
238240
sync_qdev.start(DAQ_priority=QtCore.QThread.Priority.TimeCriticalPriority)
239241

240242
app.aboutToQuit.connect(about_to_quit)
241-
242243
sys.exit(app.exec())

0 commit comments

Comments
 (0)