Skip to content

Commit d9a4457

Browse files
Bump dependencies & removed global date-time variables
1 parent a414a57 commit d9a4457

7 files changed

+110
-98
lines changed

Arduino_PyQt_demo_with_multithreading.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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__ = "16-07-2020"
9+
__date__ = "17-07-2020"
1010
__version__ = "4.1"
1111
# pylint: disable=bare-except, broad-except
1212

@@ -22,15 +22,16 @@
2222
from PyQt5.QtCore import QDateTime
2323
import pyqtgraph as pg
2424

25+
from dvg_debug_functions import dprint, print_fancy_traceback as pft
26+
from dvg_devices.Arduino_protocol_serial import Arduino
27+
from dvg_qdeviceio import QDeviceIO
28+
2529
from DvG_pyqt_FileLogger import FileLogger
2630
from DvG_pyqt_ChartHistory import ChartHistory
2731
from DvG_pyqt_controls import create_Toggle_button, SS_GROUP
28-
from dvg_debug_functions import dprint, print_fancy_traceback as pft
29-
30-
from dvg_devices.Arduino_protocol_serial import Arduino # I.e. the `device`
31-
from dvg_qdeviceio import QDeviceIO
3232

3333
try:
34+
# To enable OpenGL GPU acceleration:
3435
# `conda install pyopengl` or `pip install pyopengl`
3536
import OpenGL.GL as gl # pylint: disable=unused-import
3637
except:
@@ -45,16 +46,21 @@
4546
DAQ_INTERVAL_MS = 10 # 10 [ms]
4647
CHART_INTERVAL_MS = 10 # 10 [ms]
4748
CHART_HISTORY_TIME = 10 # 10 [s]
48-
49-
# Global variables for date-time keeping
50-
cur_date_time = QDateTime.currentDateTime()
51-
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
52-
str_cur_time = cur_date_time.toString("HH:mm:ss")
5349
# fmt: on
5450

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

54+
55+
def get_current_date_time():
56+
cur_date_time = QDateTime.currentDateTime()
57+
return (
58+
cur_date_time.toString("dd-MM-yyyy"), # Date
59+
cur_date_time.toString("HH:mm:ss"), # Time
60+
cur_date_time.toString("yyMMdd_HHmmss"), # Reverse notation date-time
61+
)
62+
63+
5864
# ------------------------------------------------------------------------------
5965
# Arduino state
6066
# ------------------------------------------------------------------------------
@@ -148,11 +154,11 @@ def __init__(self, parent=None, **kwargs):
148154
self.gw_chart.setBackground([20, 20, 20])
149155
self.pi_chart = self.gw_chart.addPlot()
150156

