Skip to content

Commit a74e7d0

Browse files
authored
Merge pull request #5 from tekktrik/doc/add-typing
Add type annotations, fix documentation
2 parents 8ee9e65 + a35d54e commit a74e7d0

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

adafruit_tla202x/__init__.py

+29-25
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
from adafruit_register.i2c_bits import RWBits
3434
from adafruit_register.i2c_bit import RWBit
3535

36+
try:
37+
from typing import Tuple, Union, Optional
38+
from busio import I2C
39+
except ImportError:
40+
pass
41+
3642
__version__ = "0.0.0-auto.0"
3743
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TLA202x.git"
3844

@@ -45,7 +51,9 @@ class CV:
4551
"""struct helper"""
4652

4753
@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:
4957
"creates CV entires"
5058
cls.string = {}
5159
cls.lsb = {}
@@ -57,7 +65,7 @@ def add_values(cls, value_tuples):
5765
cls.lsb[value] = lsb
5866

5967
@classmethod
60-
def is_valid(cls, value):
68+
def is_valid(cls, value: str) -> bool:
6169
"Returns true if the given value is a member of the CV"
6270
return value in cls.string
6371

@@ -209,7 +217,7 @@ class TLA2024: # pylint:disable=too-many-instance-attributes
209217
210218
I2C Interface for analog voltage measurements using the TI TLA2024 12-bit 4-channel ADC
211219
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.
213221
:param int address: The I2C address for the ADC. Defaults to ~0x48
214222
"""
215223

@@ -221,7 +229,7 @@ class TLA2024: # pylint:disable=too-many-instance-attributes
221229
_mode = RWBit(_CONFIG_REG, 8, 2, lsb_first=False)
222230
_data_rate = RWBits(3, _CONFIG_REG, 5, 2, lsb_first=False)
223231

224-
def __init__(self, i2c_bus, address=_TLA_DEFAULT_ADDRESS):
232+
def __init__(self, i2c_bus: I2C, address: int = _TLA_DEFAULT_ADDRESS) -> None:
225233

226234
# pylint:disable=no-member
227235

@@ -234,35 +242,31 @@ def __init__(self, i2c_bus, address=_TLA_DEFAULT_ADDRESS):
234242
self.range = Range.RANGE_6_144V
235243

236244
@property
237-
def voltage(self):
245+
def voltage(self) -> float:
238246
"""The voltage between the two selected inputs"""
239247
if self.mode == Mode.ONE_SHOT: # pylint:disable=no-member
240248
return self._last_one_shot
241249
return self._read_volts()
242250

243251
@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."""
246254
return self._mux
247255

248256
@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:
254258
if channel not in range(4):
255259
raise AttributeError("input_channel must be set to a number from 0 to 3")
256260
self._mux = 4 + channel
257261

258262
@property
259-
def mode(self):
263+
def mode(self) -> int:
260264
"""The measurement mode of the sensor. Must be a :py:const:`~Mode`. See the documentation
261265
for :py:const:`~Mode` for more information"""
262266
return self._mode
263267

264268
@mode.setter
265-
def mode(self, mode):
269+
def mode(self, mode: int) -> None:
266270
if not Mode.is_valid(mode):
267271
raise AttributeError("mode must be a valid Mode")
268272
if mode == Mode.CONTINUOUS: # pylint:disable=no-member
@@ -277,59 +281,59 @@ def mode(self, mode):
277281
self._last_one_shot = self._read_volts()
278282

279283
@property
280-
def range(self):
284+
def range(self) -> int:
281285
"""The measurement range of the ADC, changed by adjusting the Programmable Gain Amplifier
282286
`range` must be a :py:const:`~Range`. See the documentation for :py:const:`~Range`
283287
for more information"""
284288
return self._pga
285289

286290
@range.setter
287-
def range(self, measurement_range):
291+
def range(self, measurement_range: int) -> None:
288292
if not Range.is_valid(measurement_range):
289293
raise AttributeError("range must be a valid Range")
290294
self._pga = measurement_range
291295

292296
@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`
295299
. See the documentation for :py:const:`~DataRate` for more information"""
296300
return self._data_rate
297301

298302
@data_rate.setter
299-
def data_rate(self, rate):
303+
def data_rate(self, rate: int) -> None:
300304
if not DataRate.is_valid(rate): # pylint:disable=no-member
301305
raise AttributeError("data_rate must be a valid DataRate")
302306
self._data_rate = rate
303307

304308
@property
305-
def mux(self):
309+
def mux(self) -> int:
306310
"""selects the inputs that voltage will be measured between. Must be a
307311
:py:const:`~adafruit_tla202x.Mux`. See the :py:const:`~adafruit_tla202x.Mux` documentation
308312
for more information about the available options"""
309313
return self._mux
310314

311315
@mux.setter
312-
def mux(self, mux_connection):
316+
def mux(self, mux_connection: int) -> None:
313317
if not Mux.is_valid(mux_connection): # pylint:disable=no-member
314318
raise AttributeError("mux must be a valid Mux")
315319
self._mux = mux_connection
316320

317-
def read(self, channel):
321+
def read(self, channel: int) -> int:
318322
"""Switch to the given channel and take a single ADC reading in One Shot mode
319323
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
321325
322326
"""
323327
if not self.input_channel == channel:
324328
self.input_channel = channel
325329
self.mode = Mode.ONE_SHOT # pylint:disable=no-member
326330
return self._read_adc()
327331

328-
def _read_volts(self):
332+
def _read_volts(self) -> float:
329333
value_lsb = self._read_adc()
330334
return value_lsb * Range.lsb[self.range] / 1000.0
331335

332-
def _read_adc(self):
336+
def _read_adc(self) -> int:
333337
value_lsb = self._raw_adc_read
334338
value_lsb >>= 4
335339

0 commit comments

Comments
 (0)