Skip to content

Commit a198773

Browse files
committed
refactor: Remove magic numbers in ScienceLab.temperature
1 parent c35f7a6 commit a198773

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

pslab/sciencelab.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,26 @@ def __init__(self, device: ConnectionHandler | None = None):
4444
self.multimeter = Multimeter(device=self.device)
4545
self.power_supply = PowerSupply(device=self.device)
4646

47+
# Calibration parameters for CTMU temperature measurement.
48+
# Format: {current_source: (offset, slope)}
49+
_CTMU_TEMPERATURE_CALIBRATION = {
50+
1: (646, 1.92),
51+
2: (701.5, 1.74),
52+
3: (760, 1.56),
53+
}
54+
4755
@property
4856
def temperature(self):
4957
"""float: Temperature of the MCU in degrees Celsius."""
50-
# TODO: Get rid of magic numbers.
5158
cs = 3
5259
V = self._get_ctmu_voltage(0b11110, cs, 0)
5360

54-
if cs == 1:
55-
return (646 - V * 1000) / 1.92 # current source = 1
56-
elif cs == 2:
57-
return (701.5 - V * 1000) / 1.74 # current source = 2
58-
elif cs == 3:
59-
return (760 - V * 1000) / 1.56 # current source = 3
61+
try:
62+
offset, slope = self._CTMU_TEMPERATURE_CALIBRATION[cs]
63+
except KeyError as exc:
64+
msg = f"Unsupported CTMU current source: {cs}"
65+
raise ValueError(msg) from exc
66+
return (offset - V * 1000) / slope
6067

6168
def _get_ctmu_voltage(self, channel: int, current_range: int, tgen: bool = True):
6269
"""Control the Charge Time Measurement Unit (CTMU).

0 commit comments

Comments
 (0)