From 1b7f71bda9f297bc6a0b03bb6c3c92c401c47b71 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 22 Mar 2015 00:48:15 +0100 Subject: [PATCH] controllers: consistent device/simulation specification --- artiq/devices/lda/driver.py | 6 ++-- artiq/devices/novatech409b/driver.py | 4 +-- artiq/frontend/lda_controller.py | 13 +++++---- artiq/frontend/novatech409b_controller.py | 9 ++---- artiq/frontend/pdq2_controller.py | 7 +++-- artiq/frontend/thorlabs_tcube_controller.py | 32 +++++++++------------ doc/manual/developing_a_ndsp.rst | 4 +-- examples/master/ddb.pyon | 2 +- 8 files changed, 35 insertions(+), 42 deletions(-) diff --git a/artiq/devices/lda/driver.py b/artiq/devices/lda/driver.py index fc1a9ea54..4e6742e2e 100644 --- a/artiq/devices/lda/driver.py +++ b/artiq/devices/lda/driver.py @@ -13,8 +13,7 @@ class HidError(Exception): class Ldasim: - """Lab Brick Digital Attenuator simulation driver. - """ + """Lab Brick Digital Attenuator simulation driver.""" def __init__(self): self._attenuation = None @@ -117,8 +116,7 @@ class Lda: raise IOError def close(self): - """Close the device. - """ + """Close the device.""" self.hidapi.hid_close(self._dev) def get_att_step_size(self): diff --git a/artiq/devices/novatech409b/driver.py b/artiq/devices/novatech409b/driver.py index c0e3b10ee..5db76a665 100644 --- a/artiq/devices/novatech409b/driver.py +++ b/artiq/devices/novatech409b/driver.py @@ -22,8 +22,8 @@ class Novatech409B: # maximum frequency of Novatech 409B when using PLL and external reference max_freq_with_pll = 171.1276031 - def __init__(self, serial_dev="/dev/ttyUSB0"): - if serial_dev == "sim": + def __init__(self, serial_dev): + if serial_dev is None: self.simulation = True else: self.simulation = False diff --git a/artiq/frontend/lda_controller.py b/artiq/frontend/lda_controller.py index 2d3925758..225700cca 100755 --- a/artiq/frontend/lda_controller.py +++ b/artiq/frontend/lda_controller.py @@ -10,11 +10,12 @@ from artiq.tools import verbosity_args, simple_network_args, init_logger def get_argparser(): parser = argparse.ArgumentParser( description="ARTIQ controller for the Lab Brick Digital Attenuator") - parser.add_argument("-d", "--device", default="LDA-102", - choices=["LDA-102", "LDA-602", "sim"]) + parser.add_argument("-P", "--product", default="LDA-102", + choices=["LDA-102", "LDA-602"]) simple_network_args(parser, 3253) - parser.add_argument("-s", "--serial", default=None, - help="USB serial number of the device") + parser.add_argument("-d", "--device", default=None, + help="USB serial number of the device." + " Omit for simulation mode.") verbosity_args(parser) return parser @@ -22,10 +23,10 @@ def get_argparser(): def main(): args = get_argparser().parse_args() init_logger(args) - if args.device == "sim": + if args.device is None: lda = Ldasim() else: - lda = Lda(args.serial, args.device) + lda = Lda(args.serial, args.product) simple_server_loop({"lda": lda}, args.bind, args.port) diff --git a/artiq/frontend/novatech409b_controller.py b/artiq/frontend/novatech409b_controller.py index c23a43419..86b0e5f84 100755 --- a/artiq/frontend/novatech409b_controller.py +++ b/artiq/frontend/novatech409b_controller.py @@ -20,11 +20,8 @@ def get_argparser(): " 409B 4-channel DDS box") simple_network_args(parser, 3254) parser.add_argument( - "-s", "--serial-dev", - default="/dev/ttyUSB0", type=str, - help="serial port: on Windows \"COMx\"," - " on Linux a device path (e.g. \"/dev/ttyUSB0\")." - " Use \"sim\" for simulation mode.") + "-d", "--device", default=None, + help="serial port. Omit for simulation mode.") verbosity_args(parser) return parser @@ -32,7 +29,7 @@ def main(): args = get_argparser().parse_args() init_logger(args) - dev = Novatech409B(args.serial_dev) + dev = Novatech409B(args.device) try: simple_server_loop( {"novatech409b": dev}, args.bind, args.port) diff --git a/artiq/frontend/pdq2_controller.py b/artiq/frontend/pdq2_controller.py index b1bdf682e..6404d7d7f 100755 --- a/artiq/frontend/pdq2_controller.py +++ b/artiq/frontend/pdq2_controller.py @@ -11,9 +11,10 @@ def get_argparser(): parser = argparse.ArgumentParser(description="PDQ2 controller") simple_network_args(parser, 3252) parser.add_argument( - "-d", "--device", required=True, help="device url/path/name") + "-d", "--device", default=None, + help="serial port. Omit for simulation mode.") parser.add_argument( - "-u", "--dump", default=None, + "--dump", default="pdq2_dump.bin", help="file to dump pdq2 data into, for later simulation") verbosity_args(parser) return parser @@ -23,7 +24,7 @@ def main(): args = get_argparser().parse_args() init_logger(args) port = None - if args.dump: + if args.device is None: port = open(args.dump, "wb") dev = Pdq2(url=args.device, dev=port) try: diff --git a/artiq/frontend/thorlabs_tcube_controller.py b/artiq/frontend/thorlabs_tcube_controller.py index d7046d06e..bc5c32e23 100755 --- a/artiq/frontend/thorlabs_tcube_controller.py +++ b/artiq/frontend/thorlabs_tcube_controller.py @@ -9,16 +9,15 @@ from artiq.tools import verbosity_args, init_logger def get_argparser(): parser = argparse.ArgumentParser() - parser.add_argument("-d", "--device", required=True, + parser.add_argument("-P", "--product", required=True, help="type of the Thorlabs T-Cube device to control", - choices=["TDC001", "TPZ001", "TDCSim", "TPZSim"]) + choices=["TDC001", "TPZ001"]) parser.add_argument("--bind", default="::1", help="hostname or IP address to bind to") parser.add_argument("-p", "--port", default=3255, type=int, help="TCP port to listen to") - parser.add_argument("-s", "--serial", default="/dev/ttyUSB0", - help="serial port: on Windows \"COMx\", on Linux a " - "device path (e.g. \"/dev/ttyUSB0\").") + parser.add_argument("-d", "--device", default=None, + help="serial port. Omit for simulation mode.") verbosity_args(parser) return parser @@ -27,21 +26,18 @@ def main(): args = get_argparser().parse_args() init_logger(args) - devname = args.device.lower() - - if devname == "tdc001": - dev = Tdc(args.serial) - elif devname == "tpz001": - dev = Tpz(args.serial) - elif devname == "tdcsim": - dev = TdcSim() - elif devname == "tpzsim": - dev = TpzSim() + if args.device is None: + if args.product == "TDC001": + dev = TdcSim() + elif args.product == "TPZ001": + dev = TpzSim() else: - raise ValueError("Device can be either TDC001, TPZ001, TDCSim" - " or TPZSim") + if args.product == "TDC001": + dev = Tdc(args.device) + elif args.product == "TPZ001": + dev = Tpz(args.device) - simple_server_loop({devname: dev}, args.bind, args.port) + simple_server_loop({args.product.lower(): dev}, args.bind, args.port) if __name__ == "__main__": main() diff --git a/doc/manual/developing_a_ndsp.rst b/doc/manual/developing_a_ndsp.rst index 734df6f9e..98e24e15e 100644 --- a/doc/manual/developing_a_ndsp.rst +++ b/doc/manual/developing_a_ndsp.rst @@ -175,9 +175,9 @@ General guidelines * Do not use ``__del__`` to implement the cleanup code of your driver. Instead, define a ``close`` method, and call it using a ``try...finally...`` block in the controller. * Format your source code according to PEP8. We suggest using ``flake8`` to check for compliance. * Use new-style formatting (``str.format``) except for logging where it is not well supported, and double quotes for strings. -* The device identification (e.g. serial number) to attach to must be passed as a command-line parameter to the controller. We suggest using ``-s`` and ``--serial`` as parameter name. +* The device identification (e.g. serial number, or entry in ``/dev``) to attach to must be passed as a command-line parameter to the controller. We suggest using ``-d`` and ``--device`` as parameter name. * Controllers must be able to operate in "simulation" mode, where they behave properly even if the associated hardware is not connected. For example, they can print the data to the console instead of sending it to the device, or dump it into a file. -* We suggest that the simulation mode is entered by using "sim" in place of the serial number or device name. +* We suggest that the simulation mode is entered whenever the ``-d/--device`` option is omitted. * Keep command line parameters consistent across clients/controllers. When adding new command line options, look for a client/controller that does a similar thing and follow its use of ``argparse``. If the original client/controller could use ``argparse`` in a better way, improve it. * Use docstrings for all public methods of the driver (note that those will be retrieved by ``artiq_rpctool``). * Choose a free default TCP port and add it to the default port list in this manual. diff --git a/examples/master/ddb.pyon b/examples/master/ddb.pyon index ed1a4dff0..467b58755 100644 --- a/examples/master/ddb.pyon +++ b/examples/master/ddb.pyon @@ -120,7 +120,7 @@ "host": "::1", "port": 3253, "target_name": "lda", - "command": "lda_controller -p {port} --bind {bind} -d sim" + "command": "lda_controller -p {port} --bind {bind}" }, "pmt": "pmt0",