artiq/artiq/dashboard/moninj.py

984 lines
36 KiB
Python
Raw Normal View History

import asyncio
import logging
import textwrap
from collections import namedtuple
2024-05-02 14:35:59 +08:00
from functools import partial
2024-04-10 10:32:33 +08:00
from PyQt5 import QtCore, QtWidgets
from artiq.coredevice.comm_moninj import CommMonInj, TTLOverride, TTLProbe
from artiq.coredevice.ad9912_reg import AD9912_SER_CONF
2024-05-02 15:24:36 +08:00
from artiq.gui.tools import LayoutWidget, QDockWidgetCloseDetect, DoubleClickLineEdit
2024-05-02 16:01:44 +08:00
from artiq.gui.dndwidgets import VDragScrollArea, DragDropFlowLayoutWidget
2024-05-02 14:56:49 +08:00
from artiq.gui.models import DictSyncTreeSepModel
logger = logging.getLogger(__name__)
2024-04-10 10:43:44 +08:00
class _CancellableLineEdit(QtWidgets.QLineEdit):
def escapePressedConnect(self, cb):
self.esc_cb = cb
def keyPressEvent(self, event):
key = event.key()
if key == QtCore.Qt.Key_Escape:
self.esc_cb(event)
QtWidgets.QLineEdit.keyPressEvent(self, event)
2016-11-04 16:20:46 +08:00
class _TTLWidget(QtWidgets.QFrame):
def __init__(self, dm, channel, force_out, title):
QtWidgets.QFrame.__init__(self)
2015-06-06 00:03:30 +08:00
self.channel = channel
self.set_mode = dm.ttl_set_mode
2015-06-05 19:11:41 +08:00
self.force_out = force_out
2024-05-02 14:06:39 +08:00
self.title = title
2015-06-05 19:11:41 +08:00
2016-11-04 16:20:46 +08:00
self.setFrameShape(QtWidgets.QFrame.Box)
self.setFrameShadow(QtWidgets.QFrame.Raised)
2015-06-05 19:11:41 +08:00
2016-02-15 07:23:47 +08:00
grid = QtWidgets.QGridLayout()
2016-11-04 16:20:46 +08:00
grid.setContentsMargins(0, 0, 0, 0)
grid.setHorizontalSpacing(0)
grid.setVerticalSpacing(0)
2015-06-05 19:11:41 +08:00
self.setLayout(grid)
2016-02-15 07:23:47 +08:00
label = QtWidgets.QLabel(title)
2015-06-05 19:11:41 +08:00
label.setAlignment(QtCore.Qt.AlignCenter)
2016-11-04 16:20:46 +08:00
label.setSizePolicy(QtWidgets.QSizePolicy.Ignored,
QtWidgets.QSizePolicy.Preferred)
2015-06-05 19:11:41 +08:00
grid.addWidget(label, 1, 1)
self.stack = QtWidgets.QStackedWidget()
grid.addWidget(self.stack, 2, 1)
self.direction = QtWidgets.QLabel()
self.direction.setAlignment(QtCore.Qt.AlignCenter)
self.stack.addWidget(self.direction)
grid_cb = LayoutWidget()
2016-11-04 16:20:46 +08:00
grid_cb.layout.setContentsMargins(0, 0, 0, 0)
grid_cb.layout.setHorizontalSpacing(0)
grid_cb.layout.setVerticalSpacing(0)
self.override = QtWidgets.QToolButton()
self.override.setText("OVR")
self.override.setCheckable(True)
self.override.setToolTip("Override")
grid_cb.addWidget(self.override, 3, 1)
2016-11-04 16:20:46 +08:00
self.level = QtWidgets.QToolButton()
self.level.setText("LVL")
self.level.setCheckable(True)
self.level.setToolTip("Level")
grid_cb.addWidget(self.level, 3, 2)
self.stack.addWidget(grid_cb)
self.value = QtWidgets.QLabel()
self.value.setAlignment(QtCore.Qt.AlignCenter)
grid.addWidget(self.value, 3, 1)
2015-06-06 00:03:30 +08:00
2015-09-02 04:25:20 +08:00
grid.setRowStretch(1, 1)
grid.setRowStretch(2, 0)
grid.setRowStretch(3, 0)
grid.setRowStretch(4, 1)
self.programmatic_change = False
2016-11-04 16:20:46 +08:00
self.override.clicked.connect(self.override_toggled)
self.level.clicked.connect(self.level_toggled)
2015-09-02 04:25:20 +08:00
2017-02-27 11:45:04 +08:00
self.cur_level = False
2017-02-27 00:59:31 +08:00
self.cur_oe = False
self.cur_override = False
self.cur_override_level = False
2017-02-27 00:59:31 +08:00
self.refresh_display()
2015-06-06 00:03:30 +08:00
def enterEvent(self, event):
self.stack.setCurrentIndex(1)
2016-11-04 16:20:46 +08:00
QtWidgets.QFrame.enterEvent(self, event)
def leaveEvent(self, event):
if not self.override.isChecked():
self.stack.setCurrentIndex(0)
2016-11-04 16:20:46 +08:00
QtWidgets.QFrame.leaveEvent(self, event)
def override_toggled(self, override):
if self.programmatic_change:
return
if override:
if self.level.isChecked():
2017-02-27 00:59:31 +08:00
self.set_mode(self.channel, "1")
else:
2017-02-27 00:59:31 +08:00
self.set_mode(self.channel, "0")
else:
2017-02-27 00:59:31 +08:00
self.set_mode(self.channel, "exp")
def level_toggled(self, level):
if self.programmatic_change:
return
if self.override.isChecked():
if level:
2017-02-27 00:59:31 +08:00
self.set_mode(self.channel, "1")
else:
2017-02-27 00:59:31 +08:00
self.set_mode(self.channel, "0")
2017-02-27 00:59:31 +08:00
def refresh_display(self):
level = self.cur_override_level if self.cur_override else self.cur_level
value_s = "1" if level else "0"
2017-02-27 00:59:31 +08:00
if self.cur_override:
2015-06-06 00:03:30 +08:00
value_s = "<b>" + value_s + "</b>"
2015-06-05 19:11:41 +08:00
color = " color=\"red\""
else:
color = ""
2016-11-04 16:20:46 +08:00
self.value.setText("<font size=\"5\"{}>{}</font>".format(
2024-04-10 10:43:44 +08:00
color, value_s))
2017-02-27 00:59:31 +08:00
oe = self.cur_oe or self.force_out
2015-06-05 19:11:41 +08:00
direction = "OUT" if oe else "IN"
2016-11-04 16:20:46 +08:00
self.direction.setText("<font size=\"2\">" + direction + "</font>")
self.programmatic_change = True
try:
2017-02-27 00:59:31 +08:00
self.override.setChecked(self.cur_override)
if self.cur_override:
self.stack.setCurrentIndex(1)
2017-02-27 11:45:04 +08:00
self.level.setChecked(self.cur_level)
finally:
self.programmatic_change = False
2015-06-05 19:11:41 +08:00
def sort_key(self):
2024-05-02 14:10:47 +08:00
return (0, self.channel, 0)
2024-05-02 14:06:39 +08:00
def uid(self):
return self.title
2024-05-02 14:56:49 +08:00
def to_model_path(self):
return "ttl/{}".format(self.title)
2015-06-05 19:11:41 +08:00
2022-07-07 10:52:53 +08:00
class _DDSModel:
def __init__(self, dds_type, ref_clk, cpld=None, pll=1, clk_div=0):
self.cpld = cpld
self.cur_frequency = 0
self.cur_reg = 0
self.dds_type = dds_type
self.is_urukul = dds_type in ["AD9910", "AD9912"]
2024-04-10 10:43:44 +08:00
2022-07-07 10:52:53 +08:00
if dds_type == "AD9914":
self.ftw_per_hz = 2**32 / ref_clk
else:
if dds_type == "AD9910":
max_freq = 1 << 32
clk_mult = [4, 1, 2, 4]
elif dds_type == "AD9912": # AD9912
max_freq = 1 << 48
clk_mult = [1, 1, 2, 4]
else:
raise NotImplementedError
sysclk = ref_clk / clk_mult[clk_div] * pll
self.ftw_per_hz = 1 / sysclk * max_freq
def monitor_update(self, probe, value):
if self.dds_type == "AD9912":
value = value << 16
self.cur_frequency = self._ftw_to_freq(value)
def _ftw_to_freq(self, ftw):
return ftw / self.ftw_per_hz
class _DDSWidget(QtWidgets.QFrame):
def __init__(self, dm, title, bus_channel, channel,
dds_type, ref_clk, cpld=None, pll=1, clk_div=0):
self.dm = dm
2017-10-26 02:26:59 +08:00
self.bus_channel = bus_channel
self.channel = channel
self.dds_name = title
2017-10-26 02:26:59 +08:00
self.cur_frequency = 0
self.dds_model = _DDSModel(dds_type, ref_clk, cpld, pll, clk_div)
2024-05-02 14:06:39 +08:00
self.title = title
QtWidgets.QFrame.__init__(self)
self.setFrameShape(QtWidgets.QFrame.Box)
self.setFrameShadow(QtWidgets.QFrame.Raised)
grid = QtWidgets.QGridLayout()
grid.setContentsMargins(0, 0, 0, 0)
grid.setHorizontalSpacing(0)
grid.setVerticalSpacing(0)
self.setLayout(grid)
label = QtWidgets.QLabel(title)
label.setAlignment(QtCore.Qt.AlignCenter)
grid.addWidget(label, 1, 1)
# FREQ DATA/EDIT FIELD
self.data_stack = QtWidgets.QStackedWidget()
# page 1: display data
grid_disp = LayoutWidget()
grid_disp.layout.setContentsMargins(0, 0, 0, 0)
grid_disp.layout.setHorizontalSpacing(0)
grid_disp.layout.setVerticalSpacing(0)
self.value_label = QtWidgets.QLabel()
self.value_label.setAlignment(QtCore.Qt.AlignCenter)
grid_disp.addWidget(self.value_label, 0, 1, 1, 2)
unit = QtWidgets.QLabel("MHz")
unit.setAlignment(QtCore.Qt.AlignCenter)
grid_disp.addWidget(unit, 0, 3, 1, 1)
self.data_stack.addWidget(grid_disp)
# page 2: edit data
grid_edit = LayoutWidget()
grid_edit.layout.setContentsMargins(0, 0, 0, 0)
grid_edit.layout.setHorizontalSpacing(0)
grid_edit.layout.setVerticalSpacing(0)
self.value_edit = _CancellableLineEdit(self)
self.value_edit.setAlignment(QtCore.Qt.AlignRight)
grid_edit.addWidget(self.value_edit, 0, 1, 1, 2)
unit = QtWidgets.QLabel("MHz")
unit.setAlignment(QtCore.Qt.AlignCenter)
grid_edit.addWidget(unit, 0, 3, 1, 1)
self.data_stack.addWidget(grid_edit)
grid.addWidget(self.data_stack, 2, 1)
# BUTTONS
self.button_stack = QtWidgets.QStackedWidget()
# page 1: SET button
set_grid = LayoutWidget()
set_btn = QtWidgets.QToolButton()
set_btn.setText("Set")
set_btn.setToolTip("Set frequency")
set_grid.addWidget(set_btn, 0, 1, 1, 1)
2024-04-10 10:43:44 +08:00
# for urukuls also allow switching off RF
2022-07-07 10:52:53 +08:00
if self.dds_model.is_urukul:
off_btn = QtWidgets.QToolButton()
off_btn.setText("Off")
off_btn.setToolTip("Switch off the output")
set_grid.addWidget(off_btn, 0, 2, 1, 1)
self.button_stack.addWidget(set_grid)
# page 2: apply/cancel buttons
apply_grid = LayoutWidget()
apply = QtWidgets.QToolButton()
apply.setText("Apply")
apply.setToolTip("Apply changes")
apply_grid.addWidget(apply, 0, 1, 1, 1)
cancel = QtWidgets.QToolButton()
cancel.setText("Cancel")
cancel.setToolTip("Cancel changes")
apply_grid.addWidget(cancel, 0, 2, 1, 1)
self.button_stack.addWidget(apply_grid)
grid.addWidget(self.button_stack, 3, 1)
grid.setRowStretch(1, 1)
grid.setRowStretch(2, 1)
grid.setRowStretch(3, 1)
set_btn.clicked.connect(self.set_clicked)
apply.clicked.connect(self.apply_changes)
2022-07-07 10:52:53 +08:00
if self.dds_model.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.refresh_display()
def set_clicked(self, set):
self.data_stack.setCurrentIndex(1)
self.button_stack.setCurrentIndex(1)
2024-04-10 10:43:44 +08:00
self.value_edit.setText("{:.7f}".format(self.cur_frequency / 1e6))
self.value_edit.setFocus()
self.value_edit.selectAll()
def off_clicked(self, set):
2022-07-07 10:52:53 +08:00
self.dm.dds_channel_toggle(self.dds_name, self.dds_model, sw=False)
2024-04-10 10:43:44 +08:00
def apply_changes(self, apply):
self.data_stack.setCurrentIndex(0)
self.button_stack.setCurrentIndex(0)
2024-04-10 10:43:44 +08:00
frequency = float(self.value_edit.text()) * 1e6
2022-07-07 10:52:53 +08:00
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)
2017-10-26 02:26:59 +08:00
2017-02-27 00:59:31 +08:00
def refresh_display(self):
2022-07-07 10:52:53 +08:00
self.cur_frequency = self.dds_model.cur_frequency
2024-04-10 10:43:44 +08:00
self.value_label.setText("<font size=\"4\">{:.7f}</font>".format(self.cur_frequency / 1e6))
self.value_edit.setText("{:.7f}".format(self.cur_frequency / 1e6))
2015-06-12 17:41:04 +08:00
def sort_key(self):
2024-05-02 14:10:47 +08:00
return (1, self.bus_channel, self.channel)
2024-05-02 14:06:39 +08:00
def uid(self):
return self.title
2024-05-02 14:56:49 +08:00
def to_model_path(self):
return "dds/{}".format(self.title)
2015-06-12 17:41:04 +08:00
2024-04-15 16:04:22 +08:00
class _DACWidget(QtWidgets.QFrame):
def __init__(self, dm, spi_channel, channel, title):
2024-04-15 16:04:22 +08:00
QtWidgets.QFrame.__init__(self)
2017-10-26 02:26:59 +08:00
self.spi_channel = spi_channel
self.channel = channel
self.cur_value = 0
2024-05-02 14:06:39 +08:00
self.title = title
2024-04-15 16:04:22 +08:00
self.setFrameShape(QtWidgets.QFrame.Box)
self.setFrameShadow(QtWidgets.QFrame.Raised)
grid = QtWidgets.QGridLayout()
grid.setContentsMargins(0, 0, 0, 0)
grid.setHorizontalSpacing(0)
grid.setVerticalSpacing(0)
self.setLayout(grid)
label = QtWidgets.QLabel("{} ch{}".format(title, channel))
label.setAlignment(QtCore.Qt.AlignCenter)
grid.addWidget(label, 1, 1)
self.value = QtWidgets.QLabel()
self.value.setAlignment(QtCore.Qt.AlignCenter)
grid.addWidget(self.value, 2, 1, 6, 1)
grid.setRowStretch(1, 1)
grid.setRowStretch(2, 0)
grid.setRowStretch(3, 1)
self.refresh_display()
2017-10-26 02:26:59 +08:00
def refresh_display(self):
self.value.setText("<font size=\"4\">{:.3f}</font><font size=\"2\"> %</font>"
2024-04-10 10:43:44 +08:00
.format(self.cur_value * 100 / 2**16))
2017-10-26 02:26:59 +08:00
def sort_key(self):
2024-05-02 14:10:47 +08:00
return (2, self.spi_channel, self.channel)
2017-10-26 02:26:59 +08:00
2024-05-02 14:06:39 +08:00
def uid(self):
return (self.title, self.channel)
2024-05-02 14:56:49 +08:00
def to_model_path(self):
return "dac/{} ch{}".format(self.title, self.channel)
2017-10-26 02:26:59 +08:00
_WidgetDesc = namedtuple("_WidgetDesc", "uid comment cls arguments")
2017-02-27 00:59:31 +08:00
def setup_from_ddb(ddb):
mi_addr = None
mi_port = None
dds_sysclk = None
description = set()
for k, v in ddb.items():
2015-06-05 19:11:41 +08:00
try:
if isinstance(v, dict):
comment = v.get("comment")
if v["type"] == "local":
2022-03-19 22:50:36 +08:00
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)
2024-04-10 10:43:44 +08:00
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"]
2024-04-10 10:43:44 +08:00
widget = _WidgetDesc(k, comment, _DDSWidget,
(k, bus_channel, channel, v["class"], dds_sysclk))
description.add(widget)
2024-04-10 10:43:44 +08:00
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"]
2022-07-07 10:52:53 +08:00
pll = v["arguments"]["pll_n"]
refclk = ddb[dds_cpld]["arguments"]["refclk"]
clk_div = v["arguments"].get("clk_div", 0)
2024-04-10 10:43:44 +08:00
widget = _WidgetDesc(k, comment, _DDSWidget,
(k, bus_channel, channel, v["class"],
refclk, dds_cpld, pll, clk_div))
2024-04-10 10:43:44 +08:00
description.add(widget)
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):
2024-04-10 10:43:44 +08:00
widget = _WidgetDesc((k, channel), comment, _DACWidget,
(spi_channel, channel, k))
description.add(widget)
2022-03-19 22:50:36 +08:00
elif v["type"] == "controller" and k == "core_moninj":
mi_addr = v["host"]
mi_port = v.get("port_proxy", 1383)
2015-06-05 19:11:41 +08:00
except KeyError:
pass
2022-07-07 10:52:53 +08:00
return mi_addr, mi_port, description
class _DeviceManager:
def __init__(self, schedule_ctl):
self.mi_addr = None
self.mi_port = None
self.reconnect_mi = asyncio.Event()
self.mi_connection = None
self.mi_connector_task = asyncio.ensure_future(self.mi_connector())
self.schedule_ctl = schedule_ctl
self.ddb = dict()
self.description = set()
self.widgets_by_uid = dict()
self.dds_sysclk = 0
self.ttl_widgets = dict()
self.dds_widgets = dict()
self.dac_widgets = dict()
2024-05-02 14:20:36 +08:00
self.channels_cb = lambda: None
def init_ddb(self, ddb):
self.ddb = ddb
def notify_ddb(self, mod):
2022-07-07 10:52:53 +08:00
mi_addr, mi_port, description = setup_from_ddb(self.ddb)
if (mi_addr, mi_port) != (self.mi_addr, self.mi_port):
self.mi_addr = mi_addr
self.mi_port = mi_port
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]
2024-06-05 16:24:00 +08:00
self.setup_monitoring(False, widget)
widget.deleteLater()
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
2024-05-02 14:20:36 +08:00
if description != self.description:
self.channels_cb()
self.description = description
2017-02-27 00:59:31 +08:00
def ttl_set_mode(self, channel, mode):
if self.mi_connection is not None:
2024-06-05 16:24:00 +08:00
widget_uid = self.ttl_widgets[channel]
widget = self.widgets_by_uid[widget_uid]
2017-02-27 00:59:31 +08:00
if mode == "0":
widget.cur_override = True
widget.cur_level = False
self.mi_connection.inject(channel, TTLOverride.level.value, 0)
self.mi_connection.inject(channel, TTLOverride.oe.value, 1)
self.mi_connection.inject(channel, TTLOverride.en.value, 1)
2017-02-27 00:59:31 +08:00
elif mode == "1":
widget.cur_override = True
widget.cur_level = True
self.mi_connection.inject(channel, TTLOverride.level.value, 1)
self.mi_connection.inject(channel, TTLOverride.oe.value, 1)
self.mi_connection.inject(channel, TTLOverride.en.value, 1)
2017-02-27 00:59:31 +08:00
elif mode == "exp":
widget.cur_override = False
self.mi_connection.inject(channel, TTLOverride.en.value, 0)
2017-02-27 00:59:31 +08:00
else:
raise ValueError
# override state may have changed
widget.refresh_display()
2017-02-27 00:59:31 +08:00
async def _submit_by_content(self, content, class_name, title):
expid = {
"log_level": logging.WARNING,
"content": content,
"class_name": class_name,
"arguments": {}
}
scheduling = {
"pipeline_name": "main",
"priority": 0,
"due_date": None,
"flush": False
}
rid = await self.schedule_ctl.submit(
scheduling["pipeline_name"],
expid,
scheduling["priority"], scheduling["due_date"],
scheduling["flush"])
logger.info("Submitted '%s', RID is %d", title, rid)
def _dds_faux_injection(self, dds_channel, dds_model, action, title, log_msg):
# create kernel and fill it in and send-by-content
# initialize CPLD (if applicable)
2022-07-07 10:52:53 +08:00
if dds_model.is_urukul:
# urukuls need CPLD init and switch to on
cpld_dev = """self.setattr_device("core_cache")
2022-07-07 10:52:53 +08:00
self.setattr_device("{}")""".format(dds_model.cpld)
2024-04-10 10:43:44 +08:00
# `sta`/`rf_sw`` variables are guaranteed for urukuls
# so {action} can use it
# if there's no RF enabled, CPLD may have not been initialized
# but if there is, it has been initialised - no need to do again
cpld_init = """delay(15*ms)
was_init = self.core_cache.get("_{cpld}_init")
sta = self.{cpld}.sta_read()
rf_sw = urukul_sta_rf_sw(sta)
if rf_sw == 0 and len(was_init) == 0:
delay(15*ms)
self.{cpld}.init()
self.core_cache.put("_{cpld}_init", [1])
2022-07-07 10:52:53 +08:00
""".format(cpld=dds_model.cpld)
else:
cpld_dev = ""
cpld_init = ""
# AD9912/9910: init channel (if uninitialized)
if dds_model.dds_type == "AD9912":
# 0xFF before init, 0x99 after
channel_init = """
if self.{dds_channel}.read({cfgreg}, length=1) == 0xFF:
delay(10*ms)
self.{dds_channel}.init()
""".format(dds_channel=dds_channel, cfgreg=AD9912_SER_CONF)
elif dds_model.dds_type == "AD9910":
# -1 before init, 2 after
channel_init = """
if self.{dds_channel}.read32({cfgreg}) == -1:
delay(10*ms)
self.{dds_channel}.init()
""".format(dds_channel=dds_channel, cfgreg=AD9912_SER_CONF)
else:
channel_init = "self.{dds_channel}.init()".format(dds_channel=dds_channel)
dds_exp = textwrap.dedent("""
from artiq.experiment import *
from artiq.coredevice.urukul import *
class {title}(EnvExperiment):
def build(self):
self.setattr_device("core")
self.setattr_device("{dds_channel}")
{cpld_dev}
@kernel
def run(self):
self.core.break_realtime()
{cpld_init}
delay(10*ms)
{channel_init}
delay(15*ms)
{action}
""".format(title=title, action=action,
dds_channel=dds_channel,
cpld_dev=cpld_dev, cpld_init=cpld_init,
channel_init=channel_init))
asyncio.ensure_future(
self._submit_by_content(
2024-04-10 10:43:44 +08:00
dds_exp,
title,
log_msg))
def dds_set_frequency(self, dds_channel, dds_model, freq):
action = "self.{ch}.set({freq})".format(
freq=freq, ch=dds_channel)
if dds_model.is_urukul:
action += """
ch_no = self.{ch}.chip_select - 4
self.{cpld}.cfg_switches(rf_sw | 1 << ch_no)
""".format(ch=dds_channel, cpld=dds_model.cpld)
self._dds_faux_injection(
dds_channel,
dds_model,
action,
2024-04-10 10:43:44 +08:00
"SetDDS",
"Set DDS {} {}MHz".format(dds_channel, freq / 1e6))
def dds_channel_toggle(self, dds_channel, dds_model, sw=True):
# urukul only
if sw:
switch = "| 1 << ch_no"
else:
switch = "& ~(1 << ch_no)"
action = """
ch_no = self.{dds_channel}.chip_select - 4
self.{cpld}.cfg_switches(rf_sw {switch})
""".format(
dds_channel=dds_channel,
cpld=dds_model.cpld,
switch=switch
)
self._dds_faux_injection(
dds_channel,
dds_model,
action,
2024-04-10 10:43:44 +08:00
"ToggleDDS",
"Toggle DDS {} {}".format(dds_channel, "on" if sw else "off"))
2024-06-05 16:24:00 +08:00
def setup_monitoring(self, enable, widget):
if isinstance(widget, _TTLWidget):
key = widget.channel
args = (key,)
subscribers = self.ttl_widgets
subscribe_func = self.setup_ttl_monitoring
elif isinstance(widget, _DDSWidget):
key = (widget.bus_channel, widget.channel)
args = key
subscribers = self.dds_widgets
subscribe_func = self.setup_dds_monitoring
elif isinstance(widget, _DACWidget):
key = (widget.spi_channel, widget.channel)
args = key
subscribers = self.dac_widgets
subscribe_func = self.setup_dac_monitoring
else:
raise ValueError
if enable and key not in subscribers:
subscribers[key] = widget.uid()
subscribe_func(enable, *args)
elif not enable and key in subscribers:
subscribe_func(enable, *args)
del subscribers[key]
2017-02-27 00:59:31 +08:00
def setup_ttl_monitoring(self, enable, channel):
if self.mi_connection is not None:
self.mi_connection.monitor_probe(enable, channel, TTLProbe.level.value)
self.mi_connection.monitor_probe(enable, channel, TTLProbe.oe.value)
self.mi_connection.monitor_injection(enable, channel, TTLOverride.en.value)
self.mi_connection.monitor_injection(enable, channel, TTLOverride.level.value)
2017-02-27 11:45:04 +08:00
if enable:
self.mi_connection.get_injection_status(channel, TTLOverride.en.value)
2017-02-27 00:59:31 +08:00
def setup_dds_monitoring(self, enable, bus_channel, channel):
if self.mi_connection is not None:
self.mi_connection.monitor_probe(enable, bus_channel, channel)
2017-02-27 00:59:31 +08:00
2017-10-26 02:26:59 +08:00
def setup_dac_monitoring(self, enable, spi_channel, channel):
if self.mi_connection is not None:
self.mi_connection.monitor_probe(enable, spi_channel, channel)
2017-10-26 02:26:59 +08:00
2017-02-27 00:59:31 +08:00
def monitor_cb(self, channel, probe, value):
if channel in self.ttl_widgets:
2024-06-05 16:24:00 +08:00
widget_uid = self.ttl_widgets[channel]
widget = self.widgets_by_uid[widget_uid]
if probe == TTLProbe.level.value:
2017-02-27 00:59:31 +08:00
widget.cur_level = bool(value)
elif probe == TTLProbe.oe.value:
2017-02-27 00:59:31 +08:00
widget.cur_oe = bool(value)
widget.refresh_display()
2022-07-07 10:52:53 +08:00
elif (channel, probe) in self.dds_widgets:
2024-06-05 16:24:00 +08:00
widget_uid = self.dds_widgets[(channel, probe)]
widget = self.widgets_by_uid[widget_uid]
2022-07-07 10:52:53 +08:00
widget.dds_model.monitor_update(probe, value)
2017-02-27 00:59:31 +08:00
widget.refresh_display()
2022-07-07 10:52:53 +08:00
elif (channel, probe) in self.dac_widgets:
2024-06-05 16:24:00 +08:00
widget_uid = self.dac_widgets[(channel, probe)]
widget = self.widgets_by_uid[widget_uid]
2017-10-26 02:26:59 +08:00
widget.cur_value = value
widget.refresh_display()
2017-02-27 00:59:31 +08:00
def injection_status_cb(self, channel, override, value):
if channel in self.ttl_widgets:
2024-06-05 16:24:00 +08:00
widget_uid = self.ttl_widgets[channel]
widget = self.widgets_by_uid[widget_uid]
if override == TTLOverride.en.value:
widget.cur_override = bool(value)
if override == TTLOverride.level.value:
widget.cur_override_level = bool(value)
widget.refresh_display()
2017-02-27 00:59:31 +08:00
def disconnect_cb(self):
logger.error("lost connection to moninj")
self.reconnect_mi.set()
async def mi_connector(self):
2017-02-27 00:59:31 +08:00
while True:
await self.reconnect_mi.wait()
self.reconnect_mi.clear()
if self.mi_connection is not None:
await self.mi_connection.close()
self.mi_connection = None
new_mi_connection = CommMonInj(self.monitor_cb, self.injection_status_cb,
2024-04-10 10:43:44 +08:00
self.disconnect_cb)
2017-02-27 00:59:31 +08:00
try:
await new_mi_connection.connect(self.mi_addr, self.mi_port)
except Exception:
2024-04-10 10:43:44 +08:00
logger.error("failed to connect to moninj. Is aqctl_moninj_proxy running?",
exc_info=True)
await asyncio.sleep(10.)
self.reconnect_mi.set()
2017-02-27 00:59:31 +08:00
else:
2022-08-12 13:41:05 +08:00
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():
2017-02-27 00:59:31 +08:00
self.setup_ttl_monitoring(True, ttl_channel)
for bus_channel, channel in self.dds_widgets.keys():
2024-06-05 16:24:00 +08:00
self.setup_dds_monitoring(True, bus_channel, channel)
for spi_channel, channel in self.dac_widgets.keys():
2024-06-05 16:24:00 +08:00
self.setup_dac_monitoring(True, spi_channel, channel)
2017-02-27 00:59:31 +08:00
async def close(self):
self.mi_connector_task.cancel()
try:
await asyncio.wait_for(self.mi_connector_task, None)
2017-02-27 00:59:31 +08:00
except asyncio.CancelledError:
pass
if self.mi_connection is not None:
await self.mi_connection.close()
2024-05-02 14:56:49 +08:00
class Model(DictSyncTreeSepModel):
def __init__(self, init):
DictSyncTreeSepModel.__init__(self, "/", ["Channels"], init)
def clear(self):
for k in self.backing_store:
self._del_item(self, k.split(self.separator))
self.backing_store.clear()
def update(self, d):
for k, v in d.items():
self[v.to_model_path()] = v
class _AddChannelDialog(QtWidgets.QDialog):
def __init__(self, parent, model):
QtWidgets.QDialog.__init__(self, parent=parent)
self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
self.setWindowTitle("Add channels")
layout = QtWidgets.QVBoxLayout()
self.setLayout(layout)
self._model = model
self._tree_view = QtWidgets.QTreeView()
self._tree_view.setHeaderHidden(True)
self._tree_view.setSelectionBehavior(
QtWidgets.QAbstractItemView.SelectItems)
self._tree_view.setSelectionMode(
QtWidgets.QAbstractItemView.ExtendedSelection)
self._tree_view.setModel(self._model)
layout.addWidget(self._tree_view)
self._button_box = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel
)
self._button_box.setCenterButtons(True)
self._button_box.accepted.connect(self.add_channels)
self._button_box.rejected.connect(self.reject)
layout.addWidget(self._button_box)
def add_channels(self):
selection = self._tree_view.selectedIndexes()
channels = []
for select in selection:
key = self._model.index_to_key(select)
if key is not None:
channels.append(self._model[key].ref)
self.channels = channels
self.accept()
2024-05-02 14:35:59 +08:00
class _MonInjDock(QDockWidgetCloseDetect):
def __init__(self, name, manager):
QtWidgets.QDockWidget.__init__(self, "MonInj")
2016-02-14 20:46:15 +08:00
self.setObjectName(name)
self.setFeatures(QtWidgets.QDockWidget.DockWidgetMovable |
QtWidgets.QDockWidget.DockWidgetFloatable)
2024-05-02 14:43:42 +08:00
grid = LayoutWidget()
self.setWidget(grid)
2024-05-02 15:43:28 +08:00
self.manager = manager
self.widget_uids = None
2024-05-02 14:35:59 +08:00
newdock = QtWidgets.QToolButton()
newdock.setToolTip("Create new moninj dock")
newdock.setIcon(QtWidgets.QApplication.style().standardIcon(
QtWidgets.QStyle.SP_FileDialogNewFolder))
2024-05-02 15:43:28 +08:00
newdock.clicked.connect(lambda: self.manager.create_new_dock())
2024-05-02 14:35:59 +08:00
grid.addWidget(newdock, 0, 0)
2024-05-02 15:43:28 +08:00
self.channel_dialog = _AddChannelDialog(self, self.manager.channel_model)
2024-05-02 14:56:49 +08:00
self.channel_dialog.accepted.connect(self.add_channels)
dialog_btn = QtWidgets.QToolButton()
dialog_btn.setToolTip("Add channels")
dialog_btn.setIcon(
QtWidgets.QApplication.style().standardIcon(
QtWidgets.QStyle.SP_FileDialogListView))
dialog_btn.clicked.connect(self.channel_dialog.open)
grid.addWidget(dialog_btn, 0, 1)
2024-05-02 15:24:36 +08:00
self.label = DoubleClickLineEdit(name)
self.label.setStyleSheet("background:transparent;")
grid.addWidget(self.label, 0, 2)
2024-05-02 16:01:44 +08:00
scroll_area = VDragScrollArea(self)
2024-05-02 14:35:59 +08:00
grid.addWidget(scroll_area, 1, 0, 1, 10)
2024-05-02 16:01:44 +08:00
self.flow = DragDropFlowLayoutWidget()
2016-03-17 18:18:31 +08:00
scroll_area.setWidgetResizable(True)
2024-05-02 16:01:44 +08:00
scroll_area.setWidget(self.flow)
2024-05-02 17:02:52 +08:00
self.flow.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.flow.customContextMenuRequested.connect(self.custom_context_menu)
def custom_context_menu(self, pos):
index = self.flow._get_index(pos)
if index == -1:
return
menu = QtWidgets.QMenu()
delete_action = QtWidgets.QAction("Delete widget", menu)
delete_action.triggered.connect(partial(self.delete_widget, index))
menu.addAction(delete_action)
menu.exec_(self.flow.mapToGlobal(pos))
2024-06-05 16:24:00 +08:00
def delete_all_widgets(self):
for index in reversed(range(self.flow.count())):
self.delete_widget(index, True)
2024-05-02 17:02:52 +08:00
def delete_widget(self, index, checked):
widget = self.flow.itemAt(index).widget()
widget.hide()
2024-06-05 16:24:00 +08:00
self.manager.dm.setup_monitoring(False, widget)
2024-05-02 17:02:52 +08:00
self.flow.layout.takeAt(index)
2015-06-12 17:41:04 +08:00
2024-05-02 14:56:49 +08:00
def add_channels(self):
channels = self.channel_dialog.channels
self.layout_widgets(channels)
2024-05-02 14:43:42 +08:00
def layout_widgets(self, widgets):
for widget in sorted(widgets, key=lambda w: w.sort_key()):
2024-05-02 14:56:49 +08:00
widget.show()
2024-06-05 16:24:00 +08:00
self.manager.dm.setup_monitoring(True, widget)
2024-05-02 14:43:42 +08:00
self.flow.addWidget(widget)
2024-05-02 15:43:28 +08:00
def restore_widgets(self):
if self.widget_uids is not None:
widgets_by_uid = self.manager.dm.widgets_by_uid
widgets = list()
for uid in self.widget_uids:
if uid in widgets_by_uid:
widgets.append(widgets_by_uid[uid])
else:
logger.warning("removing moninj widget {}".format(uid))
self.layout_widgets(widgets)
self.widget_uids = None
def _save_widget_uids(self):
uids = []
for i in range(self.flow.count()):
uids.append(self.flow.itemAt(i).widget().uid())
return uids
def save_state(self):
return {
"dock_label": self.label.text(),
"widget_uids": self._save_widget_uids()
}
def restore_state(self, state):
try:
label = state["dock_label"]
except KeyError:
pass
else:
self.label._text = label
self.label.setText(label)
try:
self.widget_uids = state["widget_uids"]
except KeyError:
pass
2015-06-12 17:41:04 +08:00
2017-02-27 00:59:31 +08:00
class MonInj:
2024-05-02 14:35:59 +08:00
def __init__(self, schedule_ctl, main_window):
self.docks = dict()
self.main_window = main_window
self.dm = _DeviceManager(schedule_ctl)
2024-05-02 14:56:49 +08:00
self.dm.channels_cb = self.add_channels
self.channel_model = Model({})
def add_channels(self):
self.channel_model.clear()
self.channel_model.update(self.dm.widgets_by_uid)
2024-05-02 15:43:28 +08:00
for dock in self.docks.values():
dock.restore_widgets()
2024-05-02 14:35:59 +08:00
def create_new_dock(self, add_to_area=True):
n = 0
name = "moninj0"
while name in self.docks:
n += 1
name = "moninj" + str(n)
dock = _MonInjDock(name, self)
self.docks[name] = dock
if add_to_area:
self.main_window.addDockWidget(QtCore.Qt.RightDockWidgetArea, dock)
dock.setFloating(True)
dock.sigClosed.connect(partial(self.on_dock_closed, name))
self.update_closable()
return dock
def on_dock_closed(self, name):
dock = self.docks[name]
del self.docks[name]
self.update_closable()
2024-06-05 16:24:00 +08:00
dock.delete_all_widgets()
dock.hide() # dock may be parent, only delete on exit
2024-05-02 14:35:59 +08:00
def update_closable(self):
flags = (QtWidgets.QDockWidget.DockWidgetMovable |
QtWidgets.QDockWidget.DockWidgetFloatable)
if len(self.docks) > 1:
flags |= QtWidgets.QDockWidget.DockWidgetClosable
for dock in self.docks.values():
dock.setFeatures(flags)
def first_moninj_dock(self):
if self.docks:
return None
dock = self.create_new_dock(False)
return dock
2024-05-02 15:43:28 +08:00
def save_state(self):
return {name: dock.save_state() for name, dock in self.docks.items()}
def restore_state(self, state):
if self.docks:
raise NotImplementedError
for name, dock_state in state.items():
dock = _MonInjDock(name, self)
self.docks[name] = dock
dock.restore_state(dock_state)
self.main_window.addDockWidget(QtCore.Qt.RightDockWidgetArea, dock)
dock.sigClosed.connect(partial(self.on_dock_closed, name))
self.update_closable()
2015-10-03 19:28:57 +08:00
async def stop(self):
2017-02-27 00:59:31 +08:00
if self.dm is not None:
await self.dm.close()