Skip to content

Commit ba94e36

Browse files
authored
Merge pull request #19 from eirinnm/non-blocking
Improve speed of temperature retrieval and allow non-blocking measurement
2 parents 69d8135 + 582ef48 commit ba94e36

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

README.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,16 @@ Usage Example
7070
# create a thermocouple object with the above
7171
thermocouple = adafruit_max31856.MAX31856(spi, cs)
7272
73-
# print the temperature!
73+
# measure the temperature! (takes approx 160ms)
7474
print(thermocouple.temperature)
7575
76+
# alternative (non-blocking) way to get temperature
77+
thermocouple.initiate_one_shot_measurement()
78+
# <perform other tasks>
79+
# now wait for measurement to complete
80+
while thermocouple.oneshot_pending:
81+
pass
82+
print(thermocouple.unpack_temperature())
7683
7784
Documentation
7885
=============

adafruit_max31856.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,13 @@ def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K):
164164

165165
@property
166166
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)"""
168169
self._perform_one_shot_measurement()
170+
return self.unpack_temperature()
169171

172+
def unpack_temperature(self) -> float:
173+
"""Reads the probe temperature from the register"""
170174
# unpack the 3-byte temperature as 4 bytes
171175
raw_temp = unpack(
172176
">i", self._read_register(_MAX31856_LTCBH_REG, 3) + bytes([0])
@@ -182,9 +186,12 @@ def temperature(self):
182186

183187
@property
184188
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)"""
186190
self._perform_one_shot_measurement()
191+
return self.unpack_reference_temperature()
187192

193+
def unpack_reference_temperature(self) -> float:
194+
"""Reads the reference temperature from the register"""
188195
raw_read = unpack(">h", self._read_register(_MAX31856_CJTH_REG, 2))[0]
189196

190197
# effectively shift raw_read >> 8 to convert pseudo-float
@@ -264,8 +271,16 @@ def fault(self):
264271
}
265272

266273
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+
"""
269284
# read the current value of the first config register
270285
conf_reg_0 = self._read_register(_MAX31856_CR0_REG, 1)[0]
271286

@@ -277,7 +292,19 @@ def _perform_one_shot_measurement(self):
277292
# write it back with the new values, prompting the sensor to perform a measurement
278293
self._write_u8(_MAX31856_CR0_REG, conf_reg_0)
279294

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)
281308

282309
def _read_register(self, address, length):
283310
# pylint: disable=no-member

0 commit comments

Comments
 (0)