|
2 | 2 | from hardwarelibrary.daq import AnalogIOProtocol, DigitalIOProtocol |
3 | 3 | import u3 |
4 | 4 |
|
| 5 | + |
5 | 6 | class LabjackDevice(PhysicalDevice, AnalogIOProtocol, DigitalIOProtocol): |
| 7 | + """LabJack U3 (LV and HV). Use self.dev for features beyond the wrapped methods.""" |
| 8 | + |
6 | 9 | classIdVendor = 0x0cd5 |
7 | 10 | classIdProduct = 0x003 |
| 11 | + |
8 | 12 | def __init__(self, serialNumber="*", idProduct=0x003, idVendor=0x0cd5): |
9 | 13 | super().__init__(serialNumber, idProduct=idProduct, idVendor=idVendor) |
10 | 14 | self.dev = None |
11 | 15 |
|
12 | 16 | def doInitializeDevice(self): |
13 | 17 | self.dev = u3.U3(autoOpen=False) |
14 | | - self.dev.open() |
| 18 | + if self.serialNumber == "*": |
| 19 | + self.dev.open() |
| 20 | + else: |
| 21 | + self.dev.open(firstFound=False, serial=int(self.serialNumber)) |
15 | 22 | self.dev.configU3() |
| 23 | + self.dev.getCalibrationData() |
16 | 24 |
|
17 | 25 | def doShutdownDevice(self): |
18 | 26 | self.dev.close() |
19 | 27 |
|
20 | | - def setConfiguration(self, parameters:dict): |
21 | | - raise NotImplementedError("You must use default parameters or call the U3's labjack.dev functions directly") |
| 28 | + @property |
| 29 | + def isHighVoltage(self): |
| 30 | + return hasattr(self.dev, 'hardwareVersion') and self.dev.hardwareVersion >= 2.0 |
| 31 | + |
| 32 | + def setConfiguration(self, parameters: dict): |
| 33 | + raise NotImplementedError( |
| 34 | + "You must use default parameters or call the U3's labjack.dev functions directly" |
| 35 | + ) |
22 | 36 |
|
23 | 37 | def configuration(self): |
24 | 38 | return self.dev.configU3() |
25 | 39 |
|
| 40 | + def configureAnalogIO(self, parameters: dict): |
| 41 | + self.dev.configIO(**parameters) |
| 42 | + |
| 43 | + def configureDigitalIO(self, parameters: dict): |
| 44 | + self.dev.configIO(**parameters) |
| 45 | + |
26 | 46 | def getAnalogVoltage(self, channel): |
27 | | - value = self.dev.getAIN(channel) |
28 | | - return value |
| 47 | + return self.dev.getAIN(channel) |
29 | 48 |
|
30 | 49 | def setAnalogVoltage(self, value, channel): |
31 | | - # Writes the dac. |
32 | 50 | if channel == 0: |
33 | 51 | register = 5000 |
34 | 52 | elif channel == 1: |
35 | 53 | register = 5002 |
| 54 | + else: |
| 55 | + raise ValueError(f"DAC channel must be 0 or 1, got {channel}") |
36 | 56 | self.dev.writeRegister(register, value) |
37 | 57 |
|
38 | 58 | def setDigitalValue(self, value, channel): |
39 | 59 | self.dev.setDOState(channel, value) |
40 | 60 |
|
41 | 61 | def getDigitalValue(self, channel): |
42 | 62 | return self.dev.getDIState(channel) != 0 |
| 63 | + |
| 64 | + def getTemperature(self): |
| 65 | + """Returns Kelvin.""" |
| 66 | + return self.dev.getTemperature() |
| 67 | + |
| 68 | + def toggleLED(self): |
| 69 | + self.dev.toggleLED() |
| 70 | + |
| 71 | + |
| 72 | +class DebugLabjackDevice(LabjackDevice): |
| 73 | + """Hardware-free U3 for tests. DAC channels 0,1 loop back to AIN 0,1.""" |
| 74 | + |
| 75 | + classIdProduct = 0xFFFB |
| 76 | + classIdVendor = 0xFFFF |
| 77 | + |
| 78 | + def __init__(self, serialNumber='debug'): |
| 79 | + PhysicalDevice.__init__( |
| 80 | + self, serialNumber=serialNumber, |
| 81 | + idProduct=self.classIdProduct, idVendor=self.classIdVendor |
| 82 | + ) |
| 83 | + self.dev = None |
| 84 | + self._analogValues = {} |
| 85 | + self._digitalValues = {} |
| 86 | + self._temperature = 298.0 |
| 87 | + |
| 88 | + def doInitializeDevice(self): |
| 89 | + pass |
| 90 | + |
| 91 | + def doShutdownDevice(self): |
| 92 | + pass |
| 93 | + |
| 94 | + @property |
| 95 | + def isHighVoltage(self): |
| 96 | + return False |
| 97 | + |
| 98 | + def setConfiguration(self, parameters: dict): |
| 99 | + raise NotImplementedError( |
| 100 | + "You must use default parameters or call the U3's labjack.dev functions directly" |
| 101 | + ) |
| 102 | + |
| 103 | + def configuration(self): |
| 104 | + return {'DeviceName': 'DebugU3', 'SerialNumber': 0} |
| 105 | + |
| 106 | + def configureAnalogIO(self, parameters: dict): |
| 107 | + pass |
| 108 | + |
| 109 | + def configureDigitalIO(self, parameters: dict): |
| 110 | + pass |
| 111 | + |
| 112 | + def getAnalogVoltage(self, channel): |
| 113 | + return self._analogValues.get(channel, 0.0) |
| 114 | + |
| 115 | + def setAnalogVoltage(self, value, channel): |
| 116 | + if channel not in (0, 1): |
| 117 | + raise ValueError(f"DAC channel must be 0 or 1, got {channel}") |
| 118 | + self._analogValues[channel] = value |
| 119 | + |
| 120 | + def setDigitalValue(self, value, channel): |
| 121 | + self._digitalValues[channel] = bool(value) |
| 122 | + |
| 123 | + def getDigitalValue(self, channel): |
| 124 | + return self._digitalValues.get(channel, False) |
| 125 | + |
| 126 | + def getTemperature(self): |
| 127 | + return self._temperature |
| 128 | + |
| 129 | + def toggleLED(self): |
| 130 | + pass |
0 commit comments