diff --git a/artiq/dashboard/moninj.py b/artiq/dashboard/moninj.py index 28aa44ff8..be1944378 100644 --- a/artiq/dashboard/moninj.py +++ b/artiq/dashboard/moninj.py @@ -363,61 +363,72 @@ class _DACWidget(_SimpleDisplayWidget): return (self.spi_channel, self.channel) -_WidgetDesc = namedtuple("_WidgetDesc", "uid comment cls arguments") +_ChannelDesc = namedtuple("_ChannelDesc", "name widget_type clss kwargs") + + +def _setup_ttl(k, v, description): + if "ttl_urukul" in k: + return + channel = v["arguments"]["channel"] + channel_desc = _ChannelDesc(k, _TTLWidget, v["class"], {}) + description[(channel, None)] = channel_desc + + +def _setup_ad9914(k, v, description): + bus_channel = v["arguments"]["bus_channel"] + channel = v["arguments"]["channel"] + dds_sysclk = v["arguments"]["sysclk"] + channel_desc = _ChannelDesc(k, _DDSWidget, v["class"], + {"dds_sysclk": dds_sysclk}) + description[(bus_channel, channel)] = channel_desc + + +def _setup_urukul(k, v, description, ddb): + channel = v["arguments"]["chip_select"] - 4 + if channel < 0: + return + dds_cpld = v["arguments"]["cpld_device"] + spi_dev = ddb[dds_cpld]["arguments"]["spi_device"] + bus_channel = ddb[spi_dev]["arguments"]["channel"] + pll = v["arguments"]["pll_n"] + refclk = ddb[dds_cpld]["arguments"]["refclk"] + clk_div = v["arguments"].get("clk_div", 0) + channel_desc = _ChannelDesc(k, _DDSWidget, v["class"], + {"refclk": refclk, "dds_cpld": dds_cpld, + "pll": pll, "clk_div": clk_div}) + description[(bus_channel, channel)] = channel_desc + + +def _setup_dac(k, v, description, ddb): + spi_device = v["arguments"]["spi_device"] + spi_device = ddb[spi_device] + while isinstance(spi_device, str): + spi_device = ddb[spi_device] + spi_channel = spi_device["arguments"]["channel"] + for channel in range(32): + channel_desc = _ChannelDesc(k, _DACWidget, v["class"], {}) + description[(spi_channel, channel)] = channel_desc def setup_from_ddb(ddb): mi_addr = None mi_port = None - dds_sysclk = None - description = set() + description = dict() # (uid, channel) -> _ChannelDesc for k, v in ddb.items(): try: if isinstance(v, dict): - comment = v.get("comment") if v["type"] == "local": if v["module"] == "artiq.coredevice.ttl": - if "ttl_urukul" in k: - continue - channel = v["arguments"]["channel"] - force_out = v["class"] == "TTLOut" - widget = _WidgetDesc(k, comment, _TTLWidget, (channel, force_out, k)) - description.add(widget) + _setup_ttl(k, v, description) elif (v["module"] == "artiq.coredevice.ad9914" and v["class"] == "AD9914"): - bus_channel = v["arguments"]["bus_channel"] - channel = v["arguments"]["channel"] - dds_sysclk = v["arguments"]["sysclk"] - model = _DDSModel(v["class"], dds_sysclk) - widget = _WidgetDesc(k, comment, _DDSWidget, - (k, bus_channel, channel, model)) - description.add(widget) + _setup_ad9914(k, v, description) elif (v["module"] == "artiq.coredevice.ad9910" and v["class"] == "AD9910") or \ (v["module"] == "artiq.coredevice.ad9912" and v["class"] == "AD9912"): - channel = v["arguments"]["chip_select"] - 4 - if channel < 0: - continue - dds_cpld = v["arguments"]["cpld_device"] - spi_dev = ddb[dds_cpld]["arguments"]["spi_device"] - bus_channel = ddb[spi_dev]["arguments"]["channel"] - pll = v["arguments"]["pll_n"] - refclk = ddb[dds_cpld]["arguments"]["refclk"] - clk_div = v["arguments"].get("clk_div", 0) - model = _DDSModel(v["class"], refclk, dds_cpld, pll, clk_div) - widget = _WidgetDesc(k, comment, _DDSWidget, - (k, bus_channel, channel, model)) - description.add(widget) + _setup_urukul(k, v, description, ddb) elif (v["module"] == "artiq.coredevice.ad53xx" and v["class"] == "AD53xx") or \ (v["module"] == "artiq.coredevice.zotino" and v["class"] == "Zotino"): - spi_device = v["arguments"]["spi_device"] - spi_device = ddb[spi_device] - while isinstance(spi_device, str): - spi_device = ddb[spi_device] - spi_channel = spi_device["arguments"]["channel"] - for channel in range(32): - widget = _WidgetDesc((k, channel), comment, _DACWidget, - (spi_channel, channel, k)) - description.add(widget) + _setup_dac(k, v, description, ddb) elif v["type"] == "controller" and k == "core_moninj": mi_addr = v["host"] mi_port = v.get("port_proxy", 1383) @@ -437,16 +448,14 @@ class _DeviceManager: self.schedule_ctl = schedule_ctl self.ddb = dict() - self.description = set() - self.widgets_by_uid = dict() + + # only modified by notify_ddb + self.description = dict() + + self.displayed_channels = dict() self.dds_sysclk = 0 - self.ttl_cb = lambda: None - self.ttl_widgets = dict() - self.dds_cb = lambda: None - self.dds_widgets = dict() - self.dac_cb = lambda: None - self.dac_widgets = dict() + self.refresh_display_cb = lambda: None def init_ddb(self, ddb): self.ddb = ddb @@ -454,56 +463,13 @@ class _DeviceManager: def notify_ddb(self, mod): mi_addr, mi_port, description = setup_from_ddb(self.ddb) - if (mi_addr, mi_port) != (self.mi_addr, self.mi_port): + if (mi_addr, mi_port, description) != (self.mi_addr, self.mi_port, self.description): self.mi_addr = mi_addr self.mi_port = mi_port + self.description = description + self.refresh_display_cb() self.reconnect_mi.set() - for to_remove in self.description - description: - widget = self.widgets_by_uid[to_remove.uid] - del self.widgets_by_uid[to_remove.uid] - - if isinstance(widget, _TTLWidget): - self.setup_ttl_monitoring(False, widget.channel) - widget.deleteLater() - del self.ttl_widgets[widget.channel] - self.ttl_cb() - elif isinstance(widget, _DDSWidget): - self.setup_dds_monitoring(False, widget.bus_channel, widget.channel) - widget.deleteLater() - del self.dds_widgets[(widget.bus_channel, widget.channel)] - self.dds_cb() - elif isinstance(widget, _DACWidget): - self.setup_dac_monitoring(False, widget.spi_channel, widget.channel) - widget.deleteLater() - del self.dac_widgets[(widget.spi_channel, widget.channel)] - self.dac_cb() - else: - raise ValueError - - for to_add in description - self.description: - widget = to_add.cls(self, *to_add.arguments) - if to_add.comment is not None: - widget.setToolTip(to_add.comment) - self.widgets_by_uid[to_add.uid] = widget - - if isinstance(widget, _TTLWidget): - self.ttl_widgets[widget.channel] = widget - self.ttl_cb() - self.setup_ttl_monitoring(True, widget.channel) - elif isinstance(widget, _DDSWidget): - self.dds_widgets[(widget.bus_channel, widget.channel)] = widget - self.dds_cb() - self.setup_dds_monitoring(True, widget.bus_channel, widget.channel) - elif isinstance(widget, _DACWidget): - self.dac_widgets[(widget.spi_channel, widget.channel)] = widget - self.dac_cb() - self.setup_dac_monitoring(True, widget.spi_channel, widget.channel) - else: - raise ValueError - - self.description = description - def ttl_set_mode(self, channel, mode): if self.mi_connection is not None: widget = self.ttl_widgets[channel] @@ -655,6 +621,15 @@ class _DeviceManager: "ToggleDDS", "Toggle DDS {} {}".format(dds_channel, "on" if sw else "off")) + def setup_monitoring(self, uid, enable): + widget_type = self.description[uid].widget_type + if widget_type == "TTL": + self.setup_ttl_monitoring(enable, uid[0]) + elif widget_type == "DDS": + self.setup_dds_monitoring(enable, *uid) + elif widget_type == "DAC": + self.setup_dac_monitoring(enable, *uid) + def setup_ttl_monitoring(self, enable, channel): if self.mi_connection is not None: self.mi_connection.monitor_probe(enable, channel, TTLProbe.level.value) @@ -673,30 +648,15 @@ class _DeviceManager: self.mi_connection.monitor_probe(enable, spi_channel, channel) def monitor_cb(self, channel, probe, value): - if channel in self.ttl_widgets: - widget = self.ttl_widgets[channel] - if probe == TTLProbe.level.value: - widget.cur_level = bool(value) - elif probe == TTLProbe.oe.value: - widget.cur_oe = bool(value) - widget.refresh_display() - elif (channel, probe) in self.dds_widgets: - widget = self.dds_widgets[(channel, probe)] - widget.dds_model.monitor_update(probe, value) - widget.refresh_display() - elif (channel, probe) in self.dac_widgets: - widget = self.dac_widgets[(channel, probe)] - widget.cur_value = value - widget.refresh_display() + if (channel, None) in self.displayed_channels: + self.displayed_channels[(channel, None)].monitor_cb(probe, value) + elif (channel, probe) in self.displayed_channels: + self.displayed_channels[(channel, probe)].monitor_cb(probe, value) def injection_status_cb(self, channel, override, value): - if channel in self.ttl_widgets: - widget = self.ttl_widgets[channel] - if override == TTLOverride.en.value: - widget.cur_override = bool(value) - if override == TTLOverride.level.value: - widget.cur_override_level = bool(value) - widget.refresh_display() + if (channel, None) in self.displayed_channels and \ + self.description[(channel, None)].widget_type == "TTL": + self.displayed_channels[(channel, None)].injection_status_cb(override, value) def disconnect_cb(self): logger.error("lost connection to moninj") @@ -722,12 +682,8 @@ class _DeviceManager: logger.info("ARTIQ dashboard connected to moninj (%s)", self.mi_addr) self.mi_connection = new_mi_connection - for ttl_channel in self.ttl_widgets.keys(): - self.setup_ttl_monitoring(True, ttl_channel) - for bus_channel, channel in self.dds_widgets.keys(): - self.setup_dds_monitoring(True, bus_channel, channel) - for spi_channel, channel in self.dac_widgets.keys(): - self.setup_dac_monitoring(True, spi_channel, channel) + for key in self.displayed_channels: + self.setup_monitoring(key, True) async def close(self): self.mi_connector_task.cancel() @@ -756,9 +712,7 @@ class MonInjDock(QtWidgets.QDockWidget): scroll_area.setWidget(grid_widget) self.dm = _DeviceManager(schedule_ctl) - self.dm.ttl_cb = lambda: self.layout_widgets(self.dm.ttl_widgets.values()) - self.dm.dds_cb = lambda: self.layout_widgets(self.dm.dds_widgets.values()) - self.dm.dac_cb = lambda: self.layout_widgets(self.dm.dac_widgets.values()) + self.dm.refresh_display_cb = self.refresh_display_cb def layout_widgets(self, widgets): for widget in sorted(widgets, key=lambda w: w.sort_key()):