diff --git a/pyrelacs/app.py b/pyrelacs/app.py index 775a276..6e8d799 100644 --- a/pyrelacs/app.py +++ b/pyrelacs/app.py @@ -3,7 +3,7 @@ import sys import pathlib import ctypes -from PyQt6.QtCore import QSize, QThreadPool, Qt +from PyQt6.QtCore import QProcess, QSize, QThreadPool, Qt from PyQt6.QtWidgets import ( QApplication, QGridLayout, @@ -11,6 +11,7 @@ from PyQt6.QtWidgets import ( QToolBar, QWidget, QMainWindow, + QPlainTextEdit, ) import tomli import uldaq @@ -27,7 +28,11 @@ class PyRelacs(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("PyRelacs") + self.setMinimumSize(1000, 1000) + self.threadpool = QThreadPool() + # for starting a Qprocess + self.p = None self.daq_connect_button = QPushButton("Connect Daq") self.daq_connect_button.setCheckable(True) @@ -37,17 +42,17 @@ class PyRelacs(QMainWindow): self.daq_disconnect_button.setCheckable(True) self.daq_disconnect_button.clicked.connect(self.disconnect_dac) - self.repro_button = QPushButton("Load Repros") - self.repro_button.setCheckable(True) - self.repro_button.clicked.connect(self.repro) + self.text = QPlainTextEdit() + self.text.setReadOnly(True) layout = QGridLayout() layout.addWidget(self.daq_connect_button, 0, 0) layout.addWidget(self.daq_disconnect_button, 0, 1) - layout.addWidget(self.repro_button, 1, 0) + layout.addWidget(self.text, 2, 0, 1, 2) self.toolbar = QToolBar("Repros") self.addToolBar(self.toolbar) + self.repro() self.setFixedSize(QSize(400, 300)) widget = QWidget() @@ -78,70 +83,59 @@ class PyRelacs(QMainWindow): log.debug("DAQ was not connected") def repro(self): - config_path = pathlib.Path(__file__).parent.resolve() / "repro.toml" - if not config_path.is_file: - log.error("repro.toml not found") - QApplication.quit() - repors = [] - - with open(config_path, mode="rb") as fp: - self.config = tomli.load(fp) - for r in self.config: - repors.append(r) - - individual_repro_button = QAction(f"{repors[0]}", self) - individual_repro_button.setStatusTip("Button") - individual_repro_button.triggered.connect(self.run_repro) - self.toolbar.addAction(individual_repro_button) - - def run_repro(self): - daq_thread = Worker(self.read_daq) - self.threadpool.start(daq_thread) - - def on_scan_finished(self): - log.debug("Scan finished") - - def read_daq(self, progress_callback): - log.debug("running repro") - time = np.arange(0, 10, 1 / 30_000.0) - data = 2 * np.sin(2 * np.pi * 10 * time) - - buffer = ctypes.c_double * len(time) - buffer_ai = uldaq.create_float_buffer(1, len(time)) - data_c = buffer(*data) - log.debug(f"Created C_double data {data_c}") - - ao_device = self.daq_device.get_ao_device() - ai_device = self.daq_device.get_ai_device() - - er = ai_device.a_in_scan( - 1, - 1, - uldaq.AiInputMode.SINGLE_ENDED, - uldaq.Range.BIP10VOLTS, - int(len(data)), - 30_000.0, - uldaq.ScanOption.EXTTRIGGER, - uldaq.AInScanFlag.DEFAULT, - data=buffer_ai, - ) - # ai_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, timeout=-1) - - err = ao_device.a_out_scan( - 0, - 0, - uldaq.Range.BIP10VOLTS, - int(len(data)), - 30_000.0, - uldaq.ScanOption.DEFAULTIO, - uldaq.AOutScanFlag.DEFAULT, - data_c, - ) - # INFO: Need to wait for the acquistion - self.daq_disconnect_button.setDisabled(True) - self.daq_connect_button.setEnabled(True) - embed() - exit() + repos_path = pathlib.Path(__file__).parent / "repros" + repos_names = list(repos_path.glob("*.py")) + # exclude the repos.py file + repos_names = [ + f.with_suffix("").name for f in repos_names if not f.name == "repos.py" + ] + for rep in repos_names: + individual_repro_button = QAction(rep, self) + individual_repro_button.setStatusTip("Button") + individual_repro_button.triggered.connect( + lambda checked, n=rep: self.run_repro(n) + ) + self.toolbar.addAction(individual_repro_button) + + def message(self, s): + self.text.appendPlainText(s) + + def run_repro(self, name_of_repo): + if self.p is None: + self.message(f"Executing process {name_of_repo}") + self.p = QProcess() + self.p.setWorkingDirectory(str(pathlib.Path(__file__).parent / "repros/")) + # log.debug(pathlib.Path(__file__).parent / "repos") + self.p.readyReadStandardOutput.connect(self.handle_stdout) + self.p.readyReadStandardError.connect(self.handle_stderr) + self.p.stateChanged.connect(self.handle_state) + self.p.finished.connect(self.process_finished) + self.p.start("python3", [f"{name_of_repo}" + ".py"]) + + def handle_stderr(self): + if self.p is not None: + data = self.p.readAllStandardError() + stderr = bytes(data).decode("utf8") + self.message(stderr) + + def handle_stdout(self): + if self.p is not None: + data = self.p.readAllStandardOutput() + stdout = bytes(data).decode("utf8") + self.message(stdout) + + def handle_state(self, state): + states = { + QProcess.ProcessState.NotRunning: "Not running", + QProcess.ProcessState.Starting: "Starting", + QProcess.ProcessState.Running: "Running", + } + state_name = states[state] + self.message(f"State changed: {state_name}") + + def process_finished(self): + self.text.appendPlainText("Process finished") + self.p = None if __name__ == "__main__":