Skip to content

Commit

Permalink
Improved PySide/PyQt mechanism. All demos accept argument simulate
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis-van-Gils committed Oct 12, 2022
1 parent f7978d4 commit 2a578da
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 143 deletions.
73 changes: 39 additions & 34 deletions demo_A_GUI_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,50 @@
__author__ = "Dennis van Gils"
__authoremail__ = "vangils.dennis@gmail.com"
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
__date__ = "14-09-2022"
__version__ = "8.0"
__date__ = "12-10-2022"
__version__ = "8.1"
# pylint: disable=bare-except, broad-except, unnecessary-lambda

import os
import sys
import time

# Constants
# fmt: off
DAQ_INTERVAL_MS = 10 # 10 [ms]
CHART_INTERVAL_MS = 20 # 20 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]
# fmt: on

# Global flags
USE_LARGER_TEXT = False # For demonstration on a beamer
USE_PC_TIME = True # Use Arduino time or PC time?
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
if sys.argv[-1] == "simulate":
SIMULATE_ARDUINO = True

# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
DEBUG = False

# Mechanism to support both PyQt and PySide
# -----------------------------------------
import os
import sys

QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")
PYSIDE = "PySide"
PYSIDE2 = "PySide2"
PYSIDE6 = "PySide6"
PYQT4 = "PyQt4"
PYQT5 = "PyQt5"
PYQT6 = "PyQt6"
PYSIDE2 = "PySide2"
PYSIDE6 = "PySide6"
QT_LIB_ORDER = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")

# pylint: disable=import-error, no-name-in-module
# fmt: off
# pylint: disable=import-error, no-name-in-module, c-extension-no-member
if QT_LIB is None:
libOrder = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
for lib in libOrder:
for lib in QT_LIB_ORDER:
if lib in sys.modules:
QT_LIB = lib
break

if QT_LIB is None:
for lib in libOrder:
for lib in QT_LIB_ORDER:
try:
__import__(lib)
QT_LIB = lib
Expand All @@ -44,11 +58,13 @@
pass

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

# fmt: off
if QT_LIB == PYQT5:
from PyQt5 import QtCore, QtGui, QtWidgets as QtWid # type: ignore
from PyQt5.QtCore import pyqtSlot as Slot # type: ignore
Expand All @@ -61,9 +77,14 @@
elif QT_LIB == PYSIDE6:
from PySide6 import QtCore, QtGui, QtWidgets as QtWid # type: ignore
from PySide6.QtCore import Slot # type: ignore

# fmt: on
# pylint: enable=import-error, no-name-in-module

QT_VERSION = (
QtCore.QT_VERSION_STR if QT_LIB in (PYQT5, PYQT6) else QtCore.__version__
)
print(f"{QT_LIB} {QT_VERSION}")

# pylint: enable=import-error, no-name-in-module, c-extension-no-member
# \end[Mechanism to support both PyQt and PySide]
# -----------------------------------------------

Expand Down Expand Up @@ -100,23 +121,6 @@
# pg.setConfigOptions(leftButtonPan=False)
pg.setConfigOption("foreground", "#EEE")

# Constants
# fmt: off
DAQ_INTERVAL_MS = 10 # 10 [ms]
CHART_INTERVAL_MS = 20 # 20 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]
# fmt: on

# Global flags
USE_LARGER_TEXT = False # For demonstration on a beamer
USE_PC_TIME = True # Use Arduino time or PC time?
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
if sys.argv[-1] == "simulate":
SIMULATE_ARDUINO = True

# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
DEBUG = False


def get_current_date_time():
cur_date_time = QtCore.QDateTime.currentDateTime()
Expand Down Expand Up @@ -605,6 +609,7 @@ def start_stop():
# --------------------------------------------------------------------------
# Start the main GUI event loop
# --------------------------------------------------------------------------

window.show()
if QT_LIB in (PYQT5, PYSIDE2):
sys.exit(app.exec_())
Expand Down
69 changes: 41 additions & 28 deletions demo_B_GUI_minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,48 @@
__author__ = "Dennis van Gils"
__authoremail__ = "vangils.dennis@gmail.com"
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
__date__ = "14-09-2022"
__version__ = "8.0"
__date__ = "12-09-2022"
__version__ = "8.1"
# pylint: disable=bare-except, broad-except

import os
import sys
import time

# Constants
# fmt: off
DAQ_INTERVAL_MS = 10 # 10 [ms]
CHART_INTERVAL_MS = 20 # 20 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]
# fmt: on

# Global flags
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
if sys.argv[-1] == "simulate":
SIMULATE_ARDUINO = True

# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
DEBUG = False

# Mechanism to support both PyQt and PySide
# -----------------------------------------
import os
import sys

QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")
PYSIDE = "PySide"
PYSIDE2 = "PySide2"
PYSIDE6 = "PySide6"
PYQT4 = "PyQt4"
PYQT5 = "PyQt5"
PYQT6 = "PyQt6"
PYSIDE2 = "PySide2"
PYSIDE6 = "PySide6"
QT_LIB_ORDER = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")

# pylint: disable=import-error, no-name-in-module
# fmt: off
# pylint: disable=import-error, no-name-in-module, c-extension-no-member
if QT_LIB is None:
libOrder = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
for lib in libOrder:
for lib in QT_LIB_ORDER:
if lib in sys.modules:
QT_LIB = lib
break

if QT_LIB is None:
for lib in libOrder:
for lib in QT_LIB_ORDER:
try:
__import__(lib)
QT_LIB = lib
Expand All @@ -44,11 +56,13 @@
pass

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

