66__author__ = "Dennis van Gils"
77__authoremail__ = "vangils.dennis@gmail.com"
88__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
9- __date__ = "17 -07-2020"
10- __version__ = "4.1 "
9+ __date__ = "24 -07-2020"
10+ __version__ = "5.0 "
1111# pylint: disable=bare-except, broad-except
1212
1313import os
2222from PyQt5 .QtCore import QDateTime
2323import pyqtgraph as pg
2424
25- from dvg_debug_functions import dprint , print_fancy_traceback as pft
25+ from dvg_debug_functions import tprint , dprint , print_fancy_traceback as pft
2626from dvg_devices .Arduino_protocol_serial import Arduino
2727from dvg_qdeviceio import QDeviceIO
2828
3131from DvG_pyqt_controls import create_Toggle_button , SS_GROUP
3232
3333try :
34- # To enable OpenGL GPU acceleration:
35- # `conda install pyopengl` or `pip install pyopengl`
3634 import OpenGL .GL as gl # pylint: disable=unused-import
3735except :
3836 print ("OpenGL acceleration: Disabled" )
37+ print ("To install: `conda install pyopengl` or `pip install pyopengl`" )
3938else :
4039 print ("OpenGL acceleration: Enabled" )
4140 pg .setConfigOptions (useOpenGL = True )
4241 pg .setConfigOptions (antialias = True )
4342 pg .setConfigOptions (enableExperimental = True )
4443
44+ # Global pyqtgraph configuration
45+ pg .setConfigOptions (leftButtonPan = False )
46+ pg .setConfigOption ("foreground" , "#EEE" )
47+
4548# Constants
4649# fmt: off
4750DAQ_INTERVAL_MS = 10 # 10 [ms]
48- CHART_INTERVAL_MS = 10 # 10 [ms]
51+ CHART_INTERVAL_MS = 20 # 20 [ms]
4952CHART_HISTORY_TIME = 10 # 10 [s]
53+ LARGE_TEXT = False # For demonstration on a beamer
5054# fmt: on
5155
5256# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
@@ -89,13 +93,40 @@ def __init__(self):
8993# MainWindow
9094# ------------------------------------------------------------------------------
9195
96+ if LARGE_TEXT :
97+ SS_GROUP = (
98+ "QGroupBox {"
99+ "background-color: rgb(252, 208, 173);"
100+ "border: 4px solid gray;"
101+ "border-radius: 5px;"
102+ "font: bold italic;"
103+ "padding: 24 0 0 0px;"
104+ "margin-top: 2ex}"
105+ "QGroupBox::title {"
106+ "subcontrol-origin: margin;"
107+ "subcontrol-position: top center;"
108+ "padding: 0 9px}"
109+ "QGroupBox::flat {"
110+ "border: 0px;"
111+ "border-radius: 0 0px;"
112+ "padding: 0}"
113+ )
114+
115+ SS_ALL_FONTS = "QObject {font-size: 16pt}"
116+ SS_LBL_TITLE = "QLabel {font-size: 20pt}"
117+
92118
93119class MainWindow (QtWid .QWidget ):
94120 def __init__ (self , parent = None , ** kwargs ):
95121 super ().__init__ (parent , ** kwargs )
96122
97- self .setGeometry (50 , 50 , 800 , 660 )
98- self .setWindowTitle ("Multithread PyQt & Arduino demo" )
123+ self .setWindowTitle ("Arduino & PyQt multithread demo" )
124+ if LARGE_TEXT :
125+ self .setGeometry (50 , 50 , 1024 , 768 )
126+ else :
127+ self .setGeometry (350 , 50 , 800 , 660 )
128+ if LARGE_TEXT :
129+ self .setStyleSheet (SS_ALL_FONTS )
99130
100131 # -------------------------
101132 # Top frame
@@ -104,7 +135,7 @@ def __init__(self, parent=None, **kwargs):
104135 # Left box
105136 self .qlbl_update_counter = QtWid .QLabel ("0" )
106137 self .qlbl_DAQ_rate = QtWid .QLabel ("DAQ: 0 Hz" )
107- self .qlbl_DAQ_rate .setMinimumWidth (100 )
138+ self .qlbl_DAQ_rate .setMinimumWidth (200 if LARGE_TEXT else 100 )
108139
109140 vbox_left = QtWid .QVBoxLayout ()
110141 vbox_left .addWidget (self .qlbl_update_counter , stretch = 0 )
@@ -113,14 +144,17 @@ def __init__(self, parent=None, **kwargs):
113144
114145 # Middle box
115146 self .qlbl_title = QtWid .QLabel (
116- "Multithread PyQt & Arduino demo" ,
117- font = QtGui .QFont ("Palatino" , 14 , weight = QtGui .QFont .Bold ),
147+ "Arduino & PyQt multithread demo" ,
148+ font = QtGui .QFont (
149+ "Palatino" , 20 if LARGE_TEXT else 14 , weight = QtGui .QFont .Bold
150+ ),
118151 )
119152 self .qlbl_title .setAlignment (QtCore .Qt .AlignCenter )
120153 self .qlbl_cur_date_time = QtWid .QLabel ("00-00-0000 00:00:00" )
121154 self .qlbl_cur_date_time .setAlignment (QtCore .Qt .AlignCenter )
122155 self .qpbt_record = create_Toggle_button (
123- "Click to start recording to file" , minimumHeight = 40
156+ "Click to start recording to file" ,
157+ minimumHeight = 70 if LARGE_TEXT else 40 ,
124158 )
125159 self .qpbt_record .clicked .connect (self .process_qpbt_record )
126160
@@ -155,9 +189,8 @@ def __init__(self, parent=None, **kwargs):
155189 self .gw_chart .setBackground ([20 , 20 , 20 ])
156190 self .pi_chart = self .gw_chart .addPlot ()
157191
158- p = {"color" : "#CCC " , "font-size" : "10pt" }
192+ p = {"color" : "#EEE " , "font-size" : "20pt" if LARGE_TEXT else "10pt" }
159193 self .pi_chart .showGrid (x = 1 , y = 1 )
160- self .pi_chart .setTitle ("Arduino timeseries" , ** p )
161194 self .pi_chart .setLabel ("bottom" , text = "history (sec)" , ** p )
162195 self .pi_chart .setLabel ("left" , text = "amplitude" , ** p )
163196 self .pi_chart .setRange (
@@ -166,8 +199,18 @@ def __init__(self, parent=None, **kwargs):
166199 disableAutoRange = True ,
167200 )
168201
202+ if LARGE_TEXT :
203+ font = QtGui .QFont ()
204+ font .setPixelSize (26 )
205+ self .pi_chart .getAxis ("bottom" ).setTickFont (font )
206+ self .pi_chart .getAxis ("bottom" ).setStyle (tickTextOffset = 20 )
207+ self .pi_chart .getAxis ("bottom" ).setHeight (90 )
208+ self .pi_chart .getAxis ("left" ).setTickFont (font )
209+ self .pi_chart .getAxis ("left" ).setStyle (tickTextOffset = 20 )
210+ self .pi_chart .getAxis ("left" ).setWidth (120 )
211+
169212 # Create ChartHistory and PlotDataItem and link them together
170- PEN_01 = pg .mkPen (color = [0 , 200 , 0 ], width = 3 )
213+ PEN_01 = pg .mkPen (color = [255 , 255 , 90 ], width = 3 )
171214 num_samples = round (CHART_HISTORY_TIME * 1e3 / DAQ_INTERVAL_MS )
172215 self .CH_1 = ChartHistory (num_samples , self .pi_chart .plot (pen = PEN_01 ))
173216
@@ -306,16 +349,10 @@ def update_GUI():
306349@QtCore .pyqtSlot ()
307350def update_chart ():
308351 if DEBUG :
309- tick = time . perf_counter ( )
352+ tprint ( "update_curve" )
310353
311354 window .CH_1 .update_curve ()
312355
313- if DEBUG :
314- dprint (
315- " update_curve done in %.2f ms"
316- % ((time .perf_counter () - tick ) * 1000 )
317- )
318-
319356
320357# ------------------------------------------------------------------------------
321358# Program termination routines
@@ -336,15 +373,11 @@ def stop_running():
336373def notify_connection_lost ():
337374 stop_running ()
338375
339- excl = " ! ! ! ! ! ! "
340- window .qlbl_title .setText ("%sLOST CONNECTION%s" % (excl , excl ))
341-
376+ window .qlbl_title .setText (" ! ! ! LOST CONNECTION ! ! ! " )
342377 str_cur_date , str_cur_time , _ = get_current_date_time ()
343- str_msg = ( "%s %s\n " "Lost connection to Arduino.\n " " '%s': %salive" ) % (
378+ str_msg = "%s %s\n Lost connection to Arduino." % (
344379 str_cur_date ,
345380 str_cur_time ,
346- ard .name ,
347- "" if ard .is_alive else "not " ,
348381 )
349382 print ("\n CRITICAL ERROR @ %s" % str_msg )
350383 reply = QtWid .QMessageBox .warning (
@@ -496,10 +529,11 @@ def DAQ_function():
496529 qdev_ard .start (DAQ_priority = QtCore .QThread .TimeCriticalPriority )
497530
498531 # --------------------------------------------------------------------------
499- # Create timers
532+ # Create chart refresh timer
500533 # --------------------------------------------------------------------------
501534
502535 timer_chart = QtCore .QTimer ()
536+ # timer_chart.setTimerType(QtCore.Qt.PreciseTimer)
503537 timer_chart .timeout .connect (update_chart )
504538 timer_chart .start (CHART_INTERVAL_MS )
505539
0 commit comments