Skip to content

Commit 30b6e86

Browse files
authored
Merge pull request #31 from tekktrik/doc/add-typing
Add type annotations
2 parents 5ada99f + 37d4c2e commit 30b6e86

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

adafruit_mlx90393.py

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
from adafruit_bus_device.i2c_device import I2CDevice
3535
from micropython import const
3636

37+
try:
38+
from typing import Tuple
39+
from circuitpython_typing import ReadableBuffer
40+
from busio import I2C
41+
except ImportError:
42+
pass
43+
3744
__version__ = "0.0.0-auto.0"
3845
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MLX90393.git"
3946

@@ -162,6 +169,9 @@ class MLX90393: # pylint: disable=too-many-instance-attributes
162169
:param i2c_bus: The I2C bus the device is connected to
163170
:param int address: The I2C device address. Defaults to :const:`0x0C`
164171
:param int gain: The gain level to apply. Defaults to :const:`GAIN_1X`
172+
:param int resolution: The resolution level to use. Defaults to :const:`RESOLUTION_16`
173+
:param int filt: The filter to use. Defaults to :const:`FILTER_7`
174+
:param int oversampling: The oversampleing setting to use. Defaults to :const:`OSR_3`
165175
:param bool debug: Enable debug output. Defaults to `False`
166176
167177
@@ -190,16 +200,16 @@ class MLX90393: # pylint: disable=too-many-instance-attributes
190200
191201
"""
192202

193-
def __init__(
203+
def __init__( # pylint: disable=too-many-arguments
194204
self,
195-
i2c_bus,
196-
address=0x0C,
197-
gain=GAIN_1X,
198-
resolution=RESOLUTION_16,
199-
filt=FILTER_7,
200-
oversampling=OSR_3,
201-
debug=False,
202-
): # pylint: disable=too-many-arguments
205+
i2c_bus: I2C,
206+
address: int = 0x0C,
207+
gain: int = GAIN_1X,
208+
resolution: int = RESOLUTION_16,
209+
filt: int = FILTER_7,
210+
oversampling: int = OSR_3,
211+
debug: bool = False,
212+
) -> None:
203213
self.i2c_device = I2CDevice(i2c_bus, address)
204214
self._debug = debug
205215
self._status_last = 0
@@ -225,12 +235,12 @@ def __init__(
225235
# Set gain to the supplied level
226236
self.gain = self._gain_current
227237

228-
def _transceive(self, payload, rxlen=0):
238+
def _transceive(self, payload: ReadableBuffer, rxlen: int = 0) -> bytearray:
229239
"""
230240
Writes the specified 'payload' to the sensor
231241
Returns the results of the write attempt.
232242
233-
:param bytes payload: The byte array to write to the sensor
243+
:param ReadableBuffer payload: The byte array to write to the sensor
234244
:param int rxlen: numbers of bytes to read back. Defaults to :const:`0`
235245
236246
"""
@@ -266,21 +276,21 @@ def _transceive(self, payload, rxlen=0):
266276
return data
267277

268278
@property
269-
def last_status(self):
279+
def last_status(self) -> int:
270280
"""
271281
The last status byte received from the sensor.
272282
"""
273283
return self._status_last
274284

275285
@property
276-
def gain(self):
286+
def gain(self) -> int:
277287
"""
278288
The gain setting for the device.
279289
"""
280290
return self._gain_current
281291

282292
@gain.setter
283-
def gain(self, value):
293+
def gain(self, value: int) -> None:
284294
if value > GAIN_1X or value < GAIN_5X:
285295
raise ValueError("Invalid GAIN setting")
286296
if self._debug:
@@ -298,36 +308,36 @@ def gain(self, value):
298308
)
299309

300310
@property
301-
def resolution_x(self):
311+
def resolution_x(self) -> int:
302312
"""The X axis resolution."""
303313
return self._res_x
304314

305315
@resolution_x.setter
306-
def resolution_x(self, resolution):
316+
def resolution_x(self, resolution: int) -> None:
307317
self._set_resolution(0, resolution)
308318
self._res_x = resolution
309319

310320
@property
311-
def resolution_y(self):
321+
def resolution_y(self) -> int:
312322
"""The Y axis resolution."""
313323
return self._res_y
314324

315325
@resolution_y.setter
316-
def resolution_y(self, resolution):
326+
def resolution_y(self, resolution: int) -> None:
317327
self._set_resolution(1, resolution)
318328
self._res_y = resolution
319329

320330
@property
321-
def resolution_z(self):
331+
def resolution_z(self) -> int:
322332
"""The Z axis resolution."""
323333
return self._res_z
324334

325335
@resolution_z.setter
326-
def resolution_z(self, resolution):
336+
def resolution_z(self, resolution: int) -> None:
327337
self._set_resolution(2, resolution)
328338
self._res_z = resolution
329339

330-
def _set_resolution(self, axis, resolution):
340+
def _set_resolution(self, axis: int, resolution: int) -> None:
331341
if resolution not in (
332342
RESOLUTION_16,
333343
RESOLUTION_17,
@@ -343,12 +353,12 @@ def _set_resolution(self, axis, resolution):
343353
self.write_reg(_CMD_REG_CONF3, reg)
344354

345355
@property
346-
def filter(self):
356+
def filter(self) -> int:
347357
"""The filter level."""
348358
return self._filter
349359

350360
@filter.setter
351-
def filter(self, level):
361+
def filter(self, level: int) -> None:
352362
if level not in range(8):
353363
raise ValueError("Incorrect filter level.")
354364
reg = self.read_reg(_CMD_REG_CONF3)
@@ -358,12 +368,12 @@ def filter(self, level):
358368
self._filter = level
359369

360370
@property
361-
def oversampling(self):
371+
def oversampling(self) -> int:
362372
"""The oversampling level."""
363373
return self._osr
364374

365375
@oversampling.setter
366-
def oversampling(self, level):
376+
def oversampling(self, level: int) -> None:
367377
if level not in range(4):
368378
raise ValueError("Incorrect oversampling level.")
369379
reg = self.read_reg(_CMD_REG_CONF3)
@@ -372,7 +382,7 @@ def oversampling(self, level):
372382
self.write_reg(_CMD_REG_CONF3, reg)
373383
self._osr = level
374384

375-
def display_status(self):
385+
def display_status(self) -> None:
376386
"""
377387
Prints out the content of the last status byte in a human-readable
378388
format.
@@ -389,9 +399,11 @@ def display_status(self):
389399
print("Reset status :", (self._status_last & (1 << 2)) > 0)
390400
print("Response bytes available :", avail)
391401

