Compare commits

...

1 Commits

Author SHA1 Message Date
779328c4d7 Control Panel: Use new locking mechanism
Ported from Kirdy
2025-03-31 16:29:14 +08:00

View File

@ -63,6 +63,11 @@ class CtrlPanel(QObject):
self.trees_ui = trees_ui
self.NUM_CHANNELS = len(trees_ui)
def _set_value_with_lock(self, value):
if not self.opts.get("lock"):
self.setValue(value)
Parameter.set_value_with_lock = _set_value_with_lock
self.THERMOSTAT_PARAMETERS = [param_tree for i in range(self.NUM_CHANNELS)]
self.params = [
@ -81,7 +86,6 @@ class CtrlPanel(QObject):
for i, tree in enumerate(self.trees_ui):
tree.setHeaderHidden(True)
tree.setParameters(self.params[i], showTop=False)
self.params[i].setValue = self._setValue
self.params[i].sigTreeStateChanged.connect(self.send_command)
self.params[i].child("Save to flash").sigActivated.connect(
@ -101,32 +105,6 @@ class CtrlPanel(QObject):
self.thermostat.postfilter_update.connect(self.update_postfilter)
self.autotuners.autotune_state_changed.connect(self.update_pid_autotune)
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"]
def change_params_title(self, channel, path, title):
self.params[channel].child(*path).setOpts(title=title)
@ -170,42 +148,42 @@ class CtrlPanel(QObject):
for settings in pid_settings:
channel = settings["channel"]
with QSignalBlocker(self.params[channel]):
self.params[channel].child("PID Config", "Kp").setValue(
self.params[channel].child("PID Config", "Kp").set_value_with_lock(
settings["parameters"]["kp"]
)
self.params[channel].child("PID Config", "Ki").setValue(
self.params[channel].child("PID Config", "Ki").set_value_with_lock(
settings["parameters"]["ki"]
)
self.params[channel].child("PID Config", "Kd").setValue(
self.params[channel].child("PID Config", "Kd").set_value_with_lock(
settings["parameters"]["kd"]
)
self.params[channel].child(
"PID Config", "PID Output Clamping", "Minimum"
).setValue(settings["parameters"]["output_min"] * 1000)
).set_value_with_lock(settings["parameters"]["output_min"] * 1000)
self.params[channel].child(
"PID Config", "PID Output Clamping", "Maximum"
).setValue(settings["parameters"]["output_max"] * 1000)
).set_value_with_lock(settings["parameters"]["output_max"] * 1000)
self.params[channel].child(
"Output Config", "Control Method", "Set Temperature"
).setValue(settings["target"])
).set_value_with_lock(settings["target"])
@pyqtSlot(list)
def update_report(self, report_data):
for settings in report_data:
channel = settings["channel"]
with QSignalBlocker(self.params[channel]):
self.params[channel].child("Output Config", "Control Method").setValue(
self.params[channel].child("Output Config", "Control Method").set_value_with_lock(
"Temperature PID" if settings["pid_engaged"] else "Constant Current"
)
self.params[channel].child(
"Output Config", "Control Method", "Set Current"
).setValue(settings["i_set"] * 1000)
).set_value_with_lock(settings["i_set"] * 1000)
if settings["temperature"] is not None:
self.params[channel].child("Temperature").setValue(
self.params[channel].child("Temperature").set_value_with_lock(
settings["temperature"]
)
if settings["tec_i"] is not None:
self.params[channel].child("Current through TEC").setValue(
self.params[channel].child("Current through TEC").set_value_with_lock(
settings["tec_i"] * 1000
)
@ -214,13 +192,13 @@ class CtrlPanel(QObject):
for sh_param in sh_data:
channel = sh_param["channel"]
with QSignalBlocker(self.params[channel]):
self.params[channel].child("Thermistor Config", "T₀").setValue(
self.params[channel].child("Thermistor Config", "T₀").set_value_with_lock(
sh_param["params"]["t0"] - 273.15
)
self.params[channel].child("Thermistor Config", "R₀").setValue(
self.params[channel].child("Thermistor Config", "R₀").set_value_with_lock(
sh_param["params"]["r0"]
)
self.params[channel].child("Thermistor Config", "B").setValue(
self.params[channel].child("Thermistor Config", "B").set_value_with_lock(
sh_param["params"]["b"]
)
@ -231,13 +209,13 @@ class CtrlPanel(QObject):
with QSignalBlocker(self.params[channel]):
self.params[channel].child(
"Output Config", "Limits", "Max Voltage Difference"
).setValue(output_params["max_v"])
).set_value_with_lock(output_params["max_v"])
self.params[channel].child(
"Output Config", "Limits", "Max Cooling Current"
).setValue(output_params["max_i_pos"] * 1000)
).set_value_with_lock(output_params["max_i_pos"] * 1000)
self.params[channel].child(
"Output Config", "Limits", "Max Heating Current"
).setValue(output_params["max_i_neg"] * 1000)
).set_value_with_lock(output_params["max_i_neg"] * 1000)
@pyqtSlot(list)
def update_postfilter(self, postfilter_data):
@ -246,7 +224,7 @@ class CtrlPanel(QObject):
with QSignalBlocker(self.params[channel]):
self.params[channel].child(
"Thermistor Config", "Postfilter Rate"
).setValue(postfilter_params["rate"])
).set_value_with_lock(postfilter_params["rate"])
def update_pid_autotune(self, ch, state):
match state: