Skip to content

Commit b97615c

Browse files
committed
Add Misssing Type Annotations
1 parent dc073a2 commit b97615c

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

adafruit_max31856.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
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 board import SPI
38+
from digitalio import DigitalInOut
39+
except ImportError:
40+
pass
41+
3542
try:
3643
from struct import unpack
3744
except ImportError:
@@ -151,7 +158,9 @@ class MAX31856:
151158
# Tony says this isn't re-entrant or thread safe!
152159
_BUFFER = bytearray(4)
153160

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

157166
# assert on any fault
@@ -162,7 +171,7 @@ def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K):
162171
# set thermocouple type
163172
self._set_thermocouple_type(thermocouple_type)
164173

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

186195
@averaging.setter
187-
def averaging(self, num_samples: int):
196+
def averaging(self, num_samples: int) -> None:
188197
# This option is set in bits 4-6 of register CR1.
189198
if num_samples not in _AVGSEL_CONSTS:
190199
raise ValueError("Num_samples must be one of 1,2,4,8,16")
@@ -208,7 +217,7 @@ def noise_rejection(self) -> int:
208217
return 60
209218

210219
@noise_rejection.setter
211-
def noise_rejection(self, frequency: int):
220+
def noise_rejection(self, frequency: int) -> None:
212221
conf_reg_0 = self._read_register(_MAX31856_CR0_REG, 1)[0]
213222
if frequency == 50:
214223
conf_reg_0 |= _MAX31856_CR0_50HZ # set the 50hz bit
@@ -219,7 +228,7 @@ def noise_rejection(self, frequency: int):
219228
self._write_u8(_MAX31856_CR0_REG, conf_reg_0)
220229

221230
@property
222-
def temperature(self):
231+
def temperature(self) -> float:
223232
"""Measure the temperature of the sensor and wait for the result.
224233
Return value is in degrees Celsius. (read-only)"""
225234
self._perform_one_shot_measurement()
@@ -241,7 +250,7 @@ def unpack_temperature(self) -> float:
241250
return temp_float
242251

243252
@property
244-
def reference_temperature(self):
253+
def reference_temperature(self) -> float:
245254
"""Wait to retrieve temperature of the cold junction in degrees Celsius. (read-only)"""
246255
self._perform_one_shot_measurement()
247256
return self.unpack_reference_temperature()
@@ -256,7 +265,7 @@ def unpack_reference_temperature(self) -> float:
256265
return cold_junction_temp
257266

258267
@property
259-
def temperature_thresholds(self):
268+
def temperature_thresholds(self) -> Tuple[float, float]:
260269
"""The thermocouple's low and high temperature thresholds
261270
as a ``(low_temp, high_temp)`` tuple
262271
"""
@@ -267,7 +276,7 @@ def temperature_thresholds(self):
267276
return (round(raw_low[0] / 16.0, 1), round(raw_high[0] / 16.0, 1))
268277

269278
@temperature_thresholds.setter
270-
def temperature_thresholds(self, val):
279+
def temperature_thresholds(self, val: Tuple[float, float]) -> None:
271280

272281
int_low = int(val[0] * 16)
273282
int_high = int(val[1] * 16)
@@ -279,7 +288,9 @@ def temperature_thresholds(self, val):
279288
self._write_u8(_MAX31856_LTLFTL_REG, int_low)
280289

281290
@property
282-
def reference_temperature_thresholds(self): # pylint: disable=invalid-name
291+
def reference_temperature_thresholds(
292+
self,
293+
) -> Tuple[float, float]: # pylint: disable=invalid-name
283294
"""The cold junction's low and high temperature thresholds
284295
as a ``(low_temp, high_temp)`` tuple
285296
"""
@@ -289,13 +300,15 @@ def reference_temperature_thresholds(self): # pylint: disable=invalid-name
289300
)
290301

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

294307
self._write_u8(_MAX31856_CJLF_REG, int(val[0]))
295308
self._write_u8(_MAX31856_CJHF_REG, int(val[1]))
296309

297310
@property
298-
def fault(self):
311+
def fault(self) -> Dict[str, bool]:
299312
"""A dictionary with the status of each fault type where the key is the fault type and the
300313
value is a bool if the fault is currently active
301314
@@ -326,12 +339,12 @@ def fault(self):
326339
"open_tc": bool(faults & _MAX31856_FAULT_OPEN),
327340
}
328341

329-
def _perform_one_shot_measurement(self):
342+
def _perform_one_shot_measurement(self) -> None:
330343
self.initiate_one_shot_measurement()
331344
# wait for the measurement to complete
332345
self._wait_for_oneshot()
333346

334-
def initiate_one_shot_measurement(self):
347+
def initiate_one_shot_measurement(self) -> None:
335348
"""Starts a one-shot measurement and returns immediately.
336349
A measurement takes approximately 160ms.
337350
Check the status of the measurement with `oneshot_pending`; when it is false,
@@ -358,11 +371,11 @@ def oneshot_pending(self) -> bool:
358371
)
359372
return bool(oneshot_flag)
360373

361-
def _wait_for_oneshot(self):
374+
def _wait_for_oneshot(self) -> None:
362375
while self.oneshot_pending:
363376
sleep(0.01)
364377

365-
def _read_register(self, address, length):
378+
def _read_register(self, address: int, length: int) -> int:
366379
# pylint: disable=no-member
367380
# Read a 16-bit BE unsigned value from the specified 8-bit address.
368381
with self._device as device:
@@ -371,7 +384,7 @@ def _read_register(self, address, length):
371384
device.readinto(self._BUFFER, end=length)
372385
return self._BUFFER[:length]
373386

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

0 commit comments

Comments
 (0)