Skip to content

Commit 9546680

Browse files
authored
Merge pull request #32 from FoamyGuy/type_annotations
add type annotations
2 parents dcab081 + cc7f91b commit 9546680

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

adafruit_mpl3115a2.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
from adafruit_bus_device import i2c_device
3434

3535

36+
try:
37+
import typing # pylint: disable=unused-import
38+
from busio import I2C
39+
from circuitpython_typing import WriteableBuffer
40+
except ImportError:
41+
pass
42+
3643
__version__ = "0.0.0+auto.0"
3744
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MPL3115A2.git"
3845

@@ -130,7 +137,7 @@ class MPL3115A2:
130137
# creates a flag in _MPL3115A2_REGISTER_STATUS that we were not clearing depending
131138
# on the properties reading order
132139

133-
def __init__(self, i2c, *, address=_MPL3115A2_ADDRESS):
140+
def __init__(self, i2c: I2C, *, address: int = _MPL3115A2_ADDRESS):
134141
self._device = i2c_device.I2CDevice(i2c, address)
135142
# Validate the chip ID.
136143
if self._read_u8(_MPL3115A2_WHOAMI) != 0xC4:
@@ -159,7 +166,7 @@ def __init__(self, i2c, *, address=_MPL3115A2_ADDRESS):
159166
| _MPL3115A2_PT_DATA_CFG_DREM,
160167
)
161168

162-
def _read_into(self, address, buf, count=None):
169+
def _read_into(self, address: int, buf: WriteableBuffer, count: int = None) -> None:
163170
# Read bytes from the specified 8-bit address into the provided buffer.
164171
# If the count is not specified then the entire buffer is filled,
165172
# otherwise count bytes are copied in.
@@ -168,19 +175,19 @@ def _read_into(self, address, buf, count=None):
168175
with self._device as i2c:
169176
i2c.write_then_readinto(bytes([address & 0xFF]), buf, in_end=count)
170177

171-
def _read_u8(self, address):
178+
def _read_u8(self, address: int) -> int:
172179
# Read an 8-bit unsigned value from the specified 8-bit address.
173180
self._read_into(address, self._BUFFER, count=1)
174181
return self._BUFFER[0]
175182

176-
def _write_u8(self, address, val):
183+
def _write_u8(self, address: int, val: int) -> None:
177184
# Write an 8-bit unsigned value to the specified 8-bit address.
178185
with self._device as i2c:
179186
self._BUFFER[0] = address & 0xFF
180187
self._BUFFER[1] = val & 0xFF
181188
i2c.write(self._BUFFER, end=2)
182189

183-
def _write_u16_be(self, address, val):
190+
def _write_u16_be(self, address: int, val: int) -> None:
184191
# Write a 16-bit big endian unsigned value to the specified 8-bit
185192
# address.
186193
with self._device as i2c:
@@ -189,14 +196,14 @@ def _write_u16_be(self, address, val):
189196
self._BUFFER[2] = val & 0xFF
190197
i2c.write(self._BUFFER, end=3)
191198

192-
def _poll_reg1(self, mask):
199+
def _poll_reg1(self, mask: int) -> None:
193200
# Poll the CTRL REG1 value for the specified masked bits to NOT be
194201
# present.
195202
while self._read_u8(_MPL3115A2_CTRL_REG1) & mask > 0:
196203
time.sleep(0.01)
197204

198205
@property
199-
def pressure(self):
206+
def pressure(self) -> float:
200207
"""Read the barometric pressure detected by the sensor in Hectopascals."""
201208
# First poll for a measurement to be finished.
202209
self._poll_reg1(_MPL3115A2_CTRL_REG1_OST)
@@ -222,7 +229,7 @@ def pressure(self):
222229
return pressure / 400.0
223230

224231
@property
225-
def altitude(self):
232+
def altitude(self) -> float:
226233
"""Read the altitude as calculated based on the sensor pressure and
227234
previously configured pressure at sea-level. This will return a
228235
value in meters. Set the sea-level pressure by updating the
@@ -253,7 +260,7 @@ def altitude(self):
253260
return altitude / 65535.0
254261

255262
@property
256-
def temperature(self):
263+
def temperature(self) -> float:
257264
"""Read the temperature as measured by the sensor in Celsius."""
258265
# First poll for a measurement to be finished.
259266
self._poll_reg1(_MPL3115A2_CTRL_REG1_OST)
@@ -275,7 +282,7 @@ def temperature(self):
275282
return temperature / 16.0
276283

277284
@property
278-
def sealevel_pressure(self):
285+
def sealevel_pressure(self) -> float:
279286
"""Read and write the pressure at sea-level used to calculate altitude.
280287
You must look this up from a local weather or meteorological report for
281288
the best accuracy. This is a value in Hectopascals.
@@ -287,7 +294,7 @@ def sealevel_pressure(self):
287294
return pressure * 2.0 / 100
288295

289296
@sealevel_pressure.setter
290-
def sealevel_pressure(self, val):
297+
def sealevel_pressure(self, val: float) -> None:
291298
# Convert from hectopascals to bars of pressure and write to the sealevel register.
292299
bars = int(val * 50)
293300
self._write_u16_be(_MPL3115A2_BAR_IN_MSB, bars)

0 commit comments

Comments
 (0)