moninj: decouple DDS logic and display

This commit is contained in:
Simon Renblad 2024-04-15 17:22:27 +08:00
parent 6224270396
commit 6408ae45e0

View File

@ -184,14 +184,11 @@ class _DDSModel:
class _DDSWidget(QtWidgets.QFrame):
def __init__(self, dm, title, bus_channel=0, channel=0, dds_model=None):
self.dm = dm
self.bus_channel = bus_channel
self.channel = channel
self.dds_name = title
self.cur_frequency = 0
self.dds_model = dds_model
apply_changes = QtCore.pyqtSignal()
off_clicked = QtCore.pyqtSignal()
set_clicked = QtCore.pyqtSignal()
def __init__(self, title, is_urukul):
QtWidgets.QFrame.__init__(self)
self.setFrameShape(QtWidgets.QFrame.Box)
@ -253,7 +250,7 @@ class _DDSWidget(QtWidgets.QFrame):
set_grid.addWidget(set_btn, 0, 1, 1, 1)
# for urukuls also allow switching off RF
if self.dds_model.is_urukul:
if is_urukul:
off_btn = QtWidgets.QToolButton()
off_btn.setText("Off")
off_btn.setToolTip("Switch off the output")
@ -280,55 +277,73 @@ class _DDSWidget(QtWidgets.QFrame):
set_btn.clicked.connect(self.set_clicked)
apply.clicked.connect(self.apply_changes)
if self.dds_model.is_urukul:
if is_urukul:
off_btn.clicked.connect(self.off_clicked)
off_btn.setToolTip(textwrap.dedent(
"""Note: If TTL RTIO sw for the channel is switched high,
this button will not disable the channel.
Use the TTL override instead."""))
self.value_edit.returnPressed.connect(lambda: self.apply_changes(None))
self.value_edit.escapePressedConnect(self.cancel_changes)
cancel.clicked.connect(self.cancel_changes)
self.value_edit.returnPressed.connect(self.apply_changes)
self.refresh_display()
def cancel_changes(cancel):
self.set_page(0)
self.value_edit.escapePressedConnect(cancel_changes)
cancel.clicked.connect(cancel_changes)
def set_clicked(self, set):
self.data_stack.setCurrentIndex(1)
self.button_stack.setCurrentIndex(1)
self.value_edit.setText("{:.7f}".format(self.cur_frequency / 1e6))
def set_page(self, page):
self.data_stack.setCurrentIndex(page)
self.button_stack.setCurrentIndex(page)
def get_value(self):
return float(self.value_edit.text())
def set_edit_value(self, value):
self.value_edit.setText("{:.7f}".format(value))
def set_value(self, value):
self.value_label.setText("<font size=\"4\">{:.7f}</font>".format(value))
self.set_edit_value(value)
def start_edit(self):
self.value_edit.setFocus()
self.value_edit.selectAll()
def off_clicked(self, set):
self.dm.dds_channel_toggle(self.dds_name, self.dds_model, sw=False)
def apply_changes(self, apply):
self.data_stack.setCurrentIndex(0)
self.button_stack.setCurrentIndex(0)
frequency = float(self.value_edit.text()) * 1e6
self.dm.dds_set_frequency(self.dds_name, self.dds_model, frequency)
def cancel_changes(self, cancel):
self.data_stack.setCurrentIndex(0)
self.button_stack.setCurrentIndex(0)
class _DDSHandler:
def __init__(self, dm, title, bus_channel=0, channel=0, dds_model=None):
self.dm = dm
self.bus_channel = bus_channel
self.channel = channel
self.cur_frequency = 0
self.dds_name = title
self.dds_model = dds_model
self.widget = _DDSWidget(title, dds_model.is_urukul)
self.widget.apply_changes.connect(self.apply_changes)
self.widget.off_clicked.connect(self.off_clicked)
self.widget.set_clicked.connect(self.set_clicked)
self.refresh_display()
def refresh_display(self):
self.cur_frequency = self.dds_model.cur_frequency
self.value_label.setText("<font size=\"4\">{:.7f}</font>".format(self.cur_frequency / 1e6))
self.value_edit.setText("{:.7f}".format(self.cur_frequency / 1e6))
self.widget.set_value(self.cur_frequency / 1e6)
def apply_changes(self):
self.widget.set_page(0)
frequency = self.widget.get_value() * 1e6
self.dm.dds_set_frequency(self.dds_name, self.dds_model, frequency)
def set_clicked(self):
self.widget.set_page(1)
self.widget.set_edit_value(self.cur_frequency / 1e6)
self.widget.start_edit()
def off_clicked(self):
self.dm.dds_channel_toggle(self.dds_name, self.dds_model, sw=False)
def sort_key(self):
return (self.bus_channel, self.channel)
class _DDSHandler:
def __init__(self, *args, **kw):
self.widget = _DDSWidget(*args, **kw)
def __getattr__(self, attr):
return getattr(self.widget, attr)
class _DACWidget(QtWidgets.QFrame):
def __init__(self, dm, spi_channel, channel, title):
QtWidgets.QFrame.__init__(self)