32
32
from micropython import const
33
33
from adafruit_bus_device .spi_device import SPIDevice
34
34
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
+
35
43
try :
36
44
from struct import unpack
37
45
except ImportError :
@@ -151,7 +159,9 @@ class MAX31856:
151
159
# Tony says this isn't re-entrant or thread safe!
152
160
_BUFFER = bytearray (4 )
153
161
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 :
155
165
self ._device = SPIDevice (spi , cs , baudrate = 500000 , polarity = 0 , phase = 1 )
156
166
157
167
# assert on any fault
@@ -162,7 +172,7 @@ def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K):
162
172
# set thermocouple type
163
173
self ._set_thermocouple_type (thermocouple_type )
164
174
165
- def _set_thermocouple_type (self , thermocouple_type : ThermocoupleType ):
175
+ def _set_thermocouple_type (self , thermocouple_type : ThermocoupleType ) -> None :
166
176
# get current value of CR1 Reg
167
177
conf_reg_1 = self ._read_register (_MAX31856_CR1_REG , 1 )[0 ]
168
178
conf_reg_1 &= 0xF0 # mask off bottom 4 bits
@@ -184,7 +194,7 @@ def averaging(self) -> int:
184
194
raise KeyError (f"AVGSEL bit pattern was not recognised ({ avgsel :>08b} )" )
185
195
186
196
@averaging .setter
187
- def averaging (self , num_samples : int ):
197
+ def averaging (self , num_samples : int ) -> None :
188
198
# This option is set in bits 4-6 of register CR1.
189
199
if num_samples not in _AVGSEL_CONSTS :
190
200
raise ValueError ("Num_samples must be one of 1,2,4,8,16" )
@@ -197,7 +207,7 @@ def averaging(self, num_samples: int):
197
207
self ._write_u8 (_MAX31856_CR1_REG , conf_reg_1 )
198
208
199
209
@property
200
- def noise_rejection (self ) -> int :
210
+ def noise_rejection (self ) -> Literal [ 50 , 60 ] :
201
211
"""
202
212
The frequency (Hz) to be used by the noise rejection filter.
203
213
Must be 50 or 60. Default is 60."""
@@ -208,7 +218,7 @@ def noise_rejection(self) -> int:
208
218
return 60
209
219
210
220
@noise_rejection .setter
211
- def noise_rejection (self , frequency : int ) :
221
+ def noise_rejection (self , frequency : Literal [ 50 , 60 ]) -> None :
212
222
conf_reg_0 = self ._read_register (_MAX31856_CR0_REG , 1 )[0 ]
213
223
if frequency == 50 :
214
224
conf_reg_0 |= _MAX31856_CR0_50HZ # set the 50hz bit
@@ -219,7 +229,7 @@ def noise_rejection(self, frequency: int):
219
229
self ._write_u8 (_MAX31856_CR0_REG , conf_reg_0 )
220
230
221
231
@property
222
- def temperature (self ):
232
+ def temperature (self ) -> float :
223
233
"""Measure the temperature of the sensor and wait for the result.
224
234
Return value is in degrees Celsius. (read-only)"""
225
235
self ._perform_one_shot_measurement ()
@@ -241,7 +251,7 @@ def unpack_temperature(self) -> float:
241
251
return temp_float
242
252
243
253
@property
244
- def reference_temperature (self ):
254
+ def reference_temperature (self ) -> float :
245
255
"""Wait to retrieve temperature of the cold junction in degrees Celsius. (read-only)"""
246
256
self ._perform_one_shot_measurement ()
247
257
return self .unpack_reference_temperature ()
@@ -256,7 +266,7 @@ def unpack_reference_temperature(self) -> float:
256
266
return cold_junction_temp
257
267
258
268
@property
259
- def temperature_thresholds (self ):
269
+ def temperature_thresholds (self ) -> Tuple [ float , float ] :
260
270
"""The thermocouple's low and high temperature thresholds
261
271
as a ``(low_temp, high_temp)`` tuple
262
272
"""
@@ -267,7 +277,7 @@ def temperature_thresholds(self):
267
277
return (round (raw_low [0 ] / 16.0 , 1 ), round (raw_high [0 ] / 16.0 , 1 ))
268
278
269
279
@temperature_thresholds .setter
270
- def temperature_thresholds (self , val ) :
280
+ def temperature_thresholds (self , val : Tuple [ float , float ]) -> None :
271
281
272
282
int_low = int (val [0 ] * 16 )
273
283
int_high = int (val [1 ] * 16 )
@@ -279,7 +289,9 @@ def temperature_thresholds(self, val):
279
289
self ._write_u8 (_MAX31856_LTLFTL_REG , int_low )
280
290
281
291
@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 ]:
283
295
"""The cold junction's low and high temperature thresholds
284
296
as a ``(low_temp, high_temp)`` tuple
285
297
"""
@@ -289,13 +301,15 @@ def reference_temperature_thresholds(self): # pylint: disable=invalid-name
289
301
)
290
302
291
303
@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 :
293
307
294
308
self ._write_u8 (_MAX31856_CJLF_REG , int (val [0 ]))
295
309
self ._write_u8 (_MAX31856_CJHF_REG , int (val [1 ]))
296
310
297
311
@property
298
- def fault (self ):
312
+ def fault (self ) -> Dict [ str , bool ] :
299
313
"""A dictionary with the status of each fault type where the key is the fault type and the
300
314
value is a bool if the fault is currently active
301
315
@@ -326,12 +340,12 @@ def fault(self):
326
340
"open_tc" : bool (faults & _MAX31856_FAULT_OPEN ),
327
341
}
328
342
329
- def _perform_one_shot_measurement (self ):
343
+ def _perform_one_shot_measurement (self ) -> None :
330
344
self .initiate_one_shot_measurement ()
331
345
# wait for the measurement to complete
332
346
self ._wait_for_oneshot ()
333
347
334
- def initiate_one_shot_measurement (self ):
348
+ def initiate_one_shot_measurement (self ) -> None :
335
349
"""Starts a one-shot measurement and returns immediately.
336
350
A measurement takes approximately 160ms.
337
351
Check the status of the measurement with `oneshot_pending`; when it is false,
@@ -358,11 +372,11 @@ def oneshot_pending(self) -> bool:
358
372
)
359
373
return bool (oneshot_flag )
360
374
361
- def _wait_for_oneshot (self ):
375
+ def _wait_for_oneshot (self ) -> None :
362
376
while self .oneshot_pending :
363
377
sleep (0.01 )
364
378
365
- def _read_register (self , address , length ) :
379
+ def _read_register (self , address : int , length : int ) -> bytearray :
366
380
# pylint: disable=no-member
367
381
# Read a 16-bit BE unsigned value from the specified 8-bit address.
368
382
with self ._device as device :
@@ -371,7 +385,7 @@ def _read_register(self, address, length):
371
385
device .readinto (self ._BUFFER , end = length )
372
386
return self ._BUFFER [:length ]
373
387
374
- def _write_u8 (self , address , val ) :
388
+ def _write_u8 (self , address : int , val : int ) -> None :
375
389
# Write an 8-bit unsigned value to the specified 8-bit address.
376
390
with self ._device as device :
377
391
self ._BUFFER [0 ] = (address | 0x80 ) & 0xFF
0 commit comments