@@ -164,9 +164,13 @@ def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K):
164
164
165
165
@property
166
166
def temperature (self ):
167
- """The temperature of the sensor and return its value in degrees Celsius. (read-only)"""
167
+ """Measure the temperature of the sensor and wait for the result.
168
+ Return value is in degrees Celsius. (read-only)"""
168
169
self ._perform_one_shot_measurement ()
170
+ return self .unpack_temperature ()
169
171
172
+ def unpack_temperature (self ) -> float :
173
+ """Reads the probe temperature from the register"""
170
174
# unpack the 3-byte temperature as 4 bytes
171
175
raw_temp = unpack (
172
176
">i" , self ._read_register (_MAX31856_LTCBH_REG , 3 ) + bytes ([0 ])
@@ -182,9 +186,12 @@ def temperature(self):
182
186
183
187
@property
184
188
def reference_temperature (self ):
185
- """The temperature of the cold junction in degrees Celsius. (read-only)"""
189
+ """Wait to retreive temperature of the cold junction in degrees Celsius. (read-only)"""
186
190
self ._perform_one_shot_measurement ()
191
+ return self .unpack_reference_temperature ()
187
192
193
+ def unpack_reference_temperature (self ) -> float :
194
+ """Reads the reference temperature from the register"""
188
195
raw_read = unpack (">h" , self ._read_register (_MAX31856_CJTH_REG , 2 ))[0 ]
189
196
190
197
# effectively shift raw_read >> 8 to convert pseudo-float
@@ -264,8 +271,16 @@ def fault(self):
264
271
}
265
272
266
273
def _perform_one_shot_measurement (self ):
267
-
268
- self ._write_u8 (_MAX31856_CJTO_REG , 0x0 )
274
+ self .initiate_one_shot_measurement ()
275
+ # wait for the measurement to complete
276
+ self ._wait_for_oneshot ()
277
+
278
+ def initiate_one_shot_measurement (self ):
279
+ """Starts a one-shot measurement and returns immediately.
280
+ A measurement takes approximately 160ms.
281
+ Check the status of the measurement with `oneshot_pending`; when it is false,
282
+ the measurement is complete and the value can be read with `unpack_temperature`.
283
+ """
269
284
# read the current value of the first config register
270
285
conf_reg_0 = self ._read_register (_MAX31856_CR0_REG , 1 )[0 ]
271
286
@@ -277,7 +292,19 @@ def _perform_one_shot_measurement(self):
277
292
# write it back with the new values, prompting the sensor to perform a measurement
278
293
self ._write_u8 (_MAX31856_CR0_REG , conf_reg_0 )
279
294
280
- sleep (0.250 )
295
+ @property
296
+ def oneshot_pending (self ) -> bool :
297
+ """A boolean indicating the status of the one-shot flag.
298
+ A True value means the measurement is still ongoing.
299
+ A False value means measurement is complete."""
300
+ oneshot_flag = (
301
+ self ._read_register (_MAX31856_CR0_REG , 1 )[0 ] & _MAX31856_CR0_1SHOT
302
+ )
303
+ return bool (oneshot_flag )
304
+
305
+ def _wait_for_oneshot (self ):
306
+ while self .oneshot_pending :
307
+ sleep (0.01 )
281
308
282
309
def _read_register (self , address , length ):
283
310
# pylint: disable=no-member
0 commit comments