Skip to content

Commit 2a578da

Browse files
Improved PySide/PyQt mechanism. All demos accept argument simulate
1 parent f7978d4 commit 2a578da

File tree

5 files changed

+191
-143
lines changed

5 files changed

+191
-143
lines changed

demo_A_GUI_full.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,50 @@
66
__author__ = "Dennis van Gils"
77
__authoremail__ = "[email protected]"
88
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
9-
__date__ = "14-09-2022"
10-
__version__ = "8.0"
9+
__date__ = "12-10-2022"
10+
__version__ = "8.1"
1111
# pylint: disable=bare-except, broad-except, unnecessary-lambda
1212

13+
import os
14+
import sys
1315
import time
1416

17+
# Constants
18+
# fmt: off
19+
DAQ_INTERVAL_MS = 10 # 10 [ms]
20+
CHART_INTERVAL_MS = 20 # 20 [ms]
21+
CHART_HISTORY_TIME = 10 # 10 [s]
22+
# fmt: on
23+
24+
# Global flags
25+
USE_LARGER_TEXT = False # For demonstration on a beamer
26+
USE_PC_TIME = True # Use Arduino time or PC time?
27+
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
28+
if sys.argv[-1] == "simulate":
29+
SIMULATE_ARDUINO = True
30+
31+
# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
32+
DEBUG = False
33+
1534
# Mechanism to support both PyQt and PySide
1635
# -----------------------------------------
17-
import os
18-
import sys
1936

20-
QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")
21-
PYSIDE = "PySide"
22-
PYSIDE2 = "PySide2"
23-
PYSIDE6 = "PySide6"
24-
PYQT4 = "PyQt4"
2537
PYQT5 = "PyQt5"
2638
PYQT6 = "PyQt6"
39+
PYSIDE2 = "PySide2"
40+
PYSIDE6 = "PySide6"
41+
QT_LIB_ORDER = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
42+
QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")
2743

28-
# pylint: disable=import-error, no-name-in-module
29-
# fmt: off
44+
# pylint: disable=import-error, no-name-in-module, c-extension-no-member
3045
if QT_LIB is None:
31-
libOrder = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
32-
for lib in libOrder:
46+
for lib in QT_LIB_ORDER:
3347
if lib in sys.modules:
3448
QT_LIB = lib
3549
break
3650

3751
if QT_LIB is None:
38-
for lib in libOrder:
52+
for lib in QT_LIB_ORDER:
3953
try:
4054
__import__(lib)
4155
QT_LIB = lib
@@ -44,11 +58,13 @@
4458
pass
4559

4660
if QT_LIB is None:
61+
this_file = __file__.split(os.sep)[-1]
4762
raise Exception(
48-
"demo_A_GUI_full requires PyQt5, PyQt6, PySide2 or PySide6; "
63+
f"{this_file} requires PyQt5, PyQt6, PySide2 or PySide6; "
4964
"none of these packages could be imported."
5065
)
5166

67+
# fmt: off
5268
if QT_LIB == PYQT5:
5369
from PyQt5 import QtCore, QtGui, QtWidgets as QtWid # type: ignore
5470
from PyQt5.QtCore import pyqtSlot as Slot # type: ignore
@@ -61,9 +77,14 @@
6177
elif QT_LIB == PYSIDE6:
6278
from PySide6 import QtCore, QtGui, QtWidgets as QtWid # type: ignore
6379
from PySide6.QtCore import Slot # type: ignore
64-
6580
# fmt: on
66-
# pylint: enable=import-error, no-name-in-module
81+
82+
QT_VERSION = (
83+
QtCore.QT_VERSION_STR if QT_LIB in (PYQT5, PYQT6) else QtCore.__version__
84+
)
85+
print(f"{QT_LIB} {QT_VERSION}")
86+
87+
# pylint: enable=import-error, no-name-in-module, c-extension-no-member
6788
# \end[Mechanism to support both PyQt and PySide]
6889
# -----------------------------------------------
6990

@@ -100,23 +121,6 @@
100121
# pg.setConfigOptions(leftButtonPan=False)
101122
pg.setConfigOption("foreground", "#EEE")
102123

103-
# Constants
104-
# fmt: off
105-
DAQ_INTERVAL_MS = 10 # 10 [ms]
106-
CHART_INTERVAL_MS = 20 # 20 [ms]
107-
CHART_HISTORY_TIME = 10 # 10 [s]
108-
# fmt: on
109-
110-
# Global flags
111-
USE_LARGER_TEXT = False # For demonstration on a beamer
112-
USE_PC_TIME = True # Use Arduino time or PC time?
113-
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
114-
if sys.argv[-1] == "simulate":
115-
SIMULATE_ARDUINO = True
116-
117-
# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
118-
DEBUG = False
119-
120124

