3737from adafruit_register .i2c_struct import UnaryStruct , ROUnaryStruct
3838from adafruit_bus_device import i2c_device
3939
40+ try :
41+ from typing import Iterable , Optional , Tuple , Type , Union
42+ from busio import I2C
43+ except ImportError :
44+ pass
45+
4046_LIS331_DEFAULT_ADDRESS = 0x18 # If SDO/SA0 is 3V, its 0x19
4147_LIS331_CHIP_ID = 0x32 # The default response to WHO_AM_I for the H3LIS331 and LIS331HH
4248_LIS331_REG_WHOAMI = 0x0F # Device identification register. [0, 0, 1, 1, 0, 0, 1, 1] */
@@ -73,14 +79,16 @@ class ROByteArray:
7379 """
7480
7581 def __init__ ( # pylint: disable=too-many-arguments
76- self , register_address , format_str , count
82+ self , register_address : int , format_str : str , count : int
7783 ):
7884
7985 self .buffer = bytearray (1 + count )
8086 self .buffer [0 ] = register_address
8187 self .format = format_str
8288
83- def __get__ (self , obj , objtype = None ):
89+ def __get__ (
90+ self , obj : Optional ["LIS331" ], objtype : Optional [Type ["LIS331" ]] = None
91+ ) -> int :
8492 with obj .i2c_device as i2c :
8593 i2c .write_then_readinto (self .buffer , self .buffer , out_end = 1 , in_start = 1 )
8694
@@ -91,7 +99,9 @@ class CV:
9199 """struct helper"""
92100
93101 @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 :
95105 "creates CV entires"
96106 cls .string = {}
97107 cls .lsb = {}
@@ -103,7 +113,7 @@ def add_values(cls, value_tuples):
103113 cls .lsb [value ] = lsb
104114
105115 @classmethod
106- def is_valid (cls , value ) :
116+ def is_valid (cls , value : int ) -> bool :
107117 "Returns true if the given value is a member of the CV"
108118 return value in cls .string
109119
@@ -223,21 +233,21 @@ class LIS331:
223233 _hpf_enable_bit = RWBit (_LIS331_REG_CTRL2 , 4 )
224234 _hpf_cutoff = RWBits (2 , _LIS331_REG_CTRL2 , 0 )
225235
226- def __init__ (self , i2c_bus , address = _LIS331_DEFAULT_ADDRESS ):
236+ def __init__ (self , i2c_bus : I2C , address : int = _LIS331_DEFAULT_ADDRESS ) -> None :
227237 if (not isinstance (self , LIS331HH )) and (not isinstance (self , H3LIS331 )):
228238 raise RuntimeError (
229239 "Base class LIS331 cannot be instantiated directly. Use LIS331HH or H3LIS331"
230240 )
231241 self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
232242 if self ._chip_id != _LIS331_CHIP_ID :
233243 raise RuntimeError (
234- "Failed to find %s - check your wiring!" % self . __class__ . __name__
244+ f "Failed to find { self . __class__ . __name__ } - check your wiring!"
235245 )
236246 self ._range_class = None
237247 self .enable_hpf (False )
238248
239249 @property
240- def lpf_cutoff (self ):
250+ def lpf_cutoff (self ) -> int :
241251 """The frequency above which signals will be filtered out"""
242252 if self .mode == Mode .NORMAL : # pylint: disable=no-member
243253 raise RuntimeError (
@@ -246,7 +256,7 @@ def lpf_cutoff(self):
246256 return self ._data_rate_lpf_bits
247257
248258 @lpf_cutoff .setter
249- def lpf_cutoff (self , cutoff_freq ) :
259+ def lpf_cutoff (self , cutoff_freq : int ) -> None :
250260 if not Frequency .is_valid (cutoff_freq ):
251261 raise AttributeError ("lpf_cutoff must be a `Frequency`" )
252262
@@ -258,7 +268,7 @@ def lpf_cutoff(self, cutoff_freq):
258268 self ._data_rate_lpf_bits = cutoff_freq
259269
260270 @property
261- def hpf_reference (self ):
271+ def hpf_reference (self ) -> int :
262272 """The reference value to offset measurements when using the High-pass filter. To use,
263273 ``use_reference`` must be set to true when enabling the high-pass filter. The value
264274 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):
281291 return self ._reference_value
282292
283293 @hpf_reference .setter
284- def hpf_reference (self , reference_value ) :
294+ def hpf_reference (self , reference_value : int ) -> None :
285295 if reference_value < - 128 or reference_value > 127 :
286296 raise AttributeError ("`hpf_reference` must be from -128 to 127" )
287297 self ._reference_value = reference_value
288298
289- def zero_hpf (self ):
299+ def zero_hpf (self ) -> None :
290300 """When the high-pass filter is enabled with ``use_reference=False``,
291301 calling :meth:`zero_hpf` will set all measurements to zero immediately,
292302 avoiding the normal settling time seen when using the high-pass filter
293303 without a :meth:`hpf_reference`
294304 """
295305 self ._zero_hpf # pylint: disable=pointless-statement
296306
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 :
300313 """Enable or disable the high-pass filter.
301314
302315 :param enabled: Enable or disable the filter. Default is `True` to enable
@@ -318,12 +331,12 @@ def enable_hpf(
318331 self ._hpf_enable_bit = enabled
319332
320333 @property
321- def data_rate (self ):
334+ def data_rate (self ) -> float :
322335 """Select the rate at which the accelerometer takes measurements. Must be a ``Rate``"""
323336 return self ._cached_data_rate
324337
325338 @data_rate .setter
326- def data_rate (self , new_rate_bits ) :
339+ def data_rate (self , new_rate_bits : float ) -> None :
327340 if not Rate .is_valid (new_rate_bits ):
328341 raise AttributeError ("data_rate must be a `Rate`" )
329342
@@ -337,13 +350,13 @@ def data_rate(self, new_rate_bits):
337350 self ._cached_data_rate = new_mode << 2 | new_rate_bits
338351
339352 @property
340- def mode (self ):
353+ def mode (self ) -> int :
341354 """The :attr:`Mode` power mode that the sensor is set to, as determined by the current
342355 `data_rate`. To set the mode, use `data_rate` and the appropriate ``Rate``"""
343356 mode_bits = self ._mode_and_rate ()[0 ]
344357 return mode_bits
345358
346- def _mode_and_rate (self , data_rate = None ):
359+ def _mode_and_rate (self , data_rate : Optional [ int ] = None ) -> Tuple [ int , int ] :
347360 if data_rate is None :
348361 data_rate = self ._cached_data_rate
349362
@@ -354,23 +367,21 @@ def _mode_and_rate(self, data_rate=None):
354367 return (pm_value , dr_value )
355368
356369 @property
357- def range (self ):
370+ def range (self ) -> int :
358371 """Adjusts the range of values that the sensor can measure, Note that larger ranges will be
359372 less accurate. Must be a ``H3LIS331Range`` or ``LIS331HHRange``"""
360373 return self ._range_bits
361374
362375 @range .setter
363- def range (self , new_range ) :
376+ def range (self , new_range : int ) -> None :
364377 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__ } '" )
368379 self ._range_bits = new_range
369380 self ._cached_accel_range = new_range
370381 sleep (0.010 ) # give time for the new rate to settle
371382
372383 @property
373- def acceleration (self ):
384+ def acceleration (self ) -> Tuple [ int , int , int ] :
374385 """The x, y, z acceleration values returned in a 3-tuple and are in :math:`m / s ^ 2`."""
375386
376387 raw_acceleration_bytes = self ._raw_acceleration
@@ -381,7 +392,7 @@ def acceleration(self):
381392 self ._scale_acceleration (raw_acceleration_bytes [2 ]),
382393 )
383394
384- def _scale_acceleration (self , value ) :
395+ def _scale_acceleration (self , value : int ) -> int :
385396 # The measurements are 12 bits left justified to preserve the sign bit
386397 # so we'll shift them back to get the real value
387398 right_justified = value >> 4
@@ -420,7 +431,7 @@ class LIS331HH(LIS331):
420431
421432 """
422433
423- def __init__ (self , i2c_bus , address = _LIS331_DEFAULT_ADDRESS ):
434+ def __init__ (self , i2c_bus : I2C , address : int = _LIS331_DEFAULT_ADDRESS ) -> None :
424435 # pylint: disable=no-member
425436 super ().__init__ (i2c_bus , address )
426437 self ._range_class = LIS331HHRange
@@ -461,7 +472,7 @@ class H3LIS331(LIS331):
461472
462473 """
463474
464- def __init__ (self , i2c_bus , address = _LIS331_DEFAULT_ADDRESS ):
475+ def __init__ (self , i2c_bus : I2C , address : int = _LIS331_DEFAULT_ADDRESS ) -> None :
465476 # pylint: disable=no-member
466477 super ().__init__ (i2c_bus , address )
467478 self ._range_class = H3LIS331Range
0 commit comments