Skip to content

Commit 63cd5a9

Browse files
Update foo_bar__minimal.py
1 parent 974968a commit 63cd5a9

File tree

1 file changed

+59
-32
lines changed

1 file changed

+59
-32
lines changed

foo_bar__minimal.py

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,24 @@
77
__author__ = "Dennis van Gils"
88
__authoremail__ = "vangils.dennis@gmail.com"
99
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
10-
__date__ = "26-06-2020"
10+
__date__ = "28-06-2020"
1111
__version__ = "2.1"
1212

13-
import os
1413
import sys
1514
from pathlib import Path
1615

1716
import numpy as np
18-
import psutil
1917
import time
2018

21-
from PyQt5 import QtCore
19+
from PyQt5 import QtCore, QtWidgets as QtWid
2220
from DvG_debug_functions import dprint, print_fancy_traceback as pft
2321

2422
from DvG_dev_Arduino__fun_serial import Arduino # I.e. the `device`
2523
from DvG_QDeviceIO import QDeviceIO, DAQ_trigger
2624

2725
# Constants
2826
DAQ_INTERVAL = 10 # 10 [ms]
27+
TIMESTAMP_PC = True
2928

3029
# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
3130
DEBUG = False
@@ -41,13 +40,35 @@ class State(object):
4140
"""
4241

4342
def __init__(self):
44-
self.time = np.nan # [ms]
43+
self.time = np.nan # [s]
4544
self.reading_1 = np.nan
4645

4746

4847
state = State()
4948

5049

50+
# ------------------------------------------------------------------------------
51+
# MainWindow
52+
# ------------------------------------------------------------------------------
53+
54+
55+
class MainWindow(QtWid.QWidget):
56+
def __init__(self, parent=None, **kwargs):
57+
super().__init__(parent, **kwargs)
58+
59+
self.setGeometry(300, 300, 300, 100)
60+
self.setWindowTitle("Multithread PyQt & Arduino demo")
61+
62+
self.lbl = QtWid.QLabel("Press `Esc` to quit.")
63+
vbox = QtWid.QVBoxLayout(self)
64+
vbox.addWidget(self.lbl)
65+
66+
def keyPressEvent(self, event):
67+
if event.key() == QtCore.Qt.Key_Escape:
68+
app.quit()
69+
event.accept()
70+
71+
5172
# ------------------------------------------------------------------------------
5273
# Program termination routines
5374
# ------------------------------------------------------------------------------
@@ -56,31 +77,28 @@ def __init__(self):
5677
@QtCore.pyqtSlot()
5778
def notify_connection_lost():
5879
print("\nCRITICAL ERROR: Connection lost")
59-
exit_program()
80+
app.quit()
6081

6182

6283
@QtCore.pyqtSlot()
63-
def exit_program():
84+
def about_to_quit():
6485
print("\nAbout to quit")
65-
66-
app.processEvents()
67-
6886
qdev.quit()
6987
ard.close()
7088

71-
app.quit()
72-
7389

7490
# ------------------------------------------------------------------------------
75-
# update_CLI
91+
# update_terminal
7692
# ------------------------------------------------------------------------------
7793

7894

7995
@QtCore.pyqtSlot()
80-
def update_CLI():
96+
def update_terminal():
8197
print(
8298
"%i\t%.3f\t%.4f"
83-
% (qdev.update_counter_DAQ, state.time, state.reading_1)
99+
% (qdev.update_counter_DAQ - 1, state.time, state.reading_1),
100+
# end="\r",
101+
# flush=True,
84102
)
85103

86104

@@ -99,16 +117,24 @@ def DAQ_function():
99117
# Parse readings into separate state variables
100118
try:
101119
[state.time, state.reading_1] = tmp_state
120+
state.time /= 1000
102121
except Exception as err:
103122
pft(err, 3)
104123
dprint("'%s' reports IOError" % ard.name)
105124
return False
106125

107126
# Use Arduino time or PC time?
108127
# Arduino time is more accurate, but rolls over ~49 days for a 32 bit timer.
109-
use_PC_time = True
110-
if use_PC_time:
111-
state.time = time.perf_counter()
128+
if True:
129+
if qdev.update_counter_DAQ == 1:
130+
state.time_0 = time.perf_counter()
131+
state.time = 0
132+
else:
133+
state.time = time.perf_counter() - state.time_0
134+
135+
# # For demo purposes: Quit automatically after 200 updates
136+
# if qdev.update_counter_DAQ > 200:
137+
# app.quit()
112138

113139
return True
114140

@@ -131,31 +157,32 @@ def DAQ_function():
131157
sys.exit(0)
132158

133159
# Create application
134-
app = QtCore.QCoreApplication(sys.argv)
135-
app.aboutToQuit.connect(exit_program)
160+
# app = QtCore.QCoreApplication(sys.argv)
161+
app = QtWid.QApplication(sys.argv)
162+
app.aboutToQuit.connect(about_to_quit)
163+
164+
window = MainWindow()
136165

137166
# Set up multithreaded communication with the Arduino
138167
qdev = QDeviceIO(ard)
139168
qdev.create_worker_DAQ(
140-
DAQ_trigger = DAQ_trigger.INTERNAL_TIMER,
141-
DAQ_function = DAQ_function,
142-
DAQ_interval_ms = DAQ_INTERVAL,
143-
debug = DEBUG,)
169+
DAQ_trigger=DAQ_trigger.INTERNAL_TIMER,
170+
DAQ_function=DAQ_function,
171+
DAQ_interval_ms=DAQ_INTERVAL,
172+
debug=DEBUG,
173+
)
144174

145175
# Connect signals to slots
146-
qdev.signal_DAQ_updated.connect(update_CLI)
176+
qdev.signal_DAQ_updated.connect(update_terminal)
147177
qdev.signal_connection_lost.connect(notify_connection_lost)
148178

149179
# Start workers
150180
qdev.start()
151181

152182
# --------------------------------------------------------------------------
153-
# Start the main loop
183+
# Start the main event loop
154184
# --------------------------------------------------------------------------
155185

156-
while True:
157-
app.processEvents()
158-
159-
if qdev.update_counter_DAQ >= 20:
160-
exit_program()
161-
break
186+
# app.exec()
187+
window.show()
188+
sys.exit(app.exec_())

0 commit comments

Comments
 (0)