33
33
from adafruit_register .i2c_bits import RWBits
34
34
from adafruit_register .i2c_bit import RWBit
35
35
36
+ try :
37
+ from typing import Tuple , Union , Optional
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_TLA202x.git"
38
44
@@ -45,7 +51,9 @@ class CV:
45
51
"""struct helper"""
46
52
47
53
@classmethod
48
- def add_values (cls , value_tuples ):
54
+ def add_values (
55
+ cls , value_tuples : Tuple [str , int , Union [float , str ], Optional [float ]]
56
+ ) -> None :
49
57
"creates CV entires"
50
58
cls .string = {}
51
59
cls .lsb = {}
@@ -57,7 +65,7 @@ def add_values(cls, value_tuples):
57
65
cls .lsb [value ] = lsb
58
66
59
67
@classmethod
60
- def is_valid (cls , value ) :
68
+ def is_valid (cls , value : str ) -> bool :
61
69
"Returns true if the given value is a member of the CV"
62
70
return value in cls .string
63
71
@@ -209,7 +217,7 @@ class TLA2024: # pylint:disable=too-many-instance-attributes
209
217
210
218
I2C Interface for analog voltage measurements using the TI TLA2024 12-bit 4-channel ADC
211
219
212
- :param i2c_bus: The I2C bus that the ADC is on.
220
+ :param ~I2C i2c_bus: The I2C bus that the ADC is on.
213
221
:param int address: The I2C address for the ADC. Defaults to ~0x48
214
222
"""
215
223
@@ -221,7 +229,7 @@ class TLA2024: # pylint:disable=too-many-instance-attributes
221
229
_mode = RWBit (_CONFIG_REG , 8 , 2 , lsb_first = False )
222
230
_data_rate = RWBits (3 , _CONFIG_REG , 5 , 2 , lsb_first = False )
223
231
224
- def __init__ (self , i2c_bus , address = _TLA_DEFAULT_ADDRESS ):
232
+ def __init__ (self , i2c_bus : I2C , address : int = _TLA_DEFAULT_ADDRESS ) -> None :
225
233
226
234
# pylint:disable=no-member
227
235
@@ -234,35 +242,31 @@ def __init__(self, i2c_bus, address=_TLA_DEFAULT_ADDRESS):
234
242
self .range = Range .RANGE_6_144V
235
243
236
244
@property
237
- def voltage (self ):
245
+ def voltage (self ) -> float :
238
246
"""The voltage between the two selected inputs"""
239
247
if self .mode == Mode .ONE_SHOT : # pylint:disable=no-member
240
248
return self ._last_one_shot
241
249
return self ._read_volts ()
242
250
243
251
@property
244
- def input_channel (self ):
245
- """The channel to be sampled """
252
+ def input_channel (self ) -> int :
253
+ """The input channel number (0-4) to measure the voltage at, referenced to GND. """
246
254
return self ._mux
247
255
248
256
@input_channel .setter
249
- def input_channel (self , channel ):
250
- """The input number to measure the voltage at, referenced to GND.
251
-
252
- :param channel: The channel number to switch to, from 0-4"""
253
-
257
+ def input_channel (self , channel : int ) -> None :
254
258
if channel not in range (4 ):
255
259
raise AttributeError ("input_channel must be set to a number from 0 to 3" )
256
260
self ._mux = 4 + channel
257
261
258
262
@property
259
- def mode (self ):
263
+ def mode (self ) -> int :
260
264
"""The measurement mode of the sensor. Must be a :py:const:`~Mode`. See the documentation
261
265
for :py:const:`~Mode` for more information"""
262
266
return self ._mode
263
267
264
268
@mode .setter
265
- def mode (self , mode ) :
269
+ def mode (self , mode : int ) -> None :
266
270
if not Mode .is_valid (mode ):
267
271
raise AttributeError ("mode must be a valid Mode" )
268
272
if mode == Mode .CONTINUOUS : # pylint:disable=no-member
@@ -277,59 +281,59 @@ def mode(self, mode):
277
281
self ._last_one_shot = self ._read_volts ()
278
282
279
283
@property
280
- def range (self ):
284
+ def range (self ) -> int :
281
285
"""The measurement range of the ADC, changed by adjusting the Programmable Gain Amplifier
282
286
`range` must be a :py:const:`~Range`. See the documentation for :py:const:`~Range`
283
287
for more information"""
284
288
return self ._pga
285
289
286
290
@range .setter
287
- def range (self , measurement_range ) :
291
+ def range (self , measurement_range : int ) -> None :
288
292
if not Range .is_valid (measurement_range ):
289
293
raise AttributeError ("range must be a valid Range" )
290
294
self ._pga = measurement_range
291
295
292
296
@property
293
- def data_rate (self ):
294
- """selects the rate at which measurement samples are taken. Must be a :py:const:`~DataRate`
297
+ def data_rate (self ) -> int :
298
+ """Selects the rate at which measurement samples are taken. Must be a :py:const:`~DataRate`
295
299
. See the documentation for :py:const:`~DataRate` for more information"""
296
300
return self ._data_rate
297
301
298
302
@data_rate .setter
299
- def data_rate (self , rate ) :
303
+ def data_rate (self , rate : int ) -> None :
300
304
if not DataRate .is_valid (rate ): # pylint:disable=no-member
301
305
raise AttributeError ("data_rate must be a valid DataRate" )
302
306
self ._data_rate = rate
303
307
304
308
@property
305
- def mux (self ):
309
+ def mux (self ) -> int :
306
310
"""selects the inputs that voltage will be measured between. Must be a
307
311
:py:const:`~adafruit_tla202x.Mux`. See the :py:const:`~adafruit_tla202x.Mux` documentation
308
312
for more information about the available options"""
309
313
return self ._mux
310
314
311
315
@mux .setter
312
- def mux (self , mux_connection ) :
316
+ def mux (self , mux_connection : int ) -> None :
313
317
if not Mux .is_valid (mux_connection ): # pylint:disable=no-member
314
318
raise AttributeError ("mux must be a valid Mux" )
315
319
self ._mux = mux_connection
316
320
317
- def read (self , channel ) :
321
+ def read (self , channel : int ) -> int :
318
322
"""Switch to the given channel and take a single ADC reading in One Shot mode
319
323
320
- :param channel: The channel number to switch to, from 0-3
324
+ :param int channel: The channel number to switch to, from 0-3
321
325
322
326
"""
323
327
if not self .input_channel == channel :
324
328
self .input_channel = channel
325
329
self .mode = Mode .ONE_SHOT # pylint:disable=no-member
326
330
return self ._read_adc ()
327
331
328
- def _read_volts (self ):
332
+ def _read_volts (self ) -> float :
329
333
value_lsb = self ._read_adc ()
330
334
return value_lsb * Range .lsb [self .range ] / 1000.0
331
335
332
- def _read_adc (self ):
336
+ def _read_adc (self ) -> int :
333
337
value_lsb = self ._raw_adc_read
334
338
value_lsb >>= 4
335
339
0 commit comments