33
33
from adafruit_register .i2c_bits import ROBits , RWBits
34
34
from adafruit_register .i2c_bit import ROBit
35
35
36
+ try :
37
+ import typing # pylint: disable=unused-import
38
+ from busio import I2C
39
+ except ImportError :
40
+ pass
41
+
36
42
__version__ = "0.0.0+auto.0"
37
43
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_INA219.git"
38
44
@@ -105,7 +111,7 @@ class Mode:
105
111
# pylint: enable=too-few-public-methods
106
112
107
113
108
- def _to_signed (num ) :
114
+ def _to_signed (num : int ) -> int :
109
115
if num > 0x7FFF :
110
116
num -= 0x10000
111
117
return num
@@ -145,7 +151,7 @@ class INA219:
145
151
# raw_current RO : Current register (not scaled)
146
152
# calibration RW : calibration register (note: value is cached)
147
153
148
- def __init__ (self , i2c_bus , addr = 0x40 ):
154
+ def __init__ (self , i2c_bus : I2C , addr : int = 0x40 ) -> None :
149
155
self .i2c_device = I2CDevice (i2c_bus , addr )
150
156
self .i2c_addr = addr
151
157
@@ -179,32 +185,32 @@ def __init__(self, i2c_bus, addr=0x40):
179
185
_raw_calibration = UnaryStruct (_REG_CALIBRATION , ">H" )
180
186
181
187
@property
182
- def calibration (self ):
188
+ def calibration (self ) -> int :
183
189
"""Calibration register (cached value)"""
184
190
return self ._cal_value # return cached value
185
191
186
192
@calibration .setter
187
- def calibration (self , cal_value ) :
193
+ def calibration (self , cal_value : int ) -> None :
188
194
self ._cal_value = (
189
195
cal_value # value is cached for ``current`` and ``power`` properties
190
196
)
191
197
self ._raw_calibration = self ._cal_value
192
198
193
199
@property
194
- def shunt_voltage (self ):
200
+ def shunt_voltage (self ) -> float :
195
201
"""The shunt voltage (between V+ and V-) in Volts (so +-.327V)"""
196
202
# The least signficant bit is 10uV which is 0.00001 volts
197
203
return self .raw_shunt_voltage * 0.00001
198
204
199
205
@property
200
- def bus_voltage (self ):
206
+ def bus_voltage (self ) -> float :
201
207
"""The bus voltage (between V- and GND) in Volts"""
202
208
# Shift to the right 3 to drop CNVR and OVF and multiply by LSB
203
209
# Each least signficant bit is 4mV
204
210
return self .raw_bus_voltage * 0.004
205
211
206
212
@property
207
- def current (self ):
213
+ def current (self ) -> float :
208
214
"""The current through the shunt resistor in milliamps."""
209
215
# Sometimes a sharp load will reset the INA219, which will
210
216
# reset the cal register, meaning CURRENT and POWER will
@@ -215,7 +221,7 @@ def current(self):
215
221
return self .raw_current * self ._current_lsb
216
222
217
223
@property
218
- def power (self ):
224
+ def power (self ) -> float :
219
225
"""The power through the load in Watt."""
220
226
# Sometimes a sharp load will reset the INA219, which will
221
227
# reset the cal register, meaning CURRENT and POWER will
@@ -225,7 +231,7 @@ def power(self):
225
231
# Now we can safely read the CURRENT register!
226
232
return self .raw_power * self ._power_lsb
227
233
228
- def set_calibration_32V_2A (self ): # pylint: disable=invalid-name
234
+ def set_calibration_32V_2A (self ) -> None : # pylint: disable=invalid-name
229
235
"""Configures to INA219 to be able to measure up to 32V and 2A of current. Counter
230
236
overflow occurs at 3.2A.
231
237
@@ -306,7 +312,7 @@ def set_calibration_32V_2A(self): # pylint: disable=invalid-name
306
312
self .shunt_adc_resolution = ADCResolution .ADCRES_12BIT_1S
307
313
self .mode = Mode .SANDBVOLT_CONTINUOUS
308
314
309
- def set_calibration_32V_1A (self ): # pylint: disable=invalid-name
315
+ def set_calibration_32V_1A (self ) -> None : # pylint: disable=invalid-name
310
316
"""Configures to INA219 to be able to measure up to 32V and 1A of current. Counter overflow
311
317
occurs at 1.3A.
312
318
@@ -388,7 +394,7 @@ def set_calibration_32V_1A(self): # pylint: disable=invalid-name
388
394
self .shunt_adc_resolution = ADCResolution .ADCRES_12BIT_1S
389
395
self .mode = Mode .SANDBVOLT_CONTINUOUS
390
396
391
- def set_calibration_16V_400mA (self ): # pylint: disable=invalid-name
397
+ def set_calibration_16V_400mA (self ) -> None : # pylint: disable=invalid-name
392
398
"""Configures to INA219 to be able to measure up to 16V and 400mA of current. Counter
393
399
overflow occurs at 1.6A.
394
400
@@ -471,7 +477,7 @@ def set_calibration_16V_400mA(self): # pylint: disable=invalid-name
471
477
self .shunt_adc_resolution = ADCResolution .ADCRES_12BIT_1S
472
478
self .mode = Mode .SANDBVOLT_CONTINUOUS
473
479
474
- def set_calibration_16V_5A (self ): # pylint: disable=invalid-name
480
+ def set_calibration_16V_5A (self ) -> None : # pylint: disable=invalid-name
475
481
"""Configures to INA219 to be able to measure up to 16V and 5000mA of current. Counter
476
482
overflow occurs at 8.0A.
477
483
0 commit comments