aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Scholten2025-10-14 13:29:53 +1000
committerSam Scholten2025-10-14 13:32:13 +1000
commit1c107939ed5799e4b43a494f7829e237ce2c6862 (patch)
tree243f1ed29140aa533ba8f0eed05d8876584f6eb5
parent62d4a04e2c2d6bff1a3ec9a90bc1a61927ef909f (diff)
downloadpicostream-1c107939ed5799e4b43a494f7829e237ce2c6862.tar.gz
picostream-1c107939ed5799e4b43a494f7829e237ce2c6862.zip
Prevent signal handler in GUI mode and improve thread cleanup
-rw-r--r--picostream/cli.py7
-rw-r--r--picostream/main.py14
2 files changed, 11 insertions, 10 deletions
diff --git a/picostream/cli.py b/picostream/cli.py
index fa72518..c902348 100644
--- a/picostream/cli.py
+++ b/picostream/cli.py
@@ -46,11 +46,13 @@ class Streamer:
downsample_mode: str = "average",
offset_v: float = 0.0,
max_buffer_seconds: Optional[float] = None,
+ is_gui_mode: bool = False,
) -> None:
# --- Configuration ---
self.output_file = output_file
self.debug = debug
self.enable_live_plot = enable_live_plot
+ self.is_gui_mode = is_gui_mode
self.plot_window_s = plot_window_s
self.max_buffer_seconds = max_buffer_seconds
@@ -212,8 +214,9 @@ class Streamer:
# --- Signal Handling ---
# Only set the signal handler if not in GUI mode.
- # In GUI mode, the main function will handle signals to quit the Qt app.
- if not self.enable_live_plot:
+ # In GUI mode, the main window handles shutdown signals.
+ # In CLI plot mode, the main() function handles SIGINT to quit the Qt app.
+ if not self.is_gui_mode and not self.enable_live_plot:
signal.signal(signal.SIGINT, self.signal_handler)
# --- Live Plotting (optional) ---
diff --git a/picostream/main.py b/picostream/main.py
index b2f03b4..b40c2c1 100644
--- a/picostream/main.py
+++ b/picostream/main.py
@@ -45,8 +45,8 @@ class StreamerWorker(QObject):
self.streamer.run()
except Exception as e:
self.error.emit(str(e))
- return # Do not emit finished signal on error
- self.finished.emit()
+ finally:
+ self.finished.emit()
def stop(self) -> None:
"""Signal the acquisition to stop."""
@@ -174,6 +174,7 @@ class PicoStreamMainWindow(QMainWindow):
if self.live_only_checkbox.isChecked()
else None,
"enable_live_plot": False,
+ "is_gui_mode": True,
}
self.thread = QThread()
@@ -196,7 +197,7 @@ class PicoStreamMainWindow(QMainWindow):
self.stop_button.setEnabled(False)
def on_acquisition_finished(self) -> None:
- """Handle successful acquisition completion."""
+ """Handle acquisition completion (both success and failure)."""
print("Acquisition finished.")
self.start_button.setEnabled(True)
self.stop_button.setEnabled(False)
@@ -206,11 +207,8 @@ class PicoStreamMainWindow(QMainWindow):
def on_acquisition_error(self, err_msg: str) -> None:
"""Handle acquisition error."""
print(f"Acquisition error: {err_msg}")
- # In a real app, show a QMessageBox
- self.start_button.setEnabled(True)
- self.stop_button.setEnabled(False)
- self.thread = None
- self.worker = None
+ # In a real app, this would show a QMessageBox.
+ # The UI state is reset in on_acquisition_finished.
def closeEvent(self, event: QCloseEvent) -> None:
"""Handle window close event."""