Skip to content

Commit 36bb4e6

Browse files
committed
unified access to channel-specific registers
1 parent fd30159 commit 36bb4e6

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

adafruit_ina3221.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,10 @@
3838

3939
# Register Definitions
4040
CONFIGURATION = 0x00
41-
SHUNTVOLTAGE_CH1 = 0x01
42-
BUSVOLTAGE_CH1 = 0x02
43-
SHUNTVOLTAGE_CH2 = 0x03
44-
BUSVOLTAGE_CH2 = 0x04
45-
SHUNTVOLTAGE_CH3 = 0x05
46-
BUSVOLTAGE_CH3 = 0x06
47-
CRITICAL_ALERT_LIMIT_CH1 = 0x07
48-
WARNING_ALERT_LIMIT_CH1 = 0x08
49-
CRITICAL_ALERT_LIMIT_CH2 = 0x09
50-
WARNING_ALERT_LIMIT_CH2 = 0x0A
51-
CRITICAL_ALERT_LIMIT_CH3 = 0x0B
52-
WARNING_ALERT_LIMIT_CH3 = 0x0C
41+
SHUNTVOLTAGE_REGS = [0x01, 0x03, 0x05]
42+
BUSTVOLTAGE_REGS = [0x02, 0x04, 0x06]
43+
CRITICAL_ALERT_LIMIT_REGS = [0x07, 0x09, 0x0B]
44+
WARNING_ALERT_LIMIT_REGS = [0x08, 0x0A, 0x0C]
5345
SHUNTVOLTAGE_SUM = 0x0D
5446
SHUNTVOLTAGE_SUM_LIMIT = 0x0E
5547
MASK_ENABLE = 0x0F
@@ -170,7 +162,7 @@ def enable(self) -> None:
170162
@property
171163
def bus_voltage(self) -> float:
172164
"""Bus voltage in volts."""
173-
reg_address = [BUSVOLTAGE_CH1, BUSVOLTAGE_CH2, BUSVOLTAGE_CH3][self._channel]
165+
reg_address = BUSVOLTAGE_REGS[self._channel]
174166
result = self._device._read_register(reg_address, 2)
175167
raw_value = int.from_bytes(result, "big")
176168
voltage = (raw_value >> 3) * 8e-3
@@ -179,9 +171,7 @@ def bus_voltage(self) -> float:
179171
@property
180172
def shunt_voltage(self) -> float:
181173
"""Shunt voltage in millivolts."""
182-
reg_address = [SHUNTVOLTAGE_CH1, SHUNTVOLTAGE_CH2, SHUNTVOLTAGE_CH3][
183-
self._channel
184-
]
174+
reg_address = SHUNTVOLTAGE_REGS[self._channel]
185175
result = self._device._read_register(reg_address, 2)
186176
raw_value = int.from_bytes(result, "big")
187177
raw_value = (
@@ -217,15 +207,15 @@ def critical_alert_threshold(self) -> float:
217207
Returns:
218208
float: The current critical alert threshold in amperes.
219209
"""
220-
reg_addr = CRITICAL_ALERT_LIMIT_CH1 + 2 * self._channel
210+
reg_addr = CRITICAL_ALERT_LIMIT_REGS[self._channel]
221211
result = self._device._read_register(reg_addr, 2)
222212
threshold = int.from_bytes(result, "big")
223213
return (threshold >> 3) * 40e-6 / self._shunt_resistance
224214

225215
@critical_alert_threshold.setter
226216
def critical_alert_threshold(self, current: float) -> None:
227217
threshold = int(current * self._shunt_resistance / 40e-6 * 8)
228-
reg_addr = CRITICAL_ALERT_LIMIT_CH1 + 2 * self._channel
218+
reg_addr = CRITICAL_ALERT_LIMIT_REGS[self._channel]
229219
threshold_bytes = threshold.to_bytes(2, "big")
230220
self._device._write_register(reg_addr, threshold_bytes)
231221

@@ -236,15 +226,15 @@ def warning_alert_threshold(self) -> float:
236226
Returns:
237227
float: The current warning alert threshold in amperes.
238228
"""
239-
reg_addr = WARNING_ALERT_LIMIT_CH1 + self._channel
229+
reg_addr = WARNING_ALERT_LIMIT_REGS[self._channel]
240230
result = self._device._read_register(reg_addr, 2)
241231
threshold = int.from_bytes(result, "big")
242232
return threshold / (self._shunt_resistance * 8)
243233

244234
@warning_alert_threshold.setter
245235
def warning_alert_threshold(self, current: float) -> None:
246236
threshold = int(current * self._shunt_resistance * 8)
247-
reg_addr = WARNING_ALERT_LIMIT_CH1 + self._channel
237+
reg_addr = WARNING_ALERT_LIMIT_REGS[self._channel]
248238
threshold_bytes = threshold.to_bytes(2, "big")
249239
self._device._write_register(reg_addr, threshold_bytes)
250240

0 commit comments

Comments
 (0)