Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/artiq
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1407a4883640
Choose a base ref
...
head repository: m-labs/artiq
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2ad063c37763
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Jan 9, 2015

  1. lda: docstring style

    fallen authored and sbourdeauducq committed Jan 9, 2015

    Verified

    This commit was signed with the committer’s verified signature.
    makenowjust Hiroya Fujinami
    Copy the full SHA
    d21211a View commit details
  2. Lda: sanity checks on attenuation value

    - Plus use of dB unit
    fallen authored and sbourdeauducq committed Jan 9, 2015
    Copy the full SHA
    2ad063c View commit details
Showing with 55 additions and 15 deletions.
  1. +54 −15 artiq/devices/lda/driver.py
  2. +1 −0 artiq/language/units.py
69 changes: 54 additions & 15 deletions artiq/devices/lda/driver.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import ctypes
import struct

from artiq.language.units import dB, check_unit, Quantity

logger = logging.getLogger("lda")

@@ -16,27 +16,43 @@ class Ldasim:

def __init__(self):
self._attenuation = None
self._att_max = 63*dB
self._att_step_size = 0.25*dB

def get_attenuation(self):
"""Reads last attenuation value set to the simulated device.
:return: Returns the attenuation value in dB, or None if it was
never set.
:rtype: float
"""

return self._attenuation

def set_attenuation(self, attenuation):
"""Stores the new attenuation value and prints it to console.
:param attenuation: The attenuation value in dB.
:type attenuation: int, float or Fraction
"""
attenuation = round(attenuation*4)/4.
print("[LDA-sim] setting attenuation to {}".format(attenuation))
self._attenuation = attenuation

if isinstance(attenuation, Quantity):
check_unit(attenuation, 'dB')
else:
att = attenuation*dB

if att > self._att_max:
raise ValueError('Cannot set attenuation {} > {}'
.format(att, self._att_max))
elif att < 0*dB:
raise ValueError('Cannot set attenuation {} < 0'.format(att))
elif att % self._att_step_size != 0*dB:
raise ValueError('Cannot set attenuation {} with step size {}'
.format(att, self._att_step_size))
else:
att = round(att.amount*4)/4. * dB
print("[LDA-sim] setting attenuation to {}".format(att))
self._attenuation = att


class Lda:
@@ -59,12 +75,21 @@ class Lda:
"LDA-602": 0x1208,
"LDA-302P-1": 0x120E,
}
_max_att = {
"LDA-102": 63*dB,
"LDA-602": 63*dB,
"LDA-302P-1": 63*dB
}
_att_step_size = {
"LDA-102": 0.5*dB,
"LDA-602": 0.5*dB,
"LDA-302P-1": 1.0*dB
}

def __init__(self, serial=None, product="LDA-102"):
"""
:param serial: The serial number.
:param product: The product identifier string: LDA-102, LDA-602 or sim.
:param product: The product identifier string: LDA-102, LDA-602.
"""

from artiq.devices.lda.hidapi import hidapi
@@ -101,8 +126,8 @@ def write(self, command, length, data=bytes()):
:param command: command ID.
:param length: number of meaningful bytes in the data array.
:param data: a byte array containing the payload of the command.
"""

# 0 is report id/padding
buf = struct.pack("BBB6s", 0, command, length, data)
res = self._check_error(self.hidapi.hid_write(self._dev, buf,
@@ -114,8 +139,8 @@ def set(self, command, data):
:param command: command ID, must have most significant bit set.
:param data: payload of the command.
"""

assert command & 0x80
assert data
self.write(command, len(data), data)
@@ -128,8 +153,8 @@ def get(self, command, length, timeout=1000):
:param int timeout: Timeout of the HID read in ms.
:return: Returns the value read from the device.
:rtype: bytes
"""

assert not command & 0x80
status = None
self.write(command, length)
@@ -148,15 +173,29 @@ def get_attenuation(self):
:return: Returns the attenuation value in dB.
:rtype: float
"""
return ord(self.get(0x0d, 1))/4.

return (ord(self.get(0x0d, 1))/4.) * dB

def set_attenuation(self, attenuation):
"""Sets attenuation value of the Lab Brick device.
:param attenuation: Attenuation value in dB.
:type attenuation: int, float or Fraction
"""
self.set(0x8d, bytes([int(round(attenuation*4))]))

if isinstance(attenuation, Quantity):
check_unit(attenuation, 'dB')
else:
att = attenuation*dB

if att > self._max_att[self.product]:
raise ValueError('Cannot set attenuation {} > {}'
.format(att, self._max_att[self.product]))
elif att < 0:
raise ValueError('Cannot set attenuation {} < 0'.format(att))
elif att % self._att_step_size[self.product] != 0:
raise ValueError('Cannot set attenuation {} with {} step size'
.format(att, self._att_step_size[self.product]))
else:
self.set(0x8d, bytes([int(round(att.amount*4))]))
1 change: 1 addition & 0 deletions artiq/language/units.py
Original file line number Diff line number Diff line change
@@ -208,6 +208,7 @@ def _register_unit(unit, prefixes):

_register_unit("s", "pnum_")
_register_unit("Hz", "_kMG")
_register_unit("dB", "_")


def check_unit(value, unit):