40
40
from adafruit_register .i2c_bits import RWBits , ROBits
41
41
from adafruit_register .i2c_bit import RWBit
42
42
43
+ try :
44
+ from typing import Optional , Tuple , Union
45
+ from typing_extensions import Literal
46
+ from busio import I2C
47
+ except ImportError :
48
+ pass
49
+
43
50
# _LPS2X_I2CADDR_DEFAULT = 0x5D # LPS2X default i2c address
44
51
# _LPS2X_WHOAMI = 0x0F # Chip ID register
45
52
# _LPS2X_PRESS_OUT_XL =(# | 0x80) ///< | 0x80 to set auto increment on multi-byte read
@@ -74,7 +81,7 @@ class CV:
74
81
"""struct helper"""
75
82
76
83
@classmethod
77
- def add_values (cls , value_tuples ) :
84
+ def add_values (cls , value_tuples : Tuple [ str , int , float , Union [ str , None ]]) -> None :
78
85
"""creates CV entries"""
79
86
cls .string = {}
80
87
cls .lsb = {}
@@ -86,7 +93,7 @@ def add_values(cls, value_tuples):
86
93
cls .lsb [value ] = lsb
87
94
88
95
@classmethod
89
- def is_valid (cls , value ):
96
+ def is_valid (cls , value ) -> bool :
90
97
"""Returns true if the given value is a member of the CV"""
91
98
return value in cls .string
92
99
@@ -139,31 +146,36 @@ class LPS2X: # pylint: disable=too-many-instance-attributes
139
146
_raw_temperature = ROUnaryStruct (_LPS2X_TEMP_OUT_L , "<h" )
140
147
_raw_pressure = ROBits (24 , _LPS2X_PRESS_OUT_XL , 0 , 3 )
141
148
142
- def __init__ (self , i2c_bus , address = _LPS2X_DEFAULT_ADDRESS , chip_id = None ):
149
+ def __init__ (
150
+ self ,
151
+ i2c_bus : I2C ,
152
+ address : Literal [0x5C , 0x5D ] = _LPS2X_DEFAULT_ADDRESS ,
153
+ chip_id : Optional [Literal [0xB1 , 0xBD ]] = None ,
154
+ ) -> None :
143
155
self .i2c_device = i2cdevice .I2CDevice (i2c_bus , address )
144
156
if not self ._chip_id in [chip_id ]:
145
157
raise RuntimeError (
146
- "Failed to find LPS2X! Found chip ID 0x%x" % self ._chip_id
158
+ f "Failed to find LPS2X! Found chip ID { hex ( self ._chip_id ) } "
147
159
)
148
160
self .reset ()
149
161
self .initialize ()
150
162
sleep (0.010 ) # delay 10ms for first reading
151
163
152
- def initialize (self ): # pylint: disable=no-self-use
164
+ def initialize (self ) -> None : # pylint: disable=no-self-use
153
165
"""Configure the sensor with the default settings. For use after calling :meth:`reset`"""
154
166
raise RuntimeError (
155
167
"LPS2X Base class cannot be instantiated directly. Use LPS22 or LPS25 instead"
156
168
) # override in subclass
157
169
158
- def reset (self ):
170
+ def reset (self ) -> None :
159
171
"""Reset the sensor, restoring all configuration registers to their defaults"""
160
172
self ._reset = True
161
173
# wait for the reset to finish
162
174
while self ._reset :
163
175
pass
164
176
165
177
@property
166
- def pressure (self ):
178
+ def pressure (self ) -> float :
167
179
"""The current pressure measurement in hPa"""
168
180
raw = self ._raw_pressure
169
181
@@ -172,7 +184,7 @@ def pressure(self):
172
184
return raw / 4096.0
173
185
174
186
@property
175
- def temperature (self ):
187
+ def temperature (self ) -> float :
176
188
"""The current temperature measurement in degrees Celsius"""
177
189
178
190
raw_temperature = self ._raw_temperature
@@ -181,14 +193,14 @@ def temperature(self):
181
193
) + self ._temp_offset # pylint:disable=no-member
182
194
183
195
@property
184
- def data_rate (self ):
196
+ def data_rate (self ) -> int :
185
197
"""The rate at which the sensor measures :attr:`pressure` and
186
198
:attr:`temperature`. :attr:`data_rate` should be set to one of
187
199
the values of :class:`adafruit_lps2x.Rate`"""
188
200
return self ._data_rate
189
201
190
202
@data_rate .setter
191
- def data_rate (self , value ) :
203
+ def data_rate (self , value : int ) -> None :
192
204
if not Rate .is_valid (value ):
193
205
raise AttributeError ("data_rate must be a `Rate`" )
194
206
@@ -209,7 +221,9 @@ class LPS25(LPS2X):
209
221
_reset = RWBit (_LPS25_CTRL_REG2 , 2 )
210
222
_data_rate = RWBits (3 , _LPS25_CTRL_REG1 , 4 )
211
223
212
- def __init__ (self , i2c_bus , address = _LPS2X_DEFAULT_ADDRESS ):
224
+ def __init__ (
225
+ self , i2c_bus : I2C , address : Literal [0x5C , 0x5D ] = _LPS2X_DEFAULT_ADDRESS
226
+ ) -> None :
213
227
214
228
Rate .add_values (
215
229
(
@@ -226,7 +240,7 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
226
240
self ._temp_offset = 42.5
227
241
# self._inc_spi_flag = 0x40
228
242
229
- def initialize (self ):
243
+ def initialize (self ) -> None :
230
244
"""Configure the sensor with the default settings.
231
245
For use after calling :func:`LPS2X.reset`
232
246
"""
@@ -249,7 +263,9 @@ class LPS22(LPS2X):
249
263
_reset = RWBit (_LPS22_CTRL_REG2 , 2 )
250
264
_data_rate = RWBits (3 , _LPS22_CTRL_REG1 , 4 )
251
265
252
- def __init__ (self , i2c_bus , address = _LPS2X_DEFAULT_ADDRESS ):
266
+ def __init__ (
267
+ self , i2c_bus : I2C , address : Literal [0x5C , 0x5D ] = _LPS2X_DEFAULT_ADDRESS
268
+ ) -> None :
253
269
# Only adding Class-appropriate rates
254
270
Rate .add_values (
255
271
(
@@ -266,7 +282,7 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
266
282
self ._temp_scaling = 100
267
283
self ._temp_offset = 0
268
284
269
- def initialize (self ):
285
+ def initialize (self ) -> None :
270
286
"""Configure the sensor with the default settings.
271
287
For use after calling :func:`LPS2X.reset`
272
288
"""
0 commit comments