# fmt: off
if QT_LIB == PYQT5:
from PyQt5 import QtCore, QtWidgets as QtWid # type: ignore
from PyQt5.QtCore import pyqtSlot as Slot # type: ignore
Expand All @@ -61,9 +75,14 @@
elif QT_LIB == PYSIDE6:
from PySide6 import QtCore, QtWidgets as QtWid # type: ignore
from PySide6.QtCore import Slot # type: ignore

# fmt: on
# pylint: enable=import-error, no-name-in-module

QT_VERSION = (
QtCore.QT_VERSION_STR if QT_LIB in (PYQT5, PYQT6) else QtCore.__version__
)
print(f"{QT_LIB} {QT_VERSION}")

# pylint: enable=import-error, no-name-in-module, c-extension-no-member
# \end[Mechanism to support both PyQt and PySide]
# -----------------------------------------------

Expand All @@ -74,6 +93,7 @@
from dvg_debug_functions import dprint, print_fancy_traceback as pft
from dvg_pyqtgraph_threadsafe import HistoryChartCurve

from dvg_fakearduino import FakeArduino
from dvg_devices.Arduino_protocol_serial import Arduino
from dvg_qdeviceio import QDeviceIO

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

# Constants
# fmt: off
DAQ_INTERVAL_MS = 10 # 10 [ms]
CHART_INTERVAL_MS = 20 # 20 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]
# fmt: on

# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
DEBUG = False


def get_current_date_time():
cur_date_time = QtCore.QDateTime.currentDateTime()
Expand Down Expand Up @@ -252,7 +262,10 @@ def DAQ_function():
# Connect to Arduino
# --------------------------------------------------------------------------

ard = Arduino(name="Ard", connect_to_specific_ID="Wave generator")
if SIMULATE_ARDUINO:
ard = FakeArduino()
else:
ard = Arduino(name="Ard", connect_to_specific_ID="Wave generator")

ard.serial_settings["baudrate"] = 115200
ard.auto_connect()
Expand Down
70 changes: 37 additions & 33 deletions demo_C_singlethread_for_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,49 @@
__author__ = "Dennis van Gils"
__authoremail__ = "vangils.dennis@gmail.com"
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
__date__ = "14-09-2022"
__version__ = "8.0"
__date__ = "12-10-2022"
__version__ = "8.1"
# pylint: disable=bare-except, broad-except, unnecessary-lambda

import os
import sys
import time

# Constants
# fmt: off
DAQ_INTERVAL_MS = 10 # 10 [ms]
CHART_INTERVAL_MS = 20 # 20 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]
# fmt: on

# Global flags
USE_PC_TIME = True # Use Arduino time or PC time?
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
if sys.argv[-1] == "simulate":
SIMULATE_ARDUINO = True

# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
DEBUG = False

# Mechanism to support both PyQt and PySide
# -----------------------------------------
import os
import sys

QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")
PYSIDE = "PySide"
PYSIDE2 = "PySide2"
PYSIDE6 = "PySide6"
PYQT4 = "PyQt4"
PYQT5 = "PyQt5"
PYQT6 = "PyQt6"
PYSIDE2 = "PySide2"
PYSIDE6 = "PySide6"
QT_LIB_ORDER = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
QT_LIB = os.getenv("PYQTGRAPH_QT_LIB")

# pylint: disable=import-error, no-name-in-module
# fmt: off
# pylint: disable=import-error, no-name-in-module, c-extension-no-member
if QT_LIB is None:
libOrder = [PYQT5, PYSIDE2, PYSIDE6, PYQT6]
for lib in libOrder:
for lib in QT_LIB_ORDER:
if lib in sys.modules:
QT_LIB = lib
break

if QT_LIB is None:
for lib in libOrder:
for lib in QT_LIB_ORDER:
try:
__import__(lib)
QT_LIB = lib
Expand All @@ -50,11 +63,13 @@
pass

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

# fmt: off
if QT_LIB == PYQT5:
from PyQt5 import QtCore, QtGui, QtWidgets as QtWid # type: ignore
from PyQt5.QtCore import pyqtSlot as Slot # type: ignore
Expand All @@ -67,9 +82,14 @@
elif QT_LIB == PYSIDE6:
from PySide6 import QtCore, QtGui, QtWidgets as QtWid # type: ignore
from PySide6.QtCore import Slot # type: ignore

# fmt: on
# pylint: enable=import-error, no-name-in-module

QT_VERSION = (
QtCore.QT_VERSION_STR if QT_LIB in (PYQT5, PYQT6) else QtCore.__version__
)
print(f"{QT_LIB} {QT_VERSION}")

# pylint: enable=import-error, no-name-in-module, c-extension-no-member
# \end[Mechanism to support both PyQt and PySide]
# -----------------------------------------------

Expand Down Expand Up @@ -105,22 +125,6 @@
# pg.setConfigOptions(leftButtonPan=False)
pg.setConfigOption("foreground", "#EEE")

# Constants
# fmt: off
DAQ_INTERVAL_MS = 10 # 10 [ms]
CHART_INTERVAL_MS = 20 # 20 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]
# fmt: on

# Global flags
USE_PC_TIME = True # Use Arduino time or PC time?
SIMULATE_ARDUINO = False # Simulate an Arduino, instead?
if sys.argv[-1] == "simulate":
SIMULATE_ARDUINO = True

# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
DEBUG = False


def get_current_date_time():
cur_date_time = QtCore.QDateTime.currentDateTime()
Expand Down
Loading

0 comments on commit 2a578da

Please sign in to comment.