-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdemo_D_no_GUI.py
More file actions
175 lines (142 loc) · 5.33 KB
/
demo_D_no_GUI.py
File metadata and controls
175 lines (142 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Demonstration of multithreaded live Arduino data
- Terminal output only
- Mode: INTERNAL_TIMER
"""
__author__ = "Dennis van Gils"
__authoremail__ = "vangils.dennis@gmail.com"
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
__date__ = "11-06-2024"
__version__ = "9.0"
# pylint: disable=missing-function-docstring
import os
import sys
import time
import datetime
import signal # To catch CTRL+C and quit
import qtpy
from qtpy import QtCore
from qtpy.QtCore import Slot # type: ignore
import psutil
from WaveGeneratorArduino import WaveGeneratorArduino, FakeWaveGeneratorArduino
from WaveGeneratorArduino_qdev import WaveGeneratorArduino_qdev
# Constants
DAQ_INTERVAL_MS = 10
"""[ms] Update interval for the data acquisition (DAQ)"""
# Global flags
USE_PC_TIME = True
"""Use Arduino time or PC time?"""
SIMULATE_ARDUINO = False
"""Simulate an Arduino in software?"""
if sys.argv[-1] == "simulate":
SIMULATE_ARDUINO = True
DEBUG = False
"""Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
"""
print(
f"{qtpy.API_NAME:9s} "
f"{qtpy.QT_VERSION}" # pyright: ignore[reportPrivateImportUsage]
)
# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":
# Set priority of this process to maximum in the operating system
print(f"PID: {os.getpid()}\n")
try:
proc = psutil.Process(os.getpid())
if os.name == "nt":
proc.nice(psutil.REALTIME_PRIORITY_CLASS) # Windows
else:
proc.nice(-20) # Other
except Exception: # pylint: disable=broad-except
print("Warning: Could not set process to maximum priority.\n")
# --------------------------------------------------------------------------
# Connect to Arduino
# --------------------------------------------------------------------------
if SIMULATE_ARDUINO:
ard = FakeWaveGeneratorArduino()
else:
ard = WaveGeneratorArduino()
ard.serial_settings["baudrate"] = 115200
ard.auto_connect()
if not ard.is_alive:
print("\nCheck connection and try resetting the Arduino.")
print("Exiting...\n")
sys.exit(0)
# --------------------------------------------------------------------------
# Create application
# --------------------------------------------------------------------------
app = QtCore.QCoreApplication(sys.argv)
# --------------------------------------------------------------------------
# Set up multithreaded communication with the Arduino
# --------------------------------------------------------------------------
def DAQ_function() -> bool:
"""Perform a single data acquisition.
Returns: True if successful, False otherwise.
"""
# Query the Arduino for new readings, parse them and update the
# corresponding variables of its `state` member.
if not ard.perform_DAQ():
return False
# Use Arduino time or PC time?
now = time.perf_counter() if USE_PC_TIME else ard.state.time
if ard_qdev.update_counter_DAQ == 1:
ard.state.time_0 = now
ard.state.time = 0
else:
ard.state.time = now - ard.state.time_0
# For demo purposes: Quit automatically after N updates
if ard_qdev.update_counter_DAQ > 1000:
app.quit()
return True
ard_qdev = WaveGeneratorArduino_qdev(
dev=ard,
DAQ_function=DAQ_function,
DAQ_interval_ms=DAQ_INTERVAL_MS,
debug=DEBUG,
)
# --------------------------------------------------------------------------
# update_terminal
# --------------------------------------------------------------------------
@Slot()
def update_terminal():
print(
f"{ard_qdev.update_counter_DAQ - 1}\t"
f"{ard.state.time:.3f}\t"
f"{ard.state.reading_1:.4f}",
# end="\r",
# flush=True,
)
# --------------------------------------------------------------------------
# Program termination routines
# --------------------------------------------------------------------------
def keyboardInterruptHandler(
keysig, frame
): # pylint: disable=unused-argument
app.quit()
# Catch CTRL+C
signal.signal(signal.SIGINT, keyboardInterruptHandler)
@Slot()
def notify_connection_lost():
str_msg = (
f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
"Lost connection to Arduino."
)
print(f"\nCRITICAL ERROR @ {str_msg}")
app.quit()
@Slot()
def about_to_quit():
app.processEvents()
print("\nAbout to quit")
ard_qdev.quit()
ard.close()
# --------------------------------------------------------------------------
# Start the main event loop
# --------------------------------------------------------------------------
ard_qdev.signal_DAQ_updated.connect(update_terminal)
ard_qdev.signal_connection_lost.connect(notify_connection_lost)
ard_qdev.start(DAQ_priority=QtCore.QThread.Priority.TimeCriticalPriority)
app.aboutToQuit.connect(about_to_quit)
sys.exit(app.exec())