From 22a33c2b84b9051ae9adff9da8e65d172f83e819 Mon Sep 17 00:00:00 2001 From: Brendan Haines Date: Wed, 15 Jan 2025 22:14:27 -0700 Subject: [PATCH] some config stuff --- charon_vna/gui.py | 72 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/charon_vna/gui.py b/charon_vna/gui.py index 3a55ce7..653f53c 100644 --- a/charon_vna/gui.py +++ b/charon_vna/gui.py @@ -14,6 +14,8 @@ from numpy import typing as npt from PySide6.QtGui import QAction, QKeySequence from PySide6.QtWidgets import ( QApplication, + QFileDialog, + QInputDialog, QMainWindow, QMenu, QProgressBar, @@ -33,6 +35,8 @@ DEFAULT_CONFIG = dict( power=-5, # dB ) +CONFIG_SUFFIX = ".json" + class PlotWidget(QWidget): traces: List[Tuple[int | str]] @@ -157,6 +161,7 @@ class MainWindow(QMainWindow): super().__init__() self.config_path = None + self._frequency = np.linspace(1e9, 2e9, 101) # TODO: read frequency from config # self.device = Charon("ip:192.168.3.1", frequency=DEFAULT_CONFIG["frequency"]) @@ -169,10 +174,10 @@ class MainWindow(QMainWindow): menu_file = QMenu("&File") menubar.addMenu(menu_file) - action_load_config = QAction("&Open Configuration", self) - menu_file.addAction(action_load_config) - action_load_config.triggered.connect(self.load_config) - action_load_config.setShortcut(QKeySequence("Ctrl+O")) + action_open_config = QAction("&Open Configuration", self) + menu_file.addAction(action_open_config) + action_open_config.triggered.connect(self.open_config) + action_open_config.setShortcut(QKeySequence("Ctrl+O")) action_save_config = QAction("&Save Configuration", self) menu_file.addAction(action_save_config) action_save_config.triggered.connect(self.save_config) @@ -186,7 +191,7 @@ class MainWindow(QMainWindow): menubar.addMenu(menu_stimulus) action_set_frequency = QAction("&Frequency", self) menu_stimulus.addAction(action_set_frequency) - # action_set_frequency.triggered.connect(self.set_frequency) + action_set_frequency.triggered.connect(self.set_frequency) action_set_power = QAction("&Power", self) menu_stimulus.addAction(action_set_power) # action_set_power.triggered.connect(self.set_power) @@ -230,24 +235,51 @@ class MainWindow(QMainWindow): def saveas_config(self) -> None: print("Prompting for save path...") - # TODO: prompt for config path - self.config_path = Path(__file__).parent / "config.json" - print(f"Config path is now {self.config_path.resolve()}") - self.save_config() + dialog = QFileDialog(self) + dialog.setDefaultSuffix(CONFIG_SUFFIX) + dialog.setAcceptMode(QFileDialog.AcceptMode.AcceptSave) + if dialog.exec(): + config_path = Path(dialog.selectedFiles()[0]) + print(config_path) + if config_path.suffix != CONFIG_SUFFIX: + raise ValueError( + f"{config_path.name} is not a valid configuration file. Must have extension {CONFIG_SUFFIX}" + ) + self.config_path = config_path + print(f"Config path is now {self.config_path.resolve()}") + + self.save_config() + + def open_config(self) -> None: + print("Prompting for load path...") + dialog = QFileDialog(self) + dialog.setNameFilter(f"*{CONFIG_SUFFIX}") + dialog.setAcceptMode(QFileDialog.AcceptMode.AcceptOpen) + if dialog.exec(): + config_path = Path(dialog.selectedFiles()[0]) + print(config_path) + if config_path.suffix != CONFIG_SUFFIX: + raise ValueError( + f"{config_path.name} is not a valid configuration file. Must have extension {CONFIG_SUFFIX}" + ) + self.config_path = config_path + print(f"Config path is now {self.config_path.resolve()}") + + self.load_config(self.config_path) def save_config(self) -> None: if self.config_path is None: self.saveas_config() else: - print(f"saving config to {self.config_path.resolve()}") + print(f"Saving config to {self.config_path.resolve()}") # TODO: save config - def load_config(self) -> None: - print("loading config") + def load_config(self, path: Path) -> None: + print(f"Loading config from {path}...") # TODO: load config def generate_sim_data(self) -> None: - coords = {"frequency": np.linspace(1e9, 2e9, 101), "m": [1], "n": [1]} + coords = {"frequency": self._frequency, "m": [1], "n": [1]} shape = tuple(len(v) for v in coords.values()) data = xr.DataArray( ((-1 + 2 * np.random.rand(*shape)) + 1j * (-1 + 2 * np.random.rand(*shape))) / np.sqrt(2), @@ -258,6 +290,20 @@ class MainWindow(QMainWindow): for plot in self.plots: plot.update_plot(data) + def set_frequency(self, *, frequency: npt.ArrayLike | None = None): + print(frequency) + if frequency is None: + start, ok = QInputDialog.getDouble( + self, "Start Frequency", "Start Frequency", minValue=30e6, maxValue=6e9, value=1e9 + ) + stop, ok = QInputDialog.getDouble( + self, "Stop Frequency", "Stop Frequency", minValue=30e6, maxValue=6e9, value=2e9 + ) + points, ok = QInputDialog.getInt(self, "Points", "Points", minValue=2, value=101) + frequency = np.linspace(start, stop, points) + # Currently does not support zero span + self._frequency = frequency + def main() -> None: app = QApplication(sys.argv)