From 7ec7ae73294f7cf75d1d10bcede44e9b25d30d92 Mon Sep 17 00:00:00 2001 From: atse Date: Mon, 26 Aug 2024 13:49:56 +0800 Subject: [PATCH] Put send_command in CtrlPanel --- pytec/pytec/gui/view/ctrl_panel.py | 42 ++++++++++++++++++++++++++++-- pytec/tec_qt.py | 37 ++------------------------ 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index 372a3f6..2ef9061 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -5,6 +5,7 @@ from pyqtgraph.parametertree import ( Parameter, registerParameterType, ) +from qasync import asyncSlot import pytec.gui.view.lockable_unit @@ -20,14 +21,17 @@ class CtrlPanel(QObject): def __init__( self, + thermostat, + autotuners, trees_ui, param_tree, - sigTreeStateChanged_handle, sigActivated_handles, parent=None, ): super().__init__(parent) + self.thermostat = thermostat + self.autotuners = autotuners self.trees_ui = trees_ui self.NUM_CHANNELS = len(trees_ui) @@ -53,7 +57,7 @@ class CtrlPanel(QObject): set_tree_label_tips(tree) for ch, param in enumerate(self.params): - param.sigTreeStateChanged.connect(sigTreeStateChanged_handle) + param.sigTreeStateChanged.connect(self.send_command) for handle in sigActivated_handles[ch]: param.child(*handle[0]).sigActivated.connect(handle[1]) @@ -85,6 +89,40 @@ class CtrlPanel(QObject): def change_params_title(self, channel, path, title): self.params[channel].child(*path).setOpts(title=title) + @asyncSlot(object, object) + async def send_command(self, param, changes): + """Translates parameter tree changes into thermostat set_param calls""" + ch = param.value() + + for inner_param, change, data in changes: + if change == "value": + new_value = data + if "thermostat:set_param" in inner_param.opts: + thermostat_param = inner_param.opts["thermostat:set_param"] + + # Handle thermostat command irregularities + match inner_param.name(), new_value: + case "rate", None: + thermostat_param = thermostat_param.copy() + thermostat_param["field"] = "off" + new_value = "" + case "control_method", "constant_current": + thermostat_param = thermostat_param.copy() + thermostat_param["field"] = "i_set" + new_value = inner_param.child("i_set").value() + case "control_method", "temperature_pid": + new_value = "" + + inner_param.setOpts(lock=True) + await self.thermostat.set_param( + channel=ch, value=new_value, **thermostat_param + ) + inner_param.setOpts(lock=False) + + if "pid_autotune" in inner_param.opts: + auto_tuner_param = inner_param.opts["pid_autotune"] + self.autotuners.set_params(auto_tuner_param, ch, new_value) + @pyqtSlot("QVariantList") def update_pid(self, pid_settings): for settings in pid_settings: diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index 0e257c3..68ea433 100755 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -95,9 +95,10 @@ class MainWindow(QtWidgets.QMainWindow): self.thermostat.info_box_trigger.connect(self.info_box.display_info_box) self.ctrl_panel_view = CtrlPanel( + self.thermostat, + self.autotuners, [self.ch0_tree, self.ch1_tree], get_ctrl_panel_config(args), - self.send_command, param_tree_sigActivated_handles, ) @@ -270,40 +271,6 @@ class MainWindow(QtWidgets.QMainWindow): await self._on_connection_changed(ThermostatConnectionState.DISCONNECTED) await self.thermostat.end_session() - @asyncSlot(object, object) - async def send_command(self, param, changes): - """Translates parameter tree changes into thermostat set_param calls""" - ch = param.value() - - for inner_param, change, data in changes: - if change == "value": - new_value = data - if "thermostat:set_param" in inner_param.opts: - thermostat_param = inner_param.opts["thermostat:set_param"] - - # Handle thermostat command irregularities - match inner_param.name(), new_value: - case "rate", None: - thermostat_param = thermostat_param.copy() - thermostat_param["field"] = "off" - new_value = "" - case "control_method", "constant_current": - thermostat_param = thermostat_param.copy() - thermostat_param["field"] = "i_set" - new_value = inner_param.child("i_set").value() - case "control_method", "temperature_pid": - new_value = "" - - inner_param.setOpts(lock=True) - await self.thermostat.set_param( - channel=ch, value=new_value, **thermostat_param - ) - inner_param.setOpts(lock=False) - - if "pid_autotune" in inner_param.opts: - auto_tuner_param = inner_param.opts["pid_autotune"] - self.autotuners.set_params(auto_tuner_param, ch, new_value) - @asyncSlot() async def pid_autotune_request(self, ch=0): match self.autotuners.get_state(ch):