Skip to content

Commit ca6ab1d

Browse files
authored
Merge pull request #4 from tcfranks/main
Add Missing Type Annotations
2 parents ea001b7 + 9fdac59 commit ca6ab1d

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

adafruit_mlx90395.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949

5050
# from adafruit_register.i2c_bit import RWBit
5151

52+
try:
53+
from typing import Iterable, Optional, Tuple, Union
54+
from busio import I2C
55+
except ImportError:
56+
pass
57+
5258
__version__ = "0.0.0+auto.0"
5359
__repo__ = "https:#github.com/adafruit/Adafruit_CircuitPython_MLX90395.git"
5460

@@ -87,7 +93,9 @@ class CV:
8793
"""struct helper"""
8894

8995
@classmethod
90-
def add_values(cls, value_tuples):
96+
def add_values(
97+
cls, value_tuples: Iterable[Tuple[str, int, Union[str, int], Optional[float]]]
98+
) -> None:
9199
"creates CV entires"
92100
cls.string = {}
93101
cls.lsb = {}
@@ -99,7 +107,7 @@ def add_values(cls, value_tuples):
99107
cls.lsb[value] = lsb
100108

101109
@classmethod
102-
def is_valid(cls, value):
110+
def is_valid(cls, value: int) -> bool:
103111
"Returns true if the given value is a member of the CV"
104112
return value in cls.string
105113

@@ -172,7 +180,7 @@ class MLX90395:
172180
)
173181
_reg2 = UnaryStruct(_REG_2, ">H")
174182

175-
def __init__(self, i2c_bus, address=_DEFAULT_ADDR):
183+
def __init__(self, i2c_bus: I2C, address: int = _DEFAULT_ADDR) -> None:
176184
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
177185
self._ut_lsb = None
178186
self._gain_val = 0
@@ -181,7 +189,7 @@ def __init__(self, i2c_bus, address=_DEFAULT_ADDR):
181189
self.reset()
182190
self.initialize()
183191

184-
def reset(self):
192+
def reset(self) -> None:
185193
"""Reset the sensor to it's power-on state"""
186194

187195
self._command(_REG_EX)
@@ -193,7 +201,7 @@ def reset(self):
193201

194202
sleep(0.10)
195203

196-
def initialize(self):
204+
def initialize(self) -> None:
197205
"""Configure the sensor for use"""
198206
self._gain_val = self.gain
199207
if self._gain_val == 8: # default high field gain
@@ -202,37 +210,37 @@ def initialize(self):
202210
self._ut_lsb = 2.5 # medium field gain
203211

204212
@property
205-
def resolution(self):
213+
def resolution(self) -> int:
206214
"""The current resolution setting for the magnetometer"""
207215
return self._resolution
208216

209217
@resolution.setter
210-
def resolution(self, value):
218+
def resolution(self, value: int) -> None:
211219
if not Resolution.is_valid(value):
212220
raise AttributeError("resolution must be a Resolution")
213221
self._resolution = value
214222

215223
@property
216-
def gain(self):
224+
def gain(self) -> int:
217225
"""The gain applied to the magnetometer's ADC."""
218226
return self._gain
219227

220228
@gain.setter
221-
def gain(self, value):
229+
def gain(self, value: int) -> None:
222230
if not Gain.is_valid(value):
223231
raise AttributeError("gain must be a valid value")
224232
self._gain = value
225233
self._gain_val = value
226234

227-
def _command(self, command_id):
235+
def _command(self, command_id: int) -> int:
228236

229237
buffer = bytearray([0x80, command_id])
230238
with self.i2c_device as i2c:
231239
i2c.write_then_readinto(buffer, buffer, in_end=1)
232240
return buffer[0]
233241

234242
@property
235-
def magnetic(self):
243+
def magnetic(self) -> Tuple[float, float, float]:
236244
"""The processed magnetometer sensor values.
237245
A 3-tuple of X, Y, Z axis values in microteslas that are signed floats.
238246
"""
@@ -245,7 +253,7 @@ def magnetic(self):
245253

246254
return res
247255

248-
def _read_measurement(self):
256+
def _read_measurement(self) -> Tuple[float, float, float]:
249257

250258
# clear the buffer
251259
for i in range(len(self._buffer)): # pylint: disable=consider-using-enumerate
@@ -264,12 +272,12 @@ def _read_measurement(self):
264272
return (x_raw * scalar, y_raw * scalar, z_raw * scalar)
265273

266274
@property
267-
def oversample_rate(self):
275+
def oversample_rate(self) -> int:
268276
"""The number of times that the measurements are re-sampled and averaged to reduce noise"""
269277
return self._osr
270278

271279
@oversample_rate.setter
272-
def oversample_rate(self, value):
280+
def oversample_rate(self, value: int) -> None:
273281
if not OSR.is_valid(value):
274282
raise AttributeError("oversample_rate must be an OSR")
275283
self._osr = value

0 commit comments

Comments
 (0)