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