Skip to content

Commit f0cdb74

Browse files
authored
Merge pull request #13 from sdomoszlai13/type_annotations
Added type annotations.
2 parents 075c73d + 413ec61 commit f0cdb74

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

adafruit_ms8607.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
from micropython import const
3939
from adafruit_bus_device import i2c_device
4040

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+
4150
_MS8607_HSENSOR_ADDR = const(0x40) #
4251
_MS8607_PTSENSOR_ADDR = const(0x76) #
4352

@@ -65,7 +74,7 @@ class CV:
6574
"""struct helper"""
6675

6776
@classmethod
68-
def add_values(cls, value_tuples):
77+
def add_values(cls, value_tuples: Tuple[Any]) -> None:
6978
"""Add CV values to the class"""
7079
cls.string = {}
7180
cls.lsb = {}
@@ -77,7 +86,7 @@ def add_values(cls, value_tuples):
7786
cls.lsb[value] = lsb
7887

7988
@classmethod
80-
def is_valid(cls, value):
89+
def is_valid(cls, value: Any) -> bool:
8190
"""Validate that a given value is a member"""
8291
return value in cls.string
8392

@@ -150,7 +159,7 @@ class MS8607:
150159
151160
"""
152161

153-
def __init__(self, i2c_bus):
162+
def __init__(self, i2c_bus: I2C) -> None:
154163
self.humidity_i2c_device = i2c_device.I2CDevice(i2c_bus, _MS8607_HSENSOR_ADDR)
155164
self.pressure_i2c_device = i2c_device.I2CDevice(i2c_bus, _MS8607_PTSENSOR_ADDR)
156165
self._buffer = bytearray(4)
@@ -160,7 +169,7 @@ def __init__(self, i2c_bus):
160169
self.reset()
161170
self.initialize()
162171

163-
def reset(self):
172+
def reset(self) -> None:
164173
"""Reset the sensor to an initial unconfigured state"""
165174
self._buffer[0] = _MS8607_HUM_CMD_RESET
166175
with self.humidity_i2c_device as i2c:
@@ -171,7 +180,7 @@ def reset(self):
171180
with self.pressure_i2c_device as i2c:
172181
i2c.write(self._buffer, end=1)
173182

174-
def initialize(self):
183+
def initialize(self) -> None:
175184
"""Configure the sensors with the default settings and state.
176185
For use after calling `reset()`
177186
"""
@@ -183,7 +192,7 @@ def initialize(self):
183192
HumidityResolution.OSR_4096 # pylint:disable=no-member
184193
)
185194

186-
def _set_calibration_consts(self):
195+
def _set_calibration_consts(self) -> None:
187196
constants = []
188197

189198
for i in range(7):
@@ -209,15 +218,15 @@ def _set_calibration_consts(self):
209218
self._calibration_constants = constants
210219

211220
@property
212-
def pressure_and_temperature(self):
221+
def pressure_and_temperature(self) -> None:
213222
"""Pressure and Temperature, measured at the same time"""
214223
raw_temperature, raw_pressure = self._read_temp_pressure()
215224

216225
self._scale_temp_pressure(raw_temperature, raw_pressure)
217226

218227
return (self._temperature, self._pressure)
219228

220-
def _scale_temp_pressure(self, raw_temperature, raw_pressure):
229+
def _scale_temp_pressure(self, raw_temperature: int, raw_pressure: int) -> None:
221230
# See figure 7 'PRESSURE COMPENSATION (SECOND ORDER OVER TEMPERATURE)'
222231
# in the MS8607 datasheet
223232
delta_temp = self._dt(raw_temperature)
@@ -234,13 +243,13 @@ def _scale_temp_pressure(self, raw_temperature, raw_pressure):
234243
self._pressure = ((((raw_pressure * sensitivity) >> 21) - offset) >> 15) / 100
235244

236245
@property
237-
def pressure_resolution(self):
246+
def pressure_resolution(self) -> PressureResolution:
238247
"""The measurement resolution used for the pressure and temperature sensor"""
239248

240249
return self._psensor_resolution_osr
241250

242251
@pressure_resolution.setter
243-
def pressure_resolution(self, resolution):
252+
def pressure_resolution(self, resolution: PressureResolution) -> None:
244253
if not PressureResolution.is_valid(resolution):
245254
raise AttributeError(
246255
"pressure_resolution must be an `adafruit_ms8607.PressureResolution`"
@@ -249,7 +258,7 @@ def pressure_resolution(self, resolution):
249258
self._psensor_resolution_osr = resolution
250259

251260
@staticmethod
252-
def _corrections(initial_temp, delta_temp):
261+
def _corrections(initial_temp: int, delta_temp: int) -> Tuple[int, int, int]:
253262
# # Second order temperature compensation
254263
if initial_temp < 2000:
255264
delta_2k = initial_temp - 2000
@@ -271,17 +280,17 @@ def _corrections(initial_temp, delta_temp):
271280
sensitivity2 = 0
272281
return temp2, offset2, sensitivity2
273282

274-
def _pressure_scaling(self, delta_temp):
283+
def _pressure_scaling(self, delta_temp: int) -> int:
275284
return (self._calibration_constants[1] << 16) + (
276285
(self._calibration_constants[3] * delta_temp) >> 7
277286
)
278287

279-
def _pressure_offset(self, delta_temp):
288+
def _pressure_offset(self, delta_temp: int) -> int:
280289
return ((self._calibration_constants[2]) << 17) + (
281290
(self._calibration_constants[4] * delta_temp) >> 6
282291
)
283292

284-
def _read_temp_pressure(self):
293+
def _read_temp_pressure(self) -> Tuple[int, int]:
285294
# First read temperature
286295

287296
cmd = self._psensor_resolution_osr * 2
@@ -322,22 +331,22 @@ def _read_temp_pressure(self):
322331
raw_pressure = unpack_from(">I", self._buffer)[0]
323332
return raw_temperature, raw_pressure
324333

325-
def _dt(self, raw_temperature):
334+
def _dt(self, raw_temperature: int) -> int:
326335
ref_temp = self._calibration_constants[5]
327336
return raw_temperature - (ref_temp << 8)
328337

329338
@property
330-
def temperature(self):
339+
def temperature(self) -> float:
331340
"""The current temperature in degrees Celcius"""
332341
return self.pressure_and_temperature[0]
333342

334343
@property
335-
def pressure(self):
344+
def pressure(self) -> float:
336345
"""The current barometric pressure in hPa"""
337346
return self.pressure_and_temperature[1]
338347

339348
@property
340-
def relative_humidity(self):
349+
def relative_humidity(self) -> float:
341350
"""The current relative humidity in % rH"""
342351

343352
self._buffer[0] = _MS8607_HUM_CMD_READ_NO_HOLD
@@ -358,12 +367,12 @@ def relative_humidity(self):
358367
return humidity
359368

360369
@property
361-
def humidity_resolution(self):
370+
def humidity_resolution(self) -> HumidityResolution:
362371
"""The humidity sensor's measurement resolution"""
363372
return self._humidity_resolution
364373

