Skip to content

Commit 072c3df

Browse files
authored
Merge pull request #22 from tcfranks/main
Add Misssing Type Annotations
2 parents dc073a2 + 07a439b commit 072c3df

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

adafruit_max31856.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
from micropython import const
3333
from adafruit_bus_device.spi_device import SPIDevice
3434

35+
try:
36+
from typing import Dict, Tuple
37+
from typing_extensions import Literal
38+
from busio import SPI
39+
from digitalio import DigitalInOut
40+
except ImportError:
41+
pass
42+
3543
try:
3644
from struct import unpack
3745
except ImportError:
@@ -151,7 +159,9 @@ class MAX31856:
151159
# Tony says this isn't re-entrant or thread safe!
152160
_BUFFER = bytearray(4)
153161

154-
def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K):
162+
def __init__(
163+
self, spi: SPI, cs: DigitalInOut, thermocouple_type: int = ThermocoupleType.K
164+
) -> None:
155165
self._device = SPIDevice(spi, cs, baudrate=500000, polarity=0, phase=1)
156166

157167
# assert on any fault
@@ -162,7 +172,7 @@ def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K):
162172
# set thermocouple type
163173
self._set_thermocouple_type(thermocouple_type)
164174

165-
def _set_thermocouple_type(self, thermocouple_type: ThermocoupleType):
175+
def _set_thermocouple_type(self, thermocouple_type: ThermocoupleType) -> None:
166176
# get current value of CR1 Reg
167177
conf_reg_1 = self._read_register(_MAX31856_CR1_REG, 1)[0]
168178
conf_reg_1 &= 0xF0 # mask off bottom 4 bits
@@ -184,7 +194,7 @@ def averaging(self) -> int:
184194
raise KeyError(f"AVGSEL bit pattern was not recognised ({avgsel:>08b})")
185195

186196
@averaging.setter
187-
def averaging(self, num_samples: int):
197+
def averaging(self, num_samples: int) -> None:
188198
# This option is set in bits 4-6 of register CR1.
189199
if num_samples not in _AVGSEL_CONSTS:
190200
raise ValueError("Num_samples must be one of 1,2,4,8,16")
@@ -197,7 +207,7 @@ def averaging(self, num_samples: int):
197207
self._write_u8(_MAX31856_CR1_REG, conf_reg_1)
198208

199209
@property
200-
def noise_rejection(self) -> int:
210+
def noise_rejection(self) -> Literal[50, 60]:
201211
"""
202212
The frequency (Hz) to be used by the noise rejection filter.
203213
Must be 50 or 60. Default is 60."""
@@ -208,7 +218,7 @@ def noise_rejection(self) -> int:
208218
return 60
209219

210220
@noise_rejection.setter
211-
def noise_rejection(self, frequency: int):
221+
def noise_rejection(self, frequency: Literal[50, 60]) -> None:
212222
conf_reg_0 = self._read_register(_MAX31856_CR0_REG, 1)[0]
213223
if frequency == 50:
214224
conf_reg_0 |= _MAX31856_CR0_50HZ # set the 50hz bit
@@ -219,7 +229,7 @@ def noise_rejection(self, frequency: int):
219229
self._write_u8(_MAX31856_CR0_REG, conf_reg_0)
220230

221231
@property
222-
def temperature(self):
232+
def temperature(self) -> float:
223233
"""Measure the temperature of the sensor and wait for the result.
224234
Return value is in degrees Celsius. (read-only)"""
225235
self._perform_one_shot_measurement()
@@ -241,7 +251,7 @@ def unpack_temperature(self) -> float:
241251
return temp_float
242252

243253
@property
244-
def reference_temperature(self):
254+
def reference_temperature(self) -> float:
245255
"""Wait to retrieve temperature of the cold junction in degrees Celsius. (read-only)"""
246256
self._perform_one_shot_measurement()
247257
return self.unpack_reference_temperature()
@@ -256,7 +266,7 @@ def unpack_reference_temperature(self) -> float:
256266
return cold_junction_temp
257267

