From aae89256c3e3bd081ae05d7d19dd4546913b4d76 Mon Sep 17 00:00:00 2001 From: linuswck Date: Wed, 24 Jul 2024 12:19:21 +0800 Subject: [PATCH] gui: fix ctrl_panel value getting overwritten bug --- pykirdy/kirdy_qt.py | 115 ++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 68 deletions(-) diff --git a/pykirdy/kirdy_qt.py b/pykirdy/kirdy_qt.py index 4865622..d8129ab 100644 --- a/pykirdy/kirdy_qt.py +++ b/pykirdy/kirdy_qt.py @@ -449,6 +449,11 @@ class MainWindow(QtWidgets.QMainWindow): self._set_up_ctrl_btns() self._set_up_plot_menu() + def _setValuewithLock(self, value): + if not self.opts.get("lock", None): + self.setValue(value) + Parameter.setValuewithLock = _setValuewithLock + self.params = [ Parameter.create(name=f"Laser Diode Status", type='group', value=0, children=self.LASER_DIODE_STATUS), Parameter.create(name=f"Laser Diode Parameters", type='group', value=1, children=self.LASER_DIODE_PARAMETERS), @@ -606,32 +611,6 @@ class MainWindow(QtWidgets.QMainWindow): self.plot_settings.setMenu(self.plot_menu) def _set_param_tree(self): - def _setValue(self, value, blockSignal=None): - """ - Implement 'lock' mechanism for Parameter Type - - Modified from the source - """ - try: - if blockSignal is not None: - self.sigValueChanged.disconnect(blockSignal) - value = self._interpretValue(value) - if fn.eq(self.opts["value"], value): - return value - - if "lock" in self.opts.keys(): - if self.opts["lock"]: - return value - self.opts["value"] = value - self.sigValueChanged.emit( - self, value - ) # value might change after signal is received by tree item - finally: - if blockSignal is not None: - self.sigValueChanged.connect(blockSignal) - - return self.opts["value"] - status = self.ld_status status.setHeaderHidden(True) status.setParameters(self.params[0], showTop=False) @@ -640,7 +619,6 @@ class MainWindow(QtWidgets.QMainWindow): tree.setHeaderHidden(True) tree.setParameters(self.params[1], showTop=False) self.params[1].sigTreeStateChanged.connect(self.send_command) - self.params[1].setValue = _setValue status = self.tec_status status.setHeaderHidden(True) @@ -650,7 +628,6 @@ class MainWindow(QtWidgets.QMainWindow): tree.setHeaderHidden(True) tree.setParameters(self.params[3], showTop=False) self.params[3].sigTreeStateChanged.connect(self.send_command) - self.params[3].setValue = _setValue @asyncSlot() async def autotune(param): @@ -794,16 +771,16 @@ class MainWindow(QtWidgets.QMainWindow): try: settings = settings['laser'] with QSignalBlocker(self.params[1]): - self.params[1].child('Output Config', 'LD Current Set').setValue(settings["ld_drive_current"]['value']) - self.params[1].child('Output Config', 'LD Current Set Soft Limit').setValue(settings["ld_drive_current_limit"]['value']) - self.params[1].child('Output Config', 'LD Terminals Short').setValue(settings["ld_terms_short"]) - self.params[1].child('Output Config', 'Default Power On').setValue(settings["default_pwr_on"]) - self.params[1].child('Photodiode Monitor Config', 'LD Power Limit').setValue(settings["ld_pwr_limit"]) + self.params[1].child('Output Config', 'LD Current Set').setValuewithLock(settings["ld_drive_current"]['value']) + self.params[1].child('Output Config', 'LD Current Set Soft Limit').setValuewithLock(settings["ld_drive_current_limit"]['value']) + self.params[1].child('Output Config', 'LD Terminals Short').setValuewithLock(settings["ld_terms_short"]) + self.params[1].child('Output Config', 'Default Power On').setValuewithLock(settings["default_pwr_on"]) + self.params[1].child('Photodiode Monitor Config', 'LD Power Limit').setValuewithLock(settings["ld_pwr_limit"]) if settings["pd_mon_params"]["responsitivity"] is not None: - self.params[1].child('Photodiode Monitor Config', 'Responsitivity').setValue(settings["pd_mon_params"]["responsitivity"]) + self.params[1].child('Photodiode Monitor Config', 'Responsitivity').setValuewithLock(settings["pd_mon_params"]["responsitivity"]) else: - self.params[1].child('Photodiode Monitor Config', 'Responsitivity').setValue(0) - self.params[1].child('Photodiode Monitor Config', 'Dark Current').setValue(settings["pd_mon_params"]["i_dark"]) + self.params[1].child('Photodiode Monitor Config', 'Responsitivity').setValuewithLock(0) + self.params[1].child('Photodiode Monitor Config', 'Dark Current').setValuewithLock(settings["pd_mon_params"]["i_dark"]) except Exception as e: logging.error(f"Params tree cannot be updated. Data:{settings}", exc_info=True) @@ -812,17 +789,17 @@ class MainWindow(QtWidgets.QMainWindow): try: report = report['laser'] with QSignalBlocker(self.params[0]): - self.params[0].child('Power').setValue('g' if report['pwr_on'] else 'w') - self.params[0].child('Alarm').setValue('r' if report['pwr_excursion'] else 'w') + self.params[0].child('Power').setValuewithLock('g' if report['pwr_on'] else 'w') + self.params[0].child('Alarm').setValuewithLock('r' if report['pwr_excursion'] else 'w') with QSignalBlocker(self.params[1]): - self.params[1].child('Readings', 'LD Current Set').setValue(report["ld_i_set"]) - self.params[1].child('Readings', 'PD Current').setValue(report["pd_i"]) + self.params[1].child('Readings', 'LD Current Set').setValuewithLock(report["ld_i_set"]) + self.params[1].child('Readings', 'PD Current').setValuewithLock(report["pd_i"]) if report["pd_pwr"] is not None: - self.params[1].child('Readings', 'PD Power').setValue(report["pd_pwr"]) + self.params[1].child('Readings', 'PD Power').setValuewithLock(report["pd_pwr"]) else: - self.params[1].child('Readings', 'PD Power').setValue(0) - self.params[1].child('Readings', 'LF Mod Termination (50 Ohm)').setValue(report["term_50ohm"]) + self.params[1].child('Readings', 'PD Power').setValuewithLock(0) + self.params[1].child('Readings', 'LF Mod Termination (50 Ohm)').setValuewithLock(report["term_50ohm"]) except Exception as e: logging.error(f"Params tree cannot be updated. Data:{report}", exc_info=True) @@ -831,26 +808,26 @@ class MainWindow(QtWidgets.QMainWindow): try: settings = settings['thermostat'] with QSignalBlocker(self.params[3]): - self.params[3].child('Output Config', 'Control Method').setValue("Temperature PID" if settings["pid_engaged"] else "Constant Current") - self.params[3].child('Output Config', 'Control Method', 'Set Current').setValue(settings["tec_settings"]['i_set']['value']) - self.params[3].child('Output Config', 'Control Method', 'Set Temperature').setValue(float(settings["temperature_setpoint"])) - self.params[3].child('Output Config', 'Limits', 'Max Cooling Current').setValue(settings["tec_settings"]['max_i_pos']['value']) - self.params[3].child('Output Config', 'Limits', 'Max Heating Current').setValue(settings["tec_settings"]['max_i_neg']['value']) - self.params[3].child('Output Config', 'Limits', 'Max Voltage Difference').setValue(settings["tec_settings"]['max_v']['value']) - self.params[3].child('Output Config', 'Default Power On').setValue(settings["default_pwr_on"]) - # TODO: Update the Temperature ADC Settings here as well - self.params[3].child('Temperature Monitor Config', 'Upper Limit').setValue(settings["temp_mon_settings"]['upper_limit']) - self.params[3].child('Temperature Monitor Config', 'Lower Limit').setValue(settings["temp_mon_settings"]['lower_limit']) - self.params[3].child('PID Config', 'Kp').setValue(settings["pid_params"]['kp']) - self.params[3].child('PID Config', 'Ki').setValue(settings["pid_params"]['ki']) - self.params[3].child('PID Config', 'Kd').setValue(settings["pid_params"]['kd']) - self.params[3].child('PID Config', 'PID Output Clamping', 'Minimum').setValue(settings["pid_params"]['output_min']) - self.params[3].child('PID Config', 'PID Output Clamping', 'Maximum').setValue(settings["pid_params"]['output_max']) - self.params[3].child('Thermistor Settings', 'T₀').setValue(settings["thermistor_params"]['t0']) - self.params[3].child('Thermistor Settings', 'R₀').setValue(settings["thermistor_params"]['r0']) - self.params[3].child('Thermistor Settings', 'B').setValue(settings["thermistor_params"]['b']) - self.graphs.set_temp_setpoint_line(temp=round(settings["temperature_setpoint"], 6)) - self.graphs.set_temp_setpoint_line(visible=settings['pid_engaged']) + self.params[3].child('Output Config', 'Control Method').setValuewithLock("Temperature PID" if settings["pid_engaged"] else "Constant Current") + self.params[3].child('Output Config', 'Control Method', 'Set Current').setValuewithLock(settings["tec_settings"]['i_set']['value']) + self.params[3].child('Output Config', 'Control Method', 'Set Temperature').setValuewithLock(float(settings["temperature_setpoint"])) + self.params[3].child('Output Config', 'Limits', 'Max Cooling Current').setValuewithLock(settings["tec_settings"]['max_i_pos']['value']) + self.params[3].child('Output Config', 'Limits', 'Max Heating Current').setValuewithLock(settings["tec_settings"]['max_i_neg']['value']) + self.params[3].child('Output Config', 'Limits', 'Max Voltage Difference').setValuewithLock(settings["tec_settings"]['max_v']['value']) + self.params[3].child('Output Config', 'Default Power On').setValuewithLock(settings["default_pwr_on"]) + # TODO: Update the Temperature ADC Settings here as well + self.params[3].child('Temperature Monitor Config', 'Upper Limit').setValuewithLock(settings["temp_mon_settings"]['upper_limit']) + self.params[3].child('Temperature Monitor Config', 'Lower Limit').setValuewithLock(settings["temp_mon_settings"]['lower_limit']) + self.params[3].child('PID Config', 'Kp').setValuewithLock(settings["pid_params"]['kp']) + self.params[3].child('PID Config', 'Ki').setValuewithLock(settings["pid_params"]['ki']) + self.params[3].child('PID Config', 'Kd').setValuewithLock(settings["pid_params"]['kd']) + self.params[3].child('PID Config', 'PID Output Clamping', 'Minimum').setValuewithLock(settings["pid_params"]['output_min']) + self.params[3].child('PID Config', 'PID Output Clamping', 'Maximum').setValuewithLock(settings["pid_params"]['output_max']) + self.params[3].child('Thermistor Settings', 'T₀').setValuewithLock(settings["thermistor_params"]['t0']) + self.params[3].child('Thermistor Settings', 'R₀').setValuewithLock(settings["thermistor_params"]['r0']) + self.params[3].child('Thermistor Settings', 'B').setValuewithLock(settings["thermistor_params"]['b']) + self.graphs.set_temp_setpoint_line(temp=round(settings["temperature_setpoint"], 4)) + self.graphs.set_temp_setpoint_line(visible=settings['pid_engaged']) except Exception as e: logging.error(f"Params tree cannot be updated. Data:{settings}", exc_info=True) @@ -859,14 +836,14 @@ class MainWindow(QtWidgets.QMainWindow): try: report = report['thermostat'] with QSignalBlocker(self.params[2]): - self.params[2].child('Power').setValue('g' if report['pwr_on'] else 'w') - self.params[2].child('Alarm').setValue('r' if report['temp_mon_status']['over_temp_alarm'] else 'w') + self.params[2].child('Power').setValuewithLock('g' if report['pwr_on'] else 'w') + self.params[2].child('Alarm').setValuewithLock('r' if report['temp_mon_status']['over_temp_alarm'] else 'w') with QSignalBlocker(self.params[3]): if report["temperature"] == None: - self.params[3].child('Readings', 'Temperature').setValue(-273.15) + self.params[3].child('Readings', 'Temperature').setValuewithLock(-273.15) else: - self.params[3].child('Readings', 'Temperature').setValue(report["temperature"]) - self.params[3].child('Readings', 'Current through TEC').setValue(report["tec_i"]) + self.params[3].child('Readings', 'Temperature').setValuewithLock(report["temperature"]) + self.params[3].child('Readings', 'Current through TEC').setValuewithLock(report["tec_i"]) except Exception as e: logging.error(f"Params tree cannot be updated. Data:{report}", exc_info=True) @@ -935,7 +912,9 @@ class MainWindow(QtWidgets.QMainWindow): if inner_param.opts.get("target", None) is not None: if inner_param.opts.get("action", None) is not None: cmd = getattr(getattr(self.kirdy, inner_param.opts["target"]), inner_param.opts["action"]) + param.child(*param.childPath(inner_param)).setOpts(lock=True) await cmd(data) + param.child(*param.childPath(inner_param)).setOpts(lock=False) continue async def coro_main():