34
34
from adafruit_bus_device .i2c_device import I2CDevice
35
35
from micropython import const
36
36
37
+ try :
38
+ from typing import Tuple
39
+ from circuitpython_typing import ReadableBuffer
40
+ from busio import I2C
41
+ except ImportError :
42
+ pass
43
+
37
44
__version__ = "0.0.0-auto.0"
38
45
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MLX90393.git"
39
46
@@ -162,6 +169,9 @@ class MLX90393: # pylint: disable=too-many-instance-attributes
162
169
:param i2c_bus: The I2C bus the device is connected to
163
170
:param int address: The I2C device address. Defaults to :const:`0x0C`
164
171
: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`
165
175
:param bool debug: Enable debug output. Defaults to `False`
166
176
167
177
@@ -190,16 +200,16 @@ class MLX90393: # pylint: disable=too-many-instance-attributes
190
200
191
201
"""
192
202
193
- def __init__ (
203
+ def __init__ ( # pylint: disable=too-many-arguments
194
204
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 :
203
213
self .i2c_device = I2CDevice (i2c_bus , address )
204
214
self ._debug = debug
205
215
self ._status_last = 0
@@ -225,12 +235,12 @@ def __init__(
225
235
# Set gain to the supplied level
226
236
self .gain = self ._gain_current
227
237
228
- def _transceive (self , payload , rxlen = 0 ) :
238
+ def _transceive (self , payload : ReadableBuffer , rxlen : int = 0 ) -> bytearray :
229
239
"""
230
240
Writes the specified 'payload' to the sensor
231
241
Returns the results of the write attempt.
232
242
233
- :param bytes payload: The byte array to write to the sensor
243
+ :param ReadableBuffer payload: The byte array to write to the sensor
234
244
:param int rxlen: numbers of bytes to read back. Defaults to :const:`0`
235
245
236
246
"""
@@ -266,21 +276,21 @@ def _transceive(self, payload, rxlen=0):
266
276
return data
267
277
268
278
@property
269
- def last_status (self ):
279
+ def last_status (self ) -> int :
270
280
"""
271
281
The last status byte received from the sensor.
272
282
"""
273
283
return self ._status_last
274
284
275
285
@property
276
- def gain (self ):
286
+ def gain (self ) -> int :
277
287
"""
278
288
The gain setting for the device.
279
289
"""
280
290
return self ._gain_current
281
291
282
292
@gain .setter
283
- def gain (self , value ) :
293
+ def gain (self , value : int ) -> None :
284
294
if value > GAIN_1X or value < GAIN_5X :
285
295
raise ValueError ("Invalid GAIN setting" )
286
296
if self ._debug :
@@ -298,36 +308,36 @@ def gain(self, value):
298
308
)
299
309
300
310
@property
301
- def resolution_x (self ):
311
+ def resolution_x (self ) -> int :
302
312
"""The X axis resolution."""
303
313
return self ._res_x
304
314
305
315
@resolution_x .setter
306
- def resolution_x (self , resolution ) :
316
+ def resolution_x (self , resolution : int ) -> None :
307
317
self ._set_resolution (0 , resolution )
308
318
self ._res_x = resolution
309
319
310
320
@property
311
- def resolution_y (self ):
321
+ def resolution_y (self ) -> int :
312
322
"""The Y axis resolution."""
313
323
return self ._res_y
314
324
315
325
@resolution_y .setter
316
- def resolution_y (self , resolution ) :
326
+ def resolution_y (self , resolution : int ) -> None :
317
327
self ._set_resolution (1 , resolution )
318
328
self ._res_y = resolution
319
329
320
330
@property
321
- def resolution_z (self ):
331
+ def resolution_z (self ) -> int :
322
332
"""The Z axis resolution."""
323
333
return self ._res_z
324
334
325
335
@resolution_z .setter
326
- def resolution_z (self , resolution ) :
336
+ def resolution_z (self , resolution : int ) -> None :
327
337
self ._set_resolution (2 , resolution )
328
338
self ._res_z = resolution
329
339
330
- def _set_resolution (self , axis , resolution ) :
340
+ def _set_resolution (self , axis : int , resolution : int ) -> None :
331
341
if resolution not in (
332
342
RESOLUTION_16 ,
333
343
RESOLUTION_17 ,
@@ -343,12 +353,12 @@ def _set_resolution(self, axis, resolution):
343
353
self .write_reg (_CMD_REG_CONF3 , reg )
344
354
345
355
@property
346
- def filter (self ):
356
+ def filter (self ) -> int :
347
357
"""The filter level."""
348
358
return self ._filter
349
359
350
360
@filter .setter
351
- def filter (self , level ) :
361
+ def filter (self , level : int ) -> None :
352
362
if level not in range (8 ):
353
363
raise ValueError ("Incorrect filter level." )
354
364
reg = self .read_reg (_CMD_REG_CONF3 )
@@ -358,12 +368,12 @@ def filter(self, level):
358
368
self ._filter = level
359
369
360
370
@property
361
- def oversampling (self ):
371
+ def oversampling (self ) -> int :
362
372
"""The oversampling level."""
363
373
return self ._osr
364
374
365
375
@oversampling .setter
366
- def oversampling (self , level ) :
376
+ def oversampling (self , level : int ) -> None :
367
377
if level not in range (4 ):
368
378
raise ValueError ("Incorrect oversampling level." )
369
379
reg = self .read_reg (_CMD_REG_CONF3 )
@@ -372,7 +382,7 @@ def oversampling(self, level):
372
382
self .write_reg (_CMD_REG_CONF3 , reg )
373
383
self ._osr = level
374
384
375
- def display_status (self ):
385
+ def display_status (self ) -> None :
376
386
"""
377
387
Prints out the content of the last status byte in a human-readable
378
388
format.
@@ -389,9 +399,11 @@ def display_status(self):
389
399
print ("Reset status :" , (self ._status_last & (1 << 2 )) > 0 )
390
400
print ("Response bytes available :" , avail )
391
401
392
- def read_reg (self , reg ) :
402
+ def read_reg (self , reg : int ) -> int :
393
403
"""
394
404
Gets the current value of the specified register.
405
+
406
+ :param int reg: The register to read
395
407
"""
396
408
# Write 'value' to the specified register
397
409
payload = bytes ([_CMD_RR , reg << 2 ])
@@ -411,9 +423,12 @@ def read_reg(self, reg):
411
423
print ("\t Status :" , hex (data [0 ]))
412
424
return val
413
425
414
- def write_reg (self , reg , value ) :
426
+ def write_reg (self , reg : int , value : int ) -> None :
415
427
"""
416
428
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
417
432
"""
418
433
self ._transceive (
419
434
bytes (
@@ -426,7 +441,7 @@ def write_reg(self, reg, value):
426
441
)
427
442
)
428
443
429
- def reset (self ):
444
+ def reset (self ) -> None :
430
445
"""
431
446
Performs a software reset of the sensor.
432
447
"""
@@ -442,7 +457,7 @@ def reset(self):
442
457
return self ._status_last
443
458
444
459
@property
445
- def read_data (self ):
460
+ def read_data (self ) -> Tuple [ int , int , int ] :
446
461
"""
447
462
Reads a single X/Y/Z sample from the magnetometer.
448
463
"""
@@ -469,7 +484,7 @@ def read_data(self):
469
484
return m_x , m_y , m_z
470
485
471
486
# pylint: disable=no-self-use
472
- def _unpack_axis_data (self , resolution , data ) :
487
+ def _unpack_axis_data (self , resolution : int , data : ReadableBuffer ) -> int :
473
488
# see datasheet
474
489
if resolution == RESOLUTION_19 :
475
490
(value ,) = struct .unpack (">H" , data )
@@ -482,7 +497,7 @@ def _unpack_axis_data(self, resolution, data):
482
497
return value
483
498
484
499
@property
485
- def magnetic (self ):
500
+ def magnetic (self ) -> Tuple [ float , float , float ] :
486
501
"""
487
502
The processed magnetometer sensor values.
488
503
A 3-tuple of X, Y, Z axis values in microteslas that are signed floats.
0 commit comments