Skip to content

Commit

Permalink
Merge pull request #22 from tcfranks/main
Browse files Browse the repository at this point in the history
Add Misssing Type Annotations
  • Loading branch information
tekktrik authored Sep 20, 2022
2 parents dc073a2 + 07a439b commit 072c3df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
48 changes: 31 additions & 17 deletions adafruit_max31856.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
from micropython import const
from adafruit_bus_device.spi_device import SPIDevice

try:
from typing import Dict, Tuple
from typing_extensions import Literal
from busio import SPI
from digitalio import DigitalInOut
except ImportError:
pass

try:
from struct import unpack
except ImportError:
Expand Down Expand Up @@ -151,7 +159,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
Expand All @@ -162,7 +172,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
Expand All @@ -184,7 +194,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")
Expand All @@ -197,7 +207,7 @@ def averaging(self, num_samples: int):
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."""
Expand All @@ -208,7 +218,7 @@ def noise_rejection(self) -> int:
return 60

@noise_rejection.setter
def noise_rejection(self, frequency: int):
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
Expand All @@ -219,7 +229,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()
Expand All @@ -241,7 +251,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()
Expand All @@ -256,7 +266,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
"""
Expand All @@ -267,7 +277,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)
Expand All @@ -279,7 +289,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( # pylint: disable=invalid-name,
self,
) -> Tuple[float, float]:
"""The cold junction's low and high temperature thresholds
as a ``(low_temp, high_temp)`` tuple
"""
Expand All @@ -289,13 +301,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( # pylint: disable=invalid-name,
self, val: Tuple[float, float]
) -> None:

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
Expand Down Expand Up @@ -326,12 +340,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,
Expand All @@ -358,11 +372,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) -> bytearray:
# pylint: disable=no-member
# Read a 16-bit BE unsigned value from the specified 8-bit address.
with self._device as device:
Expand All @@ -371,7 +385,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
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

Adafruit-Blinka
adafruit-circuitpython-busdevice
typing-extensions~=4.0

0 comments on commit 072c3df

Please sign in to comment.