From df4b973fb2971cfc94a59499db81b456c6dd4108 Mon Sep 17 00:00:00 2001 From: sdomoszlai13 Date: Mon, 22 May 2023 14:50:31 +0200 Subject: [PATCH 01/10] Added type annotations. --- adafruit_ms8607.py | 57 +++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/adafruit_ms8607.py b/adafruit_ms8607.py index 106d0e8..05faf5b 100644 --- a/adafruit_ms8607.py +++ b/adafruit_ms8607.py @@ -38,6 +38,15 @@ from micropython import const from adafruit_bus_device import i2c_device +try: + """Needed for type annotations""" + from Typing import Tuple, Byte, Any + from busio import I2C + +except ImportError: + pass + + _MS8607_HSENSOR_ADDR = const(0x40) # _MS8607_PTSENSOR_ADDR = const(0x76) # @@ -65,7 +74,7 @@ class CV: """struct helper""" @classmethod - def add_values(cls, value_tuples): + def add_values(cls, value_tuples: Tuple) -> None: """Add CV values to the class""" cls.string = {} cls.lsb = {} @@ -77,7 +86,7 @@ def add_values(cls, value_tuples): cls.lsb[value] = lsb @classmethod - def is_valid(cls, value): + def is_valid(cls, value: Any) -> bool: """Validate that a given value is a member""" return value in cls.string @@ -150,7 +159,7 @@ class MS8607: """ - def __init__(self, i2c_bus): + def __init__(self, i2c_bus: I2C) -> None: self.humidity_i2c_device = i2c_device.I2CDevice(i2c_bus, _MS8607_HSENSOR_ADDR) self.pressure_i2c_device = i2c_device.I2CDevice(i2c_bus, _MS8607_PTSENSOR_ADDR) self._buffer = bytearray(4) @@ -160,7 +169,7 @@ def __init__(self, i2c_bus): self.reset() self.initialize() - def reset(self): + def reset(self) -> None: """Reset the sensor to an initial unconfigured state""" self._buffer[0] = _MS8607_HUM_CMD_RESET with self.humidity_i2c_device as i2c: @@ -171,7 +180,7 @@ def reset(self): with self.pressure_i2c_device as i2c: i2c.write(self._buffer, end=1) - def initialize(self): + def initialize(self) -> None: """Configure the sensors with the default settings and state. For use after calling `reset()` """ @@ -183,7 +192,7 @@ def initialize(self): HumidityResolution.OSR_4096 # pylint:disable=no-member ) - def _set_calibration_consts(self): + def _set_calibration_consts(self) -> None: constants = [] for i in range(7): @@ -209,7 +218,7 @@ def _set_calibration_consts(self): self._calibration_constants = constants @property - def pressure_and_temperature(self): + def pressure_and_temperature(self) -> None: """Pressure and Temperature, measured at the same time""" raw_temperature, raw_pressure = self._read_temp_pressure() @@ -217,7 +226,7 @@ def pressure_and_temperature(self): return (self._temperature, self._pressure) - def _scale_temp_pressure(self, raw_temperature, raw_pressure): + def _scale_temp_pressure(self, raw_temperature: int, raw_pressure: int) -> None: # See figure 7 'PRESSURE COMPENSATION (SECOND ORDER OVER TEMPERATURE)' # in the MS8607 datasheet delta_temp = self._dt(raw_temperature) @@ -234,13 +243,13 @@ def _scale_temp_pressure(self, raw_temperature, raw_pressure): self._pressure = ((((raw_pressure * sensitivity) >> 21) - offset) >> 15) / 100 @property - def pressure_resolution(self): + def pressure_resolution(self) -> PressureResolution: """The measurement resolution used for the pressure and temperature sensor""" return self._psensor_resolution_osr @pressure_resolution.setter - def pressure_resolution(self, resolution): + def pressure_resolution(self, resolution: PressureResolution) -> None: if not PressureResolution.is_valid(resolution): raise AttributeError( "pressure_resolution must be an `adafruit_ms8607.PressureResolution`" @@ -249,7 +258,7 @@ def pressure_resolution(self, resolution): self._psensor_resolution_osr = resolution @staticmethod - def _corrections(initial_temp, delta_temp): + def _corrections(initial_temp: int, delta_temp: int) -> Tuple[int, int, int]: # # Second order temperature compensation if initial_temp < 2000: delta_2k = initial_temp - 2000 @@ -271,17 +280,17 @@ def _corrections(initial_temp, delta_temp): sensitivity2 = 0 return temp2, offset2, sensitivity2 - def _pressure_scaling(self, delta_temp): + def _pressure_scaling(self, delta_temp: int) -> int: return (self._calibration_constants[1] << 16) + ( (self._calibration_constants[3] * delta_temp) >> 7 ) - def _pressure_offset(self, delta_temp): + def _pressure_offset(self, delta_temp: int) -> int: return ((self._calibration_constants[2]) << 17) + ( (self._calibration_constants[4] * delta_temp) >> 6 ) - def _read_temp_pressure(self): + def _read_temp_pressure(self) -> Tuple[int, int]: # First read temperature cmd = self._psensor_resolution_osr * 2 @@ -322,22 +331,22 @@ def _read_temp_pressure(self): raw_pressure = unpack_from(">I", self._buffer)[0] return raw_temperature, raw_pressure - def _dt(self, raw_temperature): + def _dt(self, raw_temperature: int) -> int: ref_temp = self._calibration_constants[5] return raw_temperature - (ref_temp << 8) @property - def temperature(self): + def temperature(self) -> float: """The current temperature in degrees Celcius""" return self.pressure_and_temperature[0] @property - def pressure(self): + def pressure(self) -> float: """The current barometric pressure in hPa""" return self.pressure_and_temperature[1] @property - def relative_humidity(self): + def relative_humidity(self) -> float: """The current relative humidity in % rH""" self._buffer[0] = _MS8607_HUM_CMD_READ_NO_HOLD @@ -358,12 +367,12 @@ def relative_humidity(self): return humidity @property - def humidity_resolution(self): + def humidity_resolution(self) -> HumidityResolution: """The humidity sensor's measurement resolution""" return self._humidity_resolution @humidity_resolution.setter - def humidity_resolution(self, resolution): + def humidity_resolution(self, resolution: HumidityResolution) -> None: if not HumidityResolution.is_valid(resolution): raise AttributeError("humidity_resolution must be a Humidity Resolution") @@ -377,7 +386,7 @@ def humidity_resolution(self, resolution): self._set_hum_user_register(reg_value) - def _read_hum_user_register(self): + def _read_hum_user_register(self) -> Byte: self._buffer[0] = _MS8607_HUM_CMD_READ_USR with self.humidity_i2c_device as i2c: i2c.write(self._buffer, end=1) @@ -387,7 +396,7 @@ def _read_hum_user_register(self): return self._buffer[0] - def _set_hum_user_register(self, register_value): + def _set_hum_user_register(self, register_value: Byte) -> None: self._buffer[0] = _MS8607_HUM_CMD_WRITE_USR self._buffer[1] = register_value with self.humidity_i2c_device as i2c: @@ -395,7 +404,7 @@ def _set_hum_user_register(self, register_value): i2c.write(self._buffer, end=2) @staticmethod - def _check_humidity_crc(value, crc): + def _check_humidity_crc(value: int, crc: Byte) -> bool: polynom = 0x988000 # x^8 + x^5 + x^4 + 1 msb = 0x800000 mask = 0xFF8000 @@ -416,7 +425,7 @@ def _check_humidity_crc(value, crc): return False @staticmethod - def _check_press_calibration_crc(calibration_int16s, crc): + def _check_press_calibration_crc(calibration_int16s: bytearray, crc: Byte) -> bool: cnt = 0 n_rem = 0 n_rem = 0 From 5d6a0b52dafaed5ed2e1b02c9a43dca97fb18ebe Mon Sep 17 00:00:00 2001 From: sdomoszlai13 Date: Mon, 22 May 2023 14:57:36 +0200 Subject: [PATCH 02/10] Corrected typo in typing library import line. --- adafruit_ms8607.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_ms8607.py b/adafruit_ms8607.py index 05faf5b..c461aa3 100644 --- a/adafruit_ms8607.py +++ b/adafruit_ms8607.py @@ -40,7 +40,7 @@ try: """Needed for type annotations""" - from Typing import Tuple, Byte, Any + from typing import Tuple, Byte, Any from busio import I2C except ImportError: From bc0d6411d2dd73dcea0aeb87778b283adc8df692 Mon Sep 17 00:00:00 2001 From: sdomoszlai13 Date: Mon, 22 May 2023 15:08:07 +0200 Subject: [PATCH 03/10] Fixed a Tuple error. --- adafruit_ms8607.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_ms8607.py b/adafruit_ms8607.py index c461aa3..c28d372 100644 --- a/adafruit_ms8607.py +++ b/adafruit_ms8607.py @@ -40,7 +40,7 @@ try: """Needed for type annotations""" - from typing import Tuple, Byte, Any + from typing import List, Tuple, Byte, Any from busio import I2C except ImportError: @@ -74,7 +74,7 @@ class CV: """struct helper""" @classmethod - def add_values(cls, value_tuples: Tuple) -> None: + def add_values(cls, value_tuples: Tuple[List[Any]]) -> None: """Add CV values to the class""" cls.string = {} cls.lsb = {} From e10efc9046bc317937e8adf10292dc742f5a901d Mon Sep 17 00:00:00 2001 From: sdomoszlai13 Date: Mon, 22 May 2023 15:15:49 +0200 Subject: [PATCH 04/10] Fixed tuple error. --- adafruit_ms8607.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_ms8607.py b/adafruit_ms8607.py index c28d372..36989aa 100644 --- a/adafruit_ms8607.py +++ b/adafruit_ms8607.py @@ -40,7 +40,7 @@ try: """Needed for type annotations""" - from typing import List, Tuple, Byte, Any + from typing import Tuple, Byte, Any from busio import I2C except ImportError: @@ -74,7 +74,7 @@ class CV: """struct helper""" @classmethod - def add_values(cls, value_tuples: Tuple[List[Any]]) -> None: + def add_values(cls, value_tuples: Tuple[Any, ...]) -> None: """Add CV values to the class""" cls.string = {} cls.lsb = {} From 77d093eea82e8ee31ae47cc46702aaaa08c10b98 Mon Sep 17 00:00:00 2001 From: sdomoszlai13 Date: Sat, 10 Jun 2023 21:30:22 +0200 Subject: [PATCH 05/10] Tried to fix pylint error. --- adafruit_ms8607.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_ms8607.py b/adafruit_ms8607.py index 36989aa..7ef99fe 100644 --- a/adafruit_ms8607.py +++ b/adafruit_ms8607.py @@ -40,7 +40,7 @@ try: """Needed for type annotations""" - from typing import Tuple, Byte, Any + from typing import Tuple, Any, Byte from busio import I2C except ImportError: @@ -74,7 +74,7 @@ class CV: """struct helper""" @classmethod - def add_values(cls, value_tuples: Tuple[Any, ...]) -> None: + def add_values(cls, value_tuples: Tuple[Any]) -> None: """Add CV values to the class""" cls.string = {} cls.lsb = {} From b4e35b9271c3cdc009c179e2af3fd64f4d60c3c6 Mon Sep 17 00:00:00 2001 From: sdomoszlai13 Date: Sat, 10 Jun 2023 21:47:33 +0200 Subject: [PATCH 06/10] Tried to fix pylint errors. --- adafruit_ms8607.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_ms8607.py b/adafruit_ms8607.py index 7ef99fe..c473988 100644 --- a/adafruit_ms8607.py +++ b/adafruit_ms8607.py @@ -40,8 +40,8 @@ try: """Needed for type annotations""" - from typing import Tuple, Any, Byte from busio import I2C + from typing import Tuple, Any, Byte except ImportError: pass From d3b4b20b09f49a6be6c0d6d806c7db3cf92d2ccc Mon Sep 17 00:00:00 2001 From: sdomoszlai13 Date: Sat, 10 Jun 2023 22:35:17 +0200 Subject: [PATCH 07/10] Tried to fix type annotations. --- adafruit_ms8607.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_ms8607.py b/adafruit_ms8607.py index c473988..b358dc1 100644 --- a/adafruit_ms8607.py +++ b/adafruit_ms8607.py @@ -35,16 +35,16 @@ from struct import unpack_from from time import sleep -from micropython import const +# from micropython import const from adafruit_bus_device import i2c_device try: """Needed for type annotations""" from busio import I2C - from typing import Tuple, Any, Byte + from typing import Tuple, Any except ImportError: - pass + print("Couldnt import") _MS8607_HSENSOR_ADDR = const(0x40) # @@ -386,7 +386,7 @@ def humidity_resolution(self, resolution: HumidityResolution) -> None: self._set_hum_user_register(reg_value) - def _read_hum_user_register(self) -> Byte: + def _read_hum_user_register(self) -> bytearray: self._buffer[0] = _MS8607_HUM_CMD_READ_USR with self.humidity_i2c_device as i2c: i2c.write(self._buffer, end=1) From bca2cf6511bdd960cfbda50535b0d5aaeab7d780 Mon Sep 17 00:00:00 2001 From: sdomoszlai13 Date: Sat, 10 Jun 2023 22:37:40 +0200 Subject: [PATCH 08/10] Tried to fix type annotations. --- adafruit_ms8607.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_ms8607.py b/adafruit_ms8607.py index b358dc1..a47ecd4 100644 --- a/adafruit_ms8607.py +++ b/adafruit_ms8607.py @@ -35,7 +35,7 @@ from struct import unpack_from from time import sleep -# from micropython import const +from micropython import const from adafruit_bus_device import i2c_device try: From 2497c09942721666e754efd0e945fe789a9fb3a0 Mon Sep 17 00:00:00 2001 From: sdomoszlai13 Date: Sat, 10 Jun 2023 22:40:33 +0200 Subject: [PATCH 09/10] Tried to fix type annotations. --- adafruit_ms8607.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_ms8607.py b/adafruit_ms8607.py index a47ecd4..28de586 100644 --- a/adafruit_ms8607.py +++ b/adafruit_ms8607.py @@ -396,7 +396,7 @@ def _read_hum_user_register(self) -> bytearray: return self._buffer[0] - def _set_hum_user_register(self, register_value: Byte) -> None: + def _set_hum_user_register(self, register_value: bytearray) -> None: self._buffer[0] = _MS8607_HUM_CMD_WRITE_USR self._buffer[1] = register_value with self.humidity_i2c_device as i2c: @@ -404,7 +404,7 @@ def _set_hum_user_register(self, register_value: Byte) -> None: i2c.write(self._buffer, end=2) @staticmethod - def _check_humidity_crc(value: int, crc: Byte) -> bool: + def _check_humidity_crc(value: int, crc: bytearray) -> bool: polynom = 0x988000 # x^8 + x^5 + x^4 + 1 msb = 0x800000 mask = 0xFF8000 @@ -425,7 +425,7 @@ def _check_humidity_crc(value: int, crc: Byte) -> bool: return False @staticmethod - def _check_press_calibration_crc(calibration_int16s: bytearray, crc: Byte) -> bool: + def _check_press_calibration_crc(calibration_int16s: bytearray, crc: bytearray) -> bool: cnt = 0 n_rem = 0 n_rem = 0 From 413ec61e4acd371321dd7ac2361cec1a562295b8 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sun, 30 Jul 2023 14:07:12 -0500 Subject: [PATCH 10/10] code format --- adafruit_ms8607.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_ms8607.py b/adafruit_ms8607.py index 28de586..18b7658 100644 --- a/adafruit_ms8607.py +++ b/adafruit_ms8607.py @@ -425,7 +425,9 @@ def _check_humidity_crc(value: int, crc: bytearray) -> bool: return False @staticmethod - def _check_press_calibration_crc(calibration_int16s: bytearray, crc: bytearray) -> bool: + def _check_press_calibration_crc( + calibration_int16s: bytearray, crc: bytearray + ) -> bool: cnt = 0 n_rem = 0 n_rem = 0