33
33
from adafruit_bus_device import i2c_device
34
34
35
35
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
+
36
43
__version__ = "0.0.0+auto.0"
37
44
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MPL3115A2.git"
38
45
@@ -130,7 +137,7 @@ class MPL3115A2:
130
137
# creates a flag in _MPL3115A2_REGISTER_STATUS that we were not clearing depending
131
138
# on the properties reading order
132
139
133
- def __init__ (self , i2c , * , address = _MPL3115A2_ADDRESS ):
140
+ def __init__ (self , i2c : I2C , * , address : int = _MPL3115A2_ADDRESS ):
134
141
self ._device = i2c_device .I2CDevice (i2c , address )
135
142
# Validate the chip ID.
136
143
if self ._read_u8 (_MPL3115A2_WHOAMI ) != 0xC4 :
@@ -159,7 +166,7 @@ def __init__(self, i2c, *, address=_MPL3115A2_ADDRESS):
159
166
| _MPL3115A2_PT_DATA_CFG_DREM ,
160
167
)
161
168
162
- def _read_into (self , address , buf , count = None ):
169
+ def _read_into (self , address : int , buf : WriteableBuffer , count : int = None ) -> None :
163
170
# Read bytes from the specified 8-bit address into the provided buffer.
164
171
# If the count is not specified then the entire buffer is filled,
165
172
# otherwise count bytes are copied in.
@@ -168,19 +175,19 @@ def _read_into(self, address, buf, count=None):
168
175
with self ._device as i2c :
169
176
i2c .write_then_readinto (bytes ([address & 0xFF ]), buf , in_end = count )
170
177
171
- def _read_u8 (self , address ) :
178
+ def _read_u8 (self , address : int ) -> int :
172
179
# Read an 8-bit unsigned value from the specified 8-bit address.
173
180
self ._read_into (address , self ._BUFFER , count = 1 )
174
181
return self ._BUFFER [0 ]
175
182
176
- def _write_u8 (self , address , val ) :
183
+ def _write_u8 (self , address : int , val : int ) -> None :
177
184
# Write an 8-bit unsigned value to the specified 8-bit address.
178
185
with self ._device as i2c :
179
186
self ._BUFFER [0 ] = address & 0xFF
180
187
self ._BUFFER [1 ] = val & 0xFF
181
188
i2c .write (self ._BUFFER , end = 2 )
182
189
183
- def _write_u16_be (self , address , val ) :
190
+ def _write_u16_be (self , address : int , val : int ) -> None :
184
191
# Write a 16-bit big endian unsigned value to the specified 8-bit
185
192
# address.
186
193
with self ._device as i2c :
@@ -189,14 +196,14 @@ def _write_u16_be(self, address, val):
189
196
self ._BUFFER [2 ] = val & 0xFF
190
197
i2c .write (self ._BUFFER , end = 3 )
191
198
192
- def _poll_reg1 (self , mask ) :
199
+ def _poll_reg1 (self , mask : int ) -> None :
193
200
# Poll the CTRL REG1 value for the specified masked bits to NOT be
194
201
# present.
195
202
while self ._read_u8 (_MPL3115A2_CTRL_REG1 ) & mask > 0 :
196
203
time .sleep (0.01 )
197
204
198
205
@property
199
- def pressure (self ):
206
+ def pressure (self ) -> float :
200
207
"""Read the barometric pressure detected by the sensor in Hectopascals."""
201
208
# First poll for a measurement to be finished.
202
209
self ._poll_reg1 (_MPL3115A2_CTRL_REG1_OST )
@@ -222,7 +229,7 @@ def pressure(self):
222
229
return pressure / 400.0
223
230
224
231
@property
225
- def altitude (self ):
232
+ def altitude (self ) -> float :
226
233
"""Read the altitude as calculated based on the sensor pressure and
227
234
previously configured pressure at sea-level. This will return a
228
235
value in meters. Set the sea-level pressure by updating the
@@ -253,7 +260,7 @@ def altitude(self):
253
260
return altitude / 65535.0
254
261
255
262
@property
256
- def temperature (self ):
263
+ def temperature (self ) -> float :
257
264
"""Read the temperature as measured by the sensor in Celsius."""
258
265
# First poll for a measurement to be finished.
259
266
self ._poll_reg1 (_MPL3115A2_CTRL_REG1_OST )
@@ -275,7 +282,7 @@ def temperature(self):
275
282
return temperature / 16.0
276
283
277
284
@property
278
- def sealevel_pressure (self ):
285
+ def sealevel_pressure (self ) -> float :
279
286
"""Read and write the pressure at sea-level used to calculate altitude.
280
287
You must look this up from a local weather or meteorological report for
281
288
the best accuracy. This is a value in Hectopascals.
@@ -287,7 +294,7 @@ def sealevel_pressure(self):
287
294
return pressure * 2.0 / 100
288
295
289
296
@sealevel_pressure .setter
290
- def sealevel_pressure (self , val ) :
297
+ def sealevel_pressure (self , val : float ) -> None :
291
298
# Convert from hectopascals to bars of pressure and write to the sealevel register.
292
299
bars = int (val * 50 )
293
300
self ._write_u16_be (_MPL3115A2_BAR_IN_MSB , bars )
0 commit comments