392-
def read_reg(self, reg):
402+
def read_reg(self, reg: int) -> int:
393403
"""
394404
Gets the current value of the specified register.
405+
406+
:param int reg: The register to read
395407
"""
396408
# Write 'value' to the specified register
397409
payload = bytes([_CMD_RR, reg << 2])
@@ -411,9 +423,12 @@ def read_reg(self, reg):
411423
print("\t Status :", hex(data[0]))
412424
return val
413425

414-
def write_reg(self, reg, value):
426+
def write_reg(self, reg: int, value: int) -> None:
415427
"""
416428
Writes the 16-bit value to the supplied register.
429+
430+
:param int reg: The register to write to
431+
:param int value: The value to write to the register
417432
"""
418433
self._transceive(
419434
bytes(
@@ -426,7 +441,7 @@ def write_reg(self, reg, value):
426441
)
427442
)
428443

429-
def reset(self):
444+
def reset(self) -> None:
430445
"""
431446
Performs a software reset of the sensor.
432447
"""
@@ -442,7 +457,7 @@ def reset(self):
442457
return self._status_last
443458

444459
@property
445-
def read_data(self):
460+
def read_data(self) -> Tuple[int, int, int]:
446461
"""
447462
Reads a single X/Y/Z sample from the magnetometer.
448463
"""
@@ -469,7 +484,7 @@ def read_data(self):
469484
return m_x, m_y, m_z
470485

471486
# pylint: disable=no-self-use
472-
def _unpack_axis_data(self, resolution, data):
487+
def _unpack_axis_data(self, resolution: int, data: ReadableBuffer) -> int:
473488
# see datasheet
474489
if resolution == RESOLUTION_19:
475490
(value,) = struct.unpack(">H", data)
@@ -482,7 +497,7 @@ def _unpack_axis_data(self, resolution, data):
482497
return value
483498

484499
@property
485-
def magnetic(self):
500+
def magnetic(self) -> Tuple[float, float, float]:
486501
"""
487502
The processed magnetometer sensor values.
488503
A 3-tuple of X, Y, Z axis values in microteslas that are signed floats.

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5-
Adafruit-Blinka
5+
Adafruit-Blinka>=7.0.0
66
adafruit-circuitpython-busdevice
7+
adafruit-circuitpython-typing

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
# Author details
3535
author="Adafruit Industries",
3636
author_email="[email protected]",
37-
install_requires=["Adafruit-Blinka", "adafruit-circuitpython-busdevice"],
37+
install_requires=[
38+
"Adafruit-Blinka>=7.0.0",
39+
"adafruit-circuitpython-busdevice",
40+
"adafruit-circuitpython-typing",
41+
],
3842
# Choose your license
3943
license="MIT",
4044
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)