From b97615c15663e3567686a62d5a08ea796a5e39e3 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Tue, 20 Sep 2022 11:21:16 -0400 Subject: [PATCH 1/5] Add Misssing Type Annotations --- adafruit_max31856.py | 45 ++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/adafruit_max31856.py b/adafruit_max31856.py index 4e4f960..85c97fa 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -32,6 +32,13 @@ from micropython import const from adafruit_bus_device.spi_device import SPIDevice +try: + from typing import Dict, Tuple + from board import SPI + from digitalio import DigitalInOut +except ImportError: + pass + try: from struct import unpack except ImportError: @@ -151,7 +158,9 @@ class MAX31856: # Tony says this isn't re-entrant or thread safe! _BUFFER = bytearray(4) - def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K): + def __init__( + self, spi: SPI, cs: DigitalInOut, thermocouple_type: int = ThermocoupleType.K + ) -> None: self._device = SPIDevice(spi, cs, baudrate=500000, polarity=0, phase=1) # assert on any fault @@ -162,7 +171,7 @@ def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K): # set thermocouple type self._set_thermocouple_type(thermocouple_type) - def _set_thermocouple_type(self, thermocouple_type: ThermocoupleType): + def _set_thermocouple_type(self, thermocouple_type: ThermocoupleType) -> None: # get current value of CR1 Reg conf_reg_1 = self._read_register(_MAX31856_CR1_REG, 1)[0] conf_reg_1 &= 0xF0 # mask off bottom 4 bits @@ -184,7 +193,7 @@ def averaging(self) -> int: raise KeyError(f"AVGSEL bit pattern was not recognised ({avgsel:>08b})") @averaging.setter - def averaging(self, num_samples: int): + def averaging(self, num_samples: int) -> None: # This option is set in bits 4-6 of register CR1. if num_samples not in _AVGSEL_CONSTS: raise ValueError("Num_samples must be one of 1,2,4,8,16") @@ -208,7 +217,7 @@ def noise_rejection(self) -> int: return 60 @noise_rejection.setter - def noise_rejection(self, frequency: int): + def noise_rejection(self, frequency: int) -> None: conf_reg_0 = self._read_register(_MAX31856_CR0_REG, 1)[0] if frequency == 50: conf_reg_0 |= _MAX31856_CR0_50HZ # set the 50hz bit @@ -219,7 +228,7 @@ def noise_rejection(self, frequency: int): self._write_u8(_MAX31856_CR0_REG, conf_reg_0) @property - def temperature(self): + def temperature(self) -> float: """Measure the temperature of the sensor and wait for the result. Return value is in degrees Celsius. (read-only)""" self._perform_one_shot_measurement() @@ -241,7 +250,7 @@ def unpack_temperature(self) -> float: return temp_float @property - def reference_temperature(self): + def reference_temperature(self) -> float: """Wait to retrieve temperature of the cold junction in degrees Celsius. (read-only)""" self._perform_one_shot_measurement() return self.unpack_reference_temperature() @@ -256,7 +265,7 @@ def unpack_reference_temperature(self) -> float: return cold_junction_temp @property - def temperature_thresholds(self): + def temperature_thresholds(self) -> Tuple[float, float]: """The thermocouple's low and high temperature thresholds as a ``(low_temp, high_temp)`` tuple """ @@ -267,7 +276,7 @@ def temperature_thresholds(self): return (round(raw_low[0] / 16.0, 1), round(raw_high[0] / 16.0, 1)) @temperature_thresholds.setter - def temperature_thresholds(self, val): + def temperature_thresholds(self, val: Tuple[float, float]) -> None: int_low = int(val[0] * 16) int_high = int(val[1] * 16) @@ -279,7 +288,9 @@ def temperature_thresholds(self, val): self._write_u8(_MAX31856_LTLFTL_REG, int_low) @property - def reference_temperature_thresholds(self): # pylint: disable=invalid-name + def reference_temperature_thresholds( + self, + ) -> Tuple[float, float]: # pylint: disable=invalid-name """The cold junction's low and high temperature thresholds as a ``(low_temp, high_temp)`` tuple """ @@ -289,13 +300,15 @@ def reference_temperature_thresholds(self): # pylint: disable=invalid-name ) @reference_temperature_thresholds.setter - def reference_temperature_thresholds(self, val): # pylint: disable=invalid-name + def reference_temperature_thresholds( + self, val: Tuple[float, float] + ) -> None: # pylint: disable=invalid-name self._write_u8(_MAX31856_CJLF_REG, int(val[0])) self._write_u8(_MAX31856_CJHF_REG, int(val[1])) @property - def fault(self): + def fault(self) -> Dict[str, bool]: """A dictionary with the status of each fault type where the key is the fault type and the value is a bool if the fault is currently active @@ -326,12 +339,12 @@ def fault(self): "open_tc": bool(faults & _MAX31856_FAULT_OPEN), } - def _perform_one_shot_measurement(self): + def _perform_one_shot_measurement(self) -> None: self.initiate_one_shot_measurement() # wait for the measurement to complete self._wait_for_oneshot() - def initiate_one_shot_measurement(self): + def initiate_one_shot_measurement(self) -> None: """Starts a one-shot measurement and returns immediately. A measurement takes approximately 160ms. Check the status of the measurement with `oneshot_pending`; when it is false, @@ -358,11 +371,11 @@ def oneshot_pending(self) -> bool: ) return bool(oneshot_flag) - def _wait_for_oneshot(self): + def _wait_for_oneshot(self) -> None: while self.oneshot_pending: sleep(0.01) - def _read_register(self, address, length): + def _read_register(self, address: int, length: int) -> int: # pylint: disable=no-member # Read a 16-bit BE unsigned value from the specified 8-bit address. with self._device as device: @@ -371,7 +384,7 @@ def _read_register(self, address, length): device.readinto(self._BUFFER, end=length) return self._BUFFER[:length] - def _write_u8(self, address, val): + def _write_u8(self, address: int, val: int) -> None: # Write an 8-bit unsigned value to the specified 8-bit address. with self._device as device: self._BUFFER[0] = (address | 0x80) & 0xFF From 3b47d19cd1bda7c1434686a1589090a7fd8df398 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Tue, 20 Sep 2022 11:48:51 -0400 Subject: [PATCH 2/5] corrected pylint invalid name errors --- adafruit_max31856.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_max31856.py b/adafruit_max31856.py index 85c97fa..44e1428 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -288,7 +288,7 @@ def temperature_thresholds(self, val: Tuple[float, float]) -> None: self._write_u8(_MAX31856_LTLFTL_REG, int_low) @property - def reference_temperature_thresholds( + def reference_temperature_thresholds( # pylint: disable=invalid-name, self, ) -> Tuple[float, float]: # pylint: disable=invalid-name """The cold junction's low and high temperature thresholds @@ -300,7 +300,7 @@ def reference_temperature_thresholds( ) @reference_temperature_thresholds.setter - def reference_temperature_thresholds( + def reference_temperature_thresholds( # pylint: disable=invalid-name, self, val: Tuple[float, float] ) -> None: # pylint: disable=invalid-name From 37fec2229ef3e9aa36aef47b9b0e4932a6dd1573 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Tue, 20 Sep 2022 11:52:56 -0400 Subject: [PATCH 3/5] fixed error with SPI object --- adafruit_max31856.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_max31856.py b/adafruit_max31856.py index 44e1428..882fcce 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -34,7 +34,7 @@ try: from typing import Dict, Tuple - from board import SPI + from busio import SPI from digitalio import DigitalInOut except ImportError: pass From a1c2c031bb8dc230396ae67d5faf8256fd42c6ae Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Tue, 20 Sep 2022 12:45:48 -0400 Subject: [PATCH 4/5] added Literal definitions as suggested and fixed pylint issues --- adafruit_max31856.py | 9 +++++---- requirements.txt | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/adafruit_max31856.py b/adafruit_max31856.py index 882fcce..f05e6f6 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -34,6 +34,7 @@ try: from typing import Dict, Tuple + from typing_extensions import Literal from busio import SPI from digitalio import DigitalInOut except ImportError: @@ -206,7 +207,7 @@ def averaging(self, num_samples: int) -> None: self._write_u8(_MAX31856_CR1_REG, conf_reg_1) @property - def noise_rejection(self) -> int: + def noise_rejection(self) -> Literal[50, 60]: """ The frequency (Hz) to be used by the noise rejection filter. Must be 50 or 60. Default is 60.""" @@ -217,7 +218,7 @@ def noise_rejection(self) -> int: return 60 @noise_rejection.setter - def noise_rejection(self, frequency: int) -> None: + def noise_rejection(self, frequency: Literal[50, 60]) -> None: conf_reg_0 = self._read_register(_MAX31856_CR0_REG, 1)[0] if frequency == 50: conf_reg_0 |= _MAX31856_CR0_50HZ # set the 50hz bit @@ -290,7 +291,7 @@ def temperature_thresholds(self, val: Tuple[float, float]) -> None: @property def reference_temperature_thresholds( # pylint: disable=invalid-name, self, - ) -> Tuple[float, float]: # pylint: disable=invalid-name + ) -> Tuple[float, float]: """The cold junction's low and high temperature thresholds as a ``(low_temp, high_temp)`` tuple """ @@ -302,7 +303,7 @@ def reference_temperature_thresholds( # pylint: disable=invalid-name, @reference_temperature_thresholds.setter def reference_temperature_thresholds( # pylint: disable=invalid-name, self, val: Tuple[float, float] - ) -> None: # pylint: disable=invalid-name + ) -> None: self._write_u8(_MAX31856_CJLF_REG, int(val[0])) self._write_u8(_MAX31856_CJHF_REG, int(val[1])) diff --git a/requirements.txt b/requirements.txt index a45c547..29c73c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ Adafruit-Blinka adafruit-circuitpython-busdevice +typing-extensions~=4.0 From 07a439b64bbff87df1e9e690291c61cfaed4edc0 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Tue, 20 Sep 2022 12:47:47 -0400 Subject: [PATCH 5/5] added Literal definitions as suggested and fixed pylint issues --- adafruit_max31856.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_max31856.py b/adafruit_max31856.py index f05e6f6..4008a9a 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -376,7 +376,7 @@ def _wait_for_oneshot(self) -> None: while self.oneshot_pending: sleep(0.01) - def _read_register(self, address: int, length: int) -> int: + def _read_register(self, address: int, length: int) -> bytearray: # pylint: disable=no-member # Read a 16-bit BE unsigned value from the specified 8-bit address. with self._device as device: