5858# pylint: disable=too-few-public-methods
5959
6060# Config Register (R/W)
61- _REG_CONFIG = const (0x00 )
61+ _REG_CONFIG = const (0x00 )
62+
6263
6364class BusVoltageRange :
6465 """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+
6770
6871class Gain :
6972 """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+
7479
7580class ADCResolution :
7681 """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+
8895
8996class Mode :
9097 """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+
99108
100109# SHUNT VOLTAGE REGISTER (R)
101- _REG_SHUNTVOLTAGE = const (0x01 )
110+ _REG_SHUNTVOLTAGE = const (0x01 )
102111
103112# BUS VOLTAGE REGISTER (R)
104- _REG_BUSVOLTAGE = const (0x02 )
113+ _REG_BUSVOLTAGE = const (0x02 )
105114
106115# POWER REGISTER (R)
107- _REG_POWER = const (0x03 )
116+ _REG_POWER = const (0x03 )
108117
109118# CURRENT REGISTER (R)
110- _REG_CURRENT = const (0x04 )
119+ _REG_CURRENT = const (0x04 )
111120
112121# CALIBRATION REGISTER (R/W)
113- _REG_CALIBRATION = const (0x05 )
122+ _REG_CALIBRATION = const (0x05 )
114123# pylint: enable=too-few-public-methods
115124
116125
@@ -119,6 +128,7 @@ def _to_signed(num):
119128 num -= 0x10000
120129 return num
121130
131+
122132class INA219 :
123133 """Driver for the INA219 current sensor"""
124134
@@ -164,36 +174,38 @@ def __init__(self, i2c_bus, addr=0x40):
164174 self .set_calibration_32V_2A ()
165175
166176 # 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 )
173183
174184 # shunt voltage register
175- raw_shunt_voltage = ROUnaryStruct (_REG_SHUNTVOLTAGE , ">h" )
185+ raw_shunt_voltage = ROUnaryStruct (_REG_SHUNTVOLTAGE , ">h" )
176186
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 )
181191
182192 # 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" )
185195
186196 # calibration register
187- _raw_calibration = UnaryStruct (_REG_CALIBRATION , ">H" )
197+ _raw_calibration = UnaryStruct (_REG_CALIBRATION , ">H" )
188198
189199 @property
190200 def calibration (self ):
191201 """Calibration register (cached value)"""
192- return self ._cal_value # return cached value
202+ return self ._cal_value # return cached value
193203
194204 @calibration .setter
195205 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+ )
197209 self ._raw_calibration = self ._cal_value
198210
199211 @property
@@ -231,7 +243,7 @@ def power(self):
231243 # Now we can safely read the CURRENT register!
232244 return self .raw_power * self ._power_lsb
233245
234- def set_calibration_32V_2A (self ): # pylint: disable=invalid-name
246+ def set_calibration_32V_2A (self ): # pylint: disable=invalid-name
235247 """Configures to INA219 to be able to measure up to 32V and 2A of current. Counter
236248 overflow occurs at 3.2A.
237249
@@ -264,7 +276,7 @@ def set_calibration_32V_2A(self): # pylint: disable=invalid-name
264276 # 4. Choose an LSB between the min and max values
265277 # (Preferrably a roundish number close to MinLSB)
266278 # 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
268280
269281 # 5. Compute the calibration register
270282 # Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
@@ -275,7 +287,7 @@ def set_calibration_32V_2A(self): # pylint: disable=invalid-name
275287 # 6. Calculate the power LSB
276288 # PowerLSB = 20 * CurrentLSB
277289 # 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
279291
280292 # 7. Compute the maximum current and shunt voltage values before overflow
281293 #
@@ -312,7 +324,7 @@ def set_calibration_32V_2A(self): # pylint: disable=invalid-name
312324 self .shunt_adc_resolution = ADCResolution .ADCRES_12BIT_1S
313325 self .mode = Mode .SANDBVOLT_CONTINUOUS
314326
315- def set_calibration_32V_1A (self ): # pylint: disable=invalid-name
327+ def set_calibration_32V_1A (self ): # pylint: disable=invalid-name
316328 """Configures to INA219 to be able to measure up to 32V and 1A of current. Counter overflow
317329 occurs at 1.3A.
318330
@@ -344,7 +356,7 @@ def set_calibration_32V_1A(self): # pylint: disable=invalid-name
344356 # 4. Choose an LSB between the min and max values
345357 # (Preferrably a roundish number close to MinLSB)
346358 # CurrentLSB = 0.0000400 (40uA per bit)
347- self ._current_lsb = 0.04 # In milliamps
359+ self ._current_lsb = 0.04 # In milliamps
348360
349361 # 5. Compute the calibration register
350362 # Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
@@ -394,7 +406,7 @@ def set_calibration_32V_1A(self): # pylint: disable=invalid-name
394406 self .shunt_adc_resolution = ADCResolution .ADCRES_12BIT_1S
395407 self .mode = Mode .SANDBVOLT_CONTINUOUS
396408
397- def set_calibration_16V_400mA (self ): # pylint: disable=invalid-name
409+ def set_calibration_16V_400mA (self ): # pylint: disable=invalid-name
398410 """Configures to INA219 to be able to measure up to 16V and 400mA of current. Counter
399411 overflow occurs at 1.6A.
400412
@@ -477,7 +489,7 @@ def set_calibration_16V_400mA(self): # pylint: disable=invalid-name
477489 self .shunt_adc_resolution = ADCResolution .ADCRES_12BIT_1S
478490 self .mode = Mode .SANDBVOLT_CONTINUOUS
479491
480- def set_calibration_16V_5A (self ): # pylint: disable=invalid-name
492+ def set_calibration_16V_5A (self ): # pylint: disable=invalid-name
481493 """Configures to INA219 to be able to measure up to 16V and 5000mA of current. Counter
482494 overflow occurs at 8.0A.
483495
0 commit comments