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