121125
def get_current_date_time():
122126
cur_date_time = QtCore.QDateTime.currentDateTime()
@@ -605,6 +609,7 @@ def start_stop():
605609
# --------------------------------------------------------------------------
606610
# Start the main GUI event loop
607611
# --------------------------------------------------------------------------
612+
608613
window.show()
609614
if QT_LIB in (PYQT5, PYSIDE2):
610615
sys.exit(app.exec_())

demo_B_GUI_minimal.py

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,48 @@
66
__author__ = "Dennis van Gils"
77
__authoremail__ = "[email protected]"
88
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
9-
__date__ = "14-09-2022"
10-
__version__ = "8.0"
9+
__date__ = "12-09-2022"
10+
__version__ = "8.1"
1111
# pylint: disable=bare-except, broad-except
1212

13+
import os
14+
import sys
1315
import time
1416

17+
# Constants
18+
# fmt: off
19+
DAQ_INTERVAL_MS = 10 # 10 [ms]
20+
CHART_INTERVAL_MS = 20 # 20 [ms]
21+
CHART_HISTORY_TIME = 10 # 10 [s]
22+
# fmt: on
23+
24+
# Global flags
25+
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
26+
if sys.argv[-1] == "simulate":
27+
SIMULATE_ARDUINO = True
28+
29+
# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
30+
DEBUG = False
31+
1532
# Mechanism to support both PyQt and PySide
1633
# -----------------------------------------
17-
import os
18-
import sys
1934

20-
QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")
21-
PYSIDE = "PySide"
22-
PYSIDE2 = "PySide2"
23-
PYSIDE6 = "PySide6"
24-
PYQT4 = "PyQt4"
2535
PYQT5 = "PyQt5"
2636
PYQT6 = "PyQt6"
37+
PYSIDE2 = "PySide2"
38+
PYSIDE6 = "PySide6"
39+
QT_LIB_ORDER = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
40+
QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")
2741

28-
# pylint: disable=import-error, no-name-in-module
29-
# fmt: off
42+
# pylint: disable=import-error, no-name-in-module, c-extension-no-member
3043
if QT_LIB is None:
31-
libOrder = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
32-
for lib in libOrder:
44+
for lib in QT_LIB_ORDER:
3345
if lib in sys.modules:
3446
QT_LIB = lib
3547
break
3648

3749
if QT_LIB is None:
38-
for lib in libOrder:
50+
for lib in QT_LIB_ORDER:
3951
try:
4052
__import__(lib)
4153
QT_LIB = lib
@@ -44,11 +56,13 @@
4456
pass
4557

4658
if QT_LIB is None:
59+
this_file = __file__.split(os.sep)[-1]
4760
raise Exception(
48-
"demo_B_GUI_minimal requires PyQt5, PyQt6, PySide2 or PySide6; "
61+
f"{this_file} requires PyQt5, PyQt6, PySide2 or PySide6; "
4962
"none of these packages could be imported."
5063
)
5164

65+
# fmt: off
5266
if QT_LIB == PYQT5:
5367
from PyQt5 import QtCore, QtWidgets as QtWid # type: ignore
5468
from PyQt5.QtCore import pyqtSlot as Slot # type: ignore
@@ -61,9 +75,14 @@
6175
elif QT_LIB == PYSIDE6:
6276
from PySide6 import QtCore, QtWidgets as QtWid # type: ignore
6377
from PySide6.QtCore import Slot # type: ignore
64-
6578
# fmt: on
66-
# pylint: enable=import-error, no-name-in-module
79+
80+
QT_VERSION = (
81+
QtCore.QT_VERSION_STR if QT_LIB in (PYQT5, PYQT6) else QtCore.__version__
82+
)
83+
print(f"{QT_LIB} {QT_VERSION}")
84+
85+
# pylint: enable=import-error, no-name-in-module, c-extension-no-member
6786
# \end[Mechanism to support both PyQt and PySide]
6887
# -----------------------------------------------
6988

@@ -74,6 +93,7 @@
7493
from dvg_debug_functions import dprint, print_fancy_traceback as pft
7594
from dvg_pyqtgraph_threadsafe import HistoryChartCurve
7695

96+
from dvg_fakearduino import FakeArduino
7797
from dvg_devices.Arduino_protocol_serial import Arduino
7898
from dvg_qdeviceio import QDeviceIO
7999

@@ -92,16 +112,6 @@
92112
# pg.setConfigOptions(leftButtonPan=False)
93113
pg.setConfigOption("foreground", "#EEE")
94114

95-
# Constants
96-
# fmt: off
97-
DAQ_INTERVAL_MS = 10 # 10 [ms]
98-
CHART_INTERVAL_MS = 20 # 20 [ms]
99-
CHART_HISTORY_TIME = 10 # 10 [s]
100-
# fmt: on
101-
102-
# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
103-
DEBUG = False
104-
105115

106116
def get_current_date_time():
107117
cur_date_time = QtCore.QDateTime.currentDateTime()
@@ -252,7 +262,10 @@ def DAQ_function():
252262
# Connect to Arduino
253263
# --------------------------------------------------------------------------
254264

