1313import sys
1414from pathlib import Path
1515
16- import numpy as np
1716import time
17+ import signal # To catch CTRL+C and quit
1818
1919from PyQt5 import QtCore , QtWidgets as QtWid
2020from DvG_debug_functions import dprint , print_fancy_traceback as pft
2929# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
3030DEBUG = False
3131
32+
3233# ------------------------------------------------------------------------------
3334# Device state
3435# ------------------------------------------------------------------------------
@@ -40,38 +41,20 @@ class State(object):
4041 """
4142
4243 def __init__ (self ):
43- self .time = np . nan # [s]
44- self .reading_1 = np . nan
44+ self .time = None # [s]
45+ self .reading_1 = None
4546
4647
4748state = State ()
4849
4950
5051# ------------------------------------------------------------------------------
51- # MainWindow
52+ # Program termination routines
5253# ------------------------------------------------------------------------------
5354
5455
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-
72- # ------------------------------------------------------------------------------
73- # Program termination routines
74- # ------------------------------------------------------------------------------
56+ def keyboardInterruptHandler (signal , frame ):
57+ app .quit ()
7558
7659
7760@QtCore .pyqtSlot ()
@@ -132,9 +115,9 @@ def DAQ_function():
132115 else :
133116 state .time = time .perf_counter () - state .time_0
134117
135- # # For demo purposes: Quit automatically after 200 updates
136- # if qdev.update_counter_DAQ > 200 :
137- # app.quit()
118+ # For demo purposes: Quit automatically after N updates
119+ if qdev .update_counter_DAQ > 1000 :
120+ app .quit ()
138121
139122 return True
140123
@@ -157,12 +140,9 @@ def DAQ_function():
157140 sys .exit (0 )
158141
159142 # Create application
160- # app = QtCore.QCoreApplication(sys.argv)
161- app = QtWid .QApplication (sys .argv )
143+ app = QtCore .QCoreApplication (sys .argv )
162144 app .aboutToQuit .connect (about_to_quit )
163145
164- window = MainWindow ()
165-
166146 # Set up multithreaded communication with the Arduino
167147 qdev = QDeviceIO (ard )
168148 qdev .create_worker_DAQ (
@@ -183,6 +163,7 @@ def DAQ_function():
183163 # Start the main event loop
184164 # --------------------------------------------------------------------------
185165
186- # app.exec()
187- window .show ()
166+ # Catch CTRL+C
167+ signal .signal (signal .SIGINT , keyboardInterruptHandler )
168+
188169 sys .exit (app .exec_ ())
0 commit comments