From d6442077e429173ace109067b01c5f96c917e711 Mon Sep 17 00:00:00 2001 From: CPD Date: Mon, 17 Mar 2025 18:15:18 +0100 Subject: [PATCH] Add support for a usb power switch --- cpdctrl_gui/ui/main_window.py | 20 +++++++++++++++++++ .../ui/widgets/settings/app_settings.py | 2 ++ 2 files changed, 22 insertions(+) diff --git a/cpdctrl_gui/ui/main_window.py b/cpdctrl_gui/ui/main_window.py index e346b84..32c0155 100644 --- a/cpdctrl_gui/ui/main_window.py +++ b/cpdctrl_gui/ui/main_window.py @@ -33,10 +33,12 @@ from ..utility.config import AppConfig import cpdctrl.voltage_measurement_device as vmd import cpdctrl.led_control_device as ledd +from cpdctrl.power_switch_device.impl.cleware_switch import ClewareSwitch # TODO: this needs to be refactored at some point from cpdctrl.led_script import LedScript, InvalidScript from cpdctrl.utility.data import DataCollector from cpdctrl.measurement import measure + class MainWindow(QMainWindow): """ The main window of the app. @@ -69,6 +71,7 @@ class MainWindow(QMainWindow): self.a_open_about.setIcon(QIcon.fromTheme(get_resource_path("icons/icon.svg"))) self.a_open_about.triggered.connect(lambda: self.app_open_about()) + self.create_toolbars() self.setMenuBar(MenuBar(self)) # must come after toolbars self.setStatusBar(QStatusBar(self)) @@ -125,6 +128,10 @@ class MainWindow(QMainWindow): self.verbose = True # devices + exe = AppConfig.MAIN_CFG.get_or("power_switch_exe", "") + self.power_switch = None + if os.path.isfile(exe): + self.power_switch = ClewareSwitch(exe) self.vmdev = None self.leddev = None self.vmdev_autoconnect() @@ -163,6 +170,9 @@ class MainWindow(QMainWindow): self.topbar.add_button("meas_save", "Save", QIcon.fromTheme(QIcon.ThemeIcon.DocumentSaveAs), self.measurement_save) self.topbar.add_button("meas_load", "Load", QIcon.fromTheme(QIcon.ThemeIcon.DocumentOpen), self.measurement_load_dialog) self.topbar.add_separator() + self.topbar.add_button("power_on", "Power On", get_resource_path("icons/power_on.svg"), self.power_on) + self.topbar.add_button("power_off", "Power Off", get_resource_path("icons/power_off.svg"), self.power_off) + self.topbar.add_separator() self.topbar.add_button("app_settings", "Settings", QIcon.fromTheme(QIcon.ThemeIcon.DocumentProperties), self.app_open_settings) self.topbar.addAction(self.a_open_help) self.topbar.addAction(self.a_open_about) @@ -519,6 +529,16 @@ class MainWindow(QMainWindow): metadata = self.w_metadata.get_dict() self.command_queue.put(("metadata", metadata)) + def power_on(self): + if self.power_switch is None: + raise RuntimeError("No power switch configured") + self.power_switch.on() + + def power_off(self): + if self.power_switch is None: + raise RuntimeError("No power switch configured") + self.power_switch.off() + def app_exit(self) -> None: """ Closes the application. diff --git a/cpdctrl_gui/ui/widgets/settings/app_settings.py b/cpdctrl_gui/ui/widgets/settings/app_settings.py index 21626d5..ebab1b1 100644 --- a/cpdctrl_gui/ui/widgets/settings/app_settings.py +++ b/cpdctrl_gui/ui/widgets/settings/app_settings.py @@ -28,4 +28,6 @@ class AppSettings(QWidget): self.w_form.add_form_row("led_script_save_copy", "Copy Led Script to Cache Dir", True, QCheckBox(), "Save the final version of the led script in the cache directory") self.w_form.add_form_row("metadata_auto_update", "Auto-Update Metadata", True, QCheckBox(), "Allow metadata updates while the program is running") self.w_form.add_form_row("metadata_auto_add", "Auto-Add Metadata", True, QCheckBox(), "Automatically add measurement metadata to the data file.\nThis includes: device names, measurement mode, measurement interval, start and stop times, led script") + w_usb_switch_exe = FileSelection(filemode=QFileDialog.FileMode.ExistingFile) + self.w_form.add_form_row("power_switch_exe", "Power Switch Executable", "", w_usb_switch_exe, "Path to the USBSwitchCmd executable for the Cleware USB switch\nRequires a relaunch to take effect")