38
38
from micropython import const
39
39
from adafruit_bus_device import i2c_device
40
40
41
+ try :
42
+ """Needed for type annotations"""
43
+ from busio import I2C
44
+ from typing import Tuple , Any
45
+
46
+ except ImportError :
47
+ print ("Couldnt import" )
48
+
49
+
41
50
_MS8607_HSENSOR_ADDR = const (0x40 ) #
42
51
_MS8607_PTSENSOR_ADDR = const (0x76 ) #
43
52
@@ -65,7 +74,7 @@ class CV:
65
74
"""struct helper"""
66
75
67
76
@classmethod
68
- def add_values (cls , value_tuples ) :
77
+ def add_values (cls , value_tuples : Tuple [ Any ]) -> None :
69
78
"""Add CV values to the class"""
70
79
cls .string = {}
71
80
cls .lsb = {}
@@ -77,7 +86,7 @@ def add_values(cls, value_tuples):
77
86
cls .lsb [value ] = lsb
78
87
79
88
@classmethod
80
- def is_valid (cls , value ) :
89
+ def is_valid (cls , value : Any ) -> bool :
81
90
"""Validate that a given value is a member"""
82
91
return value in cls .string
83
92
@@ -150,7 +159,7 @@ class MS8607:
150
159
151
160
"""
152
161
153
- def __init__ (self , i2c_bus ) :
162
+ def __init__ (self , i2c_bus : I2C ) -> None :
154
163
self .humidity_i2c_device = i2c_device .I2CDevice (i2c_bus , _MS8607_HSENSOR_ADDR )
155
164
self .pressure_i2c_device = i2c_device .I2CDevice (i2c_bus , _MS8607_PTSENSOR_ADDR )
156
165
self ._buffer = bytearray (4 )
@@ -160,7 +169,7 @@ def __init__(self, i2c_bus):
160
169
self .reset ()
161
170
self .initialize ()
162
171
163
- def reset (self ):
172
+ def reset (self ) -> None :
164
173
"""Reset the sensor to an initial unconfigured state"""
165
174
self ._buffer [0 ] = _MS8607_HUM_CMD_RESET
166
175
with self .humidity_i2c_device as i2c :
@@ -171,7 +180,7 @@ def reset(self):
171
180
with self .pressure_i2c_device as i2c :
172
181
i2c .write (self ._buffer , end = 1 )
173
182
174
- def initialize (self ):
183
+ def initialize (self ) -> None :
175
184
"""Configure the sensors with the default settings and state.
176
185
For use after calling `reset()`
177
186
"""
@@ -183,7 +192,7 @@ def initialize(self):
183
192
HumidityResolution .OSR_4096 # pylint:disable=no-member
184
193
)
185
194
186
- def _set_calibration_consts (self ):
195
+ def _set_calibration_consts (self ) -> None :
187
196
constants = []
188
197
189
198
for i in range (7 ):
@@ -209,15 +218,15 @@ def _set_calibration_consts(self):
209
218
self ._calibration_constants = constants
210
219
211
220
@property
212
- def pressure_and_temperature (self ):
221
+ def pressure_and_temperature (self ) -> None :
213
222
"""Pressure and Temperature, measured at the same time"""
214
223
raw_temperature , raw_pressure = self ._read_temp_pressure ()
215
224
216
225
self ._scale_temp_pressure (raw_temperature , raw_pressure )
217
226
218
227
return (self ._temperature , self ._pressure )
219
228
220
- def _scale_temp_pressure (self , raw_temperature , raw_pressure ) :
229
+ def _scale_temp_pressure (self , raw_temperature : int , raw_pressure : int ) -> None :
221
230
# See figure 7 'PRESSURE COMPENSATION (SECOND ORDER OVER TEMPERATURE)'
222
231
# in the MS8607 datasheet
223
232
delta_temp = self ._dt (raw_temperature )
@@ -234,13 +243,13 @@ def _scale_temp_pressure(self, raw_temperature, raw_pressure):
234
243
self ._pressure = ((((raw_pressure * sensitivity ) >> 21 ) - offset ) >> 15 ) / 100
235
244
236
245
@property
237
- def pressure_resolution (self ):
246
+ def pressure_resolution (self ) -> PressureResolution :
238
247
"""The measurement resolution used for the pressure and temperature sensor"""
239
248
240
249
return self ._psensor_resolution_osr
241
250
242
251
@pressure_resolution .setter
243
- def pressure_resolution (self , resolution ) :
252
+ def pressure_resolution (self , resolution : PressureResolution ) -> None :
244
253
if not PressureResolution .is_valid (resolution ):
245
254
raise AttributeError (
246
255
"pressure_resolution must be an `adafruit_ms8607.PressureResolution`"
@@ -249,7 +258,7 @@ def pressure_resolution(self, resolution):
249
258
self ._psensor_resolution_osr = resolution
250
259
251
260
@staticmethod
252
- def _corrections (initial_temp , delta_temp ) :
261
+ def _corrections (initial_temp : int , delta_temp : int ) -> Tuple [ int , int , int ] :
253
262
# # Second order temperature compensation
254
263
if initial_temp < 2000 :
255
264
delta_2k = initial_temp - 2000
@@ -271,17 +280,17 @@ def _corrections(initial_temp, delta_temp):
271
280
sensitivity2 = 0
272
281
return temp2 , offset2 , sensitivity2
273
282
274
- def _pressure_scaling (self , delta_temp ) :
283
+ def _pressure_scaling (self , delta_temp : int ) -> int :
275
284
return (self ._calibration_constants [1 ] << 16 ) + (
276
285
(self ._calibration_constants [3 ] * delta_temp ) >> 7
277
286
)
278
287
279
- def _pressure_offset (self , delta_temp ) :
288
+ def _pressure_offset (self , delta_temp : int ) -> int :
280
289
return ((self ._calibration_constants [2 ]) << 17 ) + (
281
290
(self ._calibration_constants [4 ] * delta_temp ) >> 6
282
291
)
283
292
284
- def _read_temp_pressure (self ):
293
+ def _read_temp_pressure (self ) -> Tuple [ int , int ] :
285
294
# First read temperature
286
295
287
296
cmd = self ._psensor_resolution_osr * 2
@@ -322,22 +331,22 @@ def _read_temp_pressure(self):
322
331
raw_pressure = unpack_from (">I" , self ._buffer )[0 ]
323
332
return raw_temperature , raw_pressure
324
333
325
- def _dt (self , raw_temperature ) :
334
+ def _dt (self , raw_temperature : int ) -> int :
326
335
ref_temp = self ._calibration_constants [5 ]
327
336
return raw_temperature - (ref_temp << 8 )
328
337
329
338
@property
330
- def temperature (self ):
339
+ def temperature (self ) -> float :
331
340
"""The current temperature in degrees Celcius"""
332
341
return self .pressure_and_temperature [0 ]
333
342
334
343
@property
335
- def pressure (self ):
344
+ def pressure (self ) -> float :
336
345
"""The current barometric pressure in hPa"""
337
346
return self .pressure_and_temperature [1 ]
338
347
339
348
@property
340
- def relative_humidity (self ):
349
+ def relative_humidity (self ) -> float :
341
350
"""The current relative humidity in % rH"""
342
351
343
352
self ._buffer [0 ] = _MS8607_HUM_CMD_READ_NO_HOLD
@@ -358,12 +367,12 @@ def relative_humidity(self):
358
367
return humidity
359
368
360
369
@property
361
- def humidity_resolution (self ):
370
+ def humidity_resolution (self ) -> HumidityResolution :
362
371
"""The humidity sensor's measurement resolution"""
363
372
return self ._humidity_resolution
364
373
365
374
@humidity_resolution .setter
366
- def humidity_resolution (self , resolution ) :
375
+ def humidity_resolution (self , resolution : HumidityResolution ) -> None :
367
376
if not HumidityResolution .is_valid (resolution ):
368
377
raise AttributeError ("humidity_resolution must be a Humidity Resolution" )
369
378
@@ -377,7 +386,7 @@ def humidity_resolution(self, resolution):
377
386
378
387
self ._set_hum_user_register (reg_value )
379
388
380
- def _read_hum_user_register (self ):
389
+ def _read_hum_user_register (self ) -> bytearray :
381
390
self ._buffer [0 ] = _MS8607_HUM_CMD_READ_USR
382
391
with self .humidity_i2c_device as i2c :
383
392
i2c .write (self ._buffer , end = 1 )
@@ -387,15 +396,15 @@ def _read_hum_user_register(self):
387
396
388
397
return self ._buffer [0 ]
389
398
390
- def _set_hum_user_register (self , register_value ) :
399
+ def _set_hum_user_register (self , register_value : bytearray ) -> None :
391
400
self ._buffer [0 ] = _MS8607_HUM_CMD_WRITE_USR
392
401
self ._buffer [1 ] = register_value
393
402
with self .humidity_i2c_device as i2c :
394
403
# shouldn't this end at two?
395
404
i2c .write (self ._buffer , end = 2 )
396
405
397
406
@staticmethod
398
- def _check_humidity_crc (value , crc ) :
407
+ def _check_humidity_crc (value : int , crc : bytearray ) -> bool :
399
408
polynom = 0x988000 # x^8 + x^5 + x^4 + 1
400
409
msb = 0x800000
401
410
mask = 0xFF8000
@@ -416,7 +425,9 @@ def _check_humidity_crc(value, crc):
416
425
return False
417
426
418
427
@staticmethod
419
- def _check_press_calibration_crc (calibration_int16s , crc ):
428
+ def _check_press_calibration_crc (
429
+ calibration_int16s : bytearray , crc : bytearray
430
+ ) -> bool :
420
431
cnt = 0
421
432
n_rem = 0
422
433
n_rem = 0
0 commit comments