Skip to content

Commit fe1337c

Browse files
authored
Merge pull request #86 from scirelli/enhancment/add_typing
Adding typing
2 parents 1d9f141 + 588051a commit fe1337c

File tree

7 files changed

+47
-27
lines changed

7 files changed

+47
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ _build
3737

3838
# Virtual environment-specific files
3939
.env
40+
.venv
4041

4142
# MacOS-specific files
4243
*.DS_Store

adafruit_ads1x15/ads1015.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
"""
1313
import struct
1414

15+
try:
16+
from typing import Dict, List
17+
18+
from typing_extensions import Literal
19+
except ImportError:
20+
pass
21+
1522
# pylint: disable=unused-import
1623
from .ads1x15 import ADS1x15, Mode
1724

@@ -41,26 +48,25 @@ class ADS1015(ADS1x15):
4148
"""Class for the ADS1015 12 bit ADC."""
4249

4350
@property
44-
def bits(self):
51+
def bits(self) -> Literal[12]:
4552
"""The ADC bit resolution."""
4653
return 12
4754

4855
@property
49-
def rates(self):
56+
def rates(self) -> List[int]:
5057
"""Possible data rate settings."""
5158
r = list(_ADS1015_CONFIG_DR.keys())
5259
r.sort()
5360
return r
5461

5562
@property
56-
def rate_config(self):
63+
def rate_config(self) -> Dict[int, int]:
5764
"""Rate configuration masks."""
5865
return _ADS1015_CONFIG_DR
5966

60-
def _data_rate_default(self) -> int:
67+
def _data_rate_default(self) -> Literal[1600]:
6168
return 1600
6269

6370
def _conversion_value(self, raw_adc: int) -> int:
64-
raw_adc = raw_adc.to_bytes(2, "big")
65-
value = struct.unpack(">h", raw_adc)[0]
71+
value = struct.unpack(">h", raw_adc.to_bytes(2, "big"))[0]
6672
return value >> 4

adafruit_ads1x15/ads1115.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
"""
1313
import struct
1414

15+
try:
16+
from typing import Dict, List
17+
18+
from typing_extensions import Literal
19+
except ImportError:
20+
pass
21+
1522
# pylint: disable=unused-import
1623
from .ads1x15 import ADS1x15, Mode
1724

@@ -42,26 +49,25 @@ class ADS1115(ADS1x15):
4249
"""Class for the ADS1115 16 bit ADC."""
4350

4451
@property
45-
def bits(self):
52+
def bits(self) -> Literal[16]:
4653
"""The ADC bit resolution."""
4754
return 16
4855

4956
@property
50-
def rates(self):
57+
def rates(self) -> List[int]:
5158
"""Possible data rate settings."""
5259
r = list(_ADS1115_CONFIG_DR.keys())
5360
r.sort()
5461
return r
5562

5663
@property
57-
def rate_config(self):
64+
def rate_config(self) -> Dict[int, int]:
5865
"""Rate configuration masks."""
5966
return _ADS1115_CONFIG_DR
6067

61-
def _data_rate_default(self) -> int:
68+
def _data_rate_default(self) -> Literal[128]:
6269
return 128
6370

6471
def _conversion_value(self, raw_adc: int) -> int:
65-
raw_adc = raw_adc.to_bytes(2, "big")
66-
value = struct.unpack(">h", raw_adc)[0]
72+
value = struct.unpack(">h", raw_adc.to_bytes(2, "big"))[0]
6773
return value

adafruit_ads1x15/ads1x15.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15.git"
1616

1717
import time
18-
from micropython import const
18+
1919
from adafruit_bus_device.i2c_device import I2CDevice
20+
from micropython import const
2021

2122
try:
22-
from typing import Optional
23+
from typing import Dict, List, Optional
24+
2325
from busio import I2C
2426
from microcontroller import Pin
2527
except ImportError:
@@ -69,66 +71,70 @@ def __init__(
6971
i2c: I2C,
7072
gain: float = 1,
7173
data_rate: Optional[int] = None,
72-
mode: Mode = Mode.SINGLE,
74+
mode: int = Mode.SINGLE,
7375
address: int = _ADS1X15_DEFAULT_ADDRESS,
7476
):
7577
# pylint: disable=too-many-arguments
7678
self._last_pin_read = None
7779
self.buf = bytearray(3)
78-
self._data_rate = self._gain = self._mode = None
7980
self.gain = gain
8081
self.data_rate = self._data_rate_default() if data_rate is None else data_rate
8182
self.mode = mode
8283
self.i2c_device = I2CDevice(i2c, address)
8384

8485
@property
85-
def data_rate(self):
86+
def bits(self) -> int:
87+
"""The ADC bit resolution."""
88+
raise NotImplementedError("Subclass must implement bits property.")
89+
90+
@property
91+
def data_rate(self) -> int:
8692
"""The data rate for ADC conversion in samples per second."""
8793
return self._data_rate
8894

8995
@data_rate.setter
90-
def data_rate(self, rate: int):
96+
def data_rate(self, rate: int) -> None:
9197
possible_rates = self.rates
9298
if rate not in possible_rates:
9399
raise ValueError("Data rate must be one of: {}".format(possible_rates))
94100
self._data_rate = rate
95101

96102
@property
97-
def rates(self):
103+
def rates(self) -> List[int]:
98104
"""Possible data rate settings."""
99105
raise NotImplementedError("Subclass must implement rates property.")
100106

101107
@property
102-
def rate_config(self):
108+
def rate_config(self) -> Dict[int, int]:
103109
"""Rate configuration masks."""
104110
raise NotImplementedError("Subclass must implement rate_config property.")
105111

106112
@property
107-
def gain(self):
113+
def gain(self) -> float:
108114
"""The ADC gain."""
109115
return self._gain
110116

111117
@gain.setter
112-
def gain(self, gain: float):
118+
def gain(self, gain: float) -> None:
113119
possible_gains = self.gains
114120
if gain not in possible_gains:
115121
raise ValueError("Gain must be one of: {}".format(possible_gains))
116122
self._gain = gain
117123

118124
@property
119-
def gains(self):
125+
def gains(self) -> List[float]:
120126
"""Possible gain settings."""
121127
g = list(_ADS1X15_CONFIG_GAIN.keys())
122128
g.sort()
123129
return g
124130

125131
@property
126-
def mode(self):
132+
def mode(self) -> int:
127133
"""The ADC conversion mode."""
128134
return self._mode
129135

130136
@mode.setter
131-
def mode(self, mode: Mode):
137+
def mode(self, mode: int) -> None:
132138
if mode not in (Mode.CONTINUOUS, Mode.SINGLE):
133139
raise ValueError("Unsupported mode.")
134140
self._mode = mode

adafruit_ads1x15/analog_in.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ def __init__(
4848
self.is_differential = True
4949

5050
@property
51-
def value(self):
51+
def value(self) -> int:
5252
"""Returns the value of an ADC pin as an integer."""
5353
return self._ads.read(
5454
self._pin_setting, is_differential=self.is_differential
5555
) << (16 - self._ads.bits)
5656

5757
@property
58-
def voltage(self):
58+
def voltage(self) -> float:
5959
"""Returns the voltage from the ADC pin as a floating point value."""
6060
volts = self.value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767
6161
return volts

adafruit_ads1x15/py.typed

Whitespace-only changes.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-busdevice
7+
typing-extensions~=4.0

0 commit comments

Comments
 (0)