Skip to content

Commit b0ca4ba

Browse files
authored
Merge pull request #73 from tekktrik/feature/add-typing
Add typing to library
2 parents a3c9a40 + f74dda9 commit b0ca4ba

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

adafruit_ads1x15/ads1015.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ def rate_config(self):
5353
"""Rate configuration masks."""
5454
return _ADS1015_CONFIG_DR
5555

56-
def _data_rate_default(self):
56+
def _data_rate_default(self) -> int:
5757
return 1600
5858

59-
def _conversion_value(self, raw_adc):
59+
def _conversion_value(self, raw_adc: int) -> int:
6060
raw_adc = raw_adc.to_bytes(2, "big")
6161
value = struct.unpack(">h", raw_adc)[0]
6262
return value >> 4

adafruit_ads1x15/ads1115.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ def rate_config(self):
5454
"""Rate configuration masks."""
5555
return _ADS1115_CONFIG_DR
5656

57-
def _data_rate_default(self):
57+
def _data_rate_default(self) -> int:
5858
return 128
5959

60-
def _conversion_value(self, raw_adc):
60+
def _conversion_value(self, raw_adc: int) -> int:
6161
raw_adc = raw_adc.to_bytes(2, "big")
6262
value = struct.unpack(">h", raw_adc)[0]
6363
return value

adafruit_ads1x15/ads1x15.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
from micropython import const
1919
from adafruit_bus_device.i2c_device import I2CDevice
2020

21+
try:
22+
from typing import Optional
23+
from busio import I2C
24+
from microcontroller import Pin
25+
except ImportError:
26+
pass
27+
2128
_ADS1X15_DEFAULT_ADDRESS = const(0x48)
2229
_ADS1X15_POINTER_CONVERSION = const(0x00)
2330
_ADS1X15_POINTER_CONFIG = const(0x01)
@@ -49,11 +56,11 @@ class ADS1x15:
4956

5057
def __init__(
5158
self,
52-
i2c,
53-
gain=1,
54-
data_rate=None,
55-
mode=Mode.SINGLE,
56-
address=_ADS1X15_DEFAULT_ADDRESS,
59+
i2c: I2C,
60+
gain: float = 1,
61+
data_rate: Optional[int] = None,
62+
mode: Mode = Mode.SINGLE,
63+
address: int = _ADS1X15_DEFAULT_ADDRESS,
5764
):
5865
# pylint: disable=too-many-arguments
5966
self._last_pin_read = None
@@ -70,7 +77,7 @@ def data_rate(self):
7077
return self._data_rate
7178

7279
@data_rate.setter
73-
def data_rate(self, rate):
80+
def data_rate(self, rate: int):
7481
possible_rates = self.rates
7582
if rate not in possible_rates:
7683
raise ValueError("Data rate must be one of: {}".format(possible_rates))
@@ -92,7 +99,7 @@ def gain(self):
9299
return self._gain
93100

94101
@gain.setter
95-
def gain(self, gain):
102+
def gain(self, gain: float):
96103
possible_gains = self.gains
97104
if gain not in possible_gains:
98105
raise ValueError("Gain must be one of: {}".format(possible_gains))
@@ -111,12 +118,12 @@ def mode(self):
111118
return self._mode
112119

113120
@mode.setter
114-
def mode(self, mode):
121+
def mode(self, mode: Mode):
115122
if mode not in (Mode.CONTINUOUS, Mode.SINGLE):
116123
raise ValueError("Unsupported mode.")
117124
self._mode = mode
118125

119-
def read(self, pin, is_differential=False):
126+
def read(self, pin: Pin, is_differential: bool = False) -> int:
120127
"""I2C Interface for ADS1x15-based ADCs reads.
121128
122129
params:
@@ -126,19 +133,19 @@ def read(self, pin, is_differential=False):
126133
pin = pin if is_differential else pin + 0x04
127134
return self._read(pin)
128135

129-
def _data_rate_default(self):
136+
def _data_rate_default(self) -> int:
130137
"""Retrieve the default data rate for this ADC (in samples per second).
131138
Should be implemented by subclasses.
132139
"""
133140
raise NotImplementedError("Subclasses must implement _data_rate_default!")
134141

135-
def _conversion_value(self, raw_adc):
142+
def _conversion_value(self, raw_adc: int) -> int:
136143
"""Subclasses should override this function that takes the 16 raw ADC
137144
values of a conversion result and returns a signed integer value.
138145
"""
139146
raise NotImplementedError("Subclass must implement _conversion_value function!")
140147

141-
def _read(self, pin):
148+
def _read(self, pin: Pin) -> int:
142149
"""Perform an ADC read. Returns the signed integer result of the read."""
143150
# Immediately return conversion register result if in CONTINUOUS mode
144151
# and pin has not changed
@@ -174,30 +181,30 @@ def _read(self, pin):
174181

175182
return self._conversion_value(self.get_last_result(False))
176183

177-
def _conversion_complete(self):
184+
def _conversion_complete(self) -> int:
178185
"""Return status of ADC conversion."""
179186
# OS is bit 15
180187
# OS = 0: Device is currently performing a conversion
181188
# OS = 1: Device is not currently performing a conversion
182189
return self._read_register(_ADS1X15_POINTER_CONFIG) & 0x8000
183190

184-
def get_last_result(self, fast=False):
191+
def get_last_result(self, fast: bool = False) -> int:
185192
"""Read the last conversion result when in continuous conversion mode.
186193
Will return a signed integer value. If fast is True, the register
187194
pointer is not updated as part of the read. This reduces I2C traffic
188195
and increases possible read rate.
189196
"""
190197
return self._read_register(_ADS1X15_POINTER_CONVERSION, fast)
191198

192-
def _write_register(self, reg, value):
199+
def _write_register(self, reg: int, value: int):
193200
"""Write 16 bit value to register."""
194201
self.buf[0] = reg
195202
self.buf[1] = (value >> 8) & 0xFF
196203
self.buf[2] = value & 0xFF
197204
with self.i2c_device as i2c:
198205
i2c.write(self.buf)
199206

200-
def _read_register(self, reg, fast=False):
207+
def _read_register(self, reg: int, fast: bool = False) -> int:
201208
"""Read 16 bit register value. If fast is True, the pointer register
202209
is not updated.
203210
"""

adafruit_ads1x15/analog_in.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@
1111
* Author(s): Carter Nelson, adapted from MCP3xxx original by Brent Rubell
1212
"""
1313

14+
try:
15+
from typing import Optional
16+
from .ads1x15 import ADS1x15
17+
except ImportError:
18+
pass
19+
1420
_ADS1X15_DIFF_CHANNELS = {(0, 1): 0, (0, 3): 1, (1, 3): 2, (2, 3): 3}
1521
_ADS1X15_PGA_RANGE = {2 / 3: 6.144, 1: 4.096, 2: 2.048, 4: 1.024, 8: 0.512, 16: 0.256}
1622

1723

1824
class AnalogIn:
1925
"""AnalogIn Mock Implementation for ADC Reads."""
2026

21-
def __init__(self, ads, positive_pin, negative_pin=None):
27+
def __init__(
28+
self, ads: ADS1x15, positive_pin: int, negative_pin: Optional[int] = None
29+
):
2230
"""AnalogIn
2331
2432
:param ads: The ads object.

0 commit comments

Comments
 (0)