Skip to content

Commit c99a65c

Browse files
authored
Merge pull request #9 from tcfranks/main
Add Missing Type Annotations
2 parents e154a64 + 1a86e3c commit c99a65c

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

adafruit_lps2x.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
from adafruit_register.i2c_bits import RWBits, ROBits
4141
from adafruit_register.i2c_bit import RWBit
4242

43+
try:
44+
from typing import Iterable, Optional, Tuple
45+
from typing_extensions import Literal
46+
from busio import I2C
47+
except ImportError:
48+
pass
49+
4350
# _LPS2X_I2CADDR_DEFAULT = 0x5D # LPS2X default i2c address
4451
# _LPS2X_WHOAMI = 0x0F # Chip ID register
4552
# _LPS2X_PRESS_OUT_XL =(# | 0x80) ///< | 0x80 to set auto increment on multi-byte read
@@ -74,7 +81,9 @@ class CV:
7481
"""struct helper"""
7582

7683
@classmethod
77-
def add_values(cls, value_tuples):
84+
def add_values(
85+
cls, value_tuples: Iterable[Tuple[str, int, Optional[float], Optional[float]]]
86+
) -> None:
7887
"""creates CV entries"""
7988
cls.string = {}
8089
cls.lsb = {}
@@ -86,7 +95,7 @@ def add_values(cls, value_tuples):
8695
cls.lsb[value] = lsb
8796

8897
@classmethod
89-
def is_valid(cls, value):
98+
def is_valid(cls, value: int) -> bool:
9099
"""Returns true if the given value is a member of the CV"""
91100
return value in cls.string
92101

@@ -139,31 +148,35 @@ class LPS2X: # pylint: disable=too-many-instance-attributes
139148
_raw_temperature = ROUnaryStruct(_LPS2X_TEMP_OUT_L, "<h")
140149
_raw_pressure = ROBits(24, _LPS2X_PRESS_OUT_XL, 0, 3)
141150

142-
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS, chip_id=None):
151+
def __init__(
152+
self, i2c_bus: I2C, address: int = _LPS2X_DEFAULT_ADDRESS, chip_id: int = -1
153+
) -> None:
154+
if chip_id == -1:
155+
raise ValueError("Must set the chip_id argument")
143156
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address)
144157
if not self._chip_id in [chip_id]:
145158
raise RuntimeError(
146-
"Failed to find LPS2X! Found chip ID 0x%x" % self._chip_id
159+
f"Failed to find LPS2X! Found chip ID {hex(self._chip_id)}"
147160
)
148161
self.reset()
149162
self.initialize()
150163
sleep(0.010) # delay 10ms for first reading
151164

152-
def initialize(self): # pylint: disable=no-self-use
165+
def initialize(self) -> None: # pylint: disable=no-self-use
153166
"""Configure the sensor with the default settings. For use after calling :meth:`reset`"""
154167
raise RuntimeError(
155168
"LPS2X Base class cannot be instantiated directly. Use LPS22 or LPS25 instead"
156169
) # override in subclass
157170

158-
def reset(self):
171+
def reset(self) -> None:
159172
"""Reset the sensor, restoring all configuration registers to their defaults"""
160173
self._reset = True
161174
# wait for the reset to finish
162175
while self._reset:
163176
pass
164177

165178
@property
166-
def pressure(self):
179+
def pressure(self) -> float:
167180
"""The current pressure measurement in hPa"""
168181
raw = self._raw_pressure
169182

@@ -172,7 +185,7 @@ def pressure(self):
172185
return raw / 4096.0
173186

174187
@property
175-
def temperature(self):
188+
def temperature(self) -> float:
176189
"""The current temperature measurement in degrees Celsius"""
177190

178191
raw_temperature = self._raw_temperature
@@ -181,14 +194,14 @@ def temperature(self):
181194
) + self._temp_offset # pylint:disable=no-member
182195

183196
@property
184-
def data_rate(self):
197+
def data_rate(self) -> int:
185198
"""The rate at which the sensor measures :attr:`pressure` and
186199
:attr:`temperature`. :attr:`data_rate` should be set to one of
187200
the values of :class:`adafruit_lps2x.Rate`"""
188201
return self._data_rate
189202

190203
@data_rate.setter
191-
def data_rate(self, value):
204+
def data_rate(self, value: int) -> None:
192205
if not Rate.is_valid(value):
193206
raise AttributeError("data_rate must be a `Rate`")
194207

@@ -209,7 +222,7 @@ class LPS25(LPS2X):
209222
_reset = RWBit(_LPS25_CTRL_REG2, 2)
210223
_data_rate = RWBits(3, _LPS25_CTRL_REG1, 4)
211224

212-
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
225+
def __init__(self, i2c_bus: I2C, address: int = _LPS2X_DEFAULT_ADDRESS) -> None:
213226

214227
Rate.add_values(
215228
(
@@ -226,7 +239,7 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
226239
self._temp_offset = 42.5
227240
# self._inc_spi_flag = 0x40
228241

229-
def initialize(self):
242+
def initialize(self) -> None:
230243
"""Configure the sensor with the default settings.
231244
For use after calling :func:`LPS2X.reset`
232245
"""
@@ -249,7 +262,9 @@ class LPS22(LPS2X):
249262
_reset = RWBit(_LPS22_CTRL_REG2, 2)
250263
_data_rate = RWBits(3, _LPS22_CTRL_REG1, 4)
251264

252-
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
265+
def __init__(
266+
self, i2c_bus: I2C, address: Literal[0x5C, 0x5D] = _LPS2X_DEFAULT_ADDRESS
267+
) -> None:
253268
# Only adding Class-appropriate rates
254269
Rate.add_values(
255270
(
@@ -266,7 +281,7 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
266281
self._temp_scaling = 100
267282
self._temp_offset = 0
268283

269-
def initialize(self):
284+
def initialize(self) -> None:
270285
"""Configure the sensor with the default settings.
271286
For use after calling :func:`LPS2X.reset`
272287
"""

0 commit comments

Comments
 (0)