58
58
# pylint: disable=too-few-public-methods
59
59
60
60
# Config Register (R/W)
61
- _REG_CONFIG = const (0x00 )
61
+ _REG_CONFIG = const (0x00 )
62
+
62
63
63
64
class BusVoltageRange :
64
65
"""Constants for ``bus_voltage_range``"""
65
- RANGE_16V = 0x00 # set bus voltage range to 16V
66
- RANGE_32V = 0x01 # set bus voltage range to 32V (default)
66
+
67
+ RANGE_16V = 0x00 # set bus voltage range to 16V
68
+ RANGE_32V = 0x01 # set bus voltage range to 32V (default)
69
+
67
70
68
71
class Gain :
69
72
"""Constants for ``gain``"""
70
- DIV_1_40MV = 0x00 # shunt prog. gain set to 1, 40 mV range
71
- DIV_2_80MV = 0x01 # shunt prog. gain set to /2, 80 mV range
72
- DIV_4_160MV = 0x02 # shunt prog. gain set to /4, 160 mV range
73
- DIV_8_320MV = 0x03 # shunt prog. gain set to /8, 320 mV range
73
+
74
+ DIV_1_40MV = 0x00 # shunt prog. gain set to 1, 40 mV range
75
+ DIV_2_80MV = 0x01 # shunt prog. gain set to /2, 80 mV range
76
+ DIV_4_160MV = 0x02 # shunt prog. gain set to /4, 160 mV range
77
+ DIV_8_320MV = 0x03 # shunt prog. gain set to /8, 320 mV range
78
+
74
79
75
80
class ADCResolution :
76
81
"""Constants for ``bus_adc_resolution`` or ``shunt_adc_resolution``"""
77
- ADCRES_9BIT_1S = 0x00 # 9bit, 1 sample, 84us
78
- ADCRES_10BIT_1S = 0x01 # 10bit, 1 sample, 148us
79
- ADCRES_11BIT_1S = 0x02 # 11 bit, 1 sample, 276us
80
- ADCRES_12BIT_1S = 0x03 # 12 bit, 1 sample, 532us
81
- ADCRES_12BIT_2S = 0x09 # 12 bit, 2 samples, 1.06ms
82
- ADCRES_12BIT_4S = 0x0A # 12 bit, 4 samples, 2.13ms
83
- ADCRES_12BIT_8S = 0x0B # 12bit, 8 samples, 4.26ms
84
- ADCRES_12BIT_16S = 0x0C # 12bit, 16 samples, 8.51ms
85
- ADCRES_12BIT_32S = 0x0D # 12bit, 32 samples, 17.02ms
86
- ADCRES_12BIT_64S = 0x0E # 12bit, 64 samples, 34.05ms
87
- ADCRES_12BIT_128S = 0x0F # 12bit, 128 samples, 68.10ms
82
+
83
+ ADCRES_9BIT_1S = 0x00 # 9bit, 1 sample, 84us
84
+ ADCRES_10BIT_1S = 0x01 # 10bit, 1 sample, 148us
85
+ ADCRES_11BIT_1S = 0x02 # 11 bit, 1 sample, 276us
86
+ ADCRES_12BIT_1S = 0x03 # 12 bit, 1 sample, 532us
87
+ ADCRES_12BIT_2S = 0x09 # 12 bit, 2 samples, 1.06ms
88
+ ADCRES_12BIT_4S = 0x0A # 12 bit, 4 samples, 2.13ms
89
+ ADCRES_12BIT_8S = 0x0B # 12bit, 8 samples, 4.26ms
90
+ ADCRES_12BIT_16S = 0x0C # 12bit, 16 samples, 8.51ms
91
+ ADCRES_12BIT_32S = 0x0D # 12bit, 32 samples, 17.02ms
92
+ ADCRES_12BIT_64S = 0x0E # 12bit, 64 samples, 34.05ms
93
+ ADCRES_12BIT_128S = 0x0F # 12bit, 128 samples, 68.10ms
94
+
88
95
89
96
class Mode :
90
97
"""Constants for ``mode``"""
91
- POWERDOW = 0x00 # power down
92
- SVOLT_TRIGGERED = 0x01 # shunt voltage triggered
93
- BVOLT_TRIGGERED = 0x02 # bus voltage triggered
94
- SANDBVOLT_TRIGGERED = 0x03 # shunt and bus voltage triggered
95
- ADCOFF = 0x04 # ADC off
96
- SVOLT_CONTINUOUS = 0x05 # shunt voltage continuous
97
- BVOLT_CONTINUOUS = 0x06 # bus voltage continuous
98
- SANDBVOLT_CONTINUOUS = 0x07 # shunt and bus voltage continuous
98
+
99
+ POWERDOW = 0x00 # power down
100
+ SVOLT_TRIGGERED = 0x01 # shunt voltage triggered
101
+ BVOLT_TRIGGERED = 0x02 # bus voltage triggered
102
+ SANDBVOLT_TRIGGERED = 0x03 # shunt and bus voltage triggered
103
+ ADCOFF = 0x04 # ADC off
104
+ SVOLT_CONTINUOUS = 0x05 # shunt voltage continuous
105
+ BVOLT_CONTINUOUS = 0x06 # bus voltage continuous
106
+ SANDBVOLT_CONTINUOUS = 0x07 # shunt and bus voltage continuous
107
+
99
108
100
109
# SHUNT VOLTAGE REGISTER (R)
101
- _REG_SHUNTVOLTAGE = const (0x01 )
110
+ _REG_SHUNTVOLTAGE = const (0x01 )
102
111
103
112
# BUS VOLTAGE REGISTER (R)
104
- _REG_BUSVOLTAGE = const (0x02 )
113
+ _REG_BUSVOLTAGE = const (0x02 )
105
114
106
115
# POWER REGISTER (R)
107
- _REG_POWER = const (0x03 )
116
+ _REG_POWER = const (0x03 )
108
117
109
118
# CURRENT REGISTER (R)
110
- _REG_CURRENT = const (0x04 )
119
+ _REG_CURRENT = const (0x04 )
111
120
112
121
# CALIBRATION REGISTER (R/W)
113
- _REG_CALIBRATION = const (0x05 )
122
+ _REG_CALIBRATION = const (0x05 )
114
123
# pylint: enable=too-few-public-methods
115
124
116
125
@@ -119,6 +128,7 @@ def _to_signed(num):
119
128
num -= 0x10000
120
129
return num
121
130
131
+
122
132
class INA219 :
123
133
"""Driver for the INA219 current sensor"""
124
134
@@ -164,36 +174,38 @@ def __init__(self, i2c_bus, addr=0x40):
164
174
self .set_calibration_32V_2A ()
165
175
166
176
# config register break-up
167
- reset = RWBits ( 1 , _REG_CONFIG , 15 , 2 , False )
168
- bus_voltage_range = RWBits ( 1 , _REG_CONFIG , 13 , 2 , False )
169
- gain = RWBits ( 2 , _REG_CONFIG , 11 , 2 , False )
170
- bus_adc_resolution = RWBits ( 4 , _REG_CONFIG , 7 , 2 , False )
171
- shunt_adc_resolution = RWBits ( 4 , _REG_CONFIG , 3 , 2 , False )
172
- mode = RWBits ( 3 , _REG_CONFIG , 0 , 2 , False )
177
+ reset = RWBits (1 , _REG_CONFIG , 15 , 2 , False )
178
+ bus_voltage_range = RWBits (1 , _REG_CONFIG , 13 , 2 , False )
179
+ gain = RWBits (2 , _REG_CONFIG , 11 , 2 , False )
180
+ bus_adc_resolution = RWBits (4 , _REG_CONFIG , 7 , 2 , False )
181
+ shunt_adc_resolution = RWBits (4 , _REG_CONFIG , 3 , 2 , False )
182
+ mode = RWBits (3 , _REG_CONFIG , 0 , 2 , False )
173
183
174
184
# shunt voltage register
175
- raw_shunt_voltage = ROUnaryStruct (_REG_SHUNTVOLTAGE , ">h" )
185
+ raw_shunt_voltage = ROUnaryStruct (_REG_SHUNTVOLTAGE , ">h" )
176
186
177
- #bus voltage register
178
- raw_bus_voltage = ROBits ( 13 , _REG_BUSVOLTAGE , 3 , 2 , False )
179
- conversion_ready = ROBit ( _REG_BUSVOLTAGE , 1 , 2 , False )
180
- overflow = ROBit ( _REG_BUSVOLTAGE , 0 , 2 , False )
187
+ # bus voltage register
188
+ raw_bus_voltage = ROBits (13 , _REG_BUSVOLTAGE , 3 , 2 , False )
189
+ conversion_ready = ROBit (_REG_BUSVOLTAGE , 1 , 2 , False )
190
+ overflow = ROBit (_REG_BUSVOLTAGE , 0 , 2 , False )
181
191
182
192
# power and current registers
183
- raw_power = ROUnaryStruct (_REG_POWER , ">H" )
184
- raw_current = ROUnaryStruct (_REG_CURRENT , ">h" )
193
+ raw_power = ROUnaryStruct (_REG_POWER , ">H" )
194
+ raw_current = ROUnaryStruct (_REG_CURRENT , ">h" )
185
195
186
196
# calibration register
187
- _raw_calibration = UnaryStruct (_REG_CALIBRATION , ">H" )
197
+ _raw_calibration = UnaryStruct (_REG_CALIBRATION , ">H" )
188
198
189
199
@property
190
200
def calibration (self ):
191
201
"""Calibration register (cached value)"""
192
- return self ._cal_value # return cached value
202
+ return self ._cal_value # return cached value
193
203
194
204
@calibration .setter
195
205
def calibration (self , cal_value ):
196
- self ._cal_value = cal_value # value is cached for ``current`` and ``power`` properties
206
+ self ._cal_value = (
207
+ cal_value # value is cached for ``current`` and ``power`` properties
208
+ )
197
209
self ._raw_calibration = self ._cal_value
198
210
199
211
@property
@@ -231,7 +243,7 @@ def power(self):
231
243
# Now we can safely read the CURRENT register!
232
244
return self .raw_power * self ._power_lsb
233
245
234
- def set_calibration_32V_2A (self ): # pylint: disable=invalid-name
246
+ def set_calibration_32V_2A (self ): # pylint: disable=invalid-name
235
247
"""Configures to INA219 to be able to measure up to 32V and 2A of current. Counter
236
248
overflow occurs at 3.2A.
237
249
@@ -264,7 +276,7 @@ def set_calibration_32V_2A(self): # pylint: disable=invalid-name
264
276
# 4. Choose an LSB between the min and max values
265
277
# (Preferrably a roundish number close to MinLSB)
266
278
# CurrentLSB = 0.0001 (100uA per bit)
267
- self ._current_lsb = .1 # Current LSB = 100uA per bit
279
+ self ._current_lsb = 0 .1 # Current LSB = 100uA per bit
268
280
269
281
# 5. Compute the calibration register
270
282
# Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
@@ -275,7 +287,7 @@ def set_calibration_32V_2A(self): # pylint: disable=invalid-name
275
287
# 6. Calculate the power LSB
276
288
# PowerLSB = 20 * CurrentLSB
277
289
# PowerLSB = 0.002 (2mW per bit)
278
- self ._power_lsb = .002 # Power LSB = 2mW per bit
290
+ self ._power_lsb = 0 .002 # Power LSB = 2mW per bit
279
291
280
292
# 7. Compute the maximum current and shunt voltage values before overflow
281
293
#
@@ -312,7 +324,7 @@ def set_calibration_32V_2A(self): # pylint: disable=invalid-name
312
324
self .shunt_adc_resolution = ADCResolution .ADCRES_12BIT_1S
313
325
self .mode = Mode .SANDBVOLT_CONTINUOUS
314
326
315
- def set_calibration_32V_1A (self ): # pylint: disable=invalid-name
327
+ def set_calibration_32V_1A (self ): # pylint: disable=invalid-name
316
328
"""Configures to INA219 to be able to measure up to 32V and 1A of current. Counter overflow
317
329
occurs at 1.3A.
318
330
@@ -344,7 +356,7 @@ def set_calibration_32V_1A(self): # pylint: disable=invalid-name
344
356
# 4. Choose an LSB between the min and max values
345
357
# (Preferrably a roundish number close to MinLSB)
346
358
# CurrentLSB = 0.0000400 (40uA per bit)
347
- self ._current_lsb = 0.04 # In milliamps
359
+ self ._current_lsb = 0.04 # In milliamps
348
360
349
361
# 5. Compute the calibration register
350
362
# Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
@@ -394,7 +406,7 @@ def set_calibration_32V_1A(self): # pylint: disable=invalid-name
394
406
self .shunt_adc_resolution = ADCResolution .ADCRES_12BIT_1S
395
407
self .mode = Mode .SANDBVOLT_CONTINUOUS
396
408
397
- def set_calibration_16V_400mA (self ): # pylint: disable=invalid-name
409
+ def set_calibration_16V_400mA (self ): # pylint: disable=invalid-name
398
410
"""Configures to INA219 to be able to measure up to 16V and 400mA of current. Counter
399
411
overflow occurs at 1.6A.
400
412
@@ -477,7 +489,7 @@ def set_calibration_16V_400mA(self): # pylint: disable=invalid-name
477
489
self .shunt_adc_resolution = ADCResolution .ADCRES_12BIT_1S
478
490
self .mode = Mode .SANDBVOLT_CONTINUOUS
479
491
480
- def set_calibration_16V_5A (self ): # pylint: disable=invalid-name
492
+ def set_calibration_16V_5A (self ): # pylint: disable=invalid-name
481
493
"""Configures to INA219 to be able to measure up to 16V and 5000mA of current. Counter
482
494
overflow occurs at 8.0A.
483
495
0 commit comments