151-
p = {"color": "#BBB", "font-size": "10pt"}
157+
p = {"color": "#CCC", "font-size": "10pt"}
152158
self.pi_chart.showGrid(x=1, y=1)
153159
self.pi_chart.setTitle("Arduino timeseries", **p)
154160
self.pi_chart.setLabel("bottom", text="history (sec)", **p)
155-
self.pi_chart.setLabel("left", text="readings", **p)
161+
self.pi_chart.setLabel("left", text="amplitude", **p)
156162
self.pi_chart.setRange(
157163
xRange=[-1.04 * CHART_HISTORY_TIME, CHART_HISTORY_TIME * 0.04],
158164
yRange=[-1.1, 1.1],
@@ -283,6 +289,7 @@ def set_text_qpbt_record(self, text_str):
283289

284290
@QtCore.pyqtSlot()
285291
def update_GUI():
292+
str_cur_date, str_cur_time, _ = get_current_date_time()
286293
window.qlbl_cur_date_time.setText("%s %s" % (str_cur_date, str_cur_time))
287294
window.qlbl_update_counter.setText("%i" % qdev_ard.update_counter_DAQ)
288295
window.qlbl_DAQ_rate.setText("DAQ: %.1f Hz" % qdev_ard.obtained_DAQ_rate_Hz)
@@ -328,9 +335,10 @@ def stop_running():
328335
def notify_connection_lost():
329336
stop_running()
330337

331-
excl = " ! ! ! ! ! ! ! "
338+
excl = " ! ! ! ! ! ! "
332339
window.qlbl_title.setText("%sLOST CONNECTION%s" % (excl, excl))
333340

341+
str_cur_date, str_cur_time, _ = get_current_date_time()
334342
str_msg = ("%s %s\n" "Lost connection to Arduino.\n" " '%s': %salive") % (
335343
str_cur_date,
336344
str_cur_time,
@@ -360,10 +368,7 @@ def about_to_quit():
360368

361369
def DAQ_function():
362370
# Date-time keeping
363-
global cur_date_time, str_cur_date, str_cur_time
364-
cur_date_time = QDateTime.currentDateTime()
365-
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
366-
str_cur_time = cur_date_time.toString("HH:mm:ss")
371+
str_cur_date, str_cur_time, str_cur_datetime = get_current_date_time()
367372

368373
# Query the Arduino for its state
369374
success, tmp_state = ard.query_ascii_values("?", delimiter="\t")
@@ -396,7 +401,7 @@ def DAQ_function():
396401

397402
# Logging to file
398403
if file_logger.starting:
399-
fn_log = cur_date_time.toString("yyMMdd_HHmmss") + ".txt"
404+
fn_log = str_cur_datetime + ".txt"
400405
if file_logger.create_log(state.time, fn_log, mode="w"):
401406
file_logger.signal_set_recording_text.emit(
402407
"Recording to file: " + fn_log
@@ -480,9 +485,7 @@ def DAQ_function():
480485
debug = DEBUG,
481486
)
482487
# fmt: on
483-
484488
qdev_ard.create_worker_jobs(debug=DEBUG)
485-
# """
486489

487490
# Connect signals to slots
488491
qdev_ard.signal_DAQ_updated.connect(update_GUI)

Arduino_PyQt_demo_with_multithreading__LARGE_TEXT.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
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__ = "16-07-2020"
10-
__version__ = "4.0"
9+
__date__ = "17-07-2020"
10+
__version__ = "4.1"
11+
# pylint: disable=bare-except, broad-except
1112

1213
import os
1314
import sys
@@ -21,29 +22,34 @@
2122
from PyQt5.QtCore import QDateTime
2223
import pyqtgraph as pg
2324

24-
from DvG_pyqt_FileLogger import FileLogger
25-
from DvG_pyqt_ChartHistory import ChartHistory
26-
from DvG_pyqt_controls import create_Toggle_button
2725
from dvg_debug_functions import dprint, print_fancy_traceback as pft
28-
2926
from dvg_devices.Arduino_protocol_serial import Arduino
3027
from dvg_qdeviceio import QDeviceIO
3128

29+
from DvG_pyqt_FileLogger import FileLogger
30+
from DvG_pyqt_ChartHistory import ChartHistory
31+
from DvG_pyqt_controls import create_Toggle_button
32+
3233
# Constants
3334
# fmt: off
34-
DAQ_INTERVAL_MS = 10 # 10 [ms]
35-
DRAW_INTERVAL_CHART = 10 # 10 [ms]
36-
CHART_HISTORY_TIME = 10 # 10 [s]
37-
38-
# Global variables for date-time keeping
39-
cur_date_time = QDateTime.currentDateTime()
40-
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
41-
str_cur_time = cur_date_time.toString("HH:mm:ss")
35+
DAQ_INTERVAL_MS = 10 # 10 [ms]
36+
CHART_INTERVAL_MS = 10 # 10 [ms]
37+
CHART_HISTORY_TIME = 10 # 10 [s]
4238
# fmt: on
4339

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

43+
44+
def get_current_date_time():
45+
cur_date_time = QDateTime.currentDateTime()
46+
return (
47+
cur_date_time.toString("dd-MM-yyyy"), # Date
48+
cur_date_time.toString("HH:mm:ss"), # Time
49+
cur_date_time.toString("yyMMdd_HHmmss"), # Reverse notation date-time
50+
)
51+
52+
4753
# ------------------------------------------------------------------------------
4854
# Arduino state
4955
# ------------------------------------------------------------------------------
@@ -304,6 +310,7 @@ def set_text_qpbt_record(self, text_str):
304310

305311
@QtCore.pyqtSlot()
306312
def update_GUI():
313+
str_cur_date, str_cur_time, _ = get_current_date_time()
307314
window.qlbl_cur_date_time.setText("%s %s" % (str_cur_date, str_cur_time))
308315
window.qlbl_update_counter.setText("%i" % qdev_ard.update_counter_DAQ)
309316
window.qlbl_DAQ_rate.setText("DAQ: %.1f Hz" % qdev_ard.obtained_DAQ_rate_Hz)
@@ -326,8 +333,7 @@ def update_chart():
326333
if DEBUG:
327334
dprint(
328335
" update_curve done in %.2f ms"
329-
% (time.perf_counter() - tick)
330-
* 1000
336+
% ((time.perf_counter() - tick) * 1000)
331337
)
332338

333339

@@ -350,9 +356,10 @@ def stop_running():
350356
def notify_connection_lost():
351357
stop_running()
352358

353-
excl = " ! ! ! ! ! ! ! ! "
359+
excl = " ! ! ! ! ! ! "
354360
window.qlbl_title.setText("%sLOST CONNECTION%s" % (excl, excl))
355361

362+
str_cur_date, str_cur_time, _ = get_current_date_time()
356363
str_msg = ("%s %s\n" "Lost connection to Arduino.\n" " '%s': %salive") % (
357364
str_cur_date,
358365
str_cur_time,
@@ -382,10 +389,7 @@ def about_to_quit():
382389

383390
def DAQ_function():
384391
# Date-time keeping
385-
global cur_date_time, str_cur_date, str_cur_time
386-
cur_date_time = QDateTime.currentDateTime()
387-
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
388-
str_cur_time = cur_date_time.toString("HH:mm:ss")
392+
str_cur_date, str_cur_time, str_cur_datetime = get_current_date_time()
389393

390394
# Query the Arduino for its state
391395
success, tmp_state = ard.query_ascii_values("?", delimiter="\t")
@@ -418,7 +422,7 @@ def DAQ_function():
418422

419423
# Logging to file
420424
if file_logger.starting:
421-
fn_log = cur_date_time.toString("yyMMdd_HHmmss") + ".txt"
425+
fn_log = str_cur_datetime + ".txt"
422426
if file_logger.create_log(state.time, fn_log, mode="w"):
423427
file_logger.signal_set_recording_text.emit(
424428
"Recording to file: " + fn_log
@@ -500,9 +504,8 @@ def DAQ_function():
500504
DAQ_interval_ms = DAQ_INTERVAL_MS,
501505
critical_not_alive_count = 3,
502506
debug = DEBUG,
503-
)
507+
)
504508
# fmt: on
505-
506509
qdev_ard.create_worker_jobs(debug=DEBUG)
507510

508511
# Connect signals to slots
@@ -518,7 +521,7 @@ def DAQ_function():
518521

519522
timer_chart = QtCore.QTimer()
520523
timer_chart.timeout.connect(update_chart)
521-
timer_chart.start(DRAW_INTERVAL_CHART)
524+
timer_chart.start(CHART_INTERVAL_MS)
522525

523526
# --------------------------------------------------------------------------
524527
# Start the main GUI event loop

Arduino_PyQt_demo_with_multithreading__minimalistic.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
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__ = "16-07-2020"
10-
__version__ = "4.0"
9+
__date__ = "17-07-2020"
10+
__version__ = "4.1"
11+
# pylint: disable=bare-except, broad-except
1112

1213
import os
1314
import sys
@@ -21,27 +22,32 @@
2122
from PyQt5.QtCore import QDateTime
2223
import pyqtgraph as pg
2324

24-
from DvG_pyqt_ChartHistory import ChartHistory
2525
from dvg_debug_functions import dprint, print_fancy_traceback as pft
26-
2726
from dvg_devices.Arduino_protocol_serial import Arduino
2827
from dvg_qdeviceio import QDeviceIO
2928

29+
from DvG_pyqt_ChartHistory import ChartHistory
30+
3031
# Constants
3132
# fmt: off
32-
DAQ_INTERVAL_MS = 10 # 10 [ms]
33-
DRAW_INTERVAL_CHART = 10 # 10 [ms]
34-
CHART_HISTORY_TIME = 10 # 10 [s]
35-
36-
# Global variables for date-time keeping
37-
cur_date_time = QDateTime.currentDateTime()
38-
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
39-
str_cur_time = cur_date_time.toString("HH:mm:ss")
33+
DAQ_INTERVAL_MS = 10 # 10 [ms]
34+
CHART_INTERVAL_MS = 10 # 10 [ms]
35+
CHART_HISTORY_TIME = 10 # 10 [s]
4036
# fmt: on
4137

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

41+
42+
def get_current_date_time():
43+
cur_date_time = QDateTime.currentDateTime()
44+
return (
45+
cur_date_time.toString("dd-MM-yyyy"), # Date
46+
cur_date_time.toString("HH:mm:ss"), # Time
47+
cur_date_time.toString("yyMMdd_HHmmss"), # Reverse notation date-time
48+
)
49+
50+
4551
# ------------------------------------------------------------------------------
4652
# Arduino state
4753
# ------------------------------------------------------------------------------
@@ -82,11 +88,11 @@ def __init__(self, parent=None, **kwargs):
8288
self.gw_chart.setBackground([20, 20, 20])
8389
self.pi_chart = self.gw_chart.addPlot()
8490

85-
p = {"color": "#BBB", "font-size": "10pt"}
91+
p = {"color": "#CCC", "font-size": "10pt"}
8692
self.pi_chart.showGrid(x=1, y=1)
8793
self.pi_chart.setTitle("Arduino timeseries", **p)
8894
self.pi_chart.setLabel("bottom", text="history (sec)", **p)
89-
self.pi_chart.setLabel("left", text="readings", **p)
95+
self.pi_chart.setLabel("left", text="amplitude", **p)
9096
self.pi_chart.setRange(
9197
xRange=[-1.04 * CHART_HISTORY_TIME, CHART_HISTORY_TIME * 0.04],
9298
yRange=[-1.1, 1.1],
@@ -127,10 +133,7 @@ def about_to_quit():
127133

128134
def DAQ_function():
129135
# Date-time keeping
130-
global cur_date_time, str_cur_date, str_cur_time
131-
cur_date_time = QDateTime.currentDateTime()
132-
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
133-
str_cur_time = cur_date_time.toString("HH:mm:ss")
136+
str_cur_date, str_cur_time, str_cur_datetime = get_current_date_time()
134137

135138
# Query the Arduino for its state
136139
success, tmp_state = ard.query_ascii_values("?", delimiter="\t")
@@ -154,7 +157,7 @@ def DAQ_function():
154157
return False
155158

156159
# Use Arduino time or PC time?
157-
use_PC_time = False
160+
use_PC_time = True
158161
if use_PC_time:
159162
state.time = time.perf_counter()
160163

@@ -230,8 +233,8 @@ def DAQ_function():
230233
# --------------------------------------------------------------------------
231234

232235
timer_chart = QtCore.QTimer()
233-
timer_chart.timeout.connect(lambda: window.CH_1.update_curve())
234-
timer_chart.start(DRAW_INTERVAL_CHART)
236+
timer_chart.timeout.connect(window.CH_1.update_curve)
237+
timer_chart.start(CHART_INTERVAL_MS)
235238

236239
# --------------------------------------------------------------------------
237240
# Start the main GUI event loop

0 commit comments

Comments
 (0)