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
66NOTE: This demonstrates the bad case of what happens when both the acquisition
77and the plotting happen on the same thread. You should observe a drop in the
1515__date__ = "11-06-2024"
1616__version__ = "9.0"
1717# pylint: disable=missing-function-docstring, unnecessary-lambda
18+ # pylint: disable=global-statement
1819
1920import os
2021import sys
3637
3738from 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
4650TRY_USING_OPENGL = True
4751USE_PC_TIME = True # Use Arduino time or PC time?
4852SIMULATE_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 ())
0 commit comments