From 4ac081ec02516cf1232b6ade34ee7b076f7e5942 Mon Sep 17 00:00:00 2001 From: Tammy Cravit Date: Sat, 12 Feb 2022 10:40:37 -0700 Subject: [PATCH 1/5] Added type annotations --- adafruit_adt7410.py | 51 ++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/adafruit_adt7410.py b/adafruit_adt7410.py index 22156aa..907fa10 100644 --- a/adafruit_adt7410.py +++ b/adafruit_adt7410.py @@ -36,10 +36,12 @@ import time import struct +import busio from adafruit_bus_device.i2c_device import I2CDevice from adafruit_register.i2c_bit import RWBit, ROBit from micropython import const + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADT7410.git" @@ -91,17 +93,18 @@ class ADT7410: """ # many modes can be set with register objects for simplicity - ready = ROBit(_ADT7410_STATUS, 7) - ctpin_polarity = RWBit(_ADT7410_CONFIG, 2) - intpin_polarity = RWBit(_ADT7410_CONFIG, 3) - comparator_mode = RWBit(_ADT7410_CONFIG, 4) - high_resolution = RWBit(_ADT7410_CONFIG, 7) + ready: ROBit = ROBit(_ADT7410_STATUS, 7) + ctpin_polarity: RWBit = RWBit(_ADT7410_CONFIG, 2) + intpin_polarity: RWBit = RWBit(_ADT7410_CONFIG, 3) + comparator_mode: RWBit = RWBit(_ADT7410_CONFIG, 4) + high_resolution: RWBit = RWBit(_ADT7410_CONFIG, 7) + # Status Information configuration - temp_over_critiq = ROBit(_ADT7410_STATUS, 6) - temp_over_high = ROBit(_ADT7410_STATUS, 5) - temp_under_low = ROBit(_ADT7410_STATUS, 4) + temp_over_critiq: ROBit = ROBit(_ADT7410_STATUS, 6) + temp_over_high: ROBit = ROBit(_ADT7410_STATUS, 5) + temp_under_low: ROBit = ROBit(_ADT7410_STATUS, 4) - def __init__(self, i2c_bus, address=0x48): + def __init__(self, i2c_bus: busio.I2C, address: int = 0x48): self.i2c_device = I2CDevice(i2c_bus, address) self._buf = bytearray(3) # Verify the manufacturer and device ids to ensure we are talking to @@ -114,31 +117,31 @@ def __init__(self, i2c_bus, address=0x48): self.reset() @property - def temperature(self): + def temperature(self) -> float: """The temperature in Celsius""" temp = self._read_register(_ADT7410_TEMPMSB, 2) return struct.unpack(">h", temp)[0] / 128 @property - def status(self): + def status(self) -> int: """The ADT7410 status registers current value""" return self._read_register(_ADT7410_STATUS)[0] @property - def configuration(self): + def configuration(self) -> int: """The ADT7410 configuration register""" return self._read_register(_ADT7410_CONFIG)[0] @configuration.setter - def configuration(self, val): + def configuration(self, val: int) -> None: self._write_register(_ADT7410_CONFIG, val) - def reset(self): + def reset(self) -> None: """Perform a software reset""" self._write_register(_ADT7410_SWRST) time.sleep(0.5) - def _read_register(self, addr, num=1): + def _read_register(self, addr: int, num: int = 1) -> int: self._buf[0] = addr with self.i2c_device as i2c: i2c.write_then_readinto( @@ -146,7 +149,7 @@ def _read_register(self, addr, num=1): ) return self._buf[1 : num + 1] - def _write_register(self, addr, data=None): + def _write_register(self, addr: int, data: int = None) -> None: self._buf[0] = addr end = 1 if data: @@ -156,39 +159,39 @@ def _write_register(self, addr, data=None): i2c.write(self._buf, end=end) @property - def high_temperature(self): + def high_temperature(self) -> int: """The over temperature limit value in Celsius""" temp = self._read_register(_ADT7410_THIGHMSB, 2) return struct.unpack(">h", temp)[0] / 128 @high_temperature.setter - def high_temperature(self, value): + def high_temperature(self, value: int) -> None: value = struct.pack(">h", int(value * 128)) self._write_register(_ADT7410_THIGHMSB, value[0]) self._write_register(_ADT7410_THIGHLSB, value[1]) @property - def low_temperature(self): + def low_temperature(self) -> int: """The over temperature limit value in Celsius. Only works when comparator mode is selected""" temp = self._read_register(_ADT7410_TLOWMSB, 2) return struct.unpack(">h", temp)[0] / 128 @low_temperature.setter - def low_temperature(self, value): + def low_temperature(self, value: int) -> None: value = struct.pack(">h", int(value * 128)) self._write_register(_ADT7410_TLOWMSB, value[0]) self._write_register(_ADT7410_TLOWLSB, value[1]) @property - def critical_temperature(self): + def critical_temperature(self) -> int: """The critical temperature limit value in Celsius. Only works when comparator mode is selected""" temp = self._read_register(_ADT7410_TCRITMSB, 2) return struct.unpack(">h", temp)[0] / 128 @critical_temperature.setter - def critical_temperature(self, value): + def critical_temperature(self, value: int) -> None: """The over temperature limit value in Celsius There is a bug in the sensor, so the address 0x09 could no be written to 0x00 for this reason only odd numbers could be given. We could make the 0x09 with @@ -200,14 +203,14 @@ def critical_temperature(self, value): self._write_register(_ADT7410_TCRITLSB, value[1]) @property - def hysteresis(self): + def hysteresis(self) -> int: """The hysteresis temperature limit value in Celsius. Only works when comparator mode is selected. From 0 to 15 Celsius""" temp = self._read_register(_ADT7410_THYST)[0] return temp @hysteresis.setter - def hysteresis(self, value): + def hysteresis(self, value: int) -> None: if value > 15 or isinstance(value, float): raise Exception("Hysteresis value must be an integer lower than 15 Celsius") From 413c89bc997414cf84fc01e5533daeb710b62bc4 Mon Sep 17 00:00:00 2001 From: Tammy Cravit Date: Sat, 12 Feb 2022 10:41:19 -0700 Subject: [PATCH 2/5] Changed Exception raised by `hysteresis` setter to more specific ValueError --- adafruit_adt7410.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_adt7410.py b/adafruit_adt7410.py index 907fa10..fbeb4b3 100644 --- a/adafruit_adt7410.py +++ b/adafruit_adt7410.py @@ -212,6 +212,8 @@ def hysteresis(self) -> int: @hysteresis.setter def hysteresis(self, value: int) -> None: if value > 15 or isinstance(value, float): - raise Exception("Hysteresis value must be an integer lower than 15 Celsius") + raise ValueError( + "Hysteresis value must be an integer lower than 15 Celsius" + ) self._write_register(_ADT7410_THYST, value) From 20e8e4c72827bcee23cdc32efc2bd1c72c13bb54 Mon Sep 17 00:00:00 2001 From: Tammy Cravit Date: Sat, 12 Feb 2022 14:43:06 -0700 Subject: [PATCH 3/5] Moved busio import inside a try block to save memory --- adafruit_adt7410.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/adafruit_adt7410.py b/adafruit_adt7410.py index fbeb4b3..af21230 100644 --- a/adafruit_adt7410.py +++ b/adafruit_adt7410.py @@ -36,29 +36,33 @@ import time import struct -import busio from adafruit_bus_device.i2c_device import I2CDevice from adafruit_register.i2c_bit import RWBit, ROBit from micropython import const - -__version__ = "0.0.0-auto.0" -__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADT7410.git" - - -_ADT7410_TEMPMSB = const(0x0) -_ADT7410_TEMPLSB = const(0x1) -_ADT7410_STATUS = const(0x2) -_ADT7410_CONFIG = const(0x3) -_ADT7410_THIGHMSB = const(0x4) -_ADT7410_THIGHLSB = const(0x5) -_ADT7410_TLOWMSB = const(0x6) -_ADT7410_TLOWLSB = const(0x7) -_ADT7410_TCRITMSB = const(0x8) -_ADT7410_TCRITLSB = const(0x9) -_ADT7410_THYST = const(0x0A) -_ADT7410_ID = const(0xB) -_ADT7410_SWRST = const(0x2F) +try: + # Used only for typing + import busio # pylint: disable=unused-import +except ImportError: + pass + +__version__: str = "0.0.0-auto.0" +__repo__: str = "https://github.com/adafruit/Adafruit_CircuitPython_ADT7410.git" + + +_ADT7410_TEMPMSB: int = const(0x0) +_ADT7410_TEMPLSB: int = const(0x1) +_ADT7410_STATUS: int = const(0x2) +_ADT7410_CONFIG: int = const(0x3) +_ADT7410_THIGHMSB: int = const(0x4) +_ADT7410_THIGHLSB: int = const(0x5) +_ADT7410_TLOWMSB: int = const(0x6) +_ADT7410_TLOWLSB: int = const(0x7) +_ADT7410_TCRITMSB: int = const(0x8) +_ADT7410_TCRITLSB: int = const(0x9) +_ADT7410_THYST: int = const(0x0A) +_ADT7410_ID: int = const(0xB) +_ADT7410_SWRST: int = const(0x2F) class ADT7410: From 8f94ef9189668c5e4beda66cde86c7be671a67f7 Mon Sep 17 00:00:00 2001 From: Tammy Cravit Date: Sat, 12 Feb 2022 14:44:06 -0700 Subject: [PATCH 4/5] Reformatting added by black --- adafruit_adt7410.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_adt7410.py b/adafruit_adt7410.py index af21230..b1a1be4 100644 --- a/adafruit_adt7410.py +++ b/adafruit_adt7410.py @@ -42,7 +42,7 @@ try: # Used only for typing - import busio # pylint: disable=unused-import + import busio # pylint: disable=unused-import except ImportError: pass From 75c0742b15ef789511bb74ec1c9262fe50c3cd4b Mon Sep 17 00:00:00 2001 From: Tammy Cravit Date: Sat, 12 Feb 2022 17:05:02 -0700 Subject: [PATCH 5/5] Removed type hints from __ constants --- adafruit_adt7410.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_adt7410.py b/adafruit_adt7410.py index b1a1be4..2e78b2f 100644 --- a/adafruit_adt7410.py +++ b/adafruit_adt7410.py @@ -46,8 +46,8 @@ except ImportError: pass -__version__: str = "0.0.0-auto.0" -__repo__: str = "https://github.com/adafruit/Adafruit_CircuitPython_ADT7410.git" +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADT7410.git" _ADT7410_TEMPMSB: int = const(0x0)