37
37
from adafruit_register .i2c_struct import UnaryStruct , ROUnaryStruct
38
38
from adafruit_bus_device import i2c_device
39
39
40
+ try :
41
+ from typing import Iterable , Optional , Tuple , Type , Union
42
+ from busio import I2C
43
+ except ImportError :
44
+ pass
45
+
40
46
_LIS331_DEFAULT_ADDRESS = 0x18 # If SDO/SA0 is 3V, its 0x19
41
47
_LIS331_CHIP_ID = 0x32 # The default response to WHO_AM_I for the H3LIS331 and LIS331HH
42
48
_LIS331_REG_WHOAMI = 0x0F # Device identification register. [0, 0, 1, 1, 0, 0, 1, 1] */
@@ -73,14 +79,16 @@ class ROByteArray:
73
79
"""
74
80
75
81
def __init__ ( # pylint: disable=too-many-arguments
76
- self , register_address , format_str , count
82
+ self , register_address : int , format_str : str , count : int
77
83
):
78
84
79
85
self .buffer = bytearray (1 + count )
80
86
self .buffer [0 ] = register_address
81
87
self .format = format_str
82
88
83
- def __get__ (self , obj , objtype = None ):
89
+ def __get__ (
90
+ self , obj : Optional ["LIS331" ], objtype : Optional [Type ["LIS331" ]] = None
91
+ ) -> int :
84
92
with obj .i2c_device as i2c :
85
93
i2c .write_then_readinto (self .buffer , self .buffer , out_end = 1 , in_start = 1 )
86
94
@@ -91,7 +99,9 @@ class CV:
91
99
"""struct helper"""
92
100
93
101
@classmethod
94
- def add_values (cls , value_tuples ):
102
+ def add_values (
103
+ cls , value_tuples : Iterable [Tuple [str , int , Union [str , float ], Optional [float ]]]
104
+ ) -> None :
95
105
"creates CV entires"
96
106
cls .string = {}
97
107
cls .lsb = {}
@@ -103,7 +113,7 @@ def add_values(cls, value_tuples):
103
113
cls .lsb [value ] = lsb
104
114
105
115
@classmethod
106
- def is_valid (cls , value ) :
116
+ def is_valid (cls , value : int ) -> bool :
107
117
"Returns true if the given value is a member of the CV"
108
118
return value in cls .string
109
119
@@ -223,21 +233,21 @@ class LIS331:
223
233
_hpf_enable_bit = RWBit (_LIS331_REG_CTRL2 , 4 )
224
234
_hpf_cutoff = RWBits (2 , _LIS331_REG_CTRL2 , 0 )
225
235
226
- def __init__ (self , i2c_bus , address = _LIS331_DEFAULT_ADDRESS ):
236
+ def __init__ (self , i2c_bus : I2C , address : int = _LIS331_DEFAULT_ADDRESS ) -> None :
227
237
if (not isinstance (self , LIS331HH )) and (not isinstance (self , H3LIS331 )):
228
238
raise RuntimeError (
229
239
"Base class LIS331 cannot be instantiated directly. Use LIS331HH or H3LIS331"
230
240
)
231
241
self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
232
242
if self ._chip_id != _LIS331_CHIP_ID :
233
243
raise RuntimeError (
234
- "Failed to find %s - check your wiring!" % self . __class__ . __name__
244
+ f "Failed to find { self . __class__ . __name__ } - check your wiring!"
235
245
)
236
246
self ._range_class = None
237
247
self .enable_hpf (False )
238
248
239
249
@property
240
- def lpf_cutoff (self ):
250
+ def lpf_cutoff (self ) -> int :
241
251
"""The frequency above which signals will be filtered out"""
242
252
if self .mode == Mode .NORMAL : # pylint: disable=no-member
243
253
raise RuntimeError (
@@ -246,7 +256,7 @@ def lpf_cutoff(self):
246
256
return self ._data_rate_lpf_bits
247
257
248
258
@lpf_cutoff .setter
249
- def lpf_cutoff (self , cutoff_freq ) :
259
+ def lpf_cutoff (self , cutoff_freq : int ) -> None :
250
260
if not Frequency .is_valid (cutoff_freq ):
251
261
raise AttributeError ("lpf_cutoff must be a `Frequency`" )
252
262
@@ -258,7 +268,7 @@ def lpf_cutoff(self, cutoff_freq):
258
268
self ._data_rate_lpf_bits = cutoff_freq
259
269
260
270
@property
261
- def hpf_reference (self ):
271
+ def hpf_reference (self ) -> int :
262
272
"""The reference value to offset measurements when using the High-pass filter. To use,
263
273
``use_reference`` must be set to true when enabling the high-pass filter. The value
264
274
is a signed 8-bit number from -128 to 127. The value of each increment of 1 depends on the
@@ -281,22 +291,25 @@ def hpf_reference(self):
281
291
return self ._reference_value
282
292
283
293
@hpf_reference .setter
284
- def hpf_reference (self , reference_value ) :
294
+ def hpf_reference (self , reference_value : int ) -> None :
285
295
if reference_value < - 128 or reference_value > 127 :
286
296
raise AttributeError ("`hpf_reference` must be from -128 to 127" )
287
297
self ._reference_value = reference_value
288
298
289
- def zero_hpf (self ):
299
+ def zero_hpf (self ) -> None :
290
300
"""When the high-pass filter is enabled with ``use_reference=False``,
291
301
calling :meth:`zero_hpf` will set all measurements to zero immediately,
292
302
avoiding the normal settling time seen when using the high-pass filter
293
303
without a :meth:`hpf_reference`
294
304
"""
295
305
self ._zero_hpf # pylint: disable=pointless-statement
296
306
297
- def enable_hpf (
298
- self , enabled = True , cutoff = RateDivisor .ODR_DIV_50 , use_reference = False
299
- ): # pylint: disable=no-member
307
+ def enable_hpf ( # pylint: disable=no-member
308
+ self ,
309
+ enabled : bool = True ,
310
+ cutoff : int = RateDivisor .ODR_DIV_50 ,
311
+ use_reference : bool = False ,
312
+ ) -> None :
300
313
"""Enable or disable the high-pass filter.
301
314
302
315
:param enabled: Enable or disable the filter. Default is `True` to enable
@@ -318,12 +331,12 @@ def enable_hpf(
318
331
self ._hpf_enable_bit = enabled
319
332
320
333
@property
321
- def data_rate (self ):
334
+ def data_rate (self ) -> float :
322
335
"""Select the rate at which the accelerometer takes measurements. Must be a ``Rate``"""
323
336
return self ._cached_data_rate
324
337
325
338
@data_rate .setter
326
- def data_rate (self , new_rate_bits ) :
339
+ def data_rate (self , new_rate_bits : float ) -> None :
327
340
if not Rate .is_valid (new_rate_bits ):
328
341
raise AttributeError ("data_rate must be a `Rate`" )
329
342
@@ -337,13 +350,13 @@ def data_rate(self, new_rate_bits):
337
350
self ._cached_data_rate = new_mode << 2 | new_rate_bits
338
351
339
352
@property
340
- def mode (self ):
353
+ def mode (self ) -> int :
341
354
"""The :attr:`Mode` power mode that the sensor is set to, as determined by the current
342
355
`data_rate`. To set the mode, use `data_rate` and the appropriate ``Rate``"""
343
356
mode_bits = self ._mode_and_rate ()[0 ]
344
357
return mode_bits
345
358
346
- def _mode_and_rate (self , data_rate = None ):
359
+ def _mode_and_rate (self , data_rate : Optional [ int ] = None ) -> Tuple [ int , int ] :
347
360
if data_rate is None :
348
361
data_rate = self ._cached_data_rate
349
362
@@ -354,23 +367,21 @@ def _mode_and_rate(self, data_rate=None):
354
367
return (pm_value , dr_value )
355
368
356
369
@property
357
- def range (self ):
370
+ def range (self ) -> int :
358
371
"""Adjusts the range of values that the sensor can measure, Note that larger ranges will be
359
372
less accurate. Must be a ``H3LIS331Range`` or ``LIS331HHRange``"""
360
373
return self ._range_bits
361
374
362
375
@range .setter
363
- def range (self , new_range ) :
376
+ def range (self , new_range : int ) -> None :
364
377
if not self ._range_class .is_valid (new_range ): # pylint: disable=no-member
365
- raise AttributeError (
366
- "range must be a `%s`" % self ._range_class .__qualname__
367
- )
378
+ raise AttributeError (f"range must be a `{ self ._range_class .__qualname__ } '" )
368
379
self ._range_bits = new_range
369
380
self ._cached_accel_range = new_range
370
381
sleep (0.010 ) # give time for the new rate to settle
371
382
372
383
@property
373
- def acceleration (self ):
384
+ def acceleration (self ) -> Tuple [ int , int , int ] :
374
385
"""The x, y, z acceleration values returned in a 3-tuple and are in :math:`m / s ^ 2`."""
375
386
376
387
raw_acceleration_bytes = self ._raw_acceleration
@@ -381,7 +392,7 @@ def acceleration(self):
381
392
self ._scale_acceleration (raw_acceleration_bytes [2 ]),
382
393
)
383
394
384
- def _scale_acceleration (self , value ) :
395
+ def _scale_acceleration (self , value : int ) -> int :
385
396
# The measurements are 12 bits left justified to preserve the sign bit
386
397
# so we'll shift them back to get the real value
387
398
right_justified = value >> 4
@@ -420,7 +431,7 @@ class LIS331HH(LIS331):
420
431
421
432
"""
422
433
423
- def __init__ (self , i2c_bus , address = _LIS331_DEFAULT_ADDRESS ):
434
+ def __init__ (self , i2c_bus : I2C , address : int = _LIS331_DEFAULT_ADDRESS ) -> None :
424
435
# pylint: disable=no-member
425
436
super ().__init__ (i2c_bus , address )
426
437
self ._range_class = LIS331HHRange
@@ -461,7 +472,7 @@ class H3LIS331(LIS331):
461
472
462
473
"""
463
474
464
- def __init__ (self , i2c_bus , address = _LIS331_DEFAULT_ADDRESS ):
475
+ def __init__ (self , i2c_bus : I2C , address : int = _LIS331_DEFAULT_ADDRESS ) -> None :
465
476
# pylint: disable=no-member
466
477
super ().__init__ (i2c_bus , address )
467
478
self ._range_class = H3LIS331Range
0 commit comments