6
6
__author__ = "Dennis van Gils"
7
7
__authoremail__ = "vangils.dennis@gmail.com"
8
8
__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 "
11
11
# pylint: disable=bare-except, broad-except
12
12
13
13
import os
22
22
from PyQt5 .QtCore import QDateTime
23
23
import pyqtgraph as pg
24
24
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
26
26
from dvg_devices .Arduino_protocol_serial import Arduino
27
27
from dvg_qdeviceio import QDeviceIO
28
28
31
31
from DvG_pyqt_controls import create_Toggle_button , SS_GROUP
32
32
33
33
try :
34
- # To enable OpenGL GPU acceleration:
35
- # `conda install pyopengl` or `pip install pyopengl`
36
34
import OpenGL .GL as gl # pylint: disable=unused-import
37
35
except :
38
36
print ("OpenGL acceleration: Disabled" )
37
+ print ("To install: `conda install pyopengl` or `pip install pyopengl`" )
39
38
else :
40
39
print ("OpenGL acceleration: Enabled" )
41
40
pg .setConfigOptions (useOpenGL = True )
42
41
pg .setConfigOptions (antialias = True )
43
42
pg .setConfigOptions (enableExperimental = True )
44
43
44
+ # Global pyqtgraph configuration
45
+ pg .setConfigOptions (leftButtonPan = False )
46
+ pg .setConfigOption ("foreground" , "#EEE" )
47
+
45
48
# Constants
46
49
# fmt: off
47
50
DAQ_INTERVAL_MS = 10 # 10 [ms]
48
- CHART_INTERVAL_MS = 10 # 10 [ms]
51
+ CHART_INTERVAL_MS = 20 # 20 [ms]
49
52
CHART_HISTORY_TIME = 10 # 10 [s]
53
+ LARGE_TEXT = False # For demonstration on a beamer
50
54
# fmt: on
51
55
52
56
# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
@@ -89,13 +93,40 @@ def __init__(self):
89
93
# MainWindow
90
94
# ------------------------------------------------------------------------------
91
95
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
+
92
118
93
119
class MainWindow (QtWid .QWidget ):
94
120
def __init__ (self , parent = None , ** kwargs ):
95
121
super ().__init__ (parent , ** kwargs )
96
122
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 )
99
130
100
131
# -------------------------
101
132
# Top frame
@@ -104,7 +135,7 @@ def __init__(self, parent=None, **kwargs):
104
135
# Left box
105
136
self .qlbl_update_counter = QtWid .QLabel ("0" )
106
137
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 )
108
139
109
140
vbox_left = QtWid .QVBoxLayout ()
110
141
vbox_left .addWidget (self .qlbl_update_counter , stretch = 0 )
@@ -113,14 +144,17 @@ def __init__(self, parent=None, **kwargs):
113
144
114
145
# Middle box
115
146
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
+ ),
118
151
)
119
152
self .qlbl_title .setAlignment (QtCore .Qt .AlignCenter )
120
153
self .qlbl_cur_date_time = QtWid .QLabel ("00-00-0000 00:00:00" )
121
154
self .qlbl_cur_date_time .setAlignment (QtCore .Qt .AlignCenter )
122
155
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 ,
124
158
)
125
159
self .qpbt_record .clicked .connect (self .process_qpbt_record )
126
160
@@ -155,9 +189,8 @@ def __init__(self, parent=None, **kwargs):
155
189
self .gw_chart .setBackground ([20 , 20 , 20 ])
156
190
self .pi_chart = self .gw_chart .addPlot ()
157
191
158
- p = {"color" : "#CCC " , "font-size" : "10pt" }
192
+ p = {"color" : "#EEE " , "font-size" : "20pt" if LARGE_TEXT else "10pt" }
159
193
self .pi_chart .showGrid (x = 1 , y = 1 )
160
- self .pi_chart .setTitle ("Arduino timeseries" , ** p )
161
194
self .pi_chart .setLabel ("bottom" , text = "history (sec)" , ** p )
162
195
self .pi_chart .setLabel ("left" , text = "amplitude" , ** p )
163
196
self .pi_chart .setRange (
@@ -166,8 +199,18 @@ def __init__(self, parent=None, **kwargs):
166
199
disableAutoRange = True ,
167
200
)
168
201
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
+
169
212
# 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 )
171
214
num_samples = round (CHART_HISTORY_TIME * 1e3 / DAQ_INTERVAL_MS )
172
215
self .CH_1 = ChartHistory (num_samples , self .pi_chart .plot (pen = PEN_01 ))
173
216
@@ -306,16 +349,10 @@ def update_GUI():
306
349
@QtCore .pyqtSlot ()
307
350
def update_chart ():
308
351
if DEBUG :
309
- tick = time . perf_counter ( )
352
+ tprint ( "update_curve" )
310
353
311
354
window .CH_1 .update_curve ()
312
355
313
- if DEBUG :
314
- dprint (
315
- " update_curve done in %.2f ms"
316
- % ((time .perf_counter () - tick ) * 1000 )
317
- )
318
-
319
356
320
357
# ------------------------------------------------------------------------------
321
358
# Program termination routines
@@ -336,15 +373,11 @@ def stop_running():
336
373
def notify_connection_lost ():
337
374
stop_running ()
338
375
339
- excl = " ! ! ! ! ! ! "
340
- window .qlbl_title .setText ("%sLOST CONNECTION%s" % (excl , excl ))
341
-
376
+ window .qlbl_title .setText (" ! ! ! LOST CONNECTION ! ! ! " )
342
377
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." % (
344
379
str_cur_date ,
345
380
str_cur_time ,
346
- ard .name ,
347
- "" if ard .is_alive else "not " ,
348
381
)
349
382
print ("\n CRITICAL ERROR @ %s" % str_msg )
350
383
reply = QtWid .QMessageBox .warning (
@@ -496,10 +529,11 @@ def DAQ_function():
496
529
qdev_ard .start (DAQ_priority = QtCore .QThread .TimeCriticalPriority )
497
530
498
531
# --------------------------------------------------------------------------
499
- # Create timers
532
+ # Create chart refresh timer
500
533
# --------------------------------------------------------------------------
501
534
502
535
timer_chart = QtCore .QTimer ()
536
+ # timer_chart.setTimerType(QtCore.Qt.PreciseTimer)
503
537
timer_chart .timeout .connect (update_chart )
504
538
timer_chart .start (CHART_INTERVAL_MS )
505
539
0 commit comments