365374
@humidity_resolution.setter
366-
def humidity_resolution(self, resolution):
375+
def humidity_resolution(self, resolution: HumidityResolution) -> None:
367376
if not HumidityResolution.is_valid(resolution):
368377
raise AttributeError("humidity_resolution must be a Humidity Resolution")
369378

@@ -377,7 +386,7 @@ def humidity_resolution(self, resolution):
377386

378387
self._set_hum_user_register(reg_value)
379388

380-
def _read_hum_user_register(self):
389+
def _read_hum_user_register(self) -> bytearray:
381390
self._buffer[0] = _MS8607_HUM_CMD_READ_USR
382391
with self.humidity_i2c_device as i2c:
383392
i2c.write(self._buffer, end=1)
@@ -387,15 +396,15 @@ def _read_hum_user_register(self):
387396

388397
return self._buffer[0]
389398

390-
def _set_hum_user_register(self, register_value):
399+
def _set_hum_user_register(self, register_value: bytearray) -> None:
391400
self._buffer[0] = _MS8607_HUM_CMD_WRITE_USR
392401
self._buffer[1] = register_value
393402
with self.humidity_i2c_device as i2c:
394403
# shouldn't this end at two?
395404
i2c.write(self._buffer, end=2)
396405

397406
@staticmethod
398-
def _check_humidity_crc(value, crc):
407+
def _check_humidity_crc(value: int, crc: bytearray) -> bool:
399408
polynom = 0x988000 # x^8 + x^5 + x^4 + 1
400409
msb = 0x800000
401410
mask = 0xFF8000
@@ -416,7 +425,9 @@ def _check_humidity_crc(value, crc):
416425
return False
417426

418427
@staticmethod
419-
def _check_press_calibration_crc(calibration_int16s, crc):
428+
def _check_press_calibration_crc(
429+
calibration_int16s: bytearray, crc: bytearray
430+
) -> bool:
420431
cnt = 0
421432
n_rem = 0
422433
n_rem = 0

0 commit comments

Comments
 (0)