forked from M-Labs/artiq
1
0
Fork 0
artiq/artiq/gui/moninj.py

355 lines
12 KiB
Python
Raw Normal View History

import asyncio
import logging
import socket
import struct
2015-06-05 19:11:41 +08:00
from operator import itemgetter
2016-02-15 07:23:47 +08:00
from PyQt5 import QtCore, QtWidgets
from artiq.tools import TaskObject
2015-06-12 17:41:04 +08:00
from artiq.protocols.sync_struct import Subscriber
2016-03-17 18:18:31 +08:00
from artiq.gui.flowlayout import FlowLayout
logger = logging.getLogger(__name__)
2015-06-06 00:03:30 +08:00
_mode_enc = {
"exp": 0,
"1": 1,
"0": 2,
"in": 3
}
2016-03-17 18:18:31 +08:00
_widget_size = QtCore.QSize(350, 300)
2015-06-06 00:03:30 +08:00
2016-02-15 07:23:47 +08:00
class _TTLWidget(QtWidgets.QFrame):
def __init__(self, channel, send_to_device, force_out, title):
2015-06-06 00:03:30 +08:00
self.channel = channel
self.send_to_device = send_to_device
2015-06-05 19:11:41 +08:00
self.force_out = force_out
2016-02-15 07:23:47 +08:00
QtWidgets.QFrame.__init__(self)
2016-03-17 18:18:31 +08:00
self.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
2015-06-05 19:11:41 +08:00
self.setFrameShape(QtWidgets.QFrame.Box)
2016-02-15 07:23:47 +08:00
self.setFrameShadow(QtWidgets.QFrame.Raised)
2015-06-05 19:11:41 +08:00
2016-02-15 07:23:47 +08:00
grid = QtWidgets.QGridLayout()
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)
2015-09-02 04:25:20 +08:00
label.setWordWrap(True)
2015-06-05 19:11:41 +08:00
grid.addWidget(label, 1, 1)
2016-02-15 07:23:47 +08:00
self._direction = QtWidgets.QLabel()
2015-06-05 19:11:41 +08:00
self._direction.setAlignment(QtCore.Qt.AlignCenter)
grid.addWidget(self._direction, 2, 1)
2016-02-15 07:23:47 +08:00
self._override = QtWidgets.QLabel()
2015-06-09 20:05:39 +08:00
self._override.setAlignment(QtCore.Qt.AlignCenter)
grid.addWidget(self._override, 3, 1)
2016-02-15 07:23:47 +08:00
self._value = QtWidgets.QLabel()
2015-06-06 00:03:30 +08:00
self._value.setAlignment(QtCore.Qt.AlignCenter)
2015-06-09 20:05:39 +08:00
grid.addWidget(self._value, 4, 1, 6, 1)
2015-06-05 19:11:41 +08:00
2015-06-06 00:03:30 +08:00
self._value.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
2016-02-15 07:23:47 +08:00
menu = QtWidgets.QActionGroup(self._value)
2015-06-06 00:03:30 +08:00
menu.setExclusive(True)
2016-02-15 07:23:47 +08:00
self._expctl_action = QtWidgets.QAction("Experiment controlled", self._value)
2015-06-06 00:03:30 +08:00
self._expctl_action.setCheckable(True)
menu.addAction(self._expctl_action)
self._value.addAction(self._expctl_action)
2015-06-12 17:41:04 +08:00
self._expctl_action.triggered.connect(lambda: self.set_mode("exp"))
2016-02-15 07:23:47 +08:00
separator = QtWidgets.QAction(self._value)
2015-06-12 17:41:04 +08:00
separator.setSeparator(True)
self._value.addAction(separator)
2016-02-15 07:23:47 +08:00
self._force1_action = QtWidgets.QAction("Force 1", self._value)
2015-06-06 00:03:30 +08:00
self._force1_action.setCheckable(True)
menu.addAction(self._force1_action)
self._value.addAction(self._force1_action)
2015-06-12 17:41:04 +08:00
self._force1_action.triggered.connect(lambda: self.set_mode("1"))
2016-02-15 07:23:47 +08:00
self._force0_action = QtWidgets.QAction("Force 0", self._value)
2015-06-06 00:03:30 +08:00
self._force0_action.setCheckable(True)
menu.addAction(self._force0_action)
self._value.addAction(self._force0_action)
2015-06-12 17:41:04 +08:00
self._force0_action.triggered.connect(lambda: self.set_mode("0"))
2016-02-15 07:23:47 +08:00
self._forcein_action = QtWidgets.QAction("Force input", self._value)
2015-06-06 00:03:30 +08:00
self._forcein_action.setCheckable(True)
self._forcein_action.setEnabled(not force_out)
menu.addAction(self._forcein_action)
self._value.addAction(self._forcein_action)
2015-06-12 17:41:04 +08:00
self._forcein_action.triggered.connect(lambda: self.set_mode("in"))
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, 0)
grid.setRowStretch(5, 1)
2015-06-06 00:03:30 +08:00
self.set_value(0, False, False)
2016-03-17 18:18:31 +08:00
def sizeHint(self):
return _widget_size
2015-06-12 17:41:04 +08:00
def set_mode(self, mode):
2015-06-06 00:03:30 +08:00
data = struct.pack("bbb",
2, # MONINJ_REQ_TTLSET
self.channel, _mode_enc[mode])
self.send_to_device(data)
2015-06-05 19:11:41 +08:00
def set_value(self, value, oe, override):
2015-06-06 00:03:30 +08:00
value_s = "1" if value else "0"
2015-06-05 19:11:41 +08:00
if 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\""
2015-06-09 20:05:39 +08:00
self._override.setText("<font size=\"1\" color=\"red\">OVERRIDE</font>")
2015-06-05 19:11:41 +08:00
else:
color = ""
2015-06-09 20:05:39 +08:00
self._override.setText("")
2015-06-05 19:11:41 +08:00
self._value.setText("<font size=\"9\"{}>{}</font>".format(
2015-06-06 00:03:30 +08:00
color, value_s))
2015-06-05 19:11:41 +08:00
oe = oe or self.force_out
direction = "OUT" if oe else "IN"
self._direction.setText("<font size=\"1\">" + direction + "</font>")
2015-06-06 00:03:30 +08:00
if override:
if oe:
if value:
self._force1_action.setChecked(True)
else:
self._force0_action.setChecked(True)
else:
self._forcein_action.setChecked(True)
else:
self._expctl_action.setChecked(True)
2015-06-05 19:11:41 +08:00
2016-02-15 07:23:47 +08:00
class _DDSWidget(QtWidgets.QFrame):
def __init__(self, bus_channel, channel, sysclk, title):
self.bus_channel = bus_channel
self.channel = channel
2015-06-20 05:30:17 +08:00
self.sysclk = sysclk
2015-06-12 17:41:04 +08:00
2016-02-15 07:23:47 +08:00
QtWidgets.QFrame.__init__(self)
2016-03-17 18:18:31 +08:00
self.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
2015-06-12 17:41:04 +08:00
self.setFrameShape(QtWidgets.QFrame.Box)
2016-02-15 07:23:47 +08:00
self.setFrameShadow(QtWidgets.QFrame.Raised)
2015-06-12 17:41:04 +08:00
2016-02-15 07:23:47 +08:00
grid = QtWidgets.QGridLayout()
2015-06-12 17:41:04 +08:00
self.setLayout(grid)
2016-02-15 07:23:47 +08:00
label = QtWidgets.QLabel(title)
2015-06-12 17:41:04 +08:00
label.setAlignment(QtCore.Qt.AlignCenter)
2015-09-02 04:25:20 +08:00
label.setWordWrap(True)
2015-06-12 17:41:04 +08:00
grid.addWidget(label, 1, 1)
2016-02-15 07:23:47 +08:00
self._value = QtWidgets.QLabel()
2015-06-12 17:41:04 +08:00
self._value.setAlignment(QtCore.Qt.AlignCenter)
2015-09-02 03:26:49 +08:00
self._value.setWordWrap(True)
2015-06-20 05:30:17 +08:00
grid.addWidget(self._value, 2, 1, 6, 1)
2015-06-12 17:41:04 +08:00
2015-09-02 04:25:20 +08:00
grid.setRowStretch(1, 1)
grid.setRowStretch(2, 0)
grid.setRowStretch(3, 1)
2015-06-20 05:30:17 +08:00
self.set_value(0)
2015-06-12 17:41:04 +08:00
2016-03-17 18:18:31 +08:00
def sizeHint(self):
return _widget_size
2015-06-20 05:30:17 +08:00
def set_value(self, ftw):
frequency = ftw*self.sysclk()/2**32
2015-06-20 07:55:01 +08:00
self._value.setText("<font size=\"6\">{:.7f} MHz</font>"
2015-06-20 07:36:46 +08:00
.format(float(frequency)/1e6))
2015-06-12 17:41:04 +08:00
class _DeviceManager:
2015-06-06 00:03:30 +08:00
def __init__(self, send_to_device, init):
self.dds_sysclk = 0
2015-06-06 00:03:30 +08:00
self.send_to_device = send_to_device
2015-06-05 19:11:41 +08:00
self.ddb = dict()
self.ttl_cb = lambda: None
self.ttl_widgets = dict()
2015-06-12 17:41:04 +08:00
self.dds_cb = lambda: None
self.dds_widgets = dict()
2015-06-05 19:11:41 +08:00
for k, v in init.items():
self[k] = v
def get_dds_sysclk(self):
return self.dds_sysclk
def __setitem__(self, k, v):
2015-06-05 19:11:41 +08:00
if k in self.ttl_widgets:
del self[k]
if k in self.dds_widgets:
del self[k]
self.ddb[k] = v
2015-06-05 19:11:41 +08:00
if not isinstance(v, dict):
return
try:
2015-06-12 17:41:04 +08:00
if v["type"] == "local":
2015-09-02 04:25:20 +08:00
title = k
if "comment" in v:
title += ": " + v["comment"]
2015-06-12 17:41:04 +08:00
if v["module"] == "artiq.coredevice.ttl":
channel = v["arguments"]["channel"]
force_out = v["class"] == "TTLOut"
self.ttl_widgets[k] = _TTLWidget(
channel, self.send_to_device, force_out, title)
2015-06-12 17:41:04 +08:00
self.ttl_cb()
if (v["module"] == "artiq.coredevice.dds"
and v["class"] == "CoreDDS"):
self.dds_sysclk = v["arguments"]["sysclk"]
2015-06-12 17:41:04 +08:00
if (v["module"] == "artiq.coredevice.dds"
2015-07-14 23:28:26 +08:00
and v["class"] in {"AD9858", "AD9914"}):
bus_channel = v["arguments"]["bus_channel"]
2015-06-12 17:41:04 +08:00
channel = v["arguments"]["channel"]
self.dds_widgets[channel] = _DDSWidget(
bus_channel, channel, self.get_dds_sysclk, title)
2015-06-12 17:41:04 +08:00
self.dds_cb()
2015-06-05 19:11:41 +08:00
except KeyError:
pass
def __delitem__(self, k):
del self.ddb[k]
if k in self.ttl_widgets:
self.ttl_widgets[k].deleteLater()
2015-06-05 19:11:41 +08:00
del self.ttl_widgets[k]
self.ttl_cb()
if k in self.dds_widgets:
self.dds_widgets[k].deleteLater()
del self.dds_widgets[k]
self.dds_cb()
def get_core_addr(self):
try:
2015-06-05 19:11:41 +08:00
comm = self.ddb["comm"]
while isinstance(comm, str):
comm = self.ddb[comm]
return comm["arguments"]["host"]
except KeyError:
return None
class _MonInjDock(QtWidgets.QDockWidget):
2015-06-12 17:41:04 +08:00
def __init__(self, name):
QtWidgets.QDockWidget.__init__(self, name)
2016-02-14 20:46:15 +08:00
self.setObjectName(name)
self.setFeatures(QtWidgets.QDockWidget.DockWidgetMovable |
QtWidgets.QDockWidget.DockWidgetFloatable)
2015-06-05 19:11:41 +08:00
2015-06-12 17:41:04 +08:00
def layout_widgets(self, widgets):
2016-02-15 07:57:15 +08:00
scroll_area = QtWidgets.QScrollArea()
self.setWidget(scroll_area)
2016-03-17 18:18:31 +08:00
grid = FlowLayout()
2016-02-15 07:57:15 +08:00
grid_widget = QtWidgets.QWidget()
grid_widget.setLayout(grid)
2016-03-17 18:18:31 +08:00
for _, w in sorted(widgets, key=itemgetter(0)):
grid.addWidget(w)
2016-02-15 07:57:15 +08:00
2016-03-17 18:18:31 +08:00
scroll_area.setWidgetResizable(True)
2016-02-15 07:57:15 +08:00
scroll_area.setWidget(grid_widget)
2015-06-12 17:41:04 +08:00
class MonInj(TaskObject):
def __init__(self):
self.ttl_dock = _MonInjDock("TTL")
self.dds_dock = _MonInjDock("DDS")
self.subscriber = Subscriber("devices", self.init_devices)
self.dm = None
2015-06-12 17:41:04 +08:00
self.transport = None
2015-10-03 19:28:57 +08:00
async def start(self, server, port):
loop = asyncio.get_event_loop()
2015-10-03 19:28:57 +08:00
await loop.create_datagram_endpoint(lambda: self,
2015-06-05 19:11:41 +08:00
family=socket.AF_INET)
2015-06-12 17:41:04 +08:00
try:
2015-10-03 19:28:57 +08:00
await self.subscriber.connect(server, port)
2015-06-12 17:41:04 +08:00
try:
TaskObject.start(self)
except:
2015-10-03 19:28:57 +08:00
await self.subscriber.close()
2015-06-12 17:41:04 +08:00
raise
except:
self.transport.close()
raise
2015-10-03 19:28:57 +08:00
async def stop(self):
await TaskObject.stop(self)
await self.subscriber.close()
if self.transport is not None:
self.transport.close()
self.transport = None
def connection_made(self, transport):
self.transport = transport
def datagram_received(self, data, addr):
if self.dm is None:
logger.debug("received datagram, but device manager "
"is not present yet")
return
2015-06-20 05:30:17 +08:00
try:
hlen = 8*3+4
(ttl_levels, ttl_oes, ttl_overrides,
dds_rtio_first_channel, dds_channels_per_bus) = \
struct.unpack(">QQQHH", data[:hlen])
for w in self.dm.ttl_widgets.values():
channel = w.channel
2015-06-20 05:30:17 +08:00
w.set_value(ttl_levels & (1 << channel),
ttl_oes & (1 << channel),
ttl_overrides & (1 << channel))
dds_data = data[hlen:]
2015-06-20 05:30:17 +08:00
ndds = len(dds_data)//4
ftws = struct.unpack(">" + "I"*ndds, dds_data)
for w in self.dm.dds_widgets.values():
bus_nr = w.bus_channel - dds_rtio_first_channel
offset = dds_channels_per_bus*bus_nr + w.channel
2015-06-20 05:30:17 +08:00
try:
ftw = ftws[offset]
2015-06-20 05:30:17 +08:00
except KeyError:
pass
else:
w.set_value(ftw)
except:
logger.warning("failed to process datagram", exc_info=True)
def error_received(self, exc):
logger.warning("datagram endpoint error")
def connection_lost(self, exc):
self.transport = None
2015-06-06 00:03:30 +08:00
def send_to_device(self, data):
if self.dm is None:
logger.debug("cannot sent to device yet, no device manager")
return
2015-06-06 00:03:30 +08:00
ca = self.dm.get_core_addr()
logger.debug("core device address: %s", ca)
2015-06-06 00:03:30 +08:00
if ca is None:
logger.warning("could not find core device address")
elif self.transport is None:
logger.warning("datagram endpoint not available")
else:
self.transport.sendto(data, (ca, 3250))
2015-10-03 19:28:57 +08:00
async def _do(self):
while True:
2015-10-03 19:28:57 +08:00
await asyncio.sleep(0.2)
2015-06-06 00:03:30 +08:00
# MONINJ_REQ_MONITOR
self.send_to_device(b"\x01")
def init_devices(self, d):
2015-06-06 00:03:30 +08:00
self.dm = _DeviceManager(self.send_to_device, d)
2015-06-12 17:41:04 +08:00
self.dm.ttl_cb = lambda: self.ttl_dock.layout_widgets(
self.dm.ttl_widgets.items())
self.dm.dds_cb = lambda: self.dds_dock.layout_widgets(
self.dm.dds_widgets.items())
self.dm.ttl_cb()
self.dm.dds_cb()
return self.dm