255-
ard = Arduino(name="Ard", connect_to_specific_ID="Wave generator")
265+
if SIMULATE_ARDUINO:
266+
ard = FakeArduino()
267+
else:
268+
ard = Arduino(name="Ard", connect_to_specific_ID="Wave generator")
256269

257270
ard.serial_settings["baudrate"] = 115200
258271
ard.auto_connect()

demo_C_singlethread_for_comparison.py

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,49 @@
1212
__author__ = "Dennis van Gils"
1313
__authoremail__ = "[email protected]"
1414
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
15-
__date__ = "14-09-2022"
16-
__version__ = "8.0"
15+
__date__ = "12-10-2022"
16+
__version__ = "8.1"
1717
# pylint: disable=bare-except, broad-except, unnecessary-lambda
1818

19+
import os
20+
import sys
1921
import time
2022

23+
# Constants
24+
# fmt: off
25+
DAQ_INTERVAL_MS = 10 # 10 [ms]
26+
CHART_INTERVAL_MS = 20 # 20 [ms]
27+
CHART_HISTORY_TIME = 10 # 10 [s]
28+
# fmt: on
29+
30+
# Global flags
31+
USE_PC_TIME = True # Use Arduino time or PC time?
32+
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
33+
if sys.argv[-1] == "simulate":
34+
SIMULATE_ARDUINO = True
35+
36+
# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
37+
DEBUG = False
38+
2139
# Mechanism to support both PyQt and PySide
2240
# -----------------------------------------
23-
import os
24-
import sys
2541

26-
QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")
27-
PYSIDE = "PySide"
28-
PYSIDE2 = "PySide2"
29-
PYSIDE6 = "PySide6"
30-
PYQT4 = "PyQt4"
3142
PYQT5 = "PyQt5"
3243
PYQT6 = "PyQt6"
44+
PYSIDE2 = "PySide2"
45+
PYSIDE6 = "PySide6"
46+
QT_LIB_ORDER = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
47+
QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")
3348

34-
# pylint: disable=import-error, no-name-in-module
35-
# fmt: off
49+
# pylint: disable=import-error, no-name-in-module, c-extension-no-member
3650
if QT_LIB is None:
37-
libOrder = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
38-
for lib in libOrder:
51+
for lib in QT_LIB_ORDER:
3952
if lib in sys.modules:
4053
QT_LIB = lib
4154
break
4255

4356
if QT_LIB is None:
44-
for lib in libOrder:
57+
for lib in QT_LIB_ORDER:
4558
try:
4659
__import__(lib)
4760
QT_LIB = lib
@@ -50,11 +63,13 @@
5063
pass
5164

5265
if QT_LIB is None:
66+
this_file = __file__.split(os.sep)[-1]
5367
raise Exception(
54-
"demo_C_singlethread_for_comparison requires PyQt5, PyQt6, PySide2 or PySide6; "
68+
f"{this_file} requires PyQt5, PyQt6, PySide2 or PySide6; "
5569
"none of these packages could be imported."
5670
)
5771

72+
# fmt: off
5873
if QT_LIB == PYQT5:
5974
from PyQt5 import QtCore, QtGui, QtWidgets as QtWid # type: ignore
6075
from PyQt5.QtCore import pyqtSlot as Slot # type: ignore
@@ -67,9 +82,14 @@
6782
elif QT_LIB == PYSIDE6:
6883
from PySide6 import QtCore, QtGui, QtWidgets as QtWid # type: ignore
6984
from PySide6.QtCore import Slot # type: ignore
70-
7185
# fmt: on
72-
# pylint: enable=import-error, no-name-in-module
86+
87+
QT_VERSION = (
88+
QtCore.QT_VERSION_STR if QT_LIB in (PYQT5, PYQT6) else QtCore.__version__
89+
)
90+
print(f"{QT_LIB} {QT_VERSION}")
91+
92+
# pylint: enable=import-error, no-name-in-module, c-extension-no-member
7393
# \end[Mechanism to support both PyQt and PySide]
7494
# -----------------------------------------------
7595

@@ -105,22 +125,6 @@
105125
# pg.setConfigOptions(leftButtonPan=False)
106126
pg.setConfigOption("foreground", "#EEE")
107127

108-
# Constants
109-
# fmt: off
110-
DAQ_INTERVAL_MS = 10 # 10 [ms]
111-
CHART_INTERVAL_MS = 20 # 20 [ms]
112-
CHART_HISTORY_TIME = 10 # 10 [s]
113-
# fmt: on
114-
115-
# Global flags
116-
USE_PC_TIME = True # Use Arduino time or PC time?
117-
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
118-
if sys.argv[-1] == "simulate":
119-
SIMULATE_ARDUINO = True
120-
121-
# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
122-
DEBUG = False
123-
124128

125129
def get_current_date_time():
126130
cur_date_time = QtCore.QDateTime.currentDateTime()

0 commit comments

Comments
 (0)