258268
@property
259-
def temperature_thresholds(self):
269+
def temperature_thresholds(self) -> Tuple[float, float]:
260270
"""The thermocouple's low and high temperature thresholds
261271
as a ``(low_temp, high_temp)`` tuple
262272
"""
@@ -267,7 +277,7 @@ def temperature_thresholds(self):
267277
return (round(raw_low[0] / 16.0, 1), round(raw_high[0] / 16.0, 1))
268278

269279
@temperature_thresholds.setter
270-
def temperature_thresholds(self, val):
280+
def temperature_thresholds(self, val: Tuple[float, float]) -> None:
271281

272282
int_low = int(val[0] * 16)
273283
int_high = int(val[1] * 16)
@@ -279,7 +289,9 @@ def temperature_thresholds(self, val):
279289
self._write_u8(_MAX31856_LTLFTL_REG, int_low)
280290

281291
@property
282-
def reference_temperature_thresholds(self): # pylint: disable=invalid-name
292+
def reference_temperature_thresholds( # pylint: disable=invalid-name,
293+
self,
294+
) -> Tuple[float, float]:
283295
"""The cold junction's low and high temperature thresholds
284296
as a ``(low_temp, high_temp)`` tuple
285297
"""
@@ -289,13 +301,15 @@ def reference_temperature_thresholds(self): # pylint: disable=invalid-name
289301
)
290302

291303
@reference_temperature_thresholds.setter
292-
def reference_temperature_thresholds(self, val): # pylint: disable=invalid-name
304+
def reference_temperature_thresholds( # pylint: disable=invalid-name,
305+
self, val: Tuple[float, float]
306+
) -> None:
293307

294308
self._write_u8(_MAX31856_CJLF_REG, int(val[0]))
295309
self._write_u8(_MAX31856_CJHF_REG, int(val[1]))
296310

297311
@property
298-
def fault(self):
312+
def fault(self) -> Dict[str, bool]:
299313
"""A dictionary with the status of each fault type where the key is the fault type and the
300314
value is a bool if the fault is currently active
301315
@@ -326,12 +340,12 @@ def fault(self):
326340
"open_tc": bool(faults & _MAX31856_FAULT_OPEN),
327341
}
328342

329-
def _perform_one_shot_measurement(self):
343+
def _perform_one_shot_measurement(self) -> None:
330344
self.initiate_one_shot_measurement()
331345
# wait for the measurement to complete
332346
self._wait_for_oneshot()
333347

334-
def initiate_one_shot_measurement(self):
348+
def initiate_one_shot_measurement(self) -> None:
335349
"""Starts a one-shot measurement and returns immediately.
336350
A measurement takes approximately 160ms.
337351
Check the status of the measurement with `oneshot_pending`; when it is false,
@@ -358,11 +372,11 @@ def oneshot_pending(self) -> bool:
358372
)
359373
return bool(oneshot_flag)
360374

361-
def _wait_for_oneshot(self):
375+
def _wait_for_oneshot(self) -> None:
362376
while self.oneshot_pending:
363377
sleep(0.01)
364378

365-
def _read_register(self, address, length):
379+
def _read_register(self, address: int, length: int) -> bytearray:
366380
# pylint: disable=no-member
367381
# Read a 16-bit BE unsigned value from the specified 8-bit address.
368382
with self._device as device:
@@ -371,7 +385,7 @@ def _read_register(self, address, length):
371385
device.readinto(self._BUFFER, end=length)
372386
return self._BUFFER[:length]
373387

374-
def _write_u8(self, address, val):
388+
def _write_u8(self, address: int, val: int) -> None:
375389
# Write an 8-bit unsigned value to the specified 8-bit address.
376390
with self._device as device:
377391
self._BUFFER[0] = (address | 0x80) & 0xFF

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-busdevice
7+
typing-extensions~=4.0

0 commit comments

Comments
 (0)