Skip to content

Commit 0602116

Browse files
Clean up and make coding style similar across demos
1 parent 714e4e8 commit 0602116

8 files changed

+233
-649
lines changed

Arduino_PyQt_demo_with_multithreading__LARGE_TEXT.py

Lines changed: 0 additions & 531 deletions
This file was deleted.

config/port.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

demo_A_GUI_full.py

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
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

1313
import os
@@ -22,7 +22,7 @@
2222
from PyQt5.QtCore import QDateTime
2323
import 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
2626
from dvg_devices.Arduino_protocol_serial import Arduino
2727
from dvg_qdeviceio import QDeviceIO
2828

@@ -31,22 +31,26 @@
3131
from DvG_pyqt_controls import create_Toggle_button, SS_GROUP
3232

3333
try:
34-
# To enable OpenGL GPU acceleration:
35-
# `conda install pyopengl` or `pip install pyopengl`
3634
import OpenGL.GL as gl # pylint: disable=unused-import
3735
except:
3836
print("OpenGL acceleration: Disabled")
37+
print("To install: `conda install pyopengl` or `pip install pyopengl`")
3938
else:
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
4750
DAQ_INTERVAL_MS = 10 # 10 [ms]
48-
CHART_INTERVAL_MS = 10 # 10 [ms]
51+
CHART_INTERVAL_MS = 20 # 20 [ms]
4952
CHART_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

93119
class 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()
307350
def 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():
336373
def 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\nLost connection to Arduino." % (
344379
str_cur_date,
345380
str_cur_time,
346-
ard.name,
347-
"" if ard.is_alive else "not ",
348381
)
349382
print("\nCRITICAL 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

demo_B_GUI_minimal.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
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

1313
import os
@@ -28,10 +28,25 @@
2828

2929
from DvG_pyqt_ChartHistory import ChartHistory
3030

31+
try:
32+
import OpenGL.GL as gl # pylint: disable=unused-import
33+
except:
34+
print("OpenGL acceleration: Disabled")
35+
print("To install: `conda install pyopengl` or `pip install pyopengl`")
36+
else:
37+
print("OpenGL acceleration: Enabled")
38+
pg.setConfigOptions(useOpenGL=True)
39+
pg.setConfigOptions(antialias=True)
40+
pg.setConfigOptions(enableExperimental=True)
41+
42+
# Global pyqtgraph configuration
43+
pg.setConfigOptions(leftButtonPan=False)
44+
pg.setConfigOption("foreground", "#EEE")
45+
3146
# Constants
3247
# fmt: off
3348
DAQ_INTERVAL_MS = 10 # 10 [ms]
34-
CHART_INTERVAL_MS = 10 # 10 [ms]
49+
CHART_INTERVAL_MS = 20 # 20 [ms]
3550
CHART_HISTORY_TIME = 10 # 10 [s]
3651
# fmt: on
3752

@@ -80,17 +95,16 @@ class MainWindow(QtWid.QWidget):
8095
def __init__(self, parent=None, **kwargs):
8196
super().__init__(parent, **kwargs)
8297

83-
self.setGeometry(50, 50, 800, 660)
84-
self.setWindowTitle("Multithread PyQt & Arduino demo")
98+
self.setGeometry(350, 50, 800, 660)
99+
self.setWindowTitle("Arduino & PyQt multithread demo")
85100

86101
# Create PlotItem
87102
self.gw_chart = pg.GraphicsWindow()
88103
self.gw_chart.setBackground([20, 20, 20])
89104
self.pi_chart = self.gw_chart.addPlot()
90105

91-
p = {"color": "#CCC", "font-size": "10pt"}
106+
p = {"color": "#EEE", "font-size": "10pt"}
92107
self.pi_chart.showGrid(x=1, y=1)
93-
self.pi_chart.setTitle("Arduino timeseries", **p)
94108
self.pi_chart.setLabel("bottom", text="history (sec)", **p)
95109
self.pi_chart.setLabel("left", text="amplitude", **p)
96110
self.pi_chart.setRange(
@@ -100,7 +114,7 @@ def __init__(self, parent=None, **kwargs):
100114
)
101115

102116
# Create ChartHistory and PlotDataItem and link them together
103-
PEN_01 = pg.mkPen(color=[0, 200, 0], width=3)
117+
PEN_01 = pg.mkPen(color=[255, 255, 90], width=3)
104118
num_samples = round(CHART_HISTORY_TIME * 1e3 / DAQ_INTERVAL_MS)
105119
self.CH_1 = ChartHistory(num_samples, self.pi_chart.plot(pen=PEN_01))
106120

@@ -133,7 +147,7 @@ def about_to_quit():
133147

134148
def DAQ_function():
135149
# Date-time keeping
136-
str_cur_date, str_cur_time, str_cur_datetime = get_current_date_time()
150+
str_cur_date, str_cur_time, _ = get_current_date_time()
137151

138152
# Query the Arduino for its state
139153
success, tmp_state = ard.query_ascii_values("?", delimiter="\t")

0 commit comments

Comments
 (0)