From ca5f78fa889c674cc4d4569ad2c02cb80521bbcb Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Wed, 17 Nov 2021 12:51:58 -0500 Subject: [PATCH 1/9] Add typing for HTS221 --- adafruit_hts221.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/adafruit_hts221.py b/adafruit_hts221.py index 2f32030..da136e3 100644 --- a/adafruit_hts221.py +++ b/adafruit_hts221.py @@ -34,6 +34,12 @@ from adafruit_register.i2c_bits import RWBits, ROBits from adafruit_register.i2c_bit import RWBit, ROBit +try: + from typing import Sequence, Tuple + from busio import I2C +except ImportError: + pass + _WHO_AM_I = const(0x0F) _CTRL_REG1 = const(0x20) @@ -170,7 +176,7 @@ class HTS221: # pylint: disable=too-many-instance-attributes _h0_t0_out = ROUnaryStruct(_H0_T0_OUT, " None: self.i2c_device = i2cdevice.I2CDevice(i2c_bus, _HTS221_DEFAULT_ADDRESS) if not self._chip_id in [_HTS221_CHIP_ID]: raise RuntimeError( @@ -203,14 +209,14 @@ def __init__(self, i2c_bus): self.calib_hum_meas_1 = self._h1_t0_out # This is the closest thing to a software reset. It re-loads the calibration values from flash - def _boot(self): + def _boot(self) -> None: self._boot_bit = True # wait for the reset to finish while self._boot_bit: pass @property - def relative_humidity(self): + def relative_humidity(self) -> float: """The current relative humidity measurement in %rH""" calibrated_value_delta = self.calib_hum_value_1 - self.calib_hum_value_0 calibrated_measurement_delta = self.calib_hum_meas_1 - self.calib_hum_meas_0 @@ -228,7 +234,7 @@ def relative_humidity(self): return adjusted_humidity @property - def temperature(self): + def temperature(self) -> float: """The current temperature measurement in degrees Celsius""" calibrated_value_delta = self.calibrated_value_1 - self.calib_temp_value_0 @@ -247,7 +253,7 @@ def temperature(self): return adjusted_temp @property - def data_rate(self): + def data_rate(self) -> int: """The rate at which the sensor measures :attr:`relative_humidity` and :attr:`temperature`. :attr:`data_rate` should be set to one of the values of :class:`adafruit_hts221.Rate`. Note that setting :attr:`data_rate` to ``Rate.ONE_SHOT`` will cause @@ -256,23 +262,23 @@ def data_rate(self): return self._data_rate @data_rate.setter - def data_rate(self, value): + def data_rate(self, value: int) -> None: if not Rate.is_valid(value): raise AttributeError("data_rate must be a `Rate`") self._data_rate = value @property - def humidity_data_ready(self): + def humidity_data_ready(self) -> bool: """Returns true if a new relative humidity measurement is available to be read""" return self._humidity_status_bit @property - def temperature_data_ready(self): + def temperature_data_ready(self) -> bool: """Returns true if a new temperature measurement is available to be read""" return self._temperature_status_bit - def take_measurements(self): + def take_measurements(self) -> None: """Update the value of :attr:`relative_humidity` and :attr:`temperature` by taking a single measurement. Only meaningful if :attr:`data_rate` is set to ``ONE_SHOT``""" self._one_shot_bit = True From 3a71fb028161292ca4a7b4cd3e485eb6802df1cf Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Wed, 17 Nov 2021 12:53:19 -0500 Subject: [PATCH 2/9] Refactor how CV and Rates values are initialized I think there's more room for improvement but I didn't want to make any drastic changes --- adafruit_hts221.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/adafruit_hts221.py b/adafruit_hts221.py index da136e3..19fc0a2 100644 --- a/adafruit_hts221.py +++ b/adafruit_hts221.py @@ -71,21 +71,18 @@ class CV: """struct helper""" @classmethod - def add_values(cls, value_tuples): + def add_values(cls, value_tuples: Sequence[Tuple[str, int]]) -> None: """creates CV entries""" - cls.string = {} - cls.lsb = {} + cls.contents = {} for value_tuple in value_tuples: - name, value, string, lsb = value_tuple + name, value = value_tuple setattr(cls, name, value) - cls.string[value] = string - cls.lsb[value] = lsb @classmethod - def is_valid(cls, value): + def is_valid(cls, value: int) -> bool: """Returns true if the given value is a member of the CV""" - return value in cls.string + return hasattr(cls, value) class Rate(CV): @@ -111,10 +108,10 @@ class Rate(CV): Rate.add_values( ( - ("ONE_SHOT", 0, 0, None), - ("RATE_1_HZ", 1, 1, None), - ("RATE_7_HZ", 2, 7, None), - ("RATE_12_5_HZ", 3, 12.5, None), + ("ONE_SHOT", 0), + ("RATE_1_HZ", 1), + ("RATE_7_HZ", 2), + ("RATE_12_5_HZ", 3), ) ) From 2e20a707b01733ed3ccfc2fc78eaa1f980cfed32 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 14 Dec 2021 14:26:08 -0500 Subject: [PATCH 3/9] Re-added "string" class attribute and usage --- adafruit_hts221.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/adafruit_hts221.py b/adafruit_hts221.py index 19fc0a2..c400e1c 100644 --- a/adafruit_hts221.py +++ b/adafruit_hts221.py @@ -35,7 +35,7 @@ from adafruit_register.i2c_bit import RWBit, ROBit try: - from typing import Sequence, Tuple + from typing import Union, Sequence, Tuple from busio import I2C except ImportError: pass @@ -71,13 +71,17 @@ class CV: """struct helper""" @classmethod - def add_values(cls, value_tuples: Sequence[Tuple[str, int]]) -> None: + def add_values( + cls, value_tuples: Sequence[Tuple[str, int, Union[int, str]]] + ) -> None: """creates CV entries""" cls.contents = {} + cls.string = {} for value_tuple in value_tuples: - name, value = value_tuple + name, value, string = value_tuple setattr(cls, name, value) + cls.string[value] = string @classmethod def is_valid(cls, value: int) -> bool: @@ -108,10 +112,13 @@ class Rate(CV): Rate.add_values( ( - ("ONE_SHOT", 0), - ("RATE_1_HZ", 1), - ("RATE_7_HZ", 2), - ("RATE_12_5_HZ", 3), + ("ONE_SHOT", 0, 0), + ( + "RATE_1_HZ", + 1, + ), + ("RATE_7_HZ", 2, 7), + ("RATE_12_5_HZ", 3, 12.5), ) ) From 289cb41849d99f6f5a22ba711a878deaa9ec9600 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 14 Dec 2021 14:29:59 -0500 Subject: [PATCH 4/9] Added missing "1" string value for Rate tuple --- adafruit_hts221.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/adafruit_hts221.py b/adafruit_hts221.py index c400e1c..06e1b9e 100644 --- a/adafruit_hts221.py +++ b/adafruit_hts221.py @@ -113,10 +113,7 @@ class Rate(CV): Rate.add_values( ( ("ONE_SHOT", 0, 0), - ( - "RATE_1_HZ", - 1, - ), + ("RATE_1_HZ", 1, 1), ("RATE_7_HZ", 2, 7), ("RATE_12_5_HZ", 3, 12.5), ) From c5eb1472927bc69d174be42ee831f876aa8f9535 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 14 Dec 2021 14:47:08 -0500 Subject: [PATCH 5/9] Rename string class attribute to label --- adafruit_hts221.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_hts221.py b/adafruit_hts221.py index 06e1b9e..3ae8dc0 100644 --- a/adafruit_hts221.py +++ b/adafruit_hts221.py @@ -76,12 +76,12 @@ def add_values( ) -> None: """creates CV entries""" cls.contents = {} - cls.string = {} + cls.label = {} for value_tuple in value_tuples: - name, value, string = value_tuple + name, value, label = value_tuple setattr(cls, name, value) - cls.string[value] = string + cls.label[value] = label @classmethod def is_valid(cls, value: int) -> bool: From 521dcf71adec34b7da1922765a4a1d059a38ce94 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 14 Dec 2021 14:47:23 -0500 Subject: [PATCH 6/9] Add getting data rate label to example --- examples/hts221_simpletest.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/hts221_simpletest.py b/examples/hts221_simpletest.py index 2dd96d4..d34e64f 100644 --- a/examples/hts221_simpletest.py +++ b/examples/hts221_simpletest.py @@ -8,8 +8,12 @@ i2c = board.I2C() hts = adafruit_hts221.HTS221(i2c) +data_rate = adafruit_hts221.Rate.label[hts.data_rate] +print("Using data rate of: {:.1f} Hz".format(data_rate)) +print("") + while True: - print("Relative Humidity: %.2f %% rH" % hts.relative_humidity) - print("Temperature: %.2f C" % hts.temperature) + print("Relative Humidity: {:.2f} % rH".format(hts.relative_humidity)) + print("Temperature: {:.2f} C".format(hts.temperature)) print("") time.sleep(1) From fd6b58c0e683ebfe524cc4cb4659fca4a391042d Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Fri, 7 Jan 2022 11:32:29 -0500 Subject: [PATCH 7/9] Update adafruit_hts221.py Co-authored-by: Neradoc --- adafruit_hts221.py | 1 - 1 file changed, 1 deletion(-) diff --git a/adafruit_hts221.py b/adafruit_hts221.py index 3ae8dc0..11a73cb 100644 --- a/adafruit_hts221.py +++ b/adafruit_hts221.py @@ -75,7 +75,6 @@ def add_values( cls, value_tuples: Sequence[Tuple[str, int, Union[int, str]]] ) -> None: """creates CV entries""" - cls.contents = {} cls.label = {} for value_tuple in value_tuples: From 64633cf98388ec64023d338ae94854c6f03ff45d Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Fri, 7 Jan 2022 11:34:45 -0500 Subject: [PATCH 8/9] Update adafruit_hts221.py Co-authored-by: Neradoc --- adafruit_hts221.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_hts221.py b/adafruit_hts221.py index 11a73cb..c89d0f8 100644 --- a/adafruit_hts221.py +++ b/adafruit_hts221.py @@ -72,7 +72,7 @@ class CV: @classmethod def add_values( - cls, value_tuples: Sequence[Tuple[str, int, Union[int, str]]] + cls, value_tuples: Sequence[Tuple[str, int, Union[int, float]]] ) -> None: """creates CV entries""" cls.label = {} From c780253bbd1f76a679effba35a0766647c050e03 Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Fri, 7 Jan 2022 11:36:16 -0500 Subject: [PATCH 9/9] Update adafruit_hts221.py Co-authored-by: Neradoc --- adafruit_hts221.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_hts221.py b/adafruit_hts221.py index c89d0f8..1aac71c 100644 --- a/adafruit_hts221.py +++ b/adafruit_hts221.py @@ -85,7 +85,7 @@ def add_values( @classmethod def is_valid(cls, value: int) -> bool: """Returns true if the given value is a member of the CV""" - return hasattr(cls, value) + return value in cls.label class